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

fix(): Avoid firing twice in subselection #9329

Merged
merged 4 commits into from
Sep 26, 2023
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
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]

- fix(Canvas): avoid firing event twice when working with nested objects [#9329](https://github.com/fabricjs/fabric.js/pull/9329)
- fix(Control): `calcCornerCoords` angle + calculation [#9377](https://github.com/fabricjs/fabric.js/pull/9377)
- patch(): dep findCrossPoints in favor of `isPointInPolygon` [#9374](https://github.com/fabricjs/fabric.js/pull/9374)
- docs() enable typedocs to run again [#9356](https://github.com/fabricjs/fabric.js/pull/9356)
Expand Down
4 changes: 2 additions & 2 deletions src/canvas/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
this.fire(eventType, options);
target && target.fire(eventType, options);
for (let i = 0; i < subTargets.length; i++) {
subTargets[i].fire(eventType, options);
subTargets[i] !== target && subTargets[i].fire(eventType, options);
}
return options;
}
Expand Down Expand Up @@ -943,7 +943,7 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
// this may be a little be more complicated of what we want to handle
target && target.fire(`mouse${eventType}`, options);
for (let i = 0; i < targets.length; i++) {
targets[i].fire(`mouse${eventType}`, options);
targets[i] !== target && targets[i].fire(`mouse${eventType}`, options);
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/canvas/__tests__/eventData.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable no-restricted-globals */
import '../../../jest.extend';
import type { TPointerEvent } from '../../EventTypeDefs';
import { Point } from '../../Point';
import { Group } from '../../shapes/Group';
import { IText } from '../../shapes/IText/IText';
import { FabricObject } from '../../shapes/Object/FabricObject';
import type { TMat2D } from '../../typedefs';
import { Canvas } from '../Canvas';

Expand Down Expand Up @@ -100,3 +103,22 @@ describe('Canvas event data', () => {
}
);
});

it('A selected subtarget should not fire an event twice', () => {
const target = new FabricObject();
const group = new Group([target], {
subTargetCheck: true,
interactive: true,
});
const canvas = new Canvas(null);
canvas.add(group);
const targetSpy = jest.fn();
target.on('mousedown', targetSpy);
jest.spyOn(canvas, '_checkTarget').mockReturnValue(true);
canvas.__onMouseDown({
target: canvas.getSelectionElement(),
clientX: 0,
clientY: 0,
} as unknown as TPointerEvent);
expect(targetSpy).toHaveBeenCalledTimes(1);
});