Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Commit 306e9bf

Browse files
committed
updated with jsoneditor
1 parent f6f3c23 commit 306e9bf

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

index.html

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,19 @@
44
<title>JSON Schema Validator Web Component Demo</title>
55
<script src="./index.js"></script>
66
</head>
7-
<body style="margin: 0 auto; max-width: 900px; padding: 50px; width: 100%;">
7+
<body style="box-sizing: border-box; margin: 0 auto; max-width: 900px; padding: 50px; width: 100%;">
88
<div>
9-
<json-schema-validator metaschema="./json-schema-draft-04.json" schema="./example-schema.json" />
9+
<json-schema-validator jsoneditor="./node_modules/jsoneditor" metaschema="./json-schema-draft-04.json" schema="./example-schema.json" />
1010
</div>
1111
<script>
1212
setTimeout(() => {
1313
fetch("./example-data.json")
1414
.then(response => response.text())
1515
.then(text => {
1616
const validator = document.querySelector("json-schema-validator");
17-
const textarea = validator.querySelector("textarea")
18-
textarea.innerHTML = text;
19-
validator.validateInput();
17+
validator.editor.setText(text)
2018
})
21-
}, 500)
19+
}, 1000)
2220
</script>
2321
</body>
2422
</html>

index.js

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,23 @@
99
constructor() {
1010
// establish prototype chain
1111
super();
12-
this.ajvurl = 'https://cdnjs.cloudflare.com/ajax/libs/ajv/6.5.5/ajv.min.js'
12+
this.ajv_url = 'https://unpkg.com/ajv@6.5.5/dist/ajv.min.js'
13+
this.json_editor_url = 'https://unpkg.com/jsoneditor@5.25.0';
14+
this.promises = {};
1315
}
1416

1517
static get observedAttributes() {
16-
return ['ajv', 'metaschema', 'schema'];
18+
return ['jsoneditor', 'metaschema', 'schema'];
1719
}
1820

1921
// fires after the element has been attached to the DOM
2022
connectedCallback() {
2123
const uid = Math.ceil((Math.random() * 10e10).toString())
2224
this.id = `schema-validator-${uid}`
2325
this.innerHTML = `<div>
24-
<textarea
25-
onChange="event.target.parentElement.parentElement.validateInput()"
26-
onKeyup="event.target.parentElement.parentElement.validateInput()"
27-
style="box-sizing: border-box; height: 500px; padding: 15px; resize: none; width: 100%;"
28-
></textarea>
26+
<div id="jsoneditor" style="box-sizing: border-box; height: 500px; padding: 15px; width: 100%;"></div>
2927
<div id="schema-validation-errors" style="box-sizing: border-box; padding: 15px; width: 100%;"></div>
28+
<link href="${this.json_editor_url}/dist/jsoneditor.min.css" rel="stylesheet" type="text/css">
3029
</div>`;
3130
this.update();
3231
}
@@ -60,19 +59,29 @@
6059
}
6160

6261
getAjv() {
63-
if (this.ajv) {
64-
return Promise.resolve(this.ajv);
65-
} else {
66-
return new Promise(resolve => {
62+
return this.loadScript(this.ajv_url).then(() => {
63+
if (!this.ajv) {
64+
this.ajv = new window.Ajv({ allErrors: true, schemaId: 'id' });
65+
}
66+
return this.ajv
67+
})
68+
}
69+
70+
getJSONEditor() {
71+
return this.loadScript(this.json_editor_url + '/dist/jsoneditor.min.js')
72+
.then(() => window.JSONEditor)
73+
}
74+
75+
loadScript(url) {
76+
if (!this.promises[url]) {
77+
this.promises[url] = new Promise(resolve => {
6778
const script = document.createElement("script");
68-
script.src = this.ajvurl;
69-
script.onload = () => {
70-
this.ajv = new window.Ajv({ allErrors: true, schemaId: 'id' });
71-
resolve(this.ajv)
72-
};
79+
script.src = url;
80+
script.onload = resolve
7381
document.body.appendChild(script)
7482
})
7583
}
84+
return Promise.resolve(this.promises[url])
7685
}
7786

7887
getStyle() {
@@ -103,21 +112,35 @@
103112
}
104113

105114
update() {
106-
this.ajvurl = this.getAttribute('ajv') || this.ajvurl
115+
//this.ajvurl = this.getAttribute('ajv') || this.ajvurl
107116
this.schemaurl = this.getAttribute('schema')
108117
this.metaschemaurl = this.getAttribute('metaschema')
109-
Promise.all([
118+
this.updated = Promise.all([
110119
this.getAjv(),
120+
this.getJSONEditor(),
111121
this.getMetaSchema(),
112122
this.getSchema()
113-
]).then(([ajv, metaschema, schema]) => {
114-
console.log("[ajv, metaschema, schema]:", [ajv, metaschema, schema])
123+
]).then(([ajv, JSONEditor, metaschema, schema]) => {
124+
125+
console.log("[ajv, JSONEditor, metaschema, schema]:", [ajv, JSONEditor, metaschema, schema])
115126
try {
116-
this.ajv.addMetaSchema(metaschema);
127+
ajv.addMetaSchema(metaschema);
117128
} catch (error) {
118129
console.warn(error);
119130
}
120-
this.validate = this.ajv.compile(schema);
131+
132+
if (!this.editor) {
133+
// create the editor
134+
var container = this.querySelector("#jsoneditor");
135+
var options = {
136+
ajv,
137+
mode: 'text',
138+
modes: ['text', 'tree']
139+
};
140+
this.editor = new JSONEditor(container, options, '');
141+
this.editor.setSchema(schema)
142+
this.editor.setText('')
143+
}
121144
})
122145
}
123146

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@
2323
"bugs": {
2424
"url": "https://github.com/GSA/json-schema-validator-web-component/issues"
2525
},
26-
"homepage": "https://github.com/GSA/json-schema-validator-web-component#readme"
26+
"homepage": "https://github.com/GSA/json-schema-validator-web-component#readme",
27+
"dependencies": {
28+
"jsoneditor": "^5.25.0"
29+
}
2730
}

0 commit comments

Comments
 (0)