From a6ec6d8dcc3182f4708160bfb308c4c1f59c55e4 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 16 Feb 2018 00:18:33 +0100 Subject: [PATCH] Undoing an action may lead to a misplaced caret --- .../redactor2/plugins/WoltLabCaret.js | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js index 07eacfdd4e..529c9718ef 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js @@ -95,9 +95,11 @@ $.Redactor.prototype.WoltLabCaret = function() { mpRestoreInstant.call(this, saved); + var selection = window.getSelection(); + if (!selection.rangeCount) return; + if (localSaved.isAtNodeStart === true) { - var selection = window.getSelection(); - if (selection.rangeCount && !selection.isCollapsed) { + if (!selection.isCollapsed) { var range = selection.getRangeAt(0); var start = range.startContainer; @@ -130,6 +132,21 @@ $.Redactor.prototype.WoltLabCaret = function() { } } } + else if (selection.isCollapsed) { + var anchorNode = selection.anchorNode; + var editor = this.core.editor()[0]; + + // Restoring a selection may fail if the node does was removed from the DOM, + // causing the caret to be inside a text node with the editor being the + // direct parent. We can safely move the caret inside the adjacent container, + // using `caret.start()`. + if (anchorNode.nodeType === Node.TEXT_NODE && anchorNode.parentNode === editor && selection.anchorOffset === anchorNode.textContent.length) { + var p = anchorNode.nextElementSibling; + if (p && p.nodeName === 'P') { + this.caret.start(p); + } + } + } }).bind(this); }, -- 2.20.1