Improved setting size/color when selection is empty
authorAlexander Ebert <ebert@woltlab.com>
Fri, 22 Jul 2016 11:01:43 +0000 (13:01 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 22 Jul 2016 11:01:43 +0000 (13:01 +0200)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabColor.js
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSize.js
wcfsetup/install/files/js/WoltLab/WCF/Ui/Redactor/Format.js

index 0504856cec40f30c8d8edaf224ae05fdbb9071b5..68ae83580f1b3eadde5b7e6137caa53e886719c8 100644 (file)
@@ -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));
                }
        };
index 12e5efeba499d927fea63e3ec91d86b1a0aee58e..64bea9706fa96fb53ee9882a04d91316c8e389fe 100644 (file)
@@ -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));
                }
        };
index 81052292fe38fabf54153c679375589a2048de59..0fae363b866ca30ef0252a42e935b1b1da55e11c 100644 (file)
@@ -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++) {