Skip to content

Commit

Permalink
Store panel references instead of html
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Dover authored and Scott Dover committed Oct 10, 2023
1 parent 7834fea commit 3d27e48
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 6 additions & 3 deletions client/src/components/ResultPanel/ResultPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "../Helper/SettingHelper";

let resultPanel: WebviewPanel | undefined;
export const resultsHtml: Record<string, string> = {};
export const resultPanels: Record<string, WebviewPanel> = {};

export interface ResultsContext {
preventDefaultContextMenuItems: boolean;
Expand All @@ -22,7 +22,6 @@ export const showResult = (html: string, uri?: Uri, title?: string) => {
preventDefaultContextMenuItems: true,
uuid: v4(),
};
resultsHtml[vscodeContext.uuid] = html;
html = html
// Inject vscode context into our results html body
.replace(
Expand Down Expand Up @@ -51,7 +50,10 @@ export const showResult = (html: string, uri?: Uri, title?: string) => {
}, // Editor column to show the new webview panel in.
{}, // Webview options. More on these later.
);
resultPanel.onDidDispose(() => (resultPanel = undefined));
resultPanel.onDidDispose(() => {
resultPanel = undefined;
delete resultPanels[vscodeContext.uuid];
});
} else {
const editor = uri
? window.visibleTextEditors.find(
Expand All @@ -67,4 +69,5 @@ export const showResult = (html: string, uri?: Uri, title?: string) => {
);
}
resultPanel.webview.html = html;
resultPanels[vscodeContext.uuid] = resultPanel;
};
11 changes: 7 additions & 4 deletions client/src/components/ResultPanel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import { Disposable, Uri, commands, window, workspace } from "vscode";

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

export class ResultPanelSubscriptionProvider implements SubscriptionProvider {
getSubscriptions(): Disposable[] {
return [
commands.registerCommand(
"SAS.saveHTML",
async (context: ResultsContext) => {
const html = resultsHtml[context.uuid] || "";
if (!html) {
const panel = resultPanels[context.uuid] || undefined;
if (!panel) {
return;
}
const uri = await window.showSaveDialog({
Expand All @@ -23,7 +23,10 @@ export class ResultPanelSubscriptionProvider implements SubscriptionProvider {
return;
}

await workspace.fs.writeFile(uri, new TextEncoder().encode(html));
await workspace.fs.writeFile(
uri,
new TextEncoder().encode(panel.webview.html),
);
},
),
];
Expand Down

0 comments on commit 3d27e48

Please sign in to comment.