Hookup experimental undo for customEditors

This currently is not connected to any actual actions in the editor
This commit is contained in:
Matt Bierner
2019-11-07 17:22:21 -08:00
parent b5dbace111
commit 278b4f8cfa
7 changed files with 125 additions and 22 deletions

View File

@@ -27,6 +27,7 @@ import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { extHostNamedCustomer } from '../common/extHostCustomers';
import { CustomEditorModel } from 'vs/workbench/contrib/customEditor/browser/customEditorModel';
/**
* Bi-directional map between webview handles and inputs.
@@ -94,6 +95,7 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma
private readonly _webviewInputs = new WebviewInputStore();
private readonly _revivers = new Map<string, IDisposable>();
private readonly _editorProviders = new Map<string, IDisposable>();
private readonly _models = new Map<string, CustomEditorModel>();
constructor(
context: extHostProtocol.IExtHostContext,
@@ -261,7 +263,7 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma
canResolve: (webviewInput) => {
return webviewInput instanceof CustomFileEditorInput && webviewInput.viewType === viewType;
},
resolveWebview: async (webviewInput) => {
resolveWebview: async (webviewInput: CustomFileEditorInput) => {
const handle = webviewInput.id;
this._webviewInputs.add(handle, webviewInput);
this.hookupWebviewEventDelegate(handle, webviewInput);
@@ -269,6 +271,18 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma
webviewInput.webview.options = options;
webviewInput.webview.extension = extension;
const model = new CustomEditorModel();
webviewInput.setModel(model);
this._models.set(handle, model);
webviewInput.onDispose(() => {
this._models.delete(handle);
});
model.onUndo(edit => {
this._proxy.$undoEdits(handle, [edit]);
});
try {
await this._proxy.$resolveWebviewEditor(
webviewInput.getResource(),
@@ -296,11 +310,18 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma
this._editorProviders.delete(viewType);
}
public $onEdit(handle: extHostProtocol.WebviewPanelHandle, editJson: string): void {
public $onEdit(handle: extHostProtocol.WebviewPanelHandle, editData: string): void {
const webview = this.getWebviewInput(handle);
if (!(webview instanceof CustomFileEditorInput)) {
throw new Error(`Webview is not a webview editor`);
throw new Error('Webview is not a webview editor');
}
const model = this._models.get(handle);
if (!model) {
throw new Error('Could not find model for webview editor');
}
model.makeEdit(editData);
}
private hookupWebviewEventDelegate(handle: extHostProtocol.WebviewPanelHandle, input: WebviewInput) {