diff --git a/packages/web-new/src/scripts/editor.js b/packages/web-new/src/scripts/editor.js index 799a73952..c2b93aed9 100644 --- a/packages/web-new/src/scripts/editor.js +++ b/packages/web-new/src/scripts/editor.js @@ -134,7 +134,7 @@ export function createIde(ideNumber, exampleValue) { "@context": "https://www.w3.org/2022/wot/td/v1.1", "id": "urn:uuid:0804d572-cce8-422a-bb7c-4412fcd56f06", "@type": "Thing", - "title": `My Thing Template`, + "title": `Thing Template`, "description": "This is your customizable template. Edit it to fit your Thing Description or Thing Model needs!", "securityDefinitions": { "basic_sc": { "scheme": "basic", "in": "header" } @@ -258,7 +258,7 @@ async function initEditor(ideNumber, editorValue, editorLanguage) { markTypos(editorInstance.getModel()); } } catch (err) { - console.error("Not a proper JSON object"); + console.error("Invalid JSON object"); } }); diff --git a/packages/web-new/src/scripts/json-yaml.js b/packages/web-new/src/scripts/json-yaml.js index b0e9f199d..2b1b14a6e 100644 --- a/packages/web-new/src/scripts/json-yaml.js +++ b/packages/web-new/src/scripts/json-yaml.js @@ -20,6 +20,7 @@ */ import { editorList } from './editor.js' import { generateTD } from './util.js' +import { getEditorData } from './editor.js' /***********************************************************/ /* Yaml functionality */ @@ -34,18 +35,18 @@ jsonBtn.checked = true //Click event to show the warning text before converting the td/tm -yamlBtn.addEventListener("click", ()=> { +yamlBtn.addEventListener("click", () => { editorList.forEach(editorInstance => { - if(editorInstance["_domElement"].classList.contains("active")){ - try{ + if (editorInstance["_domElement"].classList.contains("active") && editorInstance["_domElement"]["dataset"]["modeId"] == "json") { + try { JSON.parse(editorInstance.getValue()) + yamlWarning.classList.remove('closed') } - catch(err){ + catch (err) { alert('TD is not a valid JSON object'); jsonBtn.checked = true return } - yamlWarning.classList.remove('closed') } }) }) @@ -62,17 +63,34 @@ yamlConfirmBtn.addEventListener("click", () => { convertJsonYaml() }) -jsonBtn.addEventListener("click", ()=> { - convertJsonYaml() +jsonBtn.addEventListener("click", () => { + editorList.forEach(editorInstance => { + if (editorInstance["_domElement"].classList.contains("active") && editorInstance["_domElement"]["dataset"]["modeId"] == "yaml") { + try { + getEditorData(editorInstance) + convertJsonYaml() + } + catch (err) { + alert('TD is not a valid YAML object'); + yamlBtn.checked = true + return + } + } + }) }) /** - * Get the currently active editor and its value and convert to json or yaml + * Get the currently active editor and convert it to json or yaml */ -function convertJsonYaml(){ +function convertJsonYaml() { editorList.forEach(editorInstance => { - if(editorInstance["_domElement"].classList.contains("active")){ - generateTD(jsonBtn.checked === true ? "json" : "yaml", editorInstance) + if (editorInstance["_domElement"].classList.contains("active")) { + try { + const editorData = getEditorData(editorInstance) + generateTD(editorData[0] === "json" ? "yaml" : "json", editorInstance) + } catch (err) { + console.error(err) + } } }) } \ No newline at end of file