From a04510330eba14a7caa85db8b881f9a0754711a7 Mon Sep 17 00:00:00 2001 From: ShaMan123 Date: Fri, 29 Apr 2022 07:58:16 +0300 Subject: [PATCH] fix(): ctrl keys prior to non modifier keys --- src/mixins/itext_key_behavior.mixin.js | 108 +++++++++++-------------- 1 file changed, 45 insertions(+), 63 deletions(-) diff --git a/src/mixins/itext_key_behavior.mixin.js b/src/mixins/itext_key_behavior.mixin.js index 6ab826d9035..1dbeb1829a9 100644 --- a/src/mixins/itext_key_behavior.mixin.js +++ b/src/mixins/itext_key_behavior.mixin.js @@ -124,6 +124,30 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot KeyA: 'selectAll' }, + /** + * + * @param {KeyboardEvent} e + * @param {EventKeyMap} keyMap + * @returns + */ + getActionFromKeyboardEvent: function (e, keyMap) { + var func; + if (e.key in keyMap) { + func = keyMap[e.key]; + } + else if (e.code in keyMap) { + func = keyMap[e.code]; + } + else if (e.keyCode in keyMap) { + warnKeyCodeDeprecated(); + func = keyMap[e.keyCode]; + } + if (typeof func === 'string' && typeof this[func] === 'function') { + return this[func]; + } + return func; + }, + onClick: function() { // No need to trigger click event here, focus is enough to have the keyboard appear on Android this.hiddenTextarea && this.hiddenTextarea.focus(); @@ -145,49 +169,25 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot if (!this.isEditing) { return; } - var keyMap = this.direction === 'rtl' ? this.keysMapRtl : this.keysMap; - var func; - if (e.key in keyMap) { - func = keyMap[e.key]; - } - else if (e.code in keyMap) { - func = keyMap[e.code]; - } - else if (e.keyCode in keyMap) { - warnKeyCodeDeprecated(); - func = keyMap[e.keyCode]; - } - else if (e.ctrlKey || e.metaKey) { - if (e.key in this.ctrlKeysMapDown) { - func = this.ctrlKeysMapDown[e.key]; - } - else if (e.code in this.ctrlKeysMapDown) { - func = this.ctrlKeysMapDown[e.code]; - } - else if (e.keyCode in this.ctrlKeysMapDown) { - warnKeyCodeDeprecated(); - func = this.ctrlKeysMapDown[e.keyCode]; - } - } - if (typeof func === 'string' && typeof this[func] === 'function') { - func = this[func]; - } + var keyMap = e.ctrlKey || e.metaKey ? + this.ctrlKeysMapDown : + this.direction === 'rtl' ? + this.keysMapRtl : + this.keysMap; + var func = this.getActionFromKeyboardEvent(e, keyMap); if (typeof func === 'function') { func.call(this, e); - } - else { - return; - } - e.stopImmediatePropagation(); - e.preventDefault(); - if (isNavigationKey(e.code)) { - // if i press a navigation key just update selection - this.inCompositionMode = false; - this.clearContextTop(); - this.renderCursorOrSelection(); - } - else { - this.canvas && this.canvas.requestRenderAll(); + e.stopImmediatePropagation(); + e.preventDefault(); + if (isNavigationKey(e.code)) { + // if i press a navigation key just update selection + this.inCompositionMode = false; + this.clearContextTop(); + this.renderCursorOrSelection(); + } + else { + this.canvas && this.canvas.requestRenderAll(); + } } }, @@ -202,31 +202,13 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot this._copyDone = false; return; } - var func; - if (e.ctrlKey || e.metaKey) { - if (e.key in this.ctrlKeysMapUp) { - func = this.ctrlKeysMapUp[e.key]; - } - else if (e.code in this.ctrlKeysMapUp) { - func = this.ctrlKeysMapUp[e.code]; - } - else if (e.keyCode in this.ctrlKeysMapUp) { - warnKeyCodeDeprecated(); - func = this.ctrlKeysMapUp[e.keyCode]; - } - } - if (typeof func === 'string' && typeof this[func] === 'function') { - func = this[func]; - } + var func = this.getActionFromKeyboardEvent(e, this.ctrlKeysMapUp); if (typeof func === 'function') { func.call(this, e); + e.stopImmediatePropagation(); + e.preventDefault(); + this.canvas && this.canvas.requestRenderAll(); } - else { - return; - } - e.stopImmediatePropagation(); - e.preventDefault(); - this.canvas && this.canvas.requestRenderAll(); }, /**