From 4bb04dc5743c825bd18c28e9eff57c9f3e48423e Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 31 Mar 2017 14:53:17 +0200 Subject: [PATCH] Fixed inline code behavior in iOS Safari --- .../redactor2/plugins/WoltLabButton.js | 13 +++++++- .../redactor2/plugins/WoltLabInlineCode.js | 32 +++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabButton.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabButton.js index 1ce17fd2ed..a5878426d1 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabButton.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabButton.js @@ -8,6 +8,17 @@ $.Redactor.prototype.WoltLabButton = function() { // tooltips are handled on our own this.button.buildButtonTooltip = function () {}; + var mpClickCallback = this.button.clickCallback; + this.button.clickCallback = (function(e, callback, btnName, args) { + // prevent the browser from breaking the editor focus + if (typeof e.preventDefault === 'function') { + e.preventDefault(); + } + + //noinspection JSUnresolvedFunction + mpClickCallback.call(this, e, callback, btnName, args); + }).bind(this); + // add custom buttons var button, buttonName, i, length; //noinspection JSUnresolvedVariable @@ -80,7 +91,7 @@ $.Redactor.prototype.WoltLabButton = function() { button[0].classList.add('jsTooltip'); // update dropdown label for list - if (buttonName == 'lists') { + if (buttonName === 'lists') { var dropdown = button.data('dropdown'); elBySel('.redactor-dropdown-outdent span', dropdown[0]).textContent = WCF.Language.get('wcf.editor.list.outdent'); elBySel('.redactor-dropdown-indent span', dropdown[0]).textContent = WCF.Language.get('wcf.editor.list.indent'); diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabInlineCode.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabInlineCode.js index ad383b8fc3..59a7e81120 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabInlineCode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabInlineCode.js @@ -1,11 +1,15 @@ $.Redactor.prototype.WoltLabInlineCode = function() { "use strict"; + var _environment; + return { init: function() { this.opts.activeButtonsStates.kbd = 'tt'; - require(['EventHandler'], (function (EventHandler) { + require(['Environment', 'EventHandler'], (function (Environment, EventHandler) { + _environment = Environment; + EventHandler.add('com.woltlab.wcf.redactor2', 'bbcode_tt_' + this.$element[0].id, this.WoltLabInlineCode._toggle.bind(this)); }).bind(this)); }, @@ -15,11 +19,33 @@ $.Redactor.prototype.WoltLabInlineCode = function() { this.button.toggle({}, 'kbd', 'func', 'inline.format'); - var node = window.getSelection().anchorNode; + var selection = window.getSelection(); + var node = selection.anchorNode; if (node.nodeType === Node.TEXT_NODE) node = node.parentNode; if (node.nodeName === 'KBD') { var nextSibling = node.nextSibling; + + if (_environment.platform() === 'ios' && _environment.browser() === 'safari') { + // iOS Safari work-around to allow caret placement after + if (nextSibling && nextSibling.nodeName === 'BR') { + node.parentNode.insertBefore(document.createTextNode('\u200B'), nextSibling); + } + + // fix selection position + if (node.innerHTML === '\u200B') { + // the caret must be at offset 0 (before the whitespace) + var range = selection.getRangeAt(0); + if (range.collapsed && range.startOffset === 1) { + node.innerHTML = this.marker.html() + '\u200B'; + + this.selection.restore(); + } + } + + return; + } + while (nextSibling) { if (nextSibling.nodeType !== Node.TEXT_NODE || nextSibling.textContent.length) { return; @@ -30,9 +56,11 @@ $.Redactor.prototype.WoltLabInlineCode = function() { if (nextSibling) { nextSibling.textContent = '\u200B'; + console.log("this"); } else { node.parentNode.appendChild(document.createTextNode('\u200B')); + console.log("this"); } } } -- 2.20.1