Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Editor extension doesn't throw error on invalid data #65

Merged
merged 2 commits into from
Apr 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions json-editor/src/js/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ContentstackUIExtension.init().then(function (extension) {
ace: ace,
onChange: function () {
clearTimeout(typingTimer);
typingTimer = setTimeout(updateFieldValue, 1500);
typingTimer = setTimeout(updateFieldValue, 600);
},
};

Expand All @@ -36,12 +36,25 @@ ContentstackUIExtension.init().then(function (extension) {
//TODO: set focus on field to show active users or highlight the field
});

var element = document.getElementById("jsoneditor");

var NewTag = document.createElement("span");
NewTag.className = 'invalid';
var text = document.createTextNode("Invalid JSON format !!");
NewTag.appendChild(text);

async function updateFieldValue() {
try {
var value = await jsonEditor.get();
await extensionField.field.setData(value);
console.info('Set data successful');
} catch (error) {
throw error;
value = await jsonEditor.get();
await extensionField.field.setData(value).then(function(){
value = typeof value !== "string"
? JSON.stringify(value)
: value;
json = JSON.parse(value);
element.removeChild(NewTag);
return json;
})} catch (error) {
element.appendChild(NewTag);
document.getElementsByClassName("save-btn").disabled = true;
}
}