From a07120e3ee2f01dfab757b6954c516c2e58dbee9 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Wed, 4 Jan 2017 17:08:53 +0100 Subject: [PATCH] Improved pasting into `` --- .../redactor2/plugins/WoltLabClean.js | 2 +- .../redactor2/plugins/WoltLabPaste.js | 41 ++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js index f160885802..b794e1d111 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js @@ -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; }; diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabPaste.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabPaste.js index 6c014904b6..edf1bce95e 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabPaste.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabPaste.js @@ -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(//); + 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 = '' + parts[i] + (i === length - 1 ? this.marker.html() : '') + ''; + lastParent.parentNode.insertBefore(newParent, lastParent.nextSibling); + + lastParent = newParent; + } + + kbd.innerHTML = parts[0]; + + this.selection.restore(); + + } + + return; + } var div = elCreate('div'); div.innerHTML = html; -- 2.20.1