From: Alexander Ebert Date: Mon, 24 Nov 2014 00:54:24 +0000 (+0100) Subject: Using a more reliable way to save the caret position X-Git-Tag: 2.1.0_Beta_1~183 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=b3d84e0551c752c26e7d1ed250751af601430045;p=GitHub%2FWoltLab%2FWCF.git Using a more reliable way to save the caret position blur() isn't that great with Firefox because getSelection() already contains the new range and focusout is not supported. --- diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js index e647f2bbe0..a71a31884d 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js @@ -17,7 +17,7 @@ RedactorPlugins.wbbcode = function() { init: function() { var $identifier = this.$textarea.wcfIdentify(); - this.opts.initCallback = $.proxy(function() { + this.opts.initCallback = (function() { // use stored editor contents var $content = $.trim(this.wutil.getOption('woltlab.originalValue')); if ($content.length) { @@ -30,7 +30,8 @@ RedactorPlugins.wbbcode = function() { delete this.opts.woltlab.originalValue; $(document).trigger('resize'); - }, this); + this.wmonkeypatch.saveSelection(); + }).bind(this); this.opts.pasteBeforeCallback = $.proxy(this.wbbcode._pasteBeforeCallback, this); this.opts.pasteCallback = $.proxy(this.wbbcode._pasteCallback, this); @@ -99,11 +100,15 @@ 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(); } }).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(); + if (event.target === this.$editor[0]) { if (this.$editor[0].lastElementChild && this.$editor[0].lastElementChild.tagName === 'BLOCKQUOTE') { this.wutil.setCaretAfter($(this.$editor[0].lastElementChild)); diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js index d3ee1b8768..f871ac72b2 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js @@ -53,6 +53,13 @@ RedactorPlugins.wmonkeypatch = function() { this.wmonkeypatch.fixWebKit(); }, + /** + * Saves current caret position. + */ + saveSelection: function() { + this.wmonkeypatch._range = getSelection().getRangeAt(0); + }, + /** * Setups event listeners and callbacks. */ @@ -72,7 +79,9 @@ RedactorPlugins.wmonkeypatch = function() { }); // keyup - this.wutil.setOption('keyupCallback', function(event) { + this.wutil.setOption('keyupCallback', (function(event) { + this.wmonkeypatch.saveSelection(); + var $data = { cancel: false, event: event @@ -81,7 +90,7 @@ RedactorPlugins.wmonkeypatch = function() { WCF.System.Event.fireEvent('com.woltlab.wcf.redactor', 'keyup_' + $identifier, $data); return ($data.cancel ? false : true); - }); + }).bind(this)); // buttons response if (this.opts.activeButtons) { @@ -91,12 +100,13 @@ RedactorPlugins.wmonkeypatch = function() { this.$editor.on('keyup.redactor', $.proxy(this.keyup.init, this)); } - this.$editor.on('blur.wredactor', (function() { + // 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)); + }).bind(this));*/ }, /** diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js index bbf47f3e1b..24bc91d231 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js @@ -206,6 +206,7 @@ RedactorPlugins.wutil = function() { reset: function() { if (this.opts.visual) { this.$editor.html('

' + this.opts.invisibleSpace + '

'); + this.wmonkeypatch.saveSelection(); } this.$textarea.val('');