提交 b8295c1a 编写于 作者: M Matt Bierner

Try restoring the dirty state of backedup custom editors

上级 81521ba3
...@@ -35,6 +35,7 @@ import { CustomTextEditorModel } from 'vs/workbench/contrib/customEditor/common/ ...@@ -35,6 +35,7 @@ import { CustomTextEditorModel } from 'vs/workbench/contrib/customEditor/common/
import { WebviewExtensionDescription, WebviewIcons } from 'vs/workbench/contrib/webview/browser/webview'; import { WebviewExtensionDescription, WebviewIcons } from 'vs/workbench/contrib/webview/browser/webview';
import { WebviewInput } from 'vs/workbench/contrib/webview/browser/webviewEditorInput'; import { WebviewInput } from 'vs/workbench/contrib/webview/browser/webviewEditorInput';
import { ICreateWebViewShowOptions, IWebviewWorkbenchService, WebviewInputOptions } from 'vs/workbench/contrib/webview/browser/webviewWorkbenchService'; 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 { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions'; import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
...@@ -581,6 +582,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod ...@@ -581,6 +582,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod
private _currentEditIndex: number = -1; private _currentEditIndex: number = -1;
private _savePoint: number = -1; private _savePoint: number = -1;
private readonly _edits: Array<number> = []; private readonly _edits: Array<number> = [];
private _fromBackup: boolean = false;
public static async create( public static async create(
instantiationService: IInstantiationService, instantiationService: IInstantiationService,
...@@ -591,7 +593,9 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod ...@@ -591,7 +593,9 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod
cancellation: CancellationToken, cancellation: CancellationToken,
) { ) {
const { editable } = await proxy.$createWebviewCustomEditorDocument(resource, viewType, cancellation); 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( constructor(
...@@ -604,6 +608,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod ...@@ -604,6 +608,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod
@ILabelService private readonly _labelService: ILabelService, @ILabelService private readonly _labelService: ILabelService,
@IFileService private readonly _fileService: IFileService, @IFileService private readonly _fileService: IFileService,
@IUndoRedoService private readonly _undoService: IUndoRedoService, @IUndoRedoService private readonly _undoService: IUndoRedoService,
@IBackupFileService private readonly _backupFileService: IBackupFileService,
) { ) {
super(); super();
...@@ -616,6 +621,11 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod ...@@ -616,6 +621,11 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod
return this._editorResource; return this._editorResource;
} }
async init(): Promise<void> {
const backup = await this._backupFileService.resolve<CustomDocumentBackupData>(this.resource);
this._fromBackup = !!backup;
}
dispose() { dispose() {
if (this._editable) { if (this._editable) {
this._undoService.removeElements(this._editorResource); this._undoService.removeElements(this._editorResource);
...@@ -645,7 +655,10 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod ...@@ -645,7 +655,10 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod
} }
public isDirty(): boolean { 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<void> = this._register(new Emitter<void>()); private readonly _onDidChangeDirty: Emitter<void> = this._register(new Emitter<void>());
......
...@@ -27,6 +27,8 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput { ...@@ -27,6 +27,8 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput {
public static typeId = 'workbench.editors.webviewEditor'; public static typeId = 'workbench.editors.webviewEditor';
private readonly _editorResource: URI; private readonly _editorResource: URI;
private readonly _fromBackup: boolean;
get resource() { return this._editorResource; } get resource() { return this._editorResource; }
private _modelRef?: IReference<ICustomEditorModel>; private _modelRef?: IReference<ICustomEditorModel>;
...@@ -36,6 +38,7 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput { ...@@ -36,6 +38,7 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput {
viewType: string, viewType: string,
id: string, id: string,
webview: Lazy<WebviewOverlay>, webview: Lazy<WebviewOverlay>,
fromBackup: boolean,
@IWebviewService webviewService: IWebviewService, @IWebviewService webviewService: IWebviewService,
@IWebviewWorkbenchService webviewWorkbenchService: IWebviewWorkbenchService, @IWebviewWorkbenchService webviewWorkbenchService: IWebviewWorkbenchService,
@IInstantiationService private readonly instantiationService: IInstantiationService, @IInstantiationService private readonly instantiationService: IInstantiationService,
...@@ -48,6 +51,7 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput { ...@@ -48,6 +51,7 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput {
) { ) {
super(id, viewType, '', webview, webviewService, webviewWorkbenchService); super(id, viewType, '', webview, webviewService, webviewWorkbenchService);
this._editorResource = resource; this._editorResource = resource;
this._fromBackup = fromBackup;
} }
public getTypeId(): string { public getTypeId(): string {
...@@ -106,7 +110,7 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput { ...@@ -106,7 +110,7 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput {
public isDirty(): boolean { public isDirty(): boolean {
if (!this._modelRef) { if (!this._modelRef) {
return false; return this._fromBackup;
} }
return this._modelRef.object.isDirty(); return this._modelRef.object.isDirty();
} }
...@@ -188,7 +192,9 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput { ...@@ -188,7 +192,9 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput {
newResource, newResource,
this.viewType, this.viewType,
this.id, 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); this.transfer(newEditor);
newEditor.updateGroup(group); newEditor.updateGroup(group);
return { editor: newEditor }; return { editor: newEditor };
......
...@@ -78,7 +78,7 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory { ...@@ -78,7 +78,7 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory {
return webview; 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') { if (typeof data.group === 'number') {
customInput.updateGroup(data.group); customInput.updateGroup(data.group);
} }
...@@ -90,12 +90,12 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory { ...@@ -90,12 +90,12 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory {
const webviewService = accessor.get<IWebviewService>(IWebviewService); const webviewService = accessor.get<IWebviewService>(IWebviewService);
const backupFileService = accessor.get<IBackupFileService>(IBackupFileService); const backupFileService = accessor.get<IBackupFileService>(IBackupFileService);
const backup = await backupFileService.resolve(resource); const backup = await backupFileService.resolve<CustomDocumentBackupData>(resource);
if (!backup) { if (!backup?.meta) {
throw new Error(`No backup found for custom editor: ${resource}`); 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 id = backupData.webview.id;
const webview = new Lazy(() => { const webview = new Lazy(() => {
...@@ -112,7 +112,7 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory { ...@@ -112,7 +112,7 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory {
return webview; 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); editor.updateGroup(0);
return editor; return editor;
}); });
......
...@@ -269,7 +269,7 @@ export class CustomEditorService extends Disposable implements ICustomEditorServ ...@@ -269,7 +269,7 @@ export class CustomEditorService extends Disposable implements ICustomEditorServ
const webview = new Lazy(() => { const webview = new Lazy(() => {
return this.webviewService.createWebviewOverlay(id, { customClasses: options?.customClasses }, {}); 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') { if (typeof group !== 'undefined') {
input.updateGroup(group); input.updateGroup(group);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册