Skip to content

Commit

Permalink
Fix josdejong#1422: fix setSchema not working
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Mar 4, 2022
1 parent 94e5b13 commit ca43062
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/js/JSONEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ JSONEditor.prototype.setSchema = function (schema, schemaRefs) {
// add schema to the options, so that when switching to an other mode,
// the set schema is not lost
this.options.schema = schema
this.options.schemaRefs = schemaRefs

// validate now
this.validate()
Expand Down
31 changes: 23 additions & 8 deletions src/js/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -1741,12 +1741,7 @@ export class Node {
this.dom.select.appendChild(defaultOption)

// Iterate all enum values and add them as options
for (let i = 0; i < this.enum.length; i++) {
const option = document.createElement('option')
option.value = this.enum[i]
option.textContent = this.enum[i]
this.dom.select.appendChild(option)
}
this._updateEnumOptions()

this.dom.tdSelect = document.createElement('td')
this.dom.tdSelect.className = 'jsoneditor-tree'
Expand All @@ -1773,14 +1768,16 @@ export class Node {
delete this.valueFieldHTML
}
} else {
// cleanup select box when displayed
// cleanup select box when displayed, and attach the editable div instead
if (this.dom.tdSelect) {
this.dom.tdSelect.parentNode.removeChild(this.dom.tdSelect)
delete this.dom.tdSelect
delete this.dom.select
this.dom.tdValue.innerHTML = this.valueFieldHTML
this.dom.tdValue.style.visibility = ''
delete this.valueFieldHTML

this.dom.tdValue.appendChild(this.dom.value)
}
}

Expand Down Expand Up @@ -1852,6 +1849,23 @@ export class Node {
}
}

_updateEnumOptions () {
if (!this.enum || !this.dom.select) {
return
}

// clear the existing options
this.dom.select.innerHTML = ''

// Iterate all enum values and add them as options
for (let i = 0; i < this.enum.length; i++) {
const option = document.createElement('option')
option.value = this.enum[i]
option.textContent = this.enum[i]
this.dom.select.appendChild(option)
}
}

_deleteDomColor () {
if (this.dom.color) {
this.dom.tdColor.parentNode.removeChild(this.dom.tdColor)
Expand Down Expand Up @@ -2260,6 +2274,7 @@ export class Node {
domField.innerHTML = escapedField
}
this._updateSchema()
this._updateEnumOptions()
}

// apply value to DOM
Expand Down Expand Up @@ -4496,7 +4511,7 @@ Node._findSchema = (topLevelSchema, schemaRefs, path, currentSchema = topLevelSc
if (segment in currentSchema) {
currentSchema = currentSchema[segment]
} else {
throw Error(`Unable to resovle reference ${ref}`)
throw Error(`Unable to resolve reference ${ref}`)
}
}
} else if (ref.match(/#\//g)?.length === 1) {
Expand Down

0 comments on commit ca43062

Please sign in to comment.