Implemented option onError

This commit is contained in:
jos 2016-09-23 19:53:57 +02:00
parent 804c68010f
commit 0c3faa03ea
3 changed files with 42 additions and 6 deletions

View File

@ -18,12 +18,12 @@ export default class TextMode extends Component {
h('button', { h('button', {
class: 'jsoneditor-format', class: 'jsoneditor-format',
title: 'Format the JSON document', title: 'Format the JSON document',
onClick: this.format onClick: this.handleFormat
}), }),
h('button', { h('button', {
class: 'jsoneditor-compact', class: 'jsoneditor-compact',
title: 'Compact the JSON document', title: 'Compact the JSON document',
onClick: this.compact onClick: this.handleCompact
}) })
// TODO: implement a button "Fix JSON" // TODO: implement a button "Fix JSON"
]), ]),
@ -58,10 +58,40 @@ export default class TextMode extends Component {
}) })
} }
/** @private */
handleFormat = () => {
try {
this.format()
}
catch (err) {
this.handleError(err)
}
}
/** @private */
handleCompact = () => {
try {
this.compact()
}
catch (err) {
this.handleError(err)
}
}
/** @private */
handleError (err) {
if (this.props.options && this.props.options.onError) {
this.props.options.onError(err)
}
else {
console.error(err)
}
}
/** /**
* Format the json * Format the json
*/ */
format = () => { format () {
var json = this.get() var json = this.get()
var text = JSON.stringify(json, null, this.getIndentation()) var text = JSON.stringify(json, null, this.getIndentation())
this.setText(text) this.setText(text)
@ -70,7 +100,7 @@ export default class TextMode extends Component {
/** /**
* Compact the json * Compact the json
*/ */
compact = () => { compact () {
var json = this.get() var json = this.get()
var text = JSON.stringify(json) var text = JSON.stringify(json)
this.setText(text) this.setText(text)

View File

@ -37,7 +37,10 @@
window.patch = patch window.patch = patch
window.revert = revert window.revert = revert
}, },
// mode: 'text', onError: function (err) {
console.error(err)
alert(err)
},
indentation: 4 indentation: 4
} }
const editor = jsoneditor(container, options) const editor = jsoneditor(container, options)

View File

@ -25,7 +25,10 @@
* @typedef {Array.<{op: string, path?: string, from?: string, value?: *}>} JSONPatch * @typedef {Array.<{op: string, path?: string, from?: string, value?: *}>} JSONPatch
* *
* @typedef {{ * @typedef {{
* mode: 'tree' | 'text' * mode: 'tree' | 'text',
* indentation: number | string,
* onChange: function (patch: JSONPatch, revert: JSONPatch),
* onError: function (err: Error)
* }} Options * }} Options
* *
* @typedef {{ * @typedef {{