diff --git a/src/mixins/itext_behavior.mixin.js b/src/mixins/itext_behavior.mixin.js index 1e790760ffb..44b9870fb37 100644 --- a/src/mixins/itext_behavior.mixin.js +++ b/src/mixins/itext_behavior.mixin.js @@ -703,35 +703,35 @@ lineIndex = cursorLocation.lineIndex, charIndex = cursorLocation.charIndex; - if (isBeginningOfLine) { + this._removeStyleObject(isBeginningOfLine, cursorLocation, lineIndex, charIndex); + }, + + _getTextOnPreviousLine: function(lIndex) { + return this._textLines[lIndex - 1]; + }, + + _removeStyleObject: function(isBeginningOfLine, cursorLocation, lineIndex, charIndex) { - var textOnPreviousLine = this._textLines[lineIndex - 1], - newCharIndexOnPrevLine = textOnPreviousLine - ? textOnPreviousLine.length - : 0; + if (isBeginningOfLine) { + var textOnPreviousLine = this._getTextOnPreviousLine(cursorLocation.lineIndex), + newCharIndexOnPrevLine = textOnPreviousLine ? textOnPreviousLine.length : 0; if (!this.styles[lineIndex - 1]) { this.styles[lineIndex - 1] = {}; } - for (charIndex in this.styles[lineIndex]) { this.styles[lineIndex - 1][parseInt(charIndex, 10) + newCharIndexOnPrevLine] = this.styles[lineIndex][charIndex]; } - - this.shiftLineStyles(lineIndex, -1); - + this.shiftLineStyles(cursorLocation.lineIndex, -1); } else { var currentLineStyles = this.styles[lineIndex]; if (currentLineStyles) { delete currentLineStyles[charIndex]; - //console.log('deleting', lineIndex, charIndex + offset); } - var currentLineStylesCloned = clone(currentLineStyles); - // shift all styles by 1 backwards for (var i in currentLineStylesCloned) { var numericIndex = parseInt(i, 10); diff --git a/src/mixins/itext_key_behavior.mixin.js b/src/mixins/itext_key_behavior.mixin.js index e5d33fc616f..cb4a4ce3781 100644 --- a/src/mixins/itext_key_behavior.mixin.js +++ b/src/mixins/itext_key_behavior.mixin.js @@ -81,14 +81,14 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot * @param {Event} e Event object */ onInput: function(e) { - if (!this.isEditing || this._cancelOnInput) { - this._cancelOnInput = false; + if (!this.isEditing) { return; } var offset = this.selectionStart || 0, + offsetEnd = this.selectionEnd || 0, textLength = this.text.length, newTextLength = this.hiddenTextarea.value.length, - diff = newTextLength - textLength, + diff = newTextLength - textLength + offsetEnd - offset, charsToInsert = this.hiddenTextarea.value.slice(offset, offset + diff); this.insertChars(charsToInsert); e.stopPropagation(); @@ -149,7 +149,8 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot if (copiedText) { this.insertChars(copiedText, useCopiedStyle); } - this._cancelOnInput = true; + e.stopImmediatePropagation(); + e.preventDefault(); }, /** diff --git a/src/mixins/textbox_behavior.mixin.js b/src/mixins/textbox_behavior.mixin.js index 2b9d934bcb9..e8d8bc61769 100644 --- a/src/mixins/textbox_behavior.mixin.js +++ b/src/mixins/textbox_behavior.mixin.js @@ -142,42 +142,7 @@ map = this._styleMap[cursorLocation.lineIndex], lineIndex = map.line, charIndex = map.offset + cursorLocation.charIndex; - - if (isBeginningOfLine) { - var textOnPreviousLine = this._getTextOnPreviousLine(cursorLocation.lineIndex), - newCharIndexOnPrevLine = textOnPreviousLine ? textOnPreviousLine.length : 0; - - if (!this.styles[lineIndex - 1]) { - this.styles[lineIndex - 1] = {}; - } - - for (charIndex in this.styles[lineIndex]) { - this.styles[lineIndex - 1][parseInt(charIndex, 10) + newCharIndexOnPrevLine] - = this.styles[lineIndex][charIndex]; - } - - this.shiftLineStyles(cursorLocation.lineIndex, -1); - - } - else { - var currentLineStyles = this.styles[lineIndex]; - - if (currentLineStyles) { - delete currentLineStyles[charIndex]; - //console.log('deleting', lineIndex, charIndex + offset); - } - - var currentLineStylesCloned = clone(currentLineStyles); - - // shift all styles by 1 backwards - for (var i in currentLineStylesCloned) { - var numericIndex = parseInt(i, 10); - if (numericIndex >= charIndex && numericIndex !== 0) { - currentLineStyles[numericIndex - 1] = currentLineStylesCloned[numericIndex]; - delete currentLineStyles[numericIndex]; - } - } - } + this._removeStyleObject(isBeginningOfLine, cursorLocation, lineIndex, charIndex); } }); })();