From 3b109300d272f91507a55b2dd9863746966b87a5 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 31 Mar 2014 00:50:04 +0200 Subject: [PATCH] Added proper support for [code], inline editor partially works now --- .../js/3rdParty/redactor/plugins/wbbcode.js | 34 +++++++++++++++++-- .../3rdParty/redactor/plugins/wmonkeypatch.js | 4 +++ wcfsetup/install/files/js/WCF.Message.js | 22 +++++++----- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js index 7c9365165e..93e64da8d5 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js @@ -127,7 +127,7 @@ RedactorPlugins.wbbcode = { return $html; } - return $html + '
';; + return $html + '
'; }); this.sync(); }, @@ -138,6 +138,10 @@ RedactorPlugins.wbbcode = { _convertFromHtml: function() { var html = this.$source.val(); + // drop line break right before/after a
 tag (used by [code]-BBCode)
+		html = html.replace(/
\n
\n/g, '');
+		html = html.replace(/<\/pre>\n
\n/g, ''); + // drop
, they are pointless because the editor already adds a newline after them html = html.replace(/
/g, ''); html = html.replace(/ /gi," "); @@ -258,13 +262,26 @@ RedactorPlugins.wbbcode = { html = html.replace(/>/g, '>'); html = html.replace(/&/g, '&'); - // Restore (and ) + // Restore ( and ) html = html.replace(/%28/g, '('); html = html.replace(/%29/g, ')'); // Restore %20 html = html.replace(/%20/g, ' '); + // cache source code tags to preserve leading tabs + var $cachedCodes = { }; + for (var $i = 0, $length = __REDACTOR_SOURCE_BBCODES.length; $i < $length; $i++) { + var $bbcode = __REDACTOR_SOURCE_BBCODES[$i]; + + var $regExp = new RegExp('\\[' + $bbcode + '([\\S\\s]+?)\\[\\/' + $bbcode + '\\]', 'gi'); + html = html.replace($regExp, function(match) { + var $key = match.hashCode(); + $cachedCodes[$key] = match.replace(/\$/g, '$$$$'); + return '@@' + $key + '@@'; + }); + } + // trim leading tabs var $tmp = html.split("\n"); for (var $i = 0, $length = $tmp.length; $i < $length; $i++) { @@ -272,6 +289,14 @@ RedactorPlugins.wbbcode = { } html = $tmp.join("\n"); + // insert codes + if ($.getLength($cachedCodes)) { + for (var $key in $cachedCodes) { + var $regex = new RegExp('@@' + $key + '@@', 'g'); + html = html.replace($regex, $cachedCodes[$key]); + } + } + this.$source.val(html); }, @@ -304,7 +329,7 @@ RedactorPlugins.wbbcode = { for (var $i = 0, $length = __REDACTOR_SOURCE_BBCODES.length; $i < $length; $i++) { var $bbcode = __REDACTOR_SOURCE_BBCODES[$i]; - var $regExp = new RegExp('\\[' + $bbcode + '(.+?)\\[\\/' + $bbcode + '\\]', 'gi'); + var $regExp = new RegExp('\\[' + $bbcode + '([\\S\\s]+?)\\[\\/' + $bbcode + '\\]', 'gi'); data = data.replace($regExp, function(match) { var $key = match.hashCode(); $cachedCodes[$key] = match.replace(/\$/g, '$$$$'); @@ -413,6 +438,9 @@ RedactorPlugins.wbbcode = { } } + // preserve leading whitespaces in [code] tags + data = data.replace(/\[code\][\S\s]*?\[\/code\]/, '
$&
'); + this.$source.val(data); } }; diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js index cc1678d936..807cb949ed 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js @@ -60,6 +60,10 @@ RedactorPlugins.wmonkeypatch = { this.modalTemplatesInit(); }, + cleanRemoveSpaces: function(html, buffer) { + return html; + }, + /** * Overwrites $.Redactor.inlineRemoveStyle() to drop empty elements. * diff --git a/wcfsetup/install/files/js/WCF.Message.js b/wcfsetup/install/files/js/WCF.Message.js index 2dea0f1d9a..5ccbd1ac6f 100644 --- a/wcfsetup/install/files/js/WCF.Message.js +++ b/wcfsetup/install/files/js/WCF.Message.js @@ -1291,6 +1291,7 @@ WCF.Message.InlineEditor = Class.extend({ // hide message options this._container[this._activeElementID].find('.messageOptions').addClass('forceHidden'); + // TODO: support redactor here, in particular update the quote manager first if ($.browser.ckeditor) { new WCF.PeriodicalExecuter($.proxy(function(pe) { pe.stop(); @@ -1332,9 +1333,8 @@ WCF.Message.InlineEditor = Class.extend({ var $objectID = $container.data('objectID'); var $message = ''; - if ($.browser.ckeditor) { - var $ckEditor = $('#' + this._messageEditorIDPrefix + $objectID).ckeditorGet(); - $message = $ckEditor.getData(); + if ($.browser.redactor) { + $message = $('#' + this._messageEditorIDPrefix + $objectID).redactor('getText'); } else { $message = $('#' + this._messageEditorIDPrefix + $objectID).val(); @@ -1365,9 +1365,8 @@ WCF.Message.InlineEditor = Class.extend({ var $objectID = $container.data('objectID'); var $message = ''; - if ($.browser.ckeditor) { - var $ckEditor = $('#' + this._messageEditorIDPrefix + $objectID).ckeditorGet(); - $message = $ckEditor.getData(); + if ($.browser.redactor) { + $message = $('#' + this._messageEditorIDPrefix + $objectID).redactor('getText'); } else { $message = $('#' + this._messageEditorIDPrefix + $objectID).val(); @@ -1401,9 +1400,12 @@ WCF.Message.InlineEditor = Class.extend({ // show unrelated content $messageBody.find('.jsInlineEditorHideContent').show(); + /* + * TODO: the quote manager does not support redactor yet if (this._quoteManager) { this._quoteManager.clearAlternativeCKEditor(); } + */ }, /** @@ -1424,9 +1426,8 @@ WCF.Message.InlineEditor = Class.extend({ this._container[this._activeElementID].find('.messageOptions').removeClass('forceHidden'); // remove editor - if ($.browser.ckeditor) { - var $ckEditor = $('#' + this._messageEditorIDPrefix + $container.data('objectID')).ckeditorGet(); - $ckEditor.destroy(); + if ($.browser.redactor) { + $('#' + this._messageEditorIDPrefix + $container.data('objectID')).redactor('destroy'); } $content.empty(); @@ -1440,9 +1441,12 @@ WCF.Message.InlineEditor = Class.extend({ this._notification.show(); + /* + * TODO: the quote manager does not support redactor yet if (this._quoteManager) { this._quoteManager.clearAlternativeCKEditor(); } + */ }, /** -- 2.20.1