Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some code from ITEXT, do not pollute styles if not necessary #3743

Merged
merged 1 commit into from
Feb 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boundaries.leftOffset and this._getLineLeftOffset(this._getLineWidth(this.ctx, lineIndex)) carry the same value. no need to make a special case.

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create the new line style only if necessary

}
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the prototype function does exactly the same thing

},

/**
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