DEV: update and improvements to json editor (#15294)

- changes on how errors are handled to prevent weird cases
- uses didInsert/willDestroy to setup/clean state
- updates json editor library to 2.6.1
This commit is contained in:
Joffrey JAFFEUX 2021-12-14 17:21:49 +01:00 committed by GitHub
parent 27fda37a8f
commit 031f4f06d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 17 deletions

View File

@ -9,8 +9,9 @@ import { schedule } from "@ember/runloop";
export default Component.extend({ export default Component.extend({
className: "json-editor-holder", className: "json-editor-holder",
editor: null, editor: null,
saveChangesCallback: null,
didReceiveAttrs() { didInsertElement() {
this._super(...arguments); this._super(...arguments);
loadScript("/javascripts/jsoneditor.js").then(() => { loadScript("/javascripts/jsoneditor.js").then(() => {
@ -33,7 +34,7 @@ export default Component.extend({
disable_edit_json: true, disable_edit_json: true,
disable_properties: true, disable_properties: true,
disable_collapse: true, disable_collapse: true,
show_errors: "always", show_errors: "never",
startval: this.model.value ? JSON.parse(this.model.value) : null, startval: this.model.value ? JSON.parse(this.model.value) : null,
}); });
}); });
@ -47,9 +48,22 @@ export default Component.extend({
@action @action
saveChanges() { saveChanges() {
const fieldValue = JSON.stringify(this.editor.getValue()); const errors = this.editor.validate();
this.saveChangesCallback(fieldValue); if (!errors.length) {
this.editor.destroy(); const fieldValue = JSON.stringify(this.editor.getValue());
this?.saveChangesCallback(fieldValue);
} else {
this.appEvents.trigger("modal-body:flash", {
text: errors.mapBy("message").join("\n"),
messageClass: "error",
});
}
},
willDestroyElement() {
this._super(...arguments);
this.editor?.destroy();
}, },
}); });

View File

@ -3,7 +3,7 @@
export const PUBLIC_JS_VERSIONS = { export const PUBLIC_JS_VERSIONS = {
"ace/ace.js": "ace.js/1.4.12/ace.js", "ace/ace.js": "ace.js/1.4.12/ace.js",
"jsoneditor.js": "@json-editor/json-editor/2.5.2/jsoneditor.js", "jsoneditor.js": "@json-editor/json-editor/2.6.1/jsoneditor.js",
"Chart.min.js": "chart.js/3.5.1/Chart.min.js", "Chart.min.js": "chart.js/3.5.1/Chart.min.js",
"chartjs-plugin-datalabels.min.js": "chartjs-plugin-datalabels.min.js":
"chartjs-plugin-datalabels/2.0.0/chartjs-plugin-datalabels.min.js", "chartjs-plugin-datalabels/2.0.0/chartjs-plugin-datalabels.min.js",

View File

@ -1,5 +1,4 @@
{{#d-modal-body rawTitle=(i18n "admin.site_settings.json_schema.modal_title" name=settingName)}} {{#d-modal-body rawTitle=(i18n "admin.site_settings.json_schema.modal_title" name=settingName)}}
<div id="json-editor-holder"></div> <div id="json-editor-holder"></div>
{{/d-modal-body}} {{/d-modal-body}}

View File

@ -9,7 +9,7 @@
"@discourse/itsatrap": "^2.0.10", "@discourse/itsatrap": "^2.0.10",
"@fortawesome/fontawesome-free": "5.11.2", "@fortawesome/fontawesome-free": "5.11.2",
"@highlightjs/cdn-assets": "^10.7.0", "@highlightjs/cdn-assets": "^10.7.0",
"@json-editor/json-editor": "^2.5.2", "@json-editor/json-editor": "^2.6.1",
"@popperjs/core": "v2.10.2", "@popperjs/core": "v2.10.2",
"@uppy/aws-s3": "^2.0.4", "@uppy/aws-s3": "^2.0.4",
"@uppy/utils": "^4.0.3", "@uppy/utils": "^4.0.3",

File diff suppressed because one or more lines are too long

View File

@ -157,12 +157,12 @@
resolved "https://registry.yarnpkg.com/@highlightjs/cdn-assets/-/cdn-assets-10.7.3.tgz#bccdef5cdfe4bf74ed9393cba528539f94753306" resolved "https://registry.yarnpkg.com/@highlightjs/cdn-assets/-/cdn-assets-10.7.3.tgz#bccdef5cdfe4bf74ed9393cba528539f94753306"
integrity sha512-Bz1c7NOPQE1h/0S3lYaDyA5wTimP9b8ppeN3HBfv+SF0IEeLsu86xneMcBvCaXBMwcwLc6/xGpyPOmPhCM8hAg== integrity sha512-Bz1c7NOPQE1h/0S3lYaDyA5wTimP9b8ppeN3HBfv+SF0IEeLsu86xneMcBvCaXBMwcwLc6/xGpyPOmPhCM8hAg==
"@json-editor/json-editor@^2.5.2": "@json-editor/json-editor@^2.6.1":
version "2.5.2" version "2.6.1"
resolved "https://registry.yarnpkg.com/@json-editor/json-editor/-/json-editor-2.5.2.tgz#d6f348d7e2f5bf9ccf2e3b34f637143674265e7f" resolved "https://registry.yarnpkg.com/@json-editor/json-editor/-/json-editor-2.6.1.tgz#169e8b88305d71ccac391c3ae22d4145bc63c9f7"
integrity sha512-OqrPtRw8FM2mSZbAFFvvxW6j8JvXCoQbhMgSiryhmcoByh3tQk+PuhK+2+bk9K/zn9PAt9KQsAiSc9sv6WIr/A== integrity sha512-8jH0n+BLO/jqBCZ3XXaes3NYzvQ+6KfO1p79MsIsTClIi5AvSvEzg3EWk4lsMWrUAFRwcM/CaififsoYDXMdMw==
dependencies: dependencies:
core-js "^3.6.5" core-js "^3.12.1"
"@mixer/parallel-prettier@^2.0.1": "@mixer/parallel-prettier@^2.0.1":
version "2.0.1" version "2.0.1"
@ -1137,10 +1137,10 @@ copy-descriptor@^0.1.0:
resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d" resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d"
integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40= integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40=
core-js@^3.6.5: core-js@^3.12.1:
version "3.9.0" version "3.19.3"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.9.0.tgz#790b1bb11553a2272b36e2625c7179db345492f8" resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.19.3.tgz#6df8142a996337503019ff3235a7022d7cdf4559"
integrity sha512-PyFBJaLq93FlyYdsndE5VaueA9K5cNB7CGzeCj191YYLhkQM0gdZR2SKihM70oF0wdqKSKClv/tEBOpoRmdOVQ== integrity sha512-LeLBMgEGSsG7giquSzvgBrTS7V5UL6ks3eQlUSbN8dJStlLFiRzUm5iqsRyzUB8carhfKjkJ2vzKqE6z1Vga9g==
core-util-is@~1.0.0: core-util-is@~1.0.0:
version "1.0.2" version "1.0.2"