diff --git a/src/mixins/itext_behavior.mixin.js b/src/mixins/itext_behavior.mixin.js index 0cb12760c76..ddd48ae5b74 100644 --- a/src/mixins/itext_behavior.mixin.js +++ b/src/mixins/itext_behavior.mixin.js @@ -466,9 +466,7 @@ lineIndex = cursorLocation.lineIndex, charIndex = cursorLocation.charIndex, charHeight = this.getCurrentCharFontSize(lineIndex, charIndex), - leftOffset = (lineIndex === 0 && charIndex === 0) - ? this._getLineLeftOffset(this._getLineWidth(this.ctx, lineIndex)) - : boundaries.leftOffset, + leftOffset = boundaries.leftOffset, m = this.calcTransformMatrix(), p = { x: boundaries.left + leftOffset, @@ -672,10 +670,6 @@ this.shiftLineStyles(lineIndex, +1); - if (!this.styles[lineIndex + 1]) { - this.styles[lineIndex + 1] = {}; - } - var currentCharStyle = {}, newLineStyles = {}; @@ -685,21 +679,24 @@ // if there's nothing after cursor, // we clone current char style onto the next (otherwise empty) line - if (isEndOfLine) { + if (isEndOfLine && currentCharStyle) { newLineStyles[0] = clone(currentCharStyle); this.styles[lineIndex + 1] = newLineStyles; } // otherwise we clone styles of all chars // after cursor onto the next line, from the beginning else { + var somethingAdded = false; for (var index in this.styles[lineIndex]) { - if (parseInt(index, 10) >= charIndex) { - newLineStyles[parseInt(index, 10) - charIndex] = this.styles[lineIndex][index]; + var numIndex = parseInt(index, 10); + if (numIndex >= charIndex) { + somethingAdded = true; + newLineStyles[numIndex - charIndex] = this.styles[lineIndex][index]; // remove lines from the previous line since they're on a new line now delete this.styles[lineIndex][index]; } } - this.styles[lineIndex + 1] = newLineStyles; + somethingAdded && (this.styles[lineIndex + 1] = newLineStyles); } this._forceClearCache = true; }, @@ -733,9 +730,8 @@ } } } - - this.styles[lineIndex][charIndex] = - style || clone(currentLineStyles[charIndex - 1]); + var newStyle = style || currentLineStyles[charIndex - 1]; + newStyle && (this.styles[lineIndex][charIndex] = newStyle); this._forceClearCache = true; }, diff --git a/src/mixins/textbox_behavior.mixin.js b/src/mixins/textbox_behavior.mixin.js index 89913a0581c..b19476685b9 100644 --- a/src/mixins/textbox_behavior.mixin.js +++ b/src/mixins/textbox_behavior.mixin.js @@ -40,8 +40,6 @@ } }; - var clone = fabric.util.object.clone; - fabric.util.object.extend(fabric.Textbox.prototype, /** @lends fabric.IText.prototype */ { /** * @private @@ -93,24 +91,10 @@ */ shiftLineStyles: function(lineIndex, offset) { // shift all line styles by 1 upward - var clonedStyles = clone(this.styles), - map = this._styleMap[lineIndex]; - + var map = this._styleMap[lineIndex]; // adjust line index lineIndex = map.line; - - for (var line in this.styles) { - var numericLine = parseInt(line, 10); - - if (numericLine > lineIndex) { - this.styles[numericLine + offset] = clonedStyles[numericLine]; - - if (!clonedStyles[numericLine - offset]) { - delete this.styles[numericLine]; - } - } - } - //TODO: evaluate if delete old style lines with offset -1 + fabric.IText.prototype.shiftLineStyles.call(this, lineIndex, offset); }, /** diff --git a/src/shapes/itext.class.js b/src/shapes/itext.class.js index 1e920a266a2..13c1c84440b 100644 --- a/src/shapes/itext.class.js +++ b/src/shapes/itext.class.js @@ -548,9 +548,7 @@ lineIndex = cursorLocation.lineIndex, charIndex = cursorLocation.charIndex, charHeight = this.getCurrentCharFontSize(lineIndex, charIndex), - leftOffset = (lineIndex === 0 && charIndex === 0) - ? this._getLineLeftOffset(this._getLineWidth(ctx, lineIndex)) - : boundaries.leftOffset, + leftOffset = boundaries.leftOffset, multiplier = this.scaleX * this.canvas.getZoom(), cursorWidth = this.cursorWidth / multiplier;