Skip to content

Commit

Permalink
fix(IText): layout regression
Browse files Browse the repository at this point in the history
caused by #8663
  • Loading branch information
ShaMan123 committed Feb 19, 2023
1 parent 02a2f8a commit b8a4ffc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/shapes/IText/ITextBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,9 @@ export abstract class ITextBehavior<
return;
}
this.cursorOffsetCache = {};
const prevText = this.text;
this.text = this.hiddenTextarea.value;
if (this._shouldClearDimensionCache()) {
if (this._forceClearCache || prevText !== this.text) {
this.initDimensions();
this.setCoords();
}
Expand Down Expand Up @@ -657,7 +658,7 @@ export abstract class ITextBehavior<
this.selectionEnd = this.selectionStart;
this._exitEditing();
this._restoreEditingProps();
if (this._shouldClearDimensionCache()) {
if (this._forceClearCache) {
this.initDimensions();
this.setCoords();
}
Expand Down Expand Up @@ -974,7 +975,7 @@ export abstract class ITextBehavior<
this._text.splice(start, end - start);
this.text = this._text.join('');
this.set('dirty', true);
if (this._shouldClearDimensionCache()) {
if (this._forceClearCache || start !== end) {
this.initDimensions();
this.setCoords();
}
Expand Down Expand Up @@ -1011,7 +1012,7 @@ export abstract class ITextBehavior<
];
this.text = this._text.join('');
this.set('dirty', true);
if (this._shouldClearDimensionCache()) {
if (this._forceClearCache || start !== end || text.length > 0) {
this.initDimensions();
this.setCoords();
}
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/Text/StyledText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class StyledText<
> extends FabricObject<EventSpec> {
declare abstract styles: TextStyle;
protected declare abstract _textLines: string[][];
protected declare abstract _forceClearCache: boolean;
protected declare _forceClearCache: boolean;
protected declare abstract _styleProperties: string[];
abstract get2DCursorLocation(
selectionStart: number,
Expand Down
17 changes: 3 additions & 14 deletions src/shapes/Text/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ export class Text<
declare cursorWidth: number;
declare __lineHeights: number[];
declare __lineWidths: number[];
declare _forceClearCache: boolean;

declare initialized?: true;

Expand Down Expand Up @@ -428,6 +427,7 @@ export class Text<
initDimensions() {
this._splitText();
this._clearCache();
this.dirty = true;
if (this.path) {
this.width = this.path.width;
this.height = this.path.height;
Expand Down Expand Up @@ -1438,23 +1438,12 @@ export class Text<
* @private
*/
_clearCache() {
this._forceClearCache = false;
this.__lineWidths = [];
this.__lineHeights = [];
this.__charBounds = [];
}

/**
* @private
*/
_shouldClearDimensionCache() {
const shouldClear = this._forceClearCache;
if (shouldClear) {
this.dirty = true;
this._forceClearCache = false;
}
return shouldClear;
}

/**
* Measure a single line given its index. Used to calculate the initial
* text bounding box. The values are calculated and stored in __lineWidths cache.
Expand Down Expand Up @@ -1641,7 +1630,7 @@ export class Text<
) {
return;
}
if (this._shouldClearDimensionCache()) {
if (this._forceClearCache) {
this.initDimensions();
}
super.render(ctx);
Expand Down

0 comments on commit b8a4ffc

Please sign in to comment.