Handle error when switching mode

This commit is contained in:
jos 2016-09-29 14:26:20 +02:00
parent 873d5f8ae2
commit 1bcc6382aa
4 changed files with 23 additions and 8 deletions

View File

@ -33,11 +33,10 @@ export default class TextMode extends Component {
h('div', {class: 'jsoneditor-vertical-menu-separator'}),
this.props.options.modes && h(ModeButton, {
open: this.state.modeMenuOpen,
modes: this.props.options.modes,
mode: this.props.mode,
onMode: this.props.onMode,
onClick: this.handleShowModeMenu
onError: this.handleError
})
]),
@ -92,7 +91,7 @@ export default class TextMode extends Component {
}
/** @private */
handleError (err) {
handleError = (err) => {
if (this.props.options && this.props.options.onError) {
this.props.options.onError(err)
}

View File

@ -101,7 +101,8 @@ export default class TreeMode extends Component {
this.props.options.modes && h(ModeButton, {
modes: this.props.options.modes,
mode: this.props.mode,
onMode: this.props.onMode
onMode: this.props.onMode,
onError: this.handleError
})
])
}
@ -199,6 +200,16 @@ export default class TreeMode extends Component {
this.emitOnChange (actions, revert)
}
/** @private */
handleError = (err) => {
if (this.props.options && this.props.options.onError) {
this.props.options.onError(err)
}
else {
console.error(err)
}
}
/**
* Emit an onChange event when there is a listener for it.
* @param {JSONPatch} patch

View File

@ -12,7 +12,7 @@ export default class ModeButton extends Component {
}
/**
* @param {{modes: string[], mode: string, onMode: function}} props
* @param {{modes: string[], mode: string, onMode: function, onError: function}} props
* @param state
* @return {*}
*/

View File

@ -3,7 +3,7 @@ import { toCapital } from '../utils/stringUtils'
export default class ModeMenu extends Component {
/**
* @param {{open, modes, mode, onMode}} props
* @param {{open, modes, mode, onMode, onError}} props
* @param {Obect} state
* @return {JSX.Element}
*/
@ -15,9 +15,14 @@ export default class ModeMenu extends Component {
class: 'jsoneditor-menu-button jsoneditor-type-modes' +
((mode === props.mode) ? ' jsoneditor-selected' : ''),
onClick: () => {
try {
props.onMode(mode)
this.setState({ open: false })
}
catch (err) {
props.onError(err)
}
}
}, toCapital(mode))
})