Update itext_key_behavior.mixin.js

Better behaviour of selection with keyboard.
Now you can cross from select direction LEFT to RIGHT and back.
This commit is contained in:
Andrea Bogazzi 2014-08-25 15:10:12 +02:00
parent 41b53f53e4
commit f83636acb1

View file

@ -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';
},
/**