diff --git a/src/vs/workbench/contrib/positronPreview/browser/positronPreviewServiceImpl.ts b/src/vs/workbench/contrib/positronPreview/browser/positronPreviewServiceImpl.ts index a9e488440ae..eb8f73626a1 100644 --- a/src/vs/workbench/contrib/positronPreview/browser/positronPreviewServiceImpl.ts +++ b/src/vs/workbench/contrib/positronPreview/browser/positronPreviewServiceImpl.ts @@ -48,7 +48,7 @@ export class PositronPreviewService extends Disposable implements IPositronPrevi private _onDidChangeActivePreviewWebview = new Emitter; - private _editors: Map = new Map(); + private _editors: Map = new Map(); constructor( @ICommandService private readonly _commandService: ICommandService, @@ -515,7 +515,6 @@ export class PositronPreviewService extends Disposable implements IPositronPrevi const previewId = `editorPreview.${uri.toString()}`; this._editors.set(previewId, { uri: uri, - webview: this.createPreviewUrl(previewId, undefined, uri), title: title || uri.authority || uri.path }); @@ -528,7 +527,9 @@ export class PositronPreviewService extends Disposable implements IPositronPrevi } public editorWebview(editorId: string): PreviewWebview | undefined { - return this._editors.get(editorId)?.webview; + const uri = this._editors.get(editorId)?.uri; + if (!uri) { return undefined; } + return this.createPreviewUrl(editorId, undefined, uri); } public editorTitle(previewId: string): string | undefined { @@ -536,7 +537,6 @@ export class PositronPreviewService extends Disposable implements IPositronPrevi } public disposeEditor(previewId: string): void { - this._editors.get(previewId)?.webview.dispose(); // Remove the preview this._editors.delete(previewId); } diff --git a/src/vs/workbench/contrib/positronPreviewEditor/browser/positronPreviewEditor.tsx b/src/vs/workbench/contrib/positronPreviewEditor/browser/positronPreviewEditor.tsx index fe64d2f268a..c5d518b2433 100644 --- a/src/vs/workbench/contrib/positronPreviewEditor/browser/positronPreviewEditor.tsx +++ b/src/vs/workbench/contrib/positronPreviewEditor/browser/positronPreviewEditor.tsx @@ -23,6 +23,7 @@ import { PositronPreviewEditorInput } from 'vs/workbench/contrib/positronPreview import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService'; import { IPositronPreviewService } from 'vs/workbench/contrib/positronPreview/browser/positronPreview'; import { EditorPreviewContainer } from 'vs/workbench/contrib/positronPreviewEditor/browser/editorPreviewContainer'; +import { PreviewWebview } from 'vs/workbench/contrib/positronPreview/browser/previewWebview'; export interface IPositronPreviewEditorOptions extends IEditorOptions { get identifier(): string | undefined; @@ -59,6 +60,8 @@ export class PositronPreviewEditor private readonly _onFocusedEmitter = this._register(new Emitter()); + private _preview: PreviewWebview | undefined; + get identifier(): string | undefined { return this._identifier; } @@ -114,16 +117,17 @@ export class PositronPreviewEditor if (!this._positronReactRenderer) { this._positronReactRenderer = new PositronReactRenderer(this._container); } + this._preview = this._positronPreviewService.editorWebview(previewId); this._positronReactRenderer.render( ); @@ -144,31 +148,40 @@ export class PositronPreviewEditor context: IEditorOpenContext, token: CancellationToken ): Promise { - const previewId = input._previewId; + this._identifier = input._previewId; - if (!previewId) { throw Error; } + if (!this._identifier) { return; } - this.renderContainer(previewId); + this.renderContainer(this._identifier); // redraw if the editor is resized this.onSizeChanged((event: ISize) => { this._height = event.height; this._width = event.width; - if (this._positronReactRenderer) { - this.renderContainer(previewId); + if (this._positronReactRenderer && this._identifier) { + this.renderContainer(this._identifier); } }); - this.onVisibilityChanged((visible: boolean) => { - this._visible = visible; + await super.setInput(input, options, context, token); + } - if (this._positronReactRenderer) { - this.renderContainer(previewId); - } - }); + /** + * Clears the input. + */ + override clearInput(): void { + // Dispose the PositronReactRenderer. + this.disposeReactRenderer(); - await super.setInput(input, options, context, token); + // If there is an identifier, clear it. + if (this._identifier && this._preview) { + this._preview.dispose(); + this._identifier = undefined; + } + + // Call the base class's method. + super.clearInput(); } /** @@ -195,6 +208,11 @@ export class PositronPreviewEditor override dispose(): void { this.disposeReactRenderer(); + + if (this._preview) { + this._preview.dispose(); + } + super.dispose(); } }