From 1881152457454b24c25509c8323eae79526f5c6d Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 26 Sep 2016 14:14:50 +0200 Subject: [PATCH] Improved format removal --- .../WoltLabSuite/Core/Ui/Redactor/Format.js | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Format.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Format.js index 0e2f77e800..1fa40a6b4c 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Format.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Format.js @@ -31,7 +31,7 @@ define(['Dom/Util'], function(DomUtil) { } var range = selection.getRangeAt(0); - var tmpElement = null; + var markerStart = null, markerEnd = null, tmpElement = null; if (range.collapsed) { tmpElement = elCreate('strike'); tmpElement.textContent = '\u200B'; @@ -43,6 +43,30 @@ define(['Dom/Util'], function(DomUtil) { selection.removeAllRanges(); selection.addRange(range); } + else { + // removing existing format causes the selection to vanish, + // these markers are used to restore it afterwards + markerStart = elCreate('woltlab-tmp-marker'); + markerEnd = elCreate('woltlab-tmp-marker'); + + var tmpRange = range.cloneRange(); + tmpRange.collapse(true); + tmpRange.insertNode(markerStart); + + tmpRange = range.cloneRange(); + tmpRange.collapse(false); + tmpRange.insertNode(markerEnd); + + // remove existing format before applying new one + this.removeFormat(editorElement, tagName); + + range = document.createRange(); + range.setStartAfter(markerStart); + range.setEndBefore(markerEnd); + + selection.removeAllRanges(); + selection.addRange(range); + } if (tmpElement === null) { document.execCommand('strikethrough'); @@ -89,6 +113,11 @@ define(['Dom/Util'], function(DomUtil) { selection.removeAllRanges(); selection.addRange(range); } + + if (markerStart !== null) { + elRemove(markerStart); + elRemove(markerEnd); + } }, /** @@ -186,6 +215,19 @@ define(['Dom/Util'], function(DomUtil) { // remove strike element itself DomUtil.unwrapChildNodes(strikeElement); } + + // search for tags that are still floating around, but are completely empty + elBySelAll(tagName, editorElement, function (element) { + if (element.parentNode && !element.textContent.length) { + if (element.childElementCount === 1 && element.children[0].nodeName === 'WOLTLAB-TMP-MARKER') { + element.parentNode.insertBefore(element.children[0], element); + } + + if (element.childElementCount === 0) { + elRemove(element); + } + } + }); }, /** -- 2.20.1