fix selection and up + down movements

This commit is contained in:
inssein 2015-06-03 11:31:02 -07:00
parent d08dabc2ab
commit 2b3c75b6b1
2 changed files with 25 additions and 13 deletions

View file

@ -1,22 +1,24 @@
(function () {
var getNewSelectionStartFromOffsetOverriden = fabric.IText.prototype._getNewSelectionStartFromOffset;
var override = fabric.IText.prototype._getNewSelectionStartFromOffset;
/**
* Overrides the IText implementation and always sends lineIndex as 0 for Textboxes.
* Overrides the IText implementation and adjusts character index as there is not always a linebreak
*
* @param {Number} mouseOffset
* @param {Number} prevWidth
* @param {Number} width
* @param {Number} index
* @param {Number} lineIndex
* @param {Number} jlen
* @returns {Number}
*/
fabric.IText.prototype._getNewSelectionStartFromOffset = function (mouseOffset,
prevWidth, width, index, lineIndex, jlen) {
if (this instanceof fabric.Textbox) {
lineIndex = 0;
}
return getNewSelectionStartFromOffsetOverriden
.call(this, mouseOffset,
prevWidth, width, index, lineIndex, jlen);
fabric.IText.prototype._getNewSelectionStartFromOffset = function (mouseOffset, prevWidth, width, index, jlen) {
// start by getting the iText cursor location, and then figure out how many actual new lines there are
var cursor = this.get2DCursorLocation(index),
newLines = this.text.substring(0, index).split(this._reNewline).length - 1;
// adjust index by actual new lines
index -= cursor.lineIndex - newLines;
return override.call(this, mouseOffset, prevWidth, width, index, jlen);
};
})();

View file

@ -2,21 +2,31 @@ fabric.util.object.extend(fabric.Textbox.prototype, /** @lends fabric.Textbox.pr
/**
* Overrides superclass function and adjusts cursor offset value because
* lines do not necessarily end with a newline in Textbox.
*
* @param {Event} e
* @param {Boolean} isRight
* @returns {Number}
*/
getDownCursorOffset: function (e, isRight) {
return fabric.IText.prototype.getDownCursorOffset.apply(this, [e, isRight]) - 1;
var current = this.selectionStart,
offset = fabric.IText.prototype.getDownCursorOffset.apply(this, [e, isRight]),
nextNewline = this.text.indexOf('\n', current);
return nextNewline >= current && nextNewline <= current + offset ? offset : offset - 1;
},
/**
* Overrides superclass function and adjusts cursor offset value because
* lines do not necessarily end with a newline in Textbox.
*
* @param {Event} e
* @param {Boolean} isRight
* @returns {Number}
*/
getUpCursorOffset: function (e, isRight) {
return fabric.IText.prototype.getUpCursorOffset.apply(this, [e, isRight]) - 1;
var current = this.selectionStart,
offset = fabric.IText.prototype.getUpCursorOffset.apply(this, [e, isRight]),
previousNewLine = this.text.indexOf('\n', current - offset);
return previousNewLine >= current - offset && previousNewLine <= current ? offset : offset - 1;
}
});