From: Alexander Ebert Date: Thu, 15 Feb 2018 23:18:33 +0000 (+0100) Subject: Undoing an action may lead to a misplaced caret X-Git-Tag: 3.1.0~3^2~9 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=a6ec6d8dcc3182f4708160bfb308c4c1f59c55e4;p=GitHub%2FWoltLab%2FWCF.git Undoing an action may lead to a misplaced caret --- 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); },