From 5fb6f47a81199b82a86e6f3537e9d452c11192c8 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 4 Dec 2014 18:41:25 +0100 Subject: [PATCH] Moved wmonkeypatch.*Selection -> wutil, fixed selection saving --- .../js/3rdParty/redactor/plugins/wbbcode.js | 8 +-- .../3rdParty/redactor/plugins/wmonkeypatch.js | 60 ++----------------- .../js/3rdParty/redactor/plugins/wutil.js | 38 +++++++++++- 3 files changed, 46 insertions(+), 60 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js index 66d8d8f0a8..69f92204c6 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js @@ -30,7 +30,7 @@ RedactorPlugins.wbbcode = function() { delete this.opts.woltlab.originalValue; $(document).trigger('resize'); - this.wmonkeypatch.saveSelection(); + this.wutil.saveSelection(); }).bind(this); this.opts.pasteBeforeCallback = $.proxy(this.wbbcode._pasteBeforeCallback, this); @@ -101,13 +101,13 @@ RedactorPlugins.wbbcode = function() { this.button.get('html').children('i').removeClass('fa-square').addClass('fa-square-o'); $tooltip.text(WCF.Language.get('wcf.bbcode.button.toggleBBCode')); - this.wmonkeypatch.saveSelection(); + this.wutil.saveSelection(); } }).bind(this); // insert a new line if user clicked into the editor and the last children is a quote (same behavior as arrow down) this.wutil.setOption('clickCallback', (function(event) { - this.wmonkeypatch.saveSelection(); + this.wutil.saveSelection(); if (event.target === this.$editor[0]) { if (this.$editor[0].lastElementChild && this.$editor[0].lastElementChild.tagName === 'BLOCKQUOTE') { @@ -1508,7 +1508,7 @@ RedactorPlugins.wbbcode = function() { this.wutil.insertAtCaret($openTag + plainText + $closingTag); } - this.wmonkeypatch.saveSelection(); + this.wutil.saveSelection(); return $quote; }, diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js index b553b4ad2e..ce8704d823 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js @@ -18,12 +18,6 @@ RedactorPlugins.wmonkeypatch = function() { "use strict"; return { - /** - * saved selection range - * @var range - */ - _range: null, - /** * Initializes the RedactorPlugins.wmonkeypatch plugin. */ @@ -54,34 +48,6 @@ RedactorPlugins.wmonkeypatch = function() { this.wmonkeypatch.fixWebKit(); }, - /** - * Saves current caret position. - */ - saveSelection: function() { - var $selection = getSelection(); - - if ($selection.rangeCount) { - this.wmonkeypatch._range = $selection.getRangeAt(0); - } - }, - - /** - * Restores saved selection. - */ - restoreSelection: function() { - if (document.activeElement !== this.$editor[0]) { - this.$editor.focus(); - } - - if (this.wmonkeypatch._range !== null) { - var $selection = window.getSelection(); - $selection.removeAllRanges(); - $selection.addRange(this.wmonkeypatch._range); - - this.wmonkeypatch._range = null; - } - }, - /** * Setups event listeners and callbacks. */ @@ -102,7 +68,7 @@ RedactorPlugins.wmonkeypatch = function() { // keyup this.wutil.setOption('keyupCallback', (function(event) { - this.wmonkeypatch.saveSelection(); + this.wutil.saveSelection(); var $data = { cancel: false, @@ -121,14 +87,6 @@ RedactorPlugins.wmonkeypatch = function() { this.$editor.on('mouseup.redactor keyup.redactor focus.redactor', $.proxy(this.observe.buttons, this)); this.$editor.on('keyup.redactor', $.proxy(this.keyup.init, this)); } - - // blur is unreliable in Firefox, especially since 'focusout' is not available - /*this.$editor.on('blur.wredactor', (function() { - var $selection = window.getSelection(); - if ($selection.rangeCount) { - this.wmonkeypatch._range = $selection.getRangeAt(0); - } - }).bind(this));*/ }, /** @@ -393,7 +351,7 @@ RedactorPlugins.wmonkeypatch = function() { $fixDropdown($dropdown); if ($.browser.iOS) { - this.wmonkeypatch.saveSelection(); + this.wutil.saveSelection(); } $mpShow.call(this, e, key); @@ -534,15 +492,7 @@ RedactorPlugins.wmonkeypatch = function() { } else { if (document.activeElement !== this.$editor[0]) { - this.$editor.focus(); - - if (this.wmonkeypatch._range) { - var $selection = window.getSelection(); - $selection.removeAllRanges(); - $selection.addRange(this.wmonkeypatch._range); - - this.wmonkeypatch._range = null; - } + this.wutil.restoreSelection(); } } }).bind(this); @@ -568,7 +518,7 @@ RedactorPlugins.wmonkeypatch = function() { }); if ($removedSpan) { - this.wmonkeypatch.saveSelection(); + this.wutil.saveSelection(); } }).bind(this); @@ -579,7 +529,7 @@ RedactorPlugins.wmonkeypatch = function() { $mpHtml.call(this, html, clean); - this.wmonkeypatch.saveSelection(); + this.wutil.saveSelection(); if ($isWebKit) { setTimeout(function() { diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js index 2baf5d6712..89905b823d 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js @@ -20,6 +20,12 @@ RedactorPlugins.wutil = function() { */ _autosaveWorker: null, + /** + * saved selection range + * @var range + */ + _range: null, + /** * Initializes the RedactorPlugins.wutil plugin. */ @@ -50,6 +56,34 @@ RedactorPlugins.wutil = function() { }).bind(this); }, + /** + * Saves current caret position. + */ + saveSelection: function() { + var $selection = getSelection(); + + if ($selection.rangeCount) { + this.wutil._range = $selection.getRangeAt(0); + } + }, + + /** + * Restores saved selection. + */ + restoreSelection: function() { + if (document.activeElement !== this.$editor[0]) { + this.$editor.focus(); + } + + if (this.wutil._range !== null) { + var $selection = window.getSelection(); + $selection.removeAllRanges(); + $selection.addRange(this.wutil._range); + + this.wutil._range = null; + } + }, + /** * Allows inserting of text contents in Redactor's source area. * @@ -206,7 +240,7 @@ RedactorPlugins.wutil = function() { reset: function() { if (this.opts.visual) { this.$editor.html('

' + this.opts.invisibleSpace + '

'); - this.wmonkeypatch.saveSelection(); + this.wutil.saveSelection(); } this.$textarea.val(''); @@ -549,6 +583,8 @@ RedactorPlugins.wutil = function() { else { this.wutil.setCaretAfter($lastChild); } + + this.wutil.saveSelection(); }, /** -- 2.20.1