From f83636acb1c50e11e592bd5de80a0468f81709e6 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Mon, 25 Aug 2014 15:10:12 +0200 Subject: [PATCH] Update itext_key_behavior.mixin.js Better behaviour of selection with keyboard. Now you can cross from select direction LEFT to RIGHT and back. --- src/mixins/itext_key_behavior.mixin.js | 52 +++++++++++++------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/mixins/itext_key_behavior.mixin.js b/src/mixins/itext_key_behavior.mixin.js index 8f45dcb3..cdc36d1d 100644 --- a/src/mixins/itext_key_behavior.mixin.js +++ b/src/mixins/itext_key_behavior.mixin.js @@ -285,23 +285,31 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot this.selectionEnd = this.selectionStart; }, + /** + * private + */ + swapSelectionPoints: function() { + var swapSel = this.selectionEnd; + this.selectionEnd = this.selectionStart; + this.selectionStart = swapSel; + } + /** * Moves cursor down while keeping selection * @param {Number} offset */ moveCursorDownWithShift: function(offset) { - if (this._selectionDirection === 'left' && (this.selectionStart !== this.selectionEnd)) { - this.selectionStart += offset; - this._selectionDirection = 'left'; - return; - } - else { + if (this.selectionEnd === this.selectionStart) { this._selectionDirection = 'right'; - this.selectionEnd += offset; - - if (this.selectionEnd > this.text.length) { - this.selectionEnd = this.text.length; - } + } + var prop = this._selectionDirection === 'right' ? 'selectionEnd' : 'selectionStart'; + this[prop] += offset; + if (this.selectionEnd < this.selectionStart && this._selectionDirection === 'left') { + this.swapSelectionPoints(); + this._selectionDirection = 'right'; + } + if (this.selectionEnd > this.text.length) { + this.selectionEnd = this.text.length; } }, @@ -406,26 +414,18 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot * @param {Number} offset */ moveCursorUpWithShift: function(offset) { - - if (this.selectionStart === this.selectionEnd) { - this.selectionStart -= offset; + if (this.selectionEnd === this.selectionStart) { + this._selectionDirection = 'left'; } - else { - if (this._selectionDirection === 'right') { - this.selectionEnd -= offset; - this._selectionDirection = 'right'; - return; - } - else { - this.selectionStart -= offset; - } + var prop = this._selectionDirection === 'right' ? 'selectionEnd' : 'selectionStart'; + this[prop] -= offset; + if (this.selectionEnd < this.selectionStart && this._selectionDirection === 'right') { + this.swapSelectionPoints(); + this._selectionDirection = 'left'; } - if (this.selectionStart < 0) { this.selectionStart = 0; } - - this._selectionDirection = 'left'; }, /**