From b3b31f28d6b8b0d2e4c1c825acec194e3e8af343 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 25 Aug 2021 15:35:49 +0200 Subject: [PATCH] Fix opening the Transform or Sort modal in code mode with invalid JSON contents not triggering the `onError` callback (see #1364) --- HISTORY.md | 8 +++--- src/js/textmode.js | 67 ++++++++++++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 4ee49c16d..ea2689719 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,10 +5,12 @@ https://github.com/josdejong/jsoneditor ## not yet published, version 9.5.4 -- Change the default behavior of error handling to open a basic alert instead - of logging the error in the console (see #1364). - Use `noreferrer` for window.open. Thanks @rajitbanerjee. -- Fix #1363: parsing error contains html caharacters. +- Fix #1363: parsing error contains html characters. +- Fix opening the Transform or Sort modal in code mode with invalid JSON + contents not triggering the `onError` callback (see #1364). +- Change the default behavior of error handling to open a basic alert instead + of logging the error in the console (see #1364). ## 2021-07-28, version 9.5.3 diff --git a/src/js/textmode.js b/src/js/textmode.js index 68a7e5269..d76c339c4 100644 --- a/src/js/textmode.js +++ b/src/js/textmode.js @@ -456,27 +456,31 @@ textmode._updateHistoryButtons = function () { * @private */ textmode._showSortModal = function () { - const me = this - const container = this.options.modalAnchor || DEFAULT_MODAL_ANCHOR - const json = this.get() + try { + const me = this + const container = this.options.modalAnchor || DEFAULT_MODAL_ANCHOR + const json = this.get() - function onSort (sortedBy) { - if (Array.isArray(json)) { - const sortedJson = sort(json, sortedBy.path, sortedBy.direction) + function onSort (sortedBy) { + if (Array.isArray(json)) { + const sortedJson = sort(json, sortedBy.path, sortedBy.direction) - me.sortedBy = sortedBy - me.update(sortedJson) - } + me.sortedBy = sortedBy + me.update(sortedJson) + } - if (isObject(json)) { - const sortedJson = sortObjectKeys(json, sortedBy.direction) + if (isObject(json)) { + const sortedJson = sortObjectKeys(json, sortedBy.direction) - me.sortedBy = sortedBy - me.update(sortedJson) + me.sortedBy = sortedBy + me.update(sortedJson) + } } - } - showSortModal(container, json, onSort, me.sortedBy) + showSortModal(container, json, onSort, me.sortedBy) + } catch (err) { + this._onError(err) + } } /** @@ -484,20 +488,25 @@ textmode._showSortModal = function () { * @private */ textmode._showTransformModal = function () { - const { modalAnchor, createQuery, executeQuery, queryDescription } = this.options - const json = this.get() - - showTransformModal({ - container: modalAnchor || DEFAULT_MODAL_ANCHOR, - json, - queryDescription, // can be undefined - createQuery, - executeQuery, - onTransform: query => { - const updatedJson = executeQuery(json, query) - this.update(updatedJson) - } - }) + try { + const { modalAnchor, createQuery, executeQuery, queryDescription } = this.options + + const json = this.get() + + showTransformModal({ + container: modalAnchor || DEFAULT_MODAL_ANCHOR, + json, + queryDescription, // can be undefined + createQuery, + executeQuery, + onTransform: query => { + const updatedJson = executeQuery(json, query) + this.update(updatedJson) + } + }) + } catch (err) { + this._onError(err) + } } /**