squiggle/packages/vscode-ext/media/previewWebview.js

40 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

(function () {
const vscode = acquireVsCodeApi();
const container = document.getElementById("root");
const root = ReactDOM.createRoot(container);
2022-06-20 15:06:30 +00:00
function updateContent(text, showSettings) {
root.render(
React.createElement(squiggle_components.SquigglePlayground, {
code: text,
2022-06-20 14:38:45 +00:00
showEditor: false,
2022-06-20 15:06:30 +00:00
showSummary: Boolean(showSettings.showSummary),
})
);
}
// Handle messages sent from the extension to the webview
window.addEventListener("message", (event) => {
const message = event.data; // The json data that the extension sent
switch (message.type) {
case "update":
2022-06-20 15:06:30 +00:00
const { text, showSettings } = message;
// Update our webview's content
2022-06-20 15:06:30 +00:00
updateContent(text, showSettings);
// Then persist state information.
// This state is returned in the call to `vscode.getState` below when a webview is reloaded.
2022-06-20 15:06:30 +00:00
vscode.setState({ text, showSettings });
return;
}
});
const state = vscode.getState();
if (state) {
2022-06-20 15:06:30 +00:00
updateContent(state.text, state.showSettings);
}
})();