Skip to content

Commit

Permalink
json/yaml interaction bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioCasCeb committed Sep 20, 2023
1 parent 9e3bee6 commit 89b4237
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/web-new/src/scripts/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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");
}

});
Expand Down
40 changes: 29 additions & 11 deletions packages/web-new/src/scripts/json-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
import { editorList } from './editor.js'
import { generateTD } from './util.js'
import { getEditorData } from './editor.js'

/***********************************************************/
/* Yaml functionality */
Expand All @@ -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')
}
})
})
Expand All @@ -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)
}
}
})
}

0 comments on commit 89b4237

Please sign in to comment.