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

chore(TS) Ts convert event mixins #8519

Merged
merged 31 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
7c83395
broken progress
asturur Dec 12, 2022
fdf9245
save work
asturur Dec 12, 2022
aa0b140
save progress work
asturur Dec 17, 2022
6c02e74
may i have broke stuff
asturur Dec 17, 2022
84541aa
what did i do
asturur Dec 17, 2022
135ef2a
something killed fabric.config
asturur Dec 18, 2022
54e6351
stupid vscode you killed a full evening
asturur Dec 18, 2022
5ba11af
test passing
asturur Dec 18, 2022
47d717b
minimal changes
asturur Dec 18, 2022
305d6e8
clean up canvas
asturur Dec 18, 2022
cbfd44f
save work
asturur Dec 18, 2022
b9d98fa
minor adjustments
asturur Dec 18, 2022
368e129
Merge branch 'master' into ts-convert-event-mixins
asturur Dec 18, 2022
076bffa
converted more
asturur Dec 25, 2022
b21c28e
at least prettier
asturur Dec 25, 2022
6acf71a
trying to cleanup
asturur Dec 25, 2022
71d5dde
it builds at least
asturur Dec 26, 2022
db358ee
tweak index
asturur Dec 26, 2022
cfa48e1
prettier
asturur Dec 26, 2022
cee73f6
add changelog
asturur Dec 26, 2022
303a897
Merge branch 'master' into ts-convert-event-mixins
asturur Dec 26, 2022
8bd2b17
bugs removed
asturur Dec 26, 2022
af6f4ad
Merge branch 'ts-convert-event-mixins' of github.com:fabricjs/fabric.…
asturur Dec 26, 2022
a5ee9f8
correct arg order
asturur Dec 26, 2022
983f162
fixed more tests
asturur Dec 26, 2022
f1ea8e4
prettier
asturur Dec 26, 2022
e212f16
save - work
asturur Dec 26, 2022
d7e4128
moved fies in canvas folder
asturur Dec 27, 2022
f6540c9
This is a combination of 15 commits.
ShaMan123 Dec 28, 2022
dcf31b7
revert unnecessary import changes
asturur Dec 29, 2022
32c0531
Merge branch 'master' into ts-convert-event-mixins
asturur Dec 29, 2022
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]

- chore(TS): Convert Canvas events mixin and grouping mixin [#8519](https://github.com/fabricjs/fabric.js/pull/8519)
- chore(TS): Remove backward compatibility initialize methods [#8525](https://github.com/fabricjs/fabric.js/pull/8525/)
- chore(TS): replace getKlass utility with a registry that doesn't require full fabricJS to work [#8500](https://github.com/fabricjs/fabric.js/pull/8500)
- chore(): use context in static constructors [#8522](https://github.com/fabricjs/fabric.js/issues/8522)
Expand Down
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import './src/color';
import './src/gradient'; // optional gradient
import './src/pattern.class'; // optional pattern
import './src/shadow.class'; // optional shadow
import './src/static_canvas.class';
import './src/canvas.class'; // optional interaction
import './src/mixins/canvas_events.mixin'; // optional interaction
import './src/mixins/canvas_grouping.mixin'; // optional interaction
import './src/canvas/static_canvas.class';
import './src/canvas/canvas_events'; // optional interaction
import './src/mixins/canvas_dataurl_exporter.mixin';
import './src/mixins/canvas_serialization.mixin'; // optional serialization
import './src/mixins/canvas_gestures.mixin'; // optional gestures
import './src/canvas/canvas_gestures.mixin'; // optional gestures
import './src/mixins/canvas_animation.mixin'; // optional animation
import './src/mixins/canvas_straightening.mixin'; // optional animation
import './src/shapes/Object/FabricObject';
Expand Down
96 changes: 68 additions & 28 deletions src/EventTypeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import type { FabricObject } from './shapes/Object/FabricObject';
import type { Group } from './shapes/group.class';
import type { TOriginX, TOriginY, TRadian } from './typedefs';
import type { saveObjectTransform } from './util/misc/objectTransforms';
import type { Canvas } from './__types__';
import type { Canvas } from './canvas/canvas_events';
import type { IText } from './shapes/itext.class';
import type { StaticCanvas } from './static_canvas.class';
import type { StaticCanvas } from './canvas/static_canvas.class';

export type ModifierKey = keyof Pick<
MouseEvent | PointerEvent | TouchEvent,
Expand All @@ -24,9 +24,19 @@ export type TransformAction<T extends Transform = Transform, R = void> = (
y: number
) => R;

/**
* Control handlers that define a transformation
* Those handlers run when the user starts a transform and during a transform
*/
export type TransformActionHandler<T extends Transform = Transform> =
TransformAction<T, boolean>;

/**
* Control handlers that run on control click/down/up
* Those handlers run with or without a transform defined
*/
export type ControlActionHandler = TransformAction<Transform, any>;

export type ControlCallback<R = void> = (
eventData: TPointerEvent,
control: Control,
Expand All @@ -42,7 +52,7 @@ export type ControlCursorCallback = ControlCallback<string>;
export type Transform = {
target: FabricObject;
action: string;
actionHandler: TransformActionHandler;
actionHandler?: TransformActionHandler;
corner: string | 0;
scaleX: number;
scaleY: number;
Expand All @@ -65,6 +75,7 @@ export type Transform = {
originX: TOriginX;
originY: TOriginY;
};
actionPerformed: boolean;
};

export type TEvent<E extends Event = TPointerEvent> = {
Expand All @@ -87,23 +98,40 @@ export type TModificationEvents =
| 'skewing'
| 'resizing';

type ObjectModifiedEvents = Record<TModificationEvents, BasicTransformEvent> & {
modified: BasicTransformEvent | never;
export type ModifiedEvent<E extends Event = TPointerEvent> = TEvent<E> & {
transform: Transform;
target: FabricObject;
action: string;
};

type CanvasModifiedEvents = Record<
`object:${keyof ObjectModifiedEvents}`,
BasicTransformEvent & { target: FabricObject }
>;
type ModificationEventsSpec<
Prefix extends string = '',
Modification = BasicTransformEvent,
Modified = ModifiedEvent | never
> = Record<`${Prefix}${TModificationEvents}`, Modification> &
Record<`${Prefix}modified`, Modified>;

type ObjectModificationEvents = ModificationEventsSpec;

type CanvasModificationEvents = ModificationEventsSpec<
'object:',
BasicTransformEvent & { target: FabricObject },
ModifiedEvent | { target: FabricObject }
> & {
'before:transform': TEvent & { transform: Transform };
};

export type TransformEvent<T extends Event = TPointerEvent> =
BasicTransformEvent<T> & {
target: FabricObject;
subTargets: FabricObject[];
button: number;
export type TPointerEventInfo<E extends TPointerEvent = TPointerEvent> =
TEvent<E> & {
target?: FabricObject;
subTargets?: FabricObject[];
button?: number;
isClick: boolean;
pointer: Point;
transform?: Transform | null;
absolutePointer: Point;
currentSubTargets?: FabricObject[];
currentTarget?: FabricObject | null;
};

type SimpleEventHandler<T extends Event = TPointerEvent> =
Expand All @@ -119,10 +147,12 @@ type OutEvent = {
nextTarget?: FabricObject;
};

type DragEventData = TEventWithTarget<DragEvent> & {
export type DragEventData = TEvent<DragEvent> & {
target?: FabricObject;
subTargets?: FabricObject[];
dragSource?: FabricObject;
canDrop?: boolean;
didDrop?: boolean;
dropTarget?: FabricObject;
};

Expand All @@ -146,10 +176,10 @@ type CanvasDnDEvents = DnDEvents & {
};

type CanvasSelectionEvents = {
'selection:created': TEvent & {
'selection:created': Partial<TEvent> & {
selected: FabricObject[];
};
'selection:updated': TEvent & {
'selection:updated': Partial<TEvent> & {
selected: FabricObject[];
deselected: FabricObject[];
};
Expand All @@ -169,24 +199,37 @@ export type CollectionEvents = {
type BeforeSuffix<T extends string> = `${T}:before`;
type WithBeforeSuffix<T extends string> = T | BeforeSuffix<T>;

type TPointerEvents<Prefix extends string, E = Record<string, never>> = Record<
type TPointerEvents<Prefix extends string> = Record<
`${Prefix}${
| WithBeforeSuffix<'down'>
| WithBeforeSuffix<'move'>
| WithBeforeSuffix<'up'>
| 'dblclick'}`,
TransformEvent & E
TPointerEventInfo
> &
Record<`${Prefix}wheel`, TransformEvent<WheelEvent> & E> &
Record<`${Prefix}over`, TransformEvent & InEvent & E> &
Record<`${Prefix}out`, TransformEvent & OutEvent & E>;
Record<`${Prefix}wheel`, TPointerEventInfo<WheelEvent>> &
Record<`${Prefix}over`, TPointerEventInfo & InEvent> &
Record<`${Prefix}out`, TPointerEventInfo & OutEvent>;

export type TPointerEventNames =
| WithBeforeSuffix<'down'>
| WithBeforeSuffix<'move'>
| WithBeforeSuffix<'up'>
| 'dblclick'
| 'wheel';

export type ObjectPointerEvents = TPointerEvents<'mouse'>;
export type CanvasPointerEvents = TPointerEvents<'mouse:'>;

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

export type ObjectEvents = ObjectPointerEvents &
DnDEvents &
ObjectModifiedEvents & {
MiscEvents &
ObjectModificationEvents & {
// selection
selected: Partial<TEvent> & {
target: FabricObject;
Expand Down Expand Up @@ -215,7 +258,8 @@ export type StaticCanvasEvents = CollectionEvents & {
export type CanvasEvents = StaticCanvasEvents &
CanvasPointerEvents &
CanvasDnDEvents &
CanvasModifiedEvents &
MiscEvents &
CanvasModificationEvents &
CanvasSelectionEvents & {
// brushes
'before:path:created': { path: FabricObject };
Expand All @@ -240,8 +284,4 @@ export type CanvasEvents = StaticCanvasEvents &
'text:changed': { target: IText };
'text:editing:entered': { target: IText };
'text:editing:exited': { target: IText };

// misc
'contextmenu:before': SimpleEventHandler<Event>;
contextmenu: SimpleEventHandler<Event>;
};
25 changes: 0 additions & 25 deletions src/__types__.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/brushes/base_brush.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Color } from '../color';
import type { Point } from '../point.class';
import { TEvent } from '../EventTypeDefs';
import type { Shadow } from '../shadow.class';
import { Canvas } from '../__types__';
import type { Canvas } from '../canvas/canvas_events';

type TBrushEventData = TEvent & { pointer: Point };

Expand Down
2 changes: 1 addition & 1 deletion src/brushes/circle_brush.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Shadow } from '../shadow.class';
import { Circle } from '../shapes/circle.class';
import { Group } from '../shapes/group.class';
import { getRandomInt } from '../util/internals';
import { Canvas } from '../__types__';
import type { Canvas } from '../canvas/canvas_events';
import { BaseBrush } from './base_brush.class';

export type CircleBrushPoint = {
Expand Down
2 changes: 1 addition & 1 deletion src/brushes/pattern_brush.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fabric } from '../../HEADER';
import { Pattern } from '../pattern.class';
import { PathData } from '../typedefs';
import { createCanvasElement } from '../util/misc/dom';
import { Canvas } from '../__types__';
import type { Canvas } from '../canvas/canvas_events';
import { PencilBrush } from './pencil_brush.class';

export class PatternBrush extends PencilBrush {
Expand Down
2 changes: 1 addition & 1 deletion src/brushes/pencil_brush.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Shadow } from '../shadow.class';
import { Path } from '../shapes/path.class';
import { PathData } from '../typedefs';
import { getSmoothPathFromPoints, joinPath } from '../util/path';
import { Canvas } from '../__types__';
import type { Canvas } from '../canvas/canvas_events';
import { BaseBrush } from './base_brush.class';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/brushes/spray_brush.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Group } from '../shapes/group.class';
import { Shadow } from '../shadow.class';
import { Rect } from '../shapes/rect.class';
import { getRandomInt } from '../util/internals';
import { Canvas } from '../__types__';
import type { Canvas } from '../canvas/canvas_events';
import { BaseBrush } from './base_brush.class';

export type SprayBrushPoint = {
Expand Down
Loading