From 845cbb50368ebe52798ac5c0f8eec49dbab7d326 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sun, 18 Jan 2015 00:55:12 +0100 Subject: [PATCH] Improved click handler for clicks on codebox/quote margins --- .../js/3rdParty/redactor/plugins/wbbcode.js | 6 +- .../3rdParty/redactor/plugins/wmonkeypatch.js | 55 ++++++++++++++----- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js index e6b04678e7..5d23b53442 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js @@ -253,7 +253,7 @@ RedactorPlugins.wbbcode = function() { var $uuid = WCF.getUUID(); $cachedCodeListings[$uuid] = { - codeContent: codeContent.replace(/
  • /g, '').replace(/<\/li>/g, '\n'), + codeContent: codeContent.replace(/
  • /g, '').replace(/<\/li>/g, '\n').replace(/\n$/, ''), filename: $filename.replace(/['"]/g, ''), highlighter: ($highlighter === 'plain' ? '' : $highlighter), lineNumber: (lineNumber > 1 ? lineNumber : 0) @@ -663,7 +663,7 @@ RedactorPlugins.wbbcode = function() { var $bbcode = '[code' + ($attributes.length ? '=' + $attributes : '') + ']' + listing.codeContent + '[/code]\n'; - html = html.replace(new RegExp('@@@' + uuid + '@@@', 'g'), $bbcode); + html = html.replace(new RegExp('@@@' + uuid + '@@@\n?', 'g'), $bbcode); }); } @@ -1297,7 +1297,6 @@ RedactorPlugins.wbbcode = function() { * @return string */ _pasteCallback: function(html) { - console.debug(html); // reduce successive
    by one //html = html.replace(/]*>(]*>)+/g, '$1'); @@ -1989,6 +1988,7 @@ RedactorPlugins.wbbcode = function() { * Ensures that there is a paragraph in front of each block-level element because you cannot click in between two of them. */ fixBlockLevelElements: function() { + return; var $addSpacing = (function(referenceElement, target) { var $tagName = 'P'; diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js index 90624c945f..89676dafe4 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js @@ -104,23 +104,21 @@ RedactorPlugins.wmonkeypatch = function() { } }).bind(this)); - var $setCaretBeforeOrAfter = (function(event, blockquote) { - var $offset = $(blockquote).offset(); - if (event.pageY <= $offset.top) { - // set caret before - if (blockquote.previousElementSibling && blockquote.previousElementSibling.tagName === 'P') { - this.caret.setEnd(blockquote.previousElementSibling); + var $setCaretBeforeOrAfter = (function(element, setBefore) { + if (setBefore) { + if (element.previousElementSibling && element.previousElementSibling.tagName === 'P') { + this.caret.setEnd(element.previousElementSibling); } else { - this.wutil.setCaretBefore(blockquote); + this.wutil.setCaretBefore(element); } } else { - if (blockquote.nextElementSibling && blockquote.nextElementSibling.tagName === 'P') { - this.caret.setEnd(blockquote.nextElementSibling); + if (element.nextElementSibling && element.nextElementSibling.tagName === 'P') { + this.caret.setEnd(element.nextElementSibling); } else { - this.wutil.setCaretAfter(blockquote); + this.wutil.setCaretAfter(element); } } }).bind(this); @@ -128,14 +126,21 @@ RedactorPlugins.wmonkeypatch = function() { this.$editor.on('click.wmonkeypatch', (function(event) { if (event.target === this.$editor[0]) { var $range = (window.getSelection().rangeCount) ? window.getSelection().getRangeAt(0) : null; - if ($range !== null && $range.collapsed) { - // clicking on the margin created by
    will direct the cursor inside the quote + + if ($range && $range.collapsed) { var $current = $range.startContainer; while ($current !== null && $current !== this.$editor[0]) { if ($current.nodeType === Node.ELEMENT_NODE) { - if ($current.tagName === 'BLOCKQUOTE') { - $setCaretBeforeOrAfter(event, $current); + if ($current.tagName === 'BLOCKQUOTE' || ($current.tagName === 'DIV' && /\bcodeBox\b/.test($current.className))) { + var $offset = $(element).offset(); + if (event.pageY <= $offset.top) { + $setCaretBeforeOrAfter($current, true); + } + else { + $setCaretBeforeOrAfter($current, false); + } + // stop processing return false; } } @@ -143,6 +148,28 @@ RedactorPlugins.wmonkeypatch = function() { $current = $current.parentElement; } } + + var $elements = this.$editor.children('blockquote, div.codeBox'); + $elements.each(function(index, element) { + var $element = $(element); + var $offset = $element.offset(); + + if (event.pageY <= $offset.top) { + $setCaretBeforeOrAfter(element, true); + + return false; + } + else { + var $height = $element.outerHeight() + (parseInt($element.css('margin-bottom'), 10) || 0); + if ((event.pageY <= $offset.top + $height) || (index + 1) === $elements.length) { + $setCaretBeforeOrAfter(element, false); + + return false; + } + } + }); + + return false; } }).bind(this)); }, -- 2.20.1