// This script will be run within the webview itself // It cannot access the main VS Code APIs directly. (function () { const vscode = acquireVsCodeApi(); console.log("Started"); /** * @type {HTMLSelectElement} */ const toolDropDown = document.getElementById("tools"); toolDropDown.addEventListener('change', event => { vscode.postMessage({ type: "toolSelected", toolName: toolDropDown.options[toolDropDown.selectedIndex].value }); }); // Handle messages sent from the extension to the webview window.addEventListener('message', event => { const message = event.data; // The json data that the extension sent console.log("Received message"); switch (message.type) { case 'updateConstants': { updateConstants(message.constants, message.uri); break; } case 'updateOptions': { updateOptions(message.options); break; } case 'fillTools': { fillTools(message.tools); break; } } }); const runButton = document.getElementById("run-button"); runButton.addEventListener("click", event => { const state = vscode.getState(); for (const file of state.constants) { if (file.uri === state.currentUri) { vscode.postMessage({type: "runTool", uri: state.currentUri, toolName: toolDropDown.options[toolDropDown.selectedIndex].value, constants: file.constants, options: []}); // TODO: Actually send option values break; } } }); function fillTools(tools) { const select = document.querySelector("#tools"); select.innerHTML = ""; for (const tool of tools) { const option = document.createElement("option"); option.value = tool; option.text = tool; select.appendChild(option); } const oldState = vscode.getState() || { tools: [], constants: [], options: [] }; vscode.setState({ tools: tools, constants: oldState.constants, options: oldState.options }); } function addConstantItem(ul, name, value) { const li = document.createElement("li"); const nameBox = document.createElement("label"); nameBox.id = "name-" + name; nameBox.appendChild(document.createTextNode(name)); nameBox.classList.add("name"); li.appendChild(nameBox); const valueBox = document.createElement("input"); valueBox.id = "value-" + name; valueBox.type = "text"; valueBox.value = value; valueBox.classList.add("value"); valueBox.addEventListener("input", event => { let oldState = vscode.getState(); for (const file of oldState.constants) { if (file.uri === oldState.currentUri) { for (const constant of file.constants) { if (constant.name === name) { constant.value = valueBox.value; break; } } break; } } vscode.setState({ tools: oldState.tools, constants: oldState.constants, options: oldState.options, currentUri: oldState.currentUri }); }); li.appendChild(valueBox); ul.appendChild(li); } function addOptionItem(ul, name, value) { const li = document.createElement("li"); const nameBox = document.createElement("input"); nameBox.id = "name-" + name; nameBox.type = "text"; nameBox.value = name; nameBox.classList.add("name"); li.appendChild(nameBox); const valueBox = document.createElement("input"); valueBox.id = "value-" + name; valueBox.type = "text"; valueBox.value = value; valueBox.classList.add("value"); li.appendChild(valueBox); ul.appendChild(li); } /** * * @param {Array<{ uri: string, constants: Array<{ name: string, value: string }> }>} constants * @param {string} uri */ function updateConstants(constants, uri) { const constantsUl = document.querySelector("#constants"); const oldState = vscode.getState(); for (const file of constants) { for (const constant of file.constants) { const index = oldState.constants.findIndex(x => x.uri === file.uri); if (index !== -1) { const oldConstantIndex = oldState.constants[index].constants.findIndex(x => x.name === constant.name); if (oldConstantIndex !== -1) { constant.value = oldState.constants[index].constants[oldConstantIndex].value; } } } } constantsUl.innerHTML = ""; for (const file of constants) { if (file.uri === uri) { for (const constant of file.constants) { addConstantItem(constantsUl, constant.name, constant.value); } break; } } vscode.setState({ tools: oldState.tools, constants: constants, options: oldState.options, currentUri: uri }); } /** * @param {Array<{name: string, value: string}>} options */ function updateOptions(options) { const optionsUl = document.querySelector("#options"); optionsUl.innerHTML = ""; for (const option of options) { addOptionItem(optionsUl, option.name, option.value); } const oldState = vscode.getState(); vscode.setState({ tools: oldState.tools, constants: oldState.constants, options: options, currentUri: oldState.currentUri }); } const oldState = vscode.getState() || { tools: [], constants: [], options: [], currentUri: "" }; const tools = oldState.tools; const constants = oldState.constants; const options = oldState.options; const currentUri = oldState.currentUri; if (oldState.tools.length === 0) { vscode.postMessage({ type: "init" }); } else { fillTools(tools); updateOptions(options); updateConstants(constants, currentUri); } }());