From 1c6a563634ebe7dc6317c9758c4fd72753d75433 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sun, 28 Aug 2016 10:48:30 +0200 Subject: [PATCH] Improved selection handling --- .../redactor2/plugins/WoltLabCaret.js | 75 +++++++++++++++++-- .../redactor2/plugins/WoltLabSmiley.js | 2 + 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js index 7d364fdae9..7fe482755c 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js @@ -16,10 +16,60 @@ $.Redactor.prototype.WoltLabCaret = function() { this.$editor[0].addEventListener('mouseup', this.WoltLabCaret._handleEditorClick.bind(this)); + this.WoltLabCaret._initInternalRange(); + }, + + _initInternalRange: function () { + var editor = this.core.editor()[0]; var internalRange = null; var selection = window.getSelection(); - this.$editor[0].addEventListener('keyup', function () { + + var saveRange = function () { internalRange = selection.getRangeAt(0).cloneRange(); + }; + + var restoreRange = function () { + if (internalRange === null) return; + + if (document.activeElement === editor) { + var range = selection.getRangeAt(0); + if (range.startOffset !== 0) { + return; + } + + var node = range.startContainer; + while (node) { + if (node.parentNode === editor) { + if (node.previousSibling) { + return; + } + + break; + } + + if (node.previousSibling) { + return; + } + + node = node.parentNode; + } + + if (!node) return; + } + + editor.focus(); + + selection.removeAllRanges(); + selection.addRange(internalRange); + + internalRange = null; + }; + + this.$editor[0].addEventListener('keyup', saveRange); + this.$editor[0].addEventListener('mouseup', function () { + if (selection.rangeCount) { + saveRange(); + } }); var mpSave = this.selection.save; @@ -32,10 +82,7 @@ $.Redactor.prototype.WoltLabCaret = function() { var mpRestore = this.selection.restore; this.selection.restore = (function () { if (internalRange) { - selection.removeAllRanges(); - selection.addRange(internalRange); - - internalRange = null; + restoreRange(); if (selection.rangeCount && this.utils.isRedactorParent(selection.getRangeAt(0).commonAncestorContainer)) { return; @@ -44,6 +91,24 @@ $.Redactor.prototype.WoltLabCaret = function() { mpRestore.call(this); }).bind(this); + + var mpSet = this.buffer.set; + this.buffer.set = (function (type) { + if (document.activeElement !== editor) { + restoreRange(); + } + + mpSet.call(this, type); + + saveRange(); + }).bind(this); + + var mpHtml = this.insert.html; + this.insert.html = (function (html, data) { + mpHtml.call(this, html, data); + + saveRange(); + }).bind(this); }, _handleEditorClick: function (event) { diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSmiley.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSmiley.js index 5258856eeb..6d2b7f5f7e 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSmiley.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSmiley.js @@ -9,6 +9,8 @@ $.Redactor.prototype.WoltLabSmiley = function() { }, _insert: function(data) { + this.buffer.set(); + this.insert.html('' + data.code + ''); } } -- 2.20.1