Skip to content

Commit

Permalink
update(IText): Add method enterEditingImpl (#10190)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhe-he authored Oct 8, 2024
1 parent 98359dd commit 3a9b093
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- update(IText): Add method enterEditingImpl/exitEditingImpl that executes the logic of enterEditing/exitEditing without events [#10187](https://github.com/fabricjs/fabric.js/issues/10187)
- fix(FabricObject): Fix clipPath blurryness with scale [#9774](https://github.com/fabricjs/fabric.js/pull/9774)

## [6.4.3]
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/IText/IText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ export class IText<
}

dispose() {
this._exitEditing();
this.exitEditingImpl();
this.draggableTextDelegate.dispose();
super.dispose();
}
Expand Down
32 changes: 32 additions & 0 deletions src/shapes/IText/ITextBehavior.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ describe('IText cursor animation snapshot', () => {
});
jest.useFakeTimers();
});
afterAll(() => {
ValueAnimation.prototype.calculate = origCalculate;
});
beforeEach(() => {
jest.runAllTimers();
currentAnimation = [];
Expand Down Expand Up @@ -193,3 +196,32 @@ describe('IText _tick', () => {
expect(_tickMock).toHaveBeenCalledWith(0);
});
});

describe('Itext enterEditing and exitEditing', () => {
const enterMock = jest.fn();
const exitMock = jest.fn();

afterEach(() => {
enterMock.mockClear();
exitMock.mockClear();
});

test('Entering and leaving edit triggers the listener', () => {
const iText = new IText('some word');
iText.on('editing:entered', enterMock);
iText.on('editing:exited', exitMock);
iText.enterEditing();
expect(enterMock).toHaveBeenCalledTimes(1);
iText.exitEditing();
expect(exitMock).toHaveBeenCalledTimes(1);
});
test('Entering and leaving edit does not trigger the listener', () => {
const iText = new IText('some word');
iText.on('editing:entered', enterMock);
iText.on('editing:exited', exitMock);
iText.enterEditingImpl();
expect(enterMock).toHaveBeenCalledTimes(0);
iText.exitEditingImpl();
expect(exitMock).toHaveBeenCalledTimes(0);
});
});
43 changes: 31 additions & 12 deletions src/shapes/IText/ITextBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,22 @@ export abstract class ITextBehavior<
if (this.isEditing || !this.editable) {
return;
}
this.enterEditingImpl();
this.fire('editing:entered', e ? { e } : undefined);
this._fireSelectionChanged();
if (this.canvas) {
this.canvas.fire('text:editing:entered', {
target: this as unknown as IText,
e,
});
this.canvas.requestRenderAll();
}
}

/**
* runs the actual logic that enter from editing state, see {@link enterEditing}
*/
enterEditingImpl() {
if (this.canvas) {
this.canvas.calcOffset();
this.canvas.textEditingManager.exitTextEditing();
Expand All @@ -403,15 +419,6 @@ export abstract class ITextBehavior<
this._textBeforeEdit = this.text;

this._tick();
this.fire('editing:entered', e ? { e } : undefined);
this._fireSelectionChanged();
if (this.canvas) {
this.canvas.fire('text:editing:entered', {
target: this as unknown as IText,
e,
});
this.canvas.requestRenderAll();
}
}

/**
Expand Down Expand Up @@ -669,6 +676,9 @@ export abstract class ITextBehavior<

/**
* runs the actual logic that exits from editing state, see {@link exitEditing}
* Please use exitEditingImpl, this function was kept to avoid breaking changes.
* Will be removed in fabric 7.0
* @deprecated use "exitEditingImpl"
*/
protected _exitEditing() {
const hiddenTextarea = this.hiddenTextarea;
Expand All @@ -686,17 +696,26 @@ export abstract class ITextBehavior<
}

/**
* Exits from editing state and fires relevant events
* runs the actual logic that exits from editing state, see {@link exitEditing}
* But it does not fire events
*/
exitEditing() {
const isTextChanged = this._textBeforeEdit !== this.text;
exitEditingImpl() {
this._exitEditing();
this.selectionEnd = this.selectionStart;
this._restoreEditingProps();
if (this._forceClearCache) {
this.initDimensions();
this.setCoords();
}
}

/**
* Exits from editing state and fires relevant events
*/
exitEditing() {
const isTextChanged = this._textBeforeEdit !== this.text;
this.exitEditingImpl();

this.fire('editing:exited');
isTextChanged && this.fire(MODIFIED);
if (this.canvas) {
Expand Down

0 comments on commit 3a9b093

Please sign in to comment.