microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/vscode/src/webview/editor.tsx
109lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | /// <reference types="@types/vscode-webview"/> |
| 5 | |
| 6 | const vscodeApi = acquireVsCodeApi(); |
| 7 | |
| 8 | import { render } from "preact"; |
| 9 | import DOMPurify from "dompurify"; |
| 10 | import { CircuitPanel, CircuitProps } from "qsharp-lang/ux"; |
| 11 | import { setThemeStylesheet } from "./theme"; |
| 12 | |
| 13 | window.addEventListener("message", onMessage); |
| 14 | window.addEventListener("load", main); |
| 15 | |
| 16 | type CircuitState = { |
| 17 | viewType: "circuit"; |
| 18 | props: CircuitProps; |
| 19 | }; |
| 20 | |
| 21 | type State = { viewType: "loading" } | CircuitState; |
| 22 | const loadingState: State = { viewType: "loading" }; |
| 23 | let state: State = loadingState; |
| 24 | |
| 25 | function main() { |
| 26 | state = (vscodeApi.getState() as any) || loadingState; |
| 27 | render(<App state={state} />, document.body); |
| 28 | setThemeStylesheet(); |
| 29 | readFromTextDocument(); |
| 30 | } |
| 31 | |
| 32 | function onMessage(event: any) { |
| 33 | const message = event.data; |
| 34 | if (!message?.command) { |
| 35 | console.error("Unknown message: ", message); |
| 36 | return; |
| 37 | } |
| 38 | switch (message.command) { |
| 39 | case "error": { |
| 40 | const sanitizedMessage = DOMPurify.sanitize(message.props.message); |
| 41 | const sanitizedTitle = DOMPurify.sanitize(message.props.title); |
| 42 | const innerHTML = ` |
| 43 | <div class="error"> |
| 44 | <h1>${sanitizedTitle}</h1> |
| 45 | <p>${sanitizedMessage}</p> |
| 46 | </div> |
| 47 | `; |
| 48 | document.body.innerHTML = innerHTML; // CodeQL [SM04949] message data is not untrusted, handler is running in an extension, and is sanitized. |
| 49 | return; |
| 50 | } |
| 51 | case "circuit": |
| 52 | { |
| 53 | // Check if the received circuit is different from the current state |
| 54 | if ( |
| 55 | state.viewType === "circuit" && |
| 56 | JSON.stringify(state.props.circuit) === |
| 57 | JSON.stringify(message.props.circuit) |
| 58 | ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | state = { |
| 63 | viewType: "circuit", |
| 64 | ...message, |
| 65 | }; |
| 66 | } |
| 67 | break; |
| 68 | default: |
| 69 | console.error("Unknown command: ", message.command); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | vscodeApi.setState(state); |
| 74 | render(<App state={state} />, document.body); |
| 75 | } |
| 76 | |
| 77 | function readFromTextDocument() { |
| 78 | vscodeApi.postMessage({ command: "read" }); |
| 79 | } |
| 80 | |
| 81 | function updateTextDocument(circuit: any) { |
| 82 | vscodeApi.postMessage({ |
| 83 | command: "update", |
| 84 | text: JSON.stringify(circuit, null, 2), |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | function runCircuit() { |
| 89 | vscodeApi.postMessage({ command: "run" }); |
| 90 | } |
| 91 | |
| 92 | function App({ state }: { state: State }) { |
| 93 | switch (state.viewType) { |
| 94 | case "loading": |
| 95 | return <div>Loading...</div>; |
| 96 | case "circuit": |
| 97 | return ( |
| 98 | <CircuitPanel |
| 99 | {...state.props} |
| 100 | isEditable={true} |
| 101 | editCallback={updateTextDocument} |
| 102 | runCallback={runCircuit} |
| 103 | ></CircuitPanel> |
| 104 | ); |
| 105 | default: |
| 106 | console.error("Unknown view type in state", state); |
| 107 | return <div>Loading error</div>; |
| 108 | } |
| 109 | } |
| 110 | |