mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-05-10 06:44:44 +00:00
fix selection and up + down movements
This commit is contained in:
parent
d08dabc2ab
commit
2b3c75b6b1
2 changed files with 25 additions and 13 deletions
|
|
@ -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);
|
||||
};
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue