From bc0e99ab4a79e562f2aacb8e300841e4620d116e Mon Sep 17 00:00:00 2001 From: jos Date: Wed, 10 Jul 2019 14:27:46 +0200 Subject: [PATCH] Fixed #730: in `code` mode, there was an initial undo action which clears the content --- HISTORY.md | 6 ++++++ src/js/textmode.js | 26 +++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index afd3ae0..5523aa0 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,12 @@ https://github.com/josdejong/jsoneditor +## not yet published, version 6.1.1 + +- Fixed #730: in `code` mode, there was an initial undo action which clears + the content. + + ## 2019-06-22, version 6.1.0 - Implemented menu options `sort` and `transform` for modes `code` and `text`. diff --git a/src/js/textmode.js b/src/js/textmode.js index 934aa97..4556658 100644 --- a/src/js/textmode.js +++ b/src/js/textmode.js @@ -734,10 +734,12 @@ textmode.getText = function() { }; /** - * Set the text contents of the editor + * Set the text contents of the editor and optionally clear the history * @param {String} jsonText + * @param {boolean} clearHistory Only applicable for mode 'code' + * @private */ -textmode.setText = function(jsonText) { +textmode._setText = function(jsonText, clearHistory) { var text; if (this.options.escapeUnicode === true) { @@ -756,12 +758,28 @@ textmode.setText = function(jsonText) { this.aceEditor.setValue(text, -1); + if (clearHistory) { + // prevent initial undo action clearing the initial contents + var me = this; + setTimeout(function () { + me.aceEditor.session.getUndoManager().reset(); + }, 0); + } + this.onChangeDisabled = false; } // validate JSON schema this._debouncedValidate(); }; +/** + * Set the text contents of the editor + * @param {String} jsonText + */ +textmode.setText = function(jsonText) { + this._setText(jsonText, true) +}; + /** * Update the text contents * @param {string} jsonText @@ -772,9 +790,7 @@ textmode.updateText = function(jsonText) { return; } - this.onChangeDisabled = true; // don't fire an onChange event - this.setText(jsonText); - this.onChangeDisabled = false; + this._setText(jsonText, false); }; /**