diff --git a/src/vs/workbench/api/browser/mainThreadWebview.ts b/src/vs/workbench/api/browser/mainThreadWebview.ts index 627f495b9e6a306830c4030c9cf980d9129d546f..ee94a7b2007d4081a7d0321cc0de78c332e8ede0 100644 --- a/src/vs/workbench/api/browser/mainThreadWebview.ts +++ b/src/vs/workbench/api/browser/mainThreadWebview.ts @@ -35,6 +35,7 @@ import { CustomTextEditorModel } from 'vs/workbench/contrib/customEditor/common/ import { WebviewExtensionDescription, WebviewIcons } from 'vs/workbench/contrib/webview/browser/webview'; import { WebviewInput } from 'vs/workbench/contrib/webview/browser/webviewEditorInput'; import { ICreateWebViewShowOptions, IWebviewWorkbenchService, WebviewInputOptions } from 'vs/workbench/contrib/webview/browser/webviewWorkbenchService'; +import { IBackupFileService } from 'vs/workbench/services/backup/common/backup'; import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions'; @@ -581,6 +582,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod private _currentEditIndex: number = -1; private _savePoint: number = -1; private readonly _edits: Array = []; + private _fromBackup: boolean = false; public static async create( instantiationService: IInstantiationService, @@ -591,7 +593,9 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod cancellation: CancellationToken, ) { const { editable } = await proxy.$createWebviewCustomEditorDocument(resource, viewType, cancellation); - return instantiationService.createInstance(MainThreadCustomEditorModel, proxy, viewType, resource, editable, getEditors); + const model = instantiationService.createInstance(MainThreadCustomEditorModel, proxy, viewType, resource, editable, getEditors); + await model.init(); + return model; } constructor( @@ -604,6 +608,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod @ILabelService private readonly _labelService: ILabelService, @IFileService private readonly _fileService: IFileService, @IUndoRedoService private readonly _undoService: IUndoRedoService, + @IBackupFileService private readonly _backupFileService: IBackupFileService, ) { super(); @@ -616,6 +621,11 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod return this._editorResource; } + async init(): Promise { + const backup = await this._backupFileService.resolve(this.resource); + this._fromBackup = !!backup; + } + dispose() { if (this._editable) { this._undoService.removeElements(this._editorResource); @@ -645,7 +655,10 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod } public isDirty(): boolean { - return this._edits.length > 0 && this._savePoint !== this._currentEditIndex; + if (this._edits.length > 0) { + return this._savePoint !== this._currentEditIndex; + } + return this._fromBackup; } private readonly _onDidChangeDirty: Emitter = this._register(new Emitter()); diff --git a/src/vs/workbench/contrib/customEditor/browser/customEditorInput.ts b/src/vs/workbench/contrib/customEditor/browser/customEditorInput.ts index eb845fc7d88878be381ced913f0adec1a21cfc9a..429280f061d5f446ead4aa16b82fb161774f0f65 100644 --- a/src/vs/workbench/contrib/customEditor/browser/customEditorInput.ts +++ b/src/vs/workbench/contrib/customEditor/browser/customEditorInput.ts @@ -27,6 +27,8 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput { public static typeId = 'workbench.editors.webviewEditor'; private readonly _editorResource: URI; + private readonly _fromBackup: boolean; + get resource() { return this._editorResource; } private _modelRef?: IReference; @@ -36,6 +38,7 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput { viewType: string, id: string, webview: Lazy, + fromBackup: boolean, @IWebviewService webviewService: IWebviewService, @IWebviewWorkbenchService webviewWorkbenchService: IWebviewWorkbenchService, @IInstantiationService private readonly instantiationService: IInstantiationService, @@ -48,6 +51,7 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput { ) { super(id, viewType, '', webview, webviewService, webviewWorkbenchService); this._editorResource = resource; + this._fromBackup = fromBackup; } public getTypeId(): string { @@ -106,7 +110,7 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput { public isDirty(): boolean { if (!this._modelRef) { - return false; + return this._fromBackup; } return this._modelRef.object.isDirty(); } @@ -188,7 +192,9 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput { newResource, this.viewType, this.id, - new Lazy(() => undefined!)); // this webview is replaced in the transfer call + new Lazy(() => undefined!), // this webview is replaced in the transfer call + this._fromBackup, + ); this.transfer(newEditor); newEditor.updateGroup(group); return { editor: newEditor }; diff --git a/src/vs/workbench/contrib/customEditor/browser/customEditorInputFactory.ts b/src/vs/workbench/contrib/customEditor/browser/customEditorInputFactory.ts index c2acedc2a6f8f648621f19270b891a1a8ee0afce..3d4b9927c32e480639a83eae4fe06522ee26f444 100644 --- a/src/vs/workbench/contrib/customEditor/browser/customEditorInputFactory.ts +++ b/src/vs/workbench/contrib/customEditor/browser/customEditorInputFactory.ts @@ -78,7 +78,7 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory { return webview; }); - const customInput = this._instantiationService.createInstance(CustomEditorInput, URI.from((data as any).editorResource), data.viewType, id, webview); + const customInput = this._instantiationService.createInstance(CustomEditorInput, URI.from((data as any).editorResource), data.viewType, id, webview, false); if (typeof data.group === 'number') { customInput.updateGroup(data.group); } @@ -90,12 +90,12 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory { const webviewService = accessor.get(IWebviewService); const backupFileService = accessor.get(IBackupFileService); - const backup = await backupFileService.resolve(resource); - if (!backup) { + const backup = await backupFileService.resolve(resource); + if (!backup?.meta) { throw new Error(`No backup found for custom editor: ${resource}`); } - const backupData = backup.meta as CustomDocumentBackupData; + const backupData = backup.meta; const id = backupData.webview.id; const webview = new Lazy(() => { @@ -112,7 +112,7 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory { return webview; }); - const editor = instantiationService.createInstance(CustomEditorInput, URI.revive(backupData.editorResource), backupData.viewType, id, webview); + const editor = instantiationService.createInstance(CustomEditorInput, URI.revive(backupData.editorResource), backupData.viewType, id, webview, true); editor.updateGroup(0); return editor; }); diff --git a/src/vs/workbench/contrib/customEditor/browser/customEditors.ts b/src/vs/workbench/contrib/customEditor/browser/customEditors.ts index 5a7671b374ca0929be40db54c59cc3282e234920..b8bd2479149624aeb2b832cbe2d8608134857369 100644 --- a/src/vs/workbench/contrib/customEditor/browser/customEditors.ts +++ b/src/vs/workbench/contrib/customEditor/browser/customEditors.ts @@ -269,7 +269,7 @@ export class CustomEditorService extends Disposable implements ICustomEditorServ const webview = new Lazy(() => { return this.webviewService.createWebviewOverlay(id, { customClasses: options?.customClasses }, {}); }); - const input = this.instantiationService.createInstance(CustomEditorInput, resource, viewType, id, webview); + const input = this.instantiationService.createInstance(CustomEditorInput, resource, viewType, id, webview, false); if (typeof group !== 'undefined') { input.updateGroup(group); }