Skip to content

Commit

Permalink
allow for pop out viewer in editor tab and split editors (#5696)
Browse files Browse the repository at this point in the history
part of #5500 


You should now be able to pop out (needed to handle `clearInput()`
correctly)

![Screenshot 2024-12-10 at 4 55
38 PM](https://github.com/user-attachments/assets/cf45c5f8-5c64-4ab0-a2d9-c2a98dcb25f5)

and use split editors (needed to have editor create/handle webviews and
not share them per url)!

![Screenshot 2024-12-10 at 4 56
16 PM](https://github.com/user-attachments/assets/1c3f87ce-6c83-4fb9-86ac-760fecba21d2)
  • Loading branch information
isabelizimm authored Dec 11, 2024
1 parent e614f78 commit 55ddd14
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class PositronPreviewService extends Disposable implements IPositronPrevi

private _onDidChangeActivePreviewWebview = new Emitter<string>;

private _editors: Map<string, { uri: URI; webview: PreviewWebview; title?: string }> = new Map();
private _editors: Map<string, { uri: URI; title?: string }> = new Map();

constructor(
@ICommandService private readonly _commandService: ICommandService,
Expand Down Expand Up @@ -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
});

Expand All @@ -528,15 +527,16 @@ 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 {
return this._editors.get(previewId)?.title;
}

public disposeEditor(previewId: string): void {
this._editors.get(previewId)?.webview.dispose();
// Remove the preview
this._editors.delete(previewId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,6 +60,8 @@ export class PositronPreviewEditor

private readonly _onFocusedEmitter = this._register(new Emitter<void>());

private _preview: PreviewWebview | undefined;

get identifier(): string | undefined {
return this._identifier;
}
Expand Down Expand Up @@ -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(
<PositronPreviewContextProvider
positronPreviewService={this._positronPreviewService}
>
<EditorPreviewContainer
preview={this._positronPreviewService.editorWebview(previewId)}
preview={this._preview}
width={this._width}
height={this._height}
visible={this.containerVisible}
visible={this._visible}
/>
</PositronPreviewContextProvider>
);
Expand All @@ -144,31 +148,40 @@ export class PositronPreviewEditor
context: IEditorOpenContext,
token: CancellationToken
): Promise<void> {
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();
}

/**
Expand All @@ -195,6 +208,11 @@ export class PositronPreviewEditor

override dispose(): void {
this.disposeReactRenderer();

if (this._preview) {
this._preview.dispose();
}

super.dispose();
}
}

0 comments on commit 55ddd14

Please sign in to comment.