提交 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/
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<number> = [];
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<void> {
const backup = await this._backupFileService.resolve<CustomDocumentBackupData>(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<void> = this._register(new Emitter<void>());
......
......@@ -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<ICustomEditorModel>;
......@@ -36,6 +38,7 @@ export class CustomEditorInput extends LazilyResolvedWebviewEditorInput {
viewType: string,
id: string,
webview: Lazy<WebviewOverlay>,
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 };
......
......@@ -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>(IWebviewService);
const backupFileService = accessor.get<IBackupFileService>(IBackupFileService);
const backup = await backupFileService.resolve(resource);
if (!backup) {
const backup = await backupFileService.resolve<CustomDocumentBackupData>(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;
});
......
......@@ -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);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册