Skip to content

Commit

Permalink
Use view type to store result panels
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Dover authored and Scott Dover committed Oct 19, 2023
1 parent c730faa commit d3f0951
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
28 changes: 12 additions & 16 deletions client/src/components/ResultPanel/ResultPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@ import { isSideResultEnabled, isSinglePanelEnabled } from "../utils/settings";
let resultPanel: WebviewPanel | undefined;
export const resultPanels: Record<string, WebviewPanel> = {};

export interface ResultsContext {
preventDefaultContextMenuItems: boolean;
uuid: string;
}

export const showResult = (html: string, uri?: Uri, title?: string) => {
const vscodeContext: ResultsContext = {
preventDefaultContextMenuItems: true,
uuid: v4(),
};
html = html
// Inject vscode context into our results html body
.replace(
"<body ",
`<body data-vscode-context='${JSON.stringify(vscodeContext)}' `,
`<body data-vscode-context='${JSON.stringify({
preventDefaultContextMenuItems: true,
})}' `,
)
// Make sure the html and body take up the full height of the parent
// iframe so that the context menu is clickable anywhere on the page
Expand All @@ -38,19 +31,23 @@ export const showResult = (html: string, uri?: Uri, title?: string) => {
}

if (!singlePanel || !resultPanel) {
const resultPanelId = `SASResultPanel-${v4()}`;
resultPanel = window.createWebviewPanel(
"SASResultPanel", // Identifies the type of the webview. Used internally
resultPanelId, // Identifies the type of the webview. Used internally
title, // Title of the panel displayed to the user
{
preserveFocus: true,
viewColumn: sideResult ? ViewColumn.Beside : ViewColumn.Active,
}, // Editor column to show the new webview panel in.
{}, // Webview options. More on these later.
);
resultPanel.onDidDispose(() => {
resultPanel = undefined;
delete resultPanels[vscodeContext.uuid];
});
resultPanel.onDidDispose(
((id) => () => {
delete resultPanels[id];
resultPanel = undefined;
})(resultPanelId),
);
resultPanels[resultPanelId] = resultPanel;
} else {
const editor = uri
? window.visibleTextEditors.find(
Expand All @@ -66,5 +63,4 @@ export const showResult = (html: string, uri?: Uri, title?: string) => {
);
}
resultPanel.webview.html = html;
resultPanels[vscodeContext.uuid] = resultPanel;
};
6 changes: 3 additions & 3 deletions client/src/components/ResultPanel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import { Disposable, Uri, commands, window, workspace } from "vscode";

import { SubscriptionProvider } from "../SubscriptionProvider";
import { ResultsContext, resultPanels } from "./ResultPanel";
import { resultPanels } from "./ResultPanel";

export class ResultPanelSubscriptionProvider implements SubscriptionProvider {
getSubscriptions(): Disposable[] {
return [
commands.registerCommand(
"SAS.saveHTML",
async (context: ResultsContext) => {
const panel = resultPanels[context.uuid] || undefined;
async (context: { webview: string }) => {
const panel = resultPanels[context.webview] || undefined;
if (!panel) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@
"webview/context": [
{
"command": "SAS.saveHTML",
"when": "webviewId == 'SASResultPanel'"
"when": "webviewId =~ /SASResultPanel-/"
}
],
"view/title": [
Expand Down

0 comments on commit d3f0951

Please sign in to comment.