-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
types.ts
149 lines (130 loc) · 3.79 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import type {
BasicTransformEvent,
ModifiedEvent,
TModificationEvents,
} from '../EventTypeDefs';
import type { Point } from '../Point';
import type { Group } from '../shapes/Group';
import type { ITextEvents } from '../shapes/IText/ITextBehavior';
import type { FabricObject } from '../shapes/Object/FabricObject';
import type { LayoutStrategy } from './LayoutStrategies/LayoutStrategy';
import type {
LAYOUT_TYPE_INITIALIZATION,
LAYOUT_TYPE_ADDED,
LAYOUT_TYPE_IMPERATIVE,
LAYOUT_TYPE_REMOVED,
LAYOUT_TYPE_OBJECT_MODIFIED,
LAYOUT_TYPE_OBJECT_MODIFYING,
} from './constants';
export type LayoutTrigger =
| typeof LAYOUT_TYPE_INITIALIZATION
| typeof LAYOUT_TYPE_OBJECT_MODIFYING
| typeof LAYOUT_TYPE_OBJECT_MODIFIED
| typeof LAYOUT_TYPE_ADDED
| typeof LAYOUT_TYPE_REMOVED
| typeof LAYOUT_TYPE_IMPERATIVE;
export type LayoutStrategyResult = {
/**
* new center point as measured by the **containing** plane (same as `left` with `originX` set to `center`)
*/
center: Point;
/**
* correction vector to translate objects by, measured in the same plane as `center`
*
* Since objects are measured relative to the group's center, once the group's size changes we must apply a correction to
* the objects' positions so that they relate to the new center.
* In other words, this value makes it possible to layout objects relative to the tl corner, for instance, but not only.
*/
correction?: Point;
/**
* correction vector to translate objects by as measured by the plane
*/
relativeCorrection?: Point;
/**
* new width and height of the layout target
*/
size: Point;
};
export type LayoutResult = {
result?: LayoutStrategyResult;
prevCenter: Point;
nextCenter: Point;
/**
* The vector used to offset objects by, as measured by the plane
*/
offset: Point;
};
type ImperativeLayoutCommonOptions = {
overrides?: LayoutStrategyResult;
bubbles?: boolean;
deep?: boolean;
};
export type ImperativeLayoutOptions = ImperativeLayoutCommonOptions & {
strategy?: LayoutStrategy;
};
export type CommonLayoutContext = {
target: Group;
strategy?: LayoutStrategy;
type: LayoutTrigger;
/**
* array of objects starting from the object that triggered the call to the current one
*/
path?: Group[];
};
export type InitializationLayoutContext = CommonLayoutContext & {
type: typeof LAYOUT_TYPE_INITIALIZATION;
targets: FabricObject[];
x?: number;
y?: number;
};
export type CollectionChangeLayoutContext = CommonLayoutContext & {
type: typeof LAYOUT_TYPE_ADDED | typeof LAYOUT_TYPE_REMOVED;
targets: FabricObject[];
};
export type ObjectModifiedLayoutContext = CommonLayoutContext & {
type: typeof LAYOUT_TYPE_OBJECT_MODIFIED;
trigger: 'modified';
e: ModifiedEvent;
};
export type ObjectModifyingLayoutContext = CommonLayoutContext & {
type: typeof LAYOUT_TYPE_OBJECT_MODIFYING;
} & (
| {
trigger: TModificationEvents;
e: BasicTransformEvent;
}
| {
trigger: 'changed';
e: ITextEvents['changed'];
}
);
export type ImperativeLayoutContext = CommonLayoutContext &
ImperativeLayoutCommonOptions & {
type: typeof LAYOUT_TYPE_IMPERATIVE;
};
export type LayoutContext =
| InitializationLayoutContext
| CollectionChangeLayoutContext
| ObjectModifiedLayoutContext
| ObjectModifyingLayoutContext
| ImperativeLayoutContext;
export type StrictLayoutContext = LayoutContext & {
strategy: LayoutStrategy;
prevStrategy?: LayoutStrategy;
bubbles: boolean;
stopPropagation(): void;
};
export type RegistrationContext = {
targets: FabricObject[];
target: Group;
};
export type LayoutBeforeEvent = {
context: StrictLayoutContext;
};
export type LayoutAfterEvent = {
context: StrictLayoutContext;
/**
* will be undefined if layout was skipped
*/
result?: LayoutResult;
};