From 4e9ab1ee762585d4c85505daa319681580a1ea09 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 22 Jul 2016 13:01:43 +0200 Subject: [PATCH] Improved setting size/color when selection is empty --- .../redactor2/plugins/WoltLabColor.js | 8 ++----- .../3rdParty/redactor2/plugins/WoltLabSize.js | 8 ++----- .../js/WoltLab/WCF/Ui/Redactor/Format.js | 24 ++++++++++++++++++- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabColor.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabColor.js index 0504856cec..68ae83580f 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabColor.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabColor.js @@ -45,21 +45,17 @@ $.Redactor.prototype.WoltLabColor = function() { key = key.replace(/^color_/, ''); require(['WoltLab/WCF/Ui/Redactor/Format'], (function(UiRedactorFormat) { - this.selection.save(); + this.buffer.set(); UiRedactorFormat.format(this.$editor[0], 'woltlab-color', 'woltlab-color-' + key); - - this.selection.restore(); }).bind(this)); }, removeColor: function() { require(['WoltLab/WCF/Ui/Redactor/Format'], (function(UiRedactorFormat) { - this.selection.save(); + this.buffer.set(); UiRedactorFormat.removeFormat(this.$editor[0], 'woltlab-color'); - - this.selection.restore(); }).bind(this)); } }; diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSize.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSize.js index 12e5efeba4..64bea9706f 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSize.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSize.js @@ -36,21 +36,17 @@ $.Redactor.prototype.WoltLabSize = function() { setSize: function(key) { require(['WoltLab/WCF/Ui/Redactor/Format'], (function(UiRedactorFormat) { - this.selection.save(); + this.buffer.set(); UiRedactorFormat.format(this.$editor[0], 'woltlab-size', 'woltlab-size-' + key.replace(/^size_/, '')); - - this.selection.restore(); }).bind(this)); }, removeSize: function() { require(['WoltLab/WCF/Ui/Redactor/Format'], (function(UiRedactorFormat) { - this.selection.save(); + this.buffer.set(); UiRedactorFormat.removeFormat(this.$editor[0], 'woltlab-size'); - - this.selection.restore(); }).bind(this)); } }; diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Ui/Redactor/Format.js b/wcfsetup/install/files/js/WoltLab/WCF/Ui/Redactor/Format.js index 81052292fe..0fae363b86 100644 --- a/wcfsetup/install/files/js/WoltLab/WCF/Ui/Redactor/Format.js +++ b/wcfsetup/install/files/js/WoltLab/WCF/Ui/Redactor/Format.js @@ -24,7 +24,29 @@ define(['Dom/Util'], function(DomUtil) { * @param {Object=} attributes optional list of attributes for the format tag */ format: function(editorElement, tagName, className, attributes) { - document.execCommand('strikethrough'); + var selection = window.getSelection(); + if (!selection.rangeCount) { + // no active selection + return; + } + + var range = selection.getRangeAt(0); + var tmpElement = null; + if (range.collapsed) { + tmpElement = elCreate('strike'); + tmpElement.textContent = '\u200B'; + range.insertNode(tmpElement); + + range = document.createRange(); + range.selectNodeContents(tmpElement); + + selection.removeAllRanges(); + selection.addRange(range); + } + + if (tmpElement === null) { + document.execCommand('strikethrough'); + } var elements = elBySelAll('strike', editorElement), formatElement, property, strike; for (var i = 0, length = elements.length; i < length; i++) { -- 2.20.1