diff --git a/src/vs/workbench/api/browser/mainThreadWebview.ts b/src/vs/workbench/api/browser/mainThreadWebview.ts index 8663cdf3ca1..cc4ccd35397 100644 --- a/src/vs/workbench/api/browser/mainThreadWebview.ts +++ b/src/vs/workbench/api/browser/mainThreadWebview.ts @@ -35,7 +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, IResolvedBackup } from 'vs/workbench/services/backup/common/backup'; +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'; @@ -342,7 +342,7 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma let modelRef: IReference; try { - modelRef = await this.getOrCreateCustomEditorModel(modelType, resource, viewType, cancellation); + modelRef = await this.getOrCreateCustomEditorModel(modelType, resource, viewType, { backupId: webviewInput.backupId }, cancellation); } catch (error) { onUnexpectedError(error); webviewInput.webview.html = MainThreadWebviews.getWebviewResolvedFailedContent(viewType); @@ -361,7 +361,7 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma if (capabilities.supportsMove) { webviewInput.onMove(async (newResource: URI) => { const oldModel = modelRef; - modelRef = await this.getOrCreateCustomEditorModel(modelType, newResource, viewType, CancellationToken.None); + modelRef = await this.getOrCreateCustomEditorModel(modelType, newResource, viewType, {}, CancellationToken.None); this._proxy.$onMoveCustomEditor(handle, newResource, viewType); oldModel.dispose(); }); @@ -399,6 +399,7 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma modelType: ModelType, resource: URI, viewType: string, + options: { backupId?: string }, cancellation: CancellationToken, ): Promise> { const existingModel = this._customEditorService.models.tryRetain(resource, viewType); @@ -406,14 +407,21 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma return existingModel; } - const model = modelType === ModelType.Text - ? CustomTextEditorModel.create(this._instantiationService, viewType, resource) - : MainThreadCustomEditorModel.create(this._instantiationService, this._proxy, viewType, resource, () => { - return Array.from(this._webviewInputs) - .filter(editor => editor instanceof CustomEditorInput && isEqual(editor.resource, resource)) as CustomEditorInput[]; - }, cancellation, this._backupService); - - return this._customEditorService.models.add(resource, viewType, model); + switch (modelType) { + case ModelType.Text: + { + const model = CustomTextEditorModel.create(this._instantiationService, viewType, resource); + return this._customEditorService.models.add(resource, viewType, model); + } + case ModelType.Custom: + { + const model = MainThreadCustomEditorModel.create(this._instantiationService, this._proxy, viewType, resource, options, () => { + return Array.from(this._webviewInputs) + .filter(editor => editor instanceof CustomEditorInput && isEqual(editor.resource, resource)) as CustomEditorInput[]; + }, cancellation, this._backupService); + return this._customEditorService.models.add(resource, viewType, model); + } + } } public async $onDidEdit(resourceComponents: UriComponents, viewType: string, editId: number, label: string | undefined): Promise { @@ -602,20 +610,20 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod proxy: extHostProtocol.ExtHostWebviewsShape, viewType: string, resource: URI, + options: { backupId?: string }, getEditors: () => CustomEditorInput[], cancellation: CancellationToken, - backupFileService: IBackupFileService, + _backupFileService: IBackupFileService, ) { - const backup = await backupFileService.resolve(MainThreadCustomEditorModel.toWorkingCopyResource(viewType, resource)); - const { editable } = await proxy.$createCustomDocument(resource, viewType, backup?.meta?.backupId, cancellation); - return instantiationService.createInstance(MainThreadCustomEditorModel, proxy, viewType, resource, backup, editable, getEditors); + const { editable } = await proxy.$createCustomDocument(resource, viewType, options.backupId, cancellation); + return instantiationService.createInstance(MainThreadCustomEditorModel, proxy, viewType, resource, !!options.backupId, editable, getEditors); } constructor( private readonly _proxy: extHostProtocol.ExtHostWebviewsShape, private readonly _viewType: string, private readonly _editorResource: URI, - backup: IResolvedBackup | undefined, + fromBackup: boolean, private readonly _editable: boolean, private readonly _getEditors: () => CustomEditorInput[], @IWorkingCopyService workingCopyService: IWorkingCopyService, @@ -628,7 +636,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod if (_editable) { this._register(workingCopyService.registerWorkingCopy(this)); } - this._fromBackup = !!backup; + this._fromBackup = fromBackup; } get editorResource() { diff --git a/src/vs/workbench/contrib/customEditor/browser/customEditorInputFactory.ts b/src/vs/workbench/contrib/customEditor/browser/customEditorInputFactory.ts index 2a512f8462d..2bfbb0590ef 100644 --- a/src/vs/workbench/contrib/customEditor/browser/customEditorInputFactory.ts +++ b/src/vs/workbench/contrib/customEditor/browser/customEditorInputFactory.ts @@ -32,11 +32,15 @@ export interface CustomDocumentBackupData { interface SerializedCustomEditor extends SerializedWebview { readonly editorResource: UriComponents; - readonly dirty?: boolean; + readonly dirty: boolean; + readonly backupId?: string; } + interface DeserializedCustomEditor extends DeserializedWebview { readonly editorResource: URI; + readonly dirty: boolean; + readonly backupId?: string; } @@ -57,6 +61,7 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory { ...this.toJson(input), editorResource: input.resource.toJSON(), dirty: input.isDirty(), + backupId: input.backupId, }; try { @@ -66,10 +71,11 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory { } } - protected fromJson(input: SerializedCustomEditor): DeserializedCustomEditor { + protected fromJson(data: SerializedCustomEditor): DeserializedCustomEditor { return { - editorResource: URI.from(input.editorResource), - ...super.fromJson(input), + ...super.fromJson(data), + editorResource: URI.from(data.editorResource), + dirty: data.dirty, }; } @@ -79,7 +85,7 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory { ): CustomEditorInput { const data = this.fromJson(JSON.parse(serializedEditorInput)); const webview = CustomEditorInputFactory.reviveWebview(data, this._webviewService); - const customInput = this._instantiationService.createInstance(CustomEditorInput, URI.from(data.editorResource), data.viewType, data.id, webview, { startsDirty: (data as any).dirty }); + const customInput = this._instantiationService.createInstance(CustomEditorInput, data.editorResource, data.viewType, data.id, webview, { startsDirty: data.dirty, backupId: data.backupId }); if (typeof data.group === 'number') { customInput.updateGroup(data.group); } @@ -113,7 +119,7 @@ export class CustomEditorInputFactory extends WebviewEditorInputFactory { const extension = reviveWebviewExtensionDescription(backupData.extension?.id, backupData.extension?.location); const webview = CustomEditorInputFactory.reviveWebview({ id, options: backupData.webview.options, state: backupData.webview.state, extension, }, webviewService); - const editor = instantiationService.createInstance(CustomEditorInput, URI.revive(backupData.editorResource), backupData.viewType, id, webview, { startsDirty: true, backupId: backupData.backupId }); + const editor = instantiationService.createInstance(CustomEditorInput, URI.revive(backupData.editorResource), backupData.viewType, id, webview, { backupId: backupData.backupId }); editor.updateGroup(0); return editor; });