|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +/* eslint-disable no-undef */ |
| 7 | +/* eslint-disable no-case-declarations */ |
| 8 | + |
| 9 | +const autoSaveIntervalTimeout = 1000 |
| 10 | +const checkThreatComposerAPITimeout = 50 |
| 11 | + |
| 12 | +const vscode = acquireVsCodeApi() |
| 13 | +const instanceMapper = {} |
| 14 | +let disableAutoSave = false |
| 15 | +let theme = 'unknown' |
| 16 | + |
| 17 | +// Handle messages sent from the extension to the webview |
| 18 | +window.addEventListener('message', handleMessage) |
| 19 | + |
| 20 | +function handleMessage(event) { |
| 21 | + const message = event.data // The json data that the extension sent |
| 22 | + |
| 23 | + if (message.messageType === 'BROADCAST') { |
| 24 | + switch (message.command) { |
| 25 | + case 'FILE_CHANGED': |
| 26 | + const fileContents = message.fileContents |
| 27 | + |
| 28 | + // Persist state information. |
| 29 | + // This state is returned in the call to `vscode.getState` below when a webview is reloaded. |
| 30 | + vscode.setState({ |
| 31 | + fileName: message.fileName, |
| 32 | + filePath: message.filePath, |
| 33 | + fileContents: fileContents, |
| 34 | + }) |
| 35 | + |
| 36 | + // Update our webview's content |
| 37 | + updateContent(fileContents) |
| 38 | + break |
| 39 | + |
| 40 | + case 'THEME_CHANGED': |
| 41 | + applyTheme(message.newTheme) |
| 42 | + break |
| 43 | + |
| 44 | + case 'OVERWRITE_FILE': |
| 45 | + disableAutoSave = false |
| 46 | + break |
| 47 | + } |
| 48 | + } else if (message.messageType === 'RESPONSE') { |
| 49 | + switch (message.command) { |
| 50 | + case 'SAVE_FILE': |
| 51 | + if (message.isSuccess) { |
| 52 | + console.log('File Saved successfully') |
| 53 | + disableAutoSave = false |
| 54 | + } else { |
| 55 | + console.log('File Save unsuccessful') |
| 56 | + } |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function updateContent(/** @type {string} */ text) { |
| 63 | + if (document.readyState === 'complete') { |
| 64 | + void render(text) |
| 65 | + } else { |
| 66 | + document.addEventListener('DOMContentLoaded', function () { |
| 67 | + void render(text) |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +async function checkThreatComposerAPI() { |
| 73 | + while (!window.threatcomposer || !window.threatcomposer.setCurrentWorkspaceData) { |
| 74 | + await new Promise(r => setTimeout(r, checkThreatComposerAPITimeout)) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Render the document in the webview. |
| 80 | + */ |
| 81 | +async function render(/** @type {string} */ text) { |
| 82 | + await checkThreatComposerAPI() |
| 83 | + |
| 84 | + vscode.postMessage({ |
| 85 | + command: 'LOAD_STAGE', |
| 86 | + messageType: 'BROADCAST', |
| 87 | + loadStage: 'API_LOADED', |
| 88 | + }) |
| 89 | + |
| 90 | + try { |
| 91 | + let threatModel = text && JSON.parse(text) |
| 92 | + await window.threatcomposer.setCurrentWorkspaceData(threatModel) |
| 93 | + } catch (e) { |
| 94 | + disableAutoSave = true |
| 95 | + await window.threatcomposer.setCurrentWorkspaceData('') |
| 96 | + vscode.postMessage({ |
| 97 | + command: 'LOG', |
| 98 | + messageType: 'BROADCAST', |
| 99 | + logMessage: e.message, |
| 100 | + logType: 'ERROR', |
| 101 | + showNotification: true, |
| 102 | + notificationType: 'INVALID_JSON', |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + let defaultTemplate = '' |
| 107 | + |
| 108 | + if (text === '') { |
| 109 | + const initialState = await window.threatcomposer.getCurrentWorkspaceData() |
| 110 | + defaultTemplate = window.threatcomposer.stringifyWorkspaceData(initialState) |
| 111 | + } |
| 112 | + |
| 113 | + window.threatcomposer.addEventListener('save', e => { |
| 114 | + const stringyfiedData = window.threatcomposer.stringifyWorkspaceData(e.detail) |
| 115 | + const currentState = vscode.getState() |
| 116 | + currentState.fileContents = stringyfiedData |
| 117 | + vscode.setState(currentState) |
| 118 | + disableAutoSave = true |
| 119 | + |
| 120 | + vscode.postMessage({ |
| 121 | + command: 'SAVE_FILE', |
| 122 | + messageType: 'REQUEST', |
| 123 | + fileContents: stringyfiedData, |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + autoSaveInterval = setInterval(async () => { |
| 128 | + if (disableAutoSave) { |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + const data = await window.threatcomposer.getCurrentWorkspaceData() |
| 133 | + const stringyfiedData = window.threatcomposer.stringifyWorkspaceData(data) |
| 134 | + |
| 135 | + const currentState = vscode.getState() |
| 136 | + |
| 137 | + if (stringyfiedData === defaultTemplate || stringyfiedData === currentState.fileContents) { |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + currentState.fileContents = stringyfiedData |
| 142 | + vscode.setState(currentState) |
| 143 | + |
| 144 | + vscode.postMessage({ |
| 145 | + command: 'AUTO_SAVE_FILE', |
| 146 | + messageType: 'REQUEST', |
| 147 | + fileContents: stringyfiedData, |
| 148 | + }) |
| 149 | + }, autoSaveIntervalTimeout) |
| 150 | + |
| 151 | + vscode.postMessage({ |
| 152 | + command: 'LOAD_STAGE', |
| 153 | + messageType: 'BROADCAST', |
| 154 | + loadStage: 'RENDER_COMPLETE', |
| 155 | + }) |
| 156 | +} |
| 157 | + |
| 158 | +function applyTheme(newTheme) { |
| 159 | + if (!newTheme || theme === newTheme) { |
| 160 | + return |
| 161 | + } |
| 162 | + theme = newTheme |
| 163 | + |
| 164 | + window.threatcomposer.applyTheme(newTheme) |
| 165 | +} |
| 166 | + |
| 167 | +// Webviews are normally torn down when not visible and re-created when they become visible again. |
| 168 | +// State lets us save information across these re-loads |
| 169 | +const state = vscode.getState() |
| 170 | +if (state && state.fileContents) { |
| 171 | + vscode.postMessage({ |
| 172 | + command: 'RELOAD', |
| 173 | + messageType: 'REQUEST', |
| 174 | + }) |
| 175 | +} else { |
| 176 | + vscode.postMessage({ |
| 177 | + command: 'INIT', |
| 178 | + messageType: 'REQUEST', |
| 179 | + }) |
| 180 | +} |
0 commit comments