diff --git a/src/mixins/itext_behavior.mixin.js b/src/mixins/itext_behavior.mixin.js index 86b4e07b..a0904119 100644 --- a/src/mixins/itext_behavior.mixin.js +++ b/src/mixins/itext_behavior.mixin.js @@ -360,27 +360,28 @@ * Gets start offset of a selection * @return {Number} */ - getDownCursorOffset: function(e) { + getDownCursorOffset: function(e, isRight) { - var textLines = this.text.split(this._reNewline), + var selectionProp = isRight ? this.selectionEnd : this.selectionStart, + textLines = this.text.split(this._reNewline), _char, lineLeftOffset, foundMatch, - textBeforeCursor = this.text.slice(0, this.selectionStart), - textAfterCursor = this.text.slice(this.selectionStart), + textBeforeCursor = this.text.slice(0, selectionProp), + textAfterCursor = this.text.slice(selectionProp), textOnSameLineBeforeCursor = textBeforeCursor.slice(textBeforeCursor.lastIndexOf('\n') + 1), textOnSameLineAfterCursor = textAfterCursor.match(/(.*)\n?/)[1], textOnNextLine = (textAfterCursor.match(/.*\n(.*)\n?/) || { })[1] || '', - cursorLocation = this.get2DCursorLocation(); + cursorLocation = this.get2DCursorLocation(selectionProp); // if on last line, down cursor goes to end of line if (cursorLocation.lineIndex === textLines.length - 1 || e.metaKey) { // move to the end of a text - return this.text.length - this.selectionStart; + return this.text.length - selectionProp; } var widthOfSameLineBeforeCursor = this._getWidthOfLine(this.ctx, cursorLocation.lineIndex, textLines); @@ -441,7 +442,7 @@ this.abortCursorAnimation(); this._currentCursorOpacity = 1; - var offset = this.getDownCursorOffset(e); + var offset = this.getDownCursorOffset(e, this._selectionDirection === 'right'); if (e.shiftKey) { this.moveCursorDownWithShift(offset); @@ -475,8 +476,9 @@ moveCursorDownWithShift: function(offset) { if (this._selectionDirection === 'left' && (this.selectionStart !== this.selectionEnd)) { - this.selectionStart = this.selectionEnd; - this._selectionDirection = 'right'; + this.selectionStart += offset; + this._selectionDirection = 'left'; + return; } else { this._selectionDirection = 'right'; @@ -488,16 +490,17 @@ } }, - getUpCursorOffset: function(e) { + getUpCursorOffset: function(e, isRight) { - var cursorLocation = this.get2DCursorLocation(); + var selectionProp = isRight ? this.selectionEnd : this.selectionStart, + cursorLocation = this.get2DCursorLocation(selectionProp); // if on first line, up cursor goes to start of line if (cursorLocation.lineIndex === 0 || e.metaKey) { - return this.selectionStart; + return selectionProp; } - var textBeforeCursor = this.text.slice(0, this.selectionStart), + var textBeforeCursor = this.text.slice(0, selectionProp), textOnSameLineBeforeCursor = textBeforeCursor.slice(textBeforeCursor.lastIndexOf('\n') + 1), textOnPreviousLine = (textBeforeCursor.match(/\n?(.*)\n.*$/) || {})[1] || '', textLines = this.text.split(this._reNewline), @@ -563,7 +566,7 @@ this.abortCursorAnimation(); this._currentCursorOpacity = 1; - var offset = this.getUpCursorOffset(e); + var offset = this.getUpCursorOffset(e, this._selectionDirection === 'right'); if (e.shiftKey) { this.moveCursorUpWithShift(offset); @@ -580,17 +583,21 @@ * @param {Number} offset */ moveCursorUpWithShift: function(offset) { + if (this.selectionStart === this.selectionEnd) { this.selectionStart -= offset; } else { if (this._selectionDirection === 'right') { - this.selectionEnd = this.selectionStart; + this.selectionEnd -= offset; + this._selectionDirection = 'right'; + return; } else { this.selectionStart -= offset; } } + if (this.selectionStart < 0) { this.selectionStart = 0; }