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(TS): extending canvas and object event types #8926

Merged
merged 4 commits into from
May 18, 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(TS): extending canvas and object event types (`type` => `interface`) [#8926](https://github.com/fabricjs/fabric.js/pull/8926)
- chore(build) simple deps update [#8929](https://github.com/fabricjs/fabric.js/pull/8929)
- fix(Canvas): sync cleanup of dom elements in dispose [#8903](https://github.com/fabricjs/fabric.js/pull/8903)
- chore(TS): export util types [#8915](https://github.com/fabricjs/fabric.js/pull/8915)
Expand Down
124 changes: 63 additions & 61 deletions src/EventTypeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export type DragEventData = TEvent<DragEvent> & {

export type DropEventData = DragEventData & { pointer: Point };

type DnDEvents = {
interface DnDEvents {
dragstart: TEventWithTarget<DragEvent>;
drag: DragEventData;
dragover: DragEventData;
Expand All @@ -177,14 +177,14 @@ type DnDEvents = {
'drop:before': DropEventData;
drop: DropEventData;
'drop:after': DropEventData;
};
}

type CanvasDnDEvents = DnDEvents & {
interface CanvasDnDEvents extends DnDEvents {
'drag:enter': DragEventData & InEvent;
'drag:leave': DragEventData & OutEvent;
};
}

type CanvasSelectionEvents = {
interface CanvasSelectionEvents {
'selection:created': Partial<TEvent> & {
selected: FabricObject[];
};
Expand All @@ -198,12 +198,12 @@ type CanvasSelectionEvents = {
'selection:cleared': Partial<TEvent> & {
deselected: FabricObject[];
};
};
}

export type CollectionEvents = {
export interface CollectionEvents {
'object:added': { target: StaticFabricObject };
'object:removed': { target: StaticFabricObject };
};
}

type BeforeSuffix<T extends string> = `${T}:before`;
type WithBeforeSuffix<T extends string> = T | BeforeSuffix<T>;
Expand All @@ -230,67 +230,69 @@ export type TPointerEventNames =
export type ObjectPointerEvents = TPointerEvents<'mouse'>;
export type CanvasPointerEvents = TPointerEvents<'mouse:'>;

export type MiscEvents = {
export interface MiscEvents {
'contextmenu:before': SimpleEventHandler<Event>;
contextmenu: SimpleEventHandler<Event>;
};

export type ObjectEvents = ObjectPointerEvents &
DnDEvents &
MiscEvents &
ObjectModificationEvents & {
// selection
selected: Partial<TEvent> & {
target: FabricObject;
};
deselected: Partial<TEvent> & {
target: FabricObject;
};

// tree
added: { target: Group | Canvas | StaticCanvas };
removed: { target: Group | Canvas | StaticCanvas };

// erasing
'erasing:end': { path: FabricObject };
}

export interface ObjectEvents
extends ObjectPointerEvents,
DnDEvents,
MiscEvents,
ObjectModificationEvents {
// selection
selected: Partial<TEvent> & {
target: FabricObject;
};
deselected: Partial<TEvent> & {
target: FabricObject;
};

export type StaticCanvasEvents = CollectionEvents & {
// tree
added: { target: Group | Canvas | StaticCanvas };
removed: { target: Group | Canvas | StaticCanvas };

// erasing
'erasing:end': { path: FabricObject };
}

export interface StaticCanvasEvents extends CollectionEvents {
// tree
'canvas:cleared': never;

// rendering
'before:render': { ctx: CanvasRenderingContext2D };
'after:render': { ctx: CanvasRenderingContext2D };
};

export type CanvasEvents = StaticCanvasEvents &
CanvasPointerEvents &
CanvasDnDEvents &
MiscEvents &
CanvasModificationEvents &
CanvasSelectionEvents & {
// brushes
'before:path:created': { path: FabricObject };
'path:created': { path: FabricObject };

// erasing
'erasing:start': never;
'erasing:end':
| never
| {
path: FabricObject;
targets: FabricObject[];
subTargets: FabricObject[];
drawables: {
backgroundImage?: FabricObject;
overlayImage?: FabricObject;
};
}

export interface CanvasEvents
extends StaticCanvasEvents,
CanvasPointerEvents,
CanvasDnDEvents,
MiscEvents,
CanvasModificationEvents,
CanvasSelectionEvents {
// brushes
'before:path:created': { path: FabricObject };
'path:created': { path: FabricObject };

// erasing
'erasing:start': never;
'erasing:end':
| never
| {
path: FabricObject;
targets: FabricObject[];
subTargets: FabricObject[];
drawables: {
backgroundImage?: FabricObject;
overlayImage?: FabricObject;
};

// IText
'text:selection:changed': { target: IText };
'text:changed': { target: IText };
'text:editing:entered': { target: IText };
'text:editing:exited': { target: IText };
};
};

// IText
'text:selection:changed': { target: IText };
'text:changed': { target: IText };
'text:editing:entered': { target: IText };
'text:editing:exited': { target: IText };
}