diff --git a/src/vs/workbench/api/browser/mainThreadWebview.ts b/src/vs/workbench/api/browser/mainThreadWebview.ts index 1ba9ed80383..99c2aaeff52 100644 --- a/src/vs/workbench/api/browser/mainThreadWebview.ts +++ b/src/vs/workbench/api/browser/mainThreadWebview.ts @@ -273,8 +273,13 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma const model = await this._customEditorService.models.loadOrCreate(webviewInput.getResource(), webviewInput.viewType); - model.onUndo(edits => { this._proxy.$undoEdits(handle, edits); }); - model.onRedo(edits => { this._proxy.$redoEdits(handle, edits); }); + model.onUndo(edits => { this._proxy.$undoEdits(handle, edits.map(x => x.data)); }); + model.onApplyEdit(edits => { + const editsToApply = edits.filter(x => x.source !== webviewInput).map(x => x.data); + if (editsToApply.length) { + this._proxy.$applyEdits(handle, editsToApply); + } + }); model.onWillSave(e => { e.waitUntil(this._proxy.$onSave(handle)); }); model.onWillSaveAs(e => { e.waitUntil(this._proxy.$onSaveAs(handle, e.resource.toJSON(), e.targetResource.toJSON())); }); @@ -309,7 +314,7 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma this._editorProviders.delete(viewType); } - public $onEdit(handle: extHostProtocol.WebviewPanelHandle, editData: string): void { + public $onEdit(handle: extHostProtocol.WebviewPanelHandle, editData: any): void { const webview = this.getWebviewInput(handle); if (!(webview instanceof CustomFileEditorInput)) { throw new Error('Webview is not a webview editor'); @@ -320,7 +325,7 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma throw new Error('Could not find model for webview editor'); } - model.makeEdit(editData); + model.makeEdit({ source: webview, data: editData }); } private hookupWebviewEventDelegate(handle: extHostProtocol.WebviewPanelHandle, input: WebviewInput) { diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index 72d18a284d4..fd400006266 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -574,7 +574,7 @@ export interface MainThreadWebviewsShape extends IDisposable { $registerEditorProvider(extension: WebviewExtensionDescription, viewType: string, options: modes.IWebviewPanelOptions): void; $unregisterEditorProvider(viewType: string): void; - $onEdit(handle: WebviewPanelHandle, editJson: string): void; + $onEdit(handle: WebviewPanelHandle, editJson: any): void; } export interface WebviewPanelViewStateData { @@ -595,7 +595,7 @@ export interface ExtHostWebviewsShape { $resolveWebviewEditor(resource: UriComponents, newWebviewHandle: WebviewPanelHandle, viewType: string, title: string, position: EditorViewColumn, options: modes.IWebviewOptions & modes.IWebviewPanelOptions): Promise; $undoEdits(handle: WebviewPanelHandle, edits: readonly any[]): void; - $redoEdits(handle: WebviewPanelHandle, edits: readonly any[]): void; + $applyEdits(handle: WebviewPanelHandle, edits: readonly any[]): void; $onSave(handle: WebviewPanelHandle): Promise; $onSaveAs(handle: WebviewPanelHandle, resource: UriComponents, targetResource: UriComponents): Promise; diff --git a/src/vs/workbench/api/common/extHostWebview.ts b/src/vs/workbench/api/common/extHostWebview.ts index d1081d0f11e..68405468573 100644 --- a/src/vs/workbench/api/common/extHostWebview.ts +++ b/src/vs/workbench/api/common/extHostWebview.ts @@ -457,7 +457,7 @@ export class ExtHostWebviews implements ExtHostWebviewsShape { panel?._undoEdits(edits); } - $redoEdits(handle: WebviewPanelHandle, edits: string[]): void { + $applyEdits(handle: WebviewPanelHandle, edits: string[]): void { const panel = this.getWebviewPanel(handle); panel?._redoEdits(edits); } diff --git a/src/vs/workbench/contrib/customEditor/common/customEditor.ts b/src/vs/workbench/contrib/customEditor/common/customEditor.ts index 2e08c304d7a..087e4e9a17d 100644 --- a/src/vs/workbench/contrib/customEditor/common/customEditor.ts +++ b/src/vs/workbench/contrib/customEditor/common/customEditor.ts @@ -38,7 +38,7 @@ export interface ICustomEditorService { promptOpenWith(resource: URI, options?: ITextEditorOptions, group?: IEditorGroup): Promise; } -export type CustomEditorEdit = unknown; +export type CustomEditorEdit = { source?: any, data: any }; export interface ICustomEditorModelManager { get(resource: URI, viewType: string): ICustomEditorModel | undefined; @@ -61,7 +61,7 @@ export interface CustomEditorSaveAsEvent { export interface ICustomEditorModel extends IWorkingCopy { readonly onUndo: Event; - readonly onRedo: Event; + readonly onApplyEdit: Event; readonly onWillSave: Event; readonly onWillSaveAs: Event; @@ -72,7 +72,7 @@ export interface ICustomEditorModel extends IWorkingCopy { save(options?: ISaveOptions): Promise; saveAs(resource: URI, targetResource: URI, currentOptions?: ISaveOptions): Promise; - makeEdit(data: string): void; + makeEdit(edit: CustomEditorEdit): void; } export const enum CustomEditorPriority { diff --git a/src/vs/workbench/contrib/customEditor/common/customEditorModel.ts b/src/vs/workbench/contrib/customEditor/common/customEditorModel.ts index 2b36d9569d2..df42f0c3f0c 100644 --- a/src/vs/workbench/contrib/customEditor/common/customEditorModel.ts +++ b/src/vs/workbench/contrib/customEditor/common/customEditorModel.ts @@ -44,8 +44,8 @@ export class CustomEditorModel extends Disposable implements ICustomEditorModel protected readonly _onUndo = this._register(new Emitter()); readonly onUndo = this._onUndo.event; - protected readonly _onRedo = this._register(new Emitter()); - readonly onRedo = this._onRedo.event; + protected readonly _onApplyEdit = this._register(new Emitter()); + readonly onApplyEdit = this._onApplyEdit.event; protected readonly _onWillSave = this._register(new Emitter()); readonly onWillSave = this._onWillSave.event; @@ -53,10 +53,11 @@ export class CustomEditorModel extends Disposable implements ICustomEditorModel protected readonly _onWillSaveAs = this._register(new Emitter()); readonly onWillSaveAs = this._onWillSaveAs.event; - public makeEdit(data: string): void { - this._edits.splice(this._currentEditIndex + 1, this._edits.length - this._currentEditIndex, data); + public makeEdit(edit: CustomEditorEdit): void { + this._edits.splice(this._currentEditIndex + 1, this._edits.length - this._currentEditIndex, edit.data); this._currentEditIndex = this._edits.length - 1; this.updateDirty(); + this._onApplyEdit.fire([edit]); } private updateDirty() { @@ -114,7 +115,7 @@ export class CustomEditorModel extends Disposable implements ICustomEditorModel this._onUndo.fire(editsToUndo.reverse()); } else if (this._currentEditIndex < this._savePoint) { const editsToRedo = this._edits.slice(this._currentEditIndex, this._savePoint); - this._onRedo.fire(editsToRedo); + this._onApplyEdit.fire(editsToRedo); } this._currentEditIndex = this._savePoint; @@ -144,7 +145,7 @@ export class CustomEditorModel extends Disposable implements ICustomEditorModel ++this._currentEditIndex; const redoneEdit = this._edits[this._currentEditIndex]; - this._onRedo.fire([redoneEdit]); + this._onApplyEdit.fire([redoneEdit]); this.updateDirty(); }