Fix fast type in itext. (#4339)

* fix fast type
This commit is contained in:
Andrea Bogazzi 2017-09-23 00:39:20 +02:00 committed by GitHub
parent d7131d1cac
commit dd6cbe93a2
5 changed files with 28 additions and 11 deletions

View file

@ -478,6 +478,9 @@
}
this.cursorOffsetCache = { };
this.text = this.hiddenTextarea.value;
if (this._shouldClearDimensionCache()) {
this.initDimensions();
}
var newSelection = this.fromStringToGraphemeSelection(
this.hiddenTextarea.selectionStart, this.hiddenTextarea.selectionEnd, this.hiddenTextarea.value);
this.selectionEnd = this.selectionStart = newSelection.selectionEnd;

View file

@ -154,6 +154,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
this.canvas.fire('text:changed', { target: this });
this.canvas.requestRenderAll();
}
return;
}
if (this.selectionStart !== this.selectionEnd) {

View file

@ -309,6 +309,19 @@
return fabric._measuringContext;
},
/**
* @private
* Divides text into lines of text and lines of graphemes.
*/
_splitText: function() {
var newLines = this._splitTextIntoLines(this.text);
this.textLines = newLines.lines;
this._textLines = newLines.graphemeLines;
this._unwrappedTextLines = newLines._unwrappedLines;
this._text = newLines.graphemeText;
return newLines;
},
/**
* Initialize or update text dimensions.
* Updates this.width and this.height with the proper values.
@ -318,11 +331,7 @@
if (this.__skipDimension) {
return;
}
var newLines = this._splitTextIntoLines(this.text);
this.textLines = newLines.lines;
this._unwrappedTextLines = newLines._unwrappedLines;
this._textLines = newLines.graphemeLines;
this._text = newLines.graphemeText;
this._splitText();
this._clearCache();
this.width = this.calcTextWidth() || this.cursorWidth || MIN_TEXT_WIDTH;
if (this.textAlign === 'justify') {

View file

@ -89,12 +89,7 @@
// clear dynamicMinWidth as it will be different after we re-wrap line
this.dynamicMinWidth = 0;
// wrap lines
var newText = this._splitTextIntoLines(this.text);
this.textLines = newText.lines;
this._textLines = newText.graphemeLines;
this._unwrappedTextLines = newText._unwrappedLines;
this._text = newText.graphemeText;
this._styleMap = this._generateStyleMap(newText);
this._styleMap = this._generateStyleMap(this._splitText());
// if after wrapping, the width is smaller than dynamicMinWidth, change the width and re-wrap
if (this.dynamicMinWidth > this.width) {
this._set('width', this.dynamicMinWidth);

View file

@ -503,6 +503,15 @@
assert.deepEqual(iText.getStyleAtPosition(18), { fill: 'green' });
});
QUnit.test('_splitText', function(assert) {
var text = new fabric.Text('test foo bar-baz\nqux', {});
var test = text._splitText();
assert.equal(test.lines[0], 'test foo bar-baz', 'first line is correct');
assert.equal(test.lines[1], 'qux', 'second line is correct');
assert.deepEqual(test.graphemeLines[0], ['t','e','s','t',' ','f','o','o',' ','b','a','r','-','b','a','z'], 'first line is correct');
assert.deepEqual(test.graphemeLines[1], ['q','u','x'], 'second line is correct');
});
QUnit.test('getStyleAtPosition complete', function(assert) {
var iText = new fabric.Text('test foo bar-baz\nqux', {
styles: {