Skip to content

Commit

Permalink
Change the value property of the callbacks onNodeName, `onEditabl…
Browse files Browse the repository at this point in the history
…e`, and `onClassName` into a lazy getter.
  • Loading branch information
josdejong committed Jan 13, 2022
1 parent ca2a205 commit 3ea0cd9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
10 changes: 10 additions & 0 deletions HISTORY.md
Expand Up @@ -3,6 +3,16 @@
https://github.com/josdejong/jsoneditor


## not yet published, version 9.6.0

- Pass a new property `value` along with the `onNodeName` callback, see #1409.
Thanks @brianpos.
- Implement the `value` property of the callbacks `onNodeName`, `onEditable`,
and `onClassName` for objects and arrays too (was `undefined` before).
Since this can be a heavy recursive operation, the property is changed into
a lazy getter.


## 2021-12-29, version 9.5.11

- Fix the font on Ubuntu for real by add the "ubuntu mono" font. See #1405.
Expand Down
19 changes: 16 additions & 3 deletions src/js/Node.js
Expand Up @@ -95,9 +95,12 @@ export class Node {

if ((this.editor.options.mode === 'tree' || this.editor.options.mode === 'form') &&
(typeof this.editor.options.onEditable === 'function')) {
const getValue = this.getValue.bind(this)
const editable = this.editor.options.onEditable({
field: this.field,
value: this.value,
get value() {
return getValue()
},
path: this.getPath()
})

Expand Down Expand Up @@ -944,7 +947,14 @@ export class Node {
typeof this.editor.options.onClassName === 'function' &&
this.dom.tree) {
removeAllClassNames(this.dom.tree)
const addClasses = this.editor.options.onClassName({ path: this.getPath(), field: this.field, value: this.value }) || ''
const getValue = this.getValue.bind(this)
const addClasses = this.editor.options.onClassName({
path: this.getPath(),
field: this.field,
get value() {
return getValue()
}
}) || ''
addClassName(this.dom.tree, 'jsoneditor-values ' + addClasses)
}
}
Expand Down Expand Up @@ -4061,11 +4071,14 @@ export class Node {
if (this.type === 'object' || this.type === 'array') {
if (this.editor.options.onNodeName) {
try {
const getValue = this.getValue.bind(this)
nodeName = this.editor.options.onNodeName({
path: this.getPath(),
size: count,
type: this.type,
value: this.value
get value() {
return getValue()
}
})
} catch (err) {
console.error('Error in onNodeName callback: ', err)
Expand Down

0 comments on commit 3ea0cd9

Please sign in to comment.