From d293cbcfc041b036775318dc75f9acc026fd2a4f Mon Sep 17 00:00:00 2001 From: jos Date: Thu, 29 Aug 2019 14:35:57 +0200 Subject: [PATCH] Fixed #586: caret position lost when switching browser tabs --- HISTORY.md | 1 + src/js/Node.js | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index febb52c..c2487b8 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -6,6 +6,7 @@ https://github.com/josdejong/jsoneditor - Dropped support for bower, removed the `dist` folder from the git repository. - Converted the code into ES6, put Babel transpiler in place. +- Fixed #586: caret position lost when switching browser tabs. ## 2019-08-28, version 6.4.1 diff --git a/src/js/Node.js b/src/js/Node.js index db78ab9..fff131d 100644 --- a/src/js/Node.js +++ b/src/js/Node.js @@ -2817,8 +2817,10 @@ Node.prototype.onEvent = function (event) { this._getDomValue() this._clearValueError() this._updateDomValue() - if (this.value) { - domValue.innerHTML = this._escapeHTML(this.value) + const escapedValue = this._escapeHTML(this.value) + if (domValue.innerHTML !== escapedValue) { + // only update if changed, else you lose the caret position when changing tabs for example + domValue.innerHTML = escapedValue } break @@ -2867,8 +2869,10 @@ Node.prototype.onEvent = function (event) { case 'blur': this._getDomField(true) this._updateDomField() - if (this.field) { - domField.innerHTML = this._escapeHTML(this.field) + const escapedField = this._escapeHTML(this.field) + if (domField.innerHTML !== escapedField) { + // only update if changed, else you lose the caret position when changing tabs for example + domField.innerHTML = escapedField } break