Skip to content

Commit

Permalink
text fixes to style (#3743)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur committed Feb 25, 2017
1 parent a429773 commit 7873c4f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 35 deletions.
24 changes: 10 additions & 14 deletions src/mixins/itext_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -672,10 +670,6 @@

this.shiftLineStyles(lineIndex, +1);

if (!this.styles[lineIndex + 1]) {
this.styles[lineIndex + 1] = {};
}

var currentCharStyle = {},
newLineStyles = {};

Expand All @@ -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;
},
Expand Down Expand Up @@ -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;
},

Expand Down
20 changes: 2 additions & 18 deletions src/mixins/textbox_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
}
};

var clone = fabric.util.object.clone;

fabric.util.object.extend(fabric.Textbox.prototype, /** @lends fabric.IText.prototype */ {
/**
* @private
Expand Down Expand Up @@ -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);
},

/**
Expand Down
4 changes: 1 addition & 3 deletions src/shapes/itext.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 7873c4f

Please sign in to comment.