Improved pasting into `<kbd>`
authorAlexander Ebert <ebert@woltlab.com>
Wed, 4 Jan 2017 16:08:53 +0000 (17:08 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 4 Jan 2017 16:08:59 +0000 (17:08 +0100)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabPaste.js

index f160885802c99ac18cf0e84faaf9dc43a2f3fa0e..b794e1d111592cad5b10169d12f28ba8fa09bb12 100644 (file)
@@ -106,7 +106,7 @@ $.Redactor.prototype.WoltLabClean = function() {
                        
                        var mpOnPaste = this.clean.onPaste;
                        this.clean.onPaste = (function (html, data, insert) {
-                               if (data.pre) {
+                               if (data.pre || this.utils.isCurrentOrParent('kbd')) {
                                        // prevent method call when data.pre is true
                                        var mpRemoveEmptyInlineTags = this.clean.removeEmptyInlineTags;
                                        this.clean.removeEmptyInlineTags = function(html) { return html; };
index 6c014904b61dd8119aa628b013c824c4b14d9377..edf1bce95e3f270b2f1d97488b007093a0f25ccb 100644 (file)
@@ -4,14 +4,16 @@ $.Redactor.prototype.WoltLabPaste = function() {
        return {
                init: function () {
                        var clipboardData = null;
+                       var isKbd = false;
                        
                        // IE 11
                        var isIe = (document.documentMode && typeof window.clipboardData === 'object');
                        
                        var mpInit = this.paste.init;
                        this.paste.init = (function (e) {
-                               var isCode = (this.opts.type === 'pre' || this.utils.isCurrentOrParent('pre')) ? true : false;
-                               if (isCode) {
+                               var isCode = (this.opts.type === 'pre' || this.utils.isCurrentOrParent('pre'));
+                               isKbd = (!isCode && this.utils.isCurrentOrParent('kbd'));
+                               if (isCode || isKbd) {
                                        if (isIe) {
                                                clipboardData = window.clipboardData.getData('Text');
                                        }
@@ -35,6 +37,10 @@ $.Redactor.prototype.WoltLabPaste = function() {
                        this.paste.getPasteBoxCode = (function (pre) {
                                var returnValue = mpGetPasteBoxCode.call(this, pre);
                                
+                               if (isKbd) {
+                                       return clipboardData;
+                               }
+                               
                                // use clipboard data if paste box is flawed or when
                                // pasting in IE 11 where clipboard data is more reliable
                                if (pre && (!returnValue || isIe)) {
@@ -116,6 +122,37 @@ $.Redactor.prototype.WoltLabPaste = function() {
                                if (data.pre) {
                                        return mpInsert.call(this, html, data);
                                }
+                               else if (this.utils.isCurrentOrParent('kbd')) {
+                                       mpInsert.call(this, html, data);
+                                       
+                                       var current = this.selection.current();
+                                       if (current.nodeType === Node.TEXT_NODE) current = current.parentNode;
+                                       var kbd = current.closest('kbd');
+                                       var paragraphs = elByTag('p', kbd);
+                                       while (paragraphs.length) {
+                                               paragraphs[0].outerHTML = paragraphs[0].innerHTML;
+                                       }
+                                       
+                                       var parts = kbd.innerHTML.split(/<br\s*\/?>/);
+                                       if (parts.length > 1) {
+                                               var lastParent = this.selection.block();
+                                               
+                                               for (var i = 1, length = parts.length; i < length; i++) {
+                                                       var newParent = elCreate(lastParent.nodeName);
+                                                       newParent.innerHTML = '<kbd>' + parts[i] + (i === length - 1 ? this.marker.html() : '') + '</kbd>';
+                                                       lastParent.parentNode.insertBefore(newParent, lastParent.nextSibling);
+                                                       
+                                                       lastParent = newParent;
+                                               }
+                                               
+                                               kbd.innerHTML = parts[0];
+                                               
+                                               this.selection.restore();
+                                               
+                                       }
+                                       
+                                       return;
+                               }
                                
                                var div = elCreate('div');
                                div.innerHTML = html;