From c1ee1997b2fa7c1a55f9046510dbe1f4a40b9f7e Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 5 Sep 2016 16:57:40 +0200 Subject: [PATCH] Improved quote handling --- .../redactor2/plugins/WoltLabCaret.js | 11 ++++++++ wcfsetup/install/files/js/WCF.Message.js | 7 ++++- .../js/WoltLabSuite/Core/Ui/Redactor/Quote.js | 27 ++++++++++--------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js index 7077de0e0a..9e4c53e2a8 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js @@ -19,6 +19,17 @@ $.Redactor.prototype.WoltLabCaret = function() { this.WoltLabCaret._initInternalRange(); }, + paragraphAfterBlock: function (block) { + var sibling = block.nextElementSibling; + if (sibling && sibling.nodeName !== 'P') { + sibling = elCreate('p'); + sibling.textContent = '\u200B'; + block.parentNode.insertBefore(sibling, block.nextSibling); + } + + this.caret.after(block); + }, + endOfEditor: function () { var editor = this.core.editor()[0]; diff --git a/wcfsetup/install/files/js/WCF.Message.js b/wcfsetup/install/files/js/WCF.Message.js index f4bfa110ab..9077830e32 100644 --- a/wcfsetup/install/files/js/WCF.Message.js +++ b/wcfsetup/install/files/js/WCF.Message.js @@ -1707,7 +1707,7 @@ WCF.Message.Quote.Manager = Class.extend({ var button = UiPageAction.get(buttonName); if (button === undefined) { button = elCreate('a'); - button.addEventListener(WCF_CLICK_EVENT, this._click.bind(this)); + button.addEventListener('mousedown', this._click.bind(this)); UiPageAction.add(buttonName, button); } @@ -1728,6 +1728,11 @@ WCF.Message.Quote.Manager = Class.extend({ * Handles clicks on 'Show quotes'. */ _click: function() { + var editor = document.activeElement; + if (editor.classList.contains('redactor-editor')) { + $(editor.nextElementSibling).redactor('selection.save'); + } + if (this._hasTemplate) { this._dialog.wcfDialog('open'); } diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Quote.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Quote.js index 9da1fd53fe..5010ef8ac5 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Quote.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Quote.js @@ -53,26 +53,23 @@ define(['Core', 'EventHandler', 'EventKey', 'Language', 'StringUtil', 'Dom/Util' _insertQuote: function (data) { EventHandler.fire('com.woltlab.wcf.redactor2', 'showEditor'); + var editor = this._editor.core.editor()[0]; + this._editor.selection.restore(); + this._editor.buffer.set(); - // caret must be within a `

`, if it is not move it - /** @type Node */ + // caret must be within a `

`, if it is not: move it var block = this._editor.selection.block(); - var redactor = this._editor.core.editor()[0]; - while (block.parentNode && block.parentNode !== redactor) { - block = block.parentNode; - } - this._editor.caret.after(block); - var quoteId = Core.getUuid(); - this._editor.insert.html(''); + while (block && block.parentNode !== editor) { + block = block.parentNode; + } - var quote = elById(quoteId); + var quote = elCreate('woltlab-quote'); elData(quote, 'author', data.author); elData(quote, 'link', data.link); - this._editor.selection.restore(); var content = data.content; if (data.isText) { content = StringUtil.escapeHTML(content); @@ -84,9 +81,13 @@ define(['Core', 'EventHandler', 'EventKey', 'Language', 'StringUtil', 'Dom/Util' // bypass the editor as `insert.html()` doesn't like us quote.innerHTML = content; - quote.removeAttribute('id'); + block.parentNode.insertBefore(quote, block.nextSibling); + + if (block.nodeName === 'P' && (block.innerHTML === '
' || block.innerHTML.replace(/\u200B/g, '') === '')) { + block.parentNode.removeChild(block); + } - this._editor.caret.after(quote); + this._editor.WoltLabCaret.paragraphAfterBlock(quote); this._editor.buffer.set(); }, -- 2.20.1