Skip to content

Commit

Permalink
support for graph webview in web extension
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardoferretti committed Sep 16, 2024
1 parent 57af145 commit 5508d8c
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions packages/foam-vscode/src/features/panels/dataviz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,33 @@ async function getWebviewContent(
context: vscode.ExtensionContext,
panel: vscode.WebviewPanel
) {
const datavizPath = vscode.Uri.joinPath(
vscode.Uri.file(context.extensionPath),
const datavizUri = vscode.Uri.joinPath(
context.extensionUri,
'static',
'dataviz'
);

const getWebviewUri = (fileName: string) =>
panel.webview.asWebviewUri(vscode.Uri.joinPath(datavizPath, fileName));
panel.webview.asWebviewUri(vscode.Uri.joinPath(datavizUri, fileName));

const indexHtml = await vscode.workspace.fs.readFile(
vscode.Uri.joinPath(datavizPath, 'index.html')
);
const indexHtml =
vscode.env.uiKind == vscode.UIKind.Desktop
? new TextDecoder('utf-8').decode(
await vscode.workspace.fs.readFile(
vscode.Uri.joinPath(datavizUri, 'index.html')
)
)
: await fetch(getWebviewUri('index.html').toString()).then(r => r.text());

// Replace the script paths with the appropriate webview URI.
const filled = new TextDecoder('utf-8')
.decode(indexHtml)
.replace(/data-replace (src|href)="[^"]+"/g, match => {
const filled = indexHtml.replace(
/data-replace (src|href)="[^"]+"/g,
match => {
const i = match.indexOf(' ');
const j = match.indexOf('=');
const uri = getWebviewUri(match.slice(j + 2, -1).trim());
return match.slice(i + 1, j) + '="' + uri.toString() + '"';
});
}
);

return filled;
}
Expand Down

0 comments on commit 5508d8c

Please sign in to comment.