@pooder/kit 4.3.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/.test-dist/src/CanvasService.js +249 -0
  2. package/.test-dist/src/ViewportSystem.js +75 -0
  3. package/.test-dist/src/background.js +203 -0
  4. package/.test-dist/src/bridgeSelection.js +20 -0
  5. package/.test-dist/src/constraints.js +237 -0
  6. package/.test-dist/src/coordinate.js +74 -0
  7. package/.test-dist/src/dieline.js +723 -0
  8. package/.test-dist/src/edgeScale.js +12 -0
  9. package/.test-dist/src/feature.js +752 -0
  10. package/.test-dist/src/featureComplete.js +32 -0
  11. package/.test-dist/src/film.js +167 -0
  12. package/.test-dist/src/geometry.js +506 -0
  13. package/.test-dist/src/image.js +1234 -0
  14. package/.test-dist/src/index.js +35 -0
  15. package/.test-dist/src/maskOps.js +270 -0
  16. package/.test-dist/src/mirror.js +104 -0
  17. package/.test-dist/src/renderSpec.js +2 -0
  18. package/.test-dist/src/ruler.js +343 -0
  19. package/.test-dist/src/sceneLayout.js +99 -0
  20. package/.test-dist/src/sceneLayoutModel.js +196 -0
  21. package/.test-dist/src/sceneView.js +40 -0
  22. package/.test-dist/src/sceneVisibility.js +42 -0
  23. package/.test-dist/src/size.js +332 -0
  24. package/.test-dist/src/tracer.js +544 -0
  25. package/.test-dist/src/units.js +30 -0
  26. package/.test-dist/src/white-ink.js +829 -0
  27. package/.test-dist/src/wrappedOffsets.js +33 -0
  28. package/.test-dist/tests/run.js +94 -0
  29. package/CHANGELOG.md +17 -0
  30. package/dist/index.d.mts +339 -36
  31. package/dist/index.d.ts +339 -36
  32. package/dist/index.js +3587 -854
  33. package/dist/index.mjs +3580 -856
  34. package/package.json +2 -2
  35. package/src/CanvasService.ts +300 -96
  36. package/src/ViewportSystem.ts +92 -92
  37. package/src/background.ts +230 -230
  38. package/src/bridgeSelection.ts +17 -0
  39. package/src/coordinate.ts +106 -106
  40. package/src/dieline.ts +897 -955
  41. package/src/edgeScale.ts +19 -0
  42. package/src/feature.ts +83 -30
  43. package/src/film.ts +194 -194
  44. package/src/geometry.ts +234 -80
  45. package/src/image.ts +1582 -512
  46. package/src/index.ts +14 -10
  47. package/src/maskOps.ts +326 -0
  48. package/src/mirror.ts +128 -128
  49. package/src/renderSpec.ts +18 -0
  50. package/src/ruler.ts +449 -508
  51. package/src/sceneLayout.ts +121 -0
  52. package/src/sceneLayoutModel.ts +335 -0
  53. package/src/sceneVisibility.ts +49 -0
  54. package/src/size.ts +379 -0
  55. package/src/tracer.ts +719 -570
  56. package/src/units.ts +27 -27
  57. package/src/white-ink.ts +1018 -373
  58. package/src/wrappedOffsets.ts +33 -0
  59. package/tests/run.ts +118 -0
  60. package/tsconfig.test.json +15 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pooder/kit",
3
- "version": "4.3.0",
3
+ "version": "5.0.0",
4
4
  "description": "Standard plugins for Pooder editor",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -19,7 +19,7 @@
19
19
  "dependencies": {
20
20
  "paper": "^0.12.18",
21
21
  "fabric": "^7.0.0",
22
- "@pooder/core": "2.0.0"
22
+ "@pooder/core": "2.1.0"
23
23
  },
24
24
  "scripts": {
25
25
  "build": "tsup src/index.ts --format cjs,esm --dts",
@@ -1,96 +1,300 @@
1
- import { Canvas, Group, FabricObject } from "fabric";
2
- import { Service, EventBus } from "@pooder/core";
3
- import { ViewportSystem } from "./ViewportSystem";
4
-
5
- export default class CanvasService implements Service {
6
- public canvas: Canvas;
7
- public viewport: ViewportSystem;
8
- private eventBus?: EventBus;
9
-
10
- constructor(el: HTMLCanvasElement | string | Canvas, options?: any) {
11
- if (el instanceof Canvas) {
12
- this.canvas = el;
13
- } else {
14
- this.canvas = new Canvas(el, {
15
- preserveObjectStacking: true,
16
- ...options,
17
- });
18
- }
19
-
20
- this.viewport = new ViewportSystem();
21
- if (this.canvas.width !== undefined && this.canvas.height !== undefined) {
22
- this.viewport.updateContainer(this.canvas.width, this.canvas.height);
23
- }
24
-
25
- if (options?.eventBus) {
26
- this.setEventBus(options.eventBus);
27
- }
28
- }
29
-
30
- setEventBus(eventBus: EventBus) {
31
- this.eventBus = eventBus;
32
- this.setupEvents();
33
- }
34
-
35
- private setupEvents() {
36
- if (!this.eventBus) return;
37
- const bus = this.eventBus;
38
-
39
- const forward = (name: string) => (e: any) => bus.emit(name, e);
40
-
41
- this.canvas.on("selection:created", forward("selection:created"));
42
- this.canvas.on("selection:updated", forward("selection:updated"));
43
- this.canvas.on("selection:cleared", forward("selection:cleared"));
44
- this.canvas.on("object:modified", forward("object:modified"));
45
- this.canvas.on("object:added", forward("object:added"));
46
- this.canvas.on("object:removed", forward("object:removed"));
47
- }
48
-
49
- dispose() {
50
- this.canvas.dispose();
51
- }
52
-
53
- /**
54
- * Get a layer (Group) by its ID.
55
- * We assume layers are Groups directly on the canvas with a data.id property.
56
- */
57
- getLayer(id: string): Group | undefined {
58
- return this.canvas.getObjects().find((obj: any) => obj.data?.id === id) as
59
- | Group
60
- | undefined;
61
- }
62
-
63
- /**
64
- * Create a layer (Group) with the given ID if it doesn't exist.
65
- */
66
- createLayer(id: string, options: any = {}): Group {
67
- let layer = this.getLayer(id);
68
- if (!layer) {
69
- const defaultOptions = {
70
- selectable: false,
71
- evented: false,
72
- ...options,
73
- data: { ...options.data, id },
74
- };
75
- layer = new Group([], defaultOptions);
76
- this.canvas.add(layer);
77
- }
78
- return layer;
79
- }
80
-
81
- /**
82
- * Find an object by ID, optionally within a specific layer.
83
- */
84
- getObject(id: string, layerId?: string): FabricObject | undefined {
85
- if (layerId) {
86
- const layer = this.getLayer(layerId);
87
- if (!layer) return undefined;
88
- return layer.getObjects().find((obj: any) => obj.data?.id === id);
89
- }
90
- return this.canvas.getObjects().find((obj: any) => obj.data?.id === id);
91
- }
92
-
93
- requestRenderAll() {
94
- this.canvas.requestRenderAll();
95
- }
96
- }
1
+ import { Canvas, Group, FabricObject, Rect, Path, Image } from "fabric";
2
+ import { Service, EventBus } from "@pooder/core";
3
+ import { ViewportSystem } from "./ViewportSystem";
4
+ import type { RenderLayerSpec, RenderObjectSpec } from "./renderSpec";
5
+
6
+ export default class CanvasService implements Service {
7
+ public canvas: Canvas;
8
+ public viewport: ViewportSystem;
9
+ private eventBus?: EventBus;
10
+
11
+ constructor(el: HTMLCanvasElement | string | Canvas, options?: any) {
12
+ if (el instanceof Canvas) {
13
+ this.canvas = el;
14
+ } else {
15
+ this.canvas = new Canvas(el, {
16
+ preserveObjectStacking: true,
17
+ ...options,
18
+ });
19
+ }
20
+
21
+ this.viewport = new ViewportSystem();
22
+ if (this.canvas.width !== undefined && this.canvas.height !== undefined) {
23
+ this.viewport.updateContainer(this.canvas.width, this.canvas.height);
24
+ }
25
+
26
+ if (options?.eventBus) {
27
+ this.setEventBus(options.eventBus);
28
+ }
29
+ }
30
+
31
+ setEventBus(eventBus: EventBus) {
32
+ this.eventBus = eventBus;
33
+ this.setupEvents();
34
+ }
35
+
36
+ private setupEvents() {
37
+ if (!this.eventBus) return;
38
+ const bus = this.eventBus;
39
+
40
+ const forward = (name: string) => (e: any) => bus.emit(name, e);
41
+
42
+ this.canvas.on("selection:created", forward("selection:created"));
43
+ this.canvas.on("selection:updated", forward("selection:updated"));
44
+ this.canvas.on("selection:cleared", forward("selection:cleared"));
45
+ this.canvas.on("object:modified", forward("object:modified"));
46
+ this.canvas.on("object:added", forward("object:added"));
47
+ this.canvas.on("object:removed", forward("object:removed"));
48
+ }
49
+
50
+ dispose() {
51
+ this.canvas.dispose();
52
+ }
53
+
54
+ /**
55
+ * Get a layer (Group) by its ID.
56
+ * We assume layers are Groups directly on the canvas with a data.id property.
57
+ */
58
+ getLayer(id: string): Group | undefined {
59
+ return this.canvas.getObjects().find((obj: any) => obj.data?.id === id) as
60
+ | Group
61
+ | undefined;
62
+ }
63
+
64
+ /**
65
+ * Create a layer (Group) with the given ID if it doesn't exist.
66
+ */
67
+ createLayer(id: string, options: any = {}): Group {
68
+ let layer = this.getLayer(id);
69
+ if (!layer) {
70
+ const defaultOptions = {
71
+ selectable: false,
72
+ evented: false,
73
+ ...options,
74
+ data: { ...options.data, id },
75
+ };
76
+ layer = new Group([], defaultOptions);
77
+ this.canvas.add(layer);
78
+ }
79
+ return layer;
80
+ }
81
+
82
+ /**
83
+ * Find an object by ID, optionally within a specific layer.
84
+ */
85
+ getObject(id: string, layerId?: string): FabricObject | undefined {
86
+ if (layerId) {
87
+ const layer = this.getLayer(layerId);
88
+ if (!layer) return undefined;
89
+ return layer.getObjects().find((obj: any) => obj.data?.id === id);
90
+ }
91
+ return this.canvas.getObjects().find((obj: any) => obj.data?.id === id);
92
+ }
93
+
94
+ requestRenderAll() {
95
+ this.canvas.requestRenderAll();
96
+ }
97
+
98
+ resize(width: number, height: number) {
99
+ this.canvas.setDimensions({ width, height });
100
+ this.viewport.updateContainer(width, height);
101
+ this.eventBus?.emit("canvas:resized", { width, height });
102
+ this.requestRenderAll();
103
+ }
104
+
105
+ async applyLayerSpec(spec: RenderLayerSpec): Promise<void> {
106
+ const layer = this.createLayer(spec.id, spec.props || {});
107
+ await this.applyObjectSpecsToContainer(layer, spec.objects);
108
+ }
109
+
110
+ async applyObjectSpecsToLayer(
111
+ layerId: string,
112
+ objects: RenderObjectSpec[],
113
+ ): Promise<void> {
114
+ const layer = this.createLayer(layerId, {});
115
+ await this.applyObjectSpecsToContainer(layer, objects);
116
+ }
117
+
118
+ getRootLayerObjects(layerId: string): FabricObject[] {
119
+ return this.canvas
120
+ .getObjects()
121
+ .filter((obj: any) => obj?.data?.layerId === layerId);
122
+ }
123
+
124
+ async applyObjectSpecsToRootLayer(
125
+ layerId: string,
126
+ specs: RenderObjectSpec[],
127
+ ): Promise<void> {
128
+ const desiredIds = new Set(specs.map((s) => s.id));
129
+ const existing = this.getRootLayerObjects(layerId) as any[];
130
+ existing.forEach((obj) => {
131
+ const id = obj?.data?.id;
132
+ if (typeof id === "string" && !desiredIds.has(id)) {
133
+ this.canvas.remove(obj);
134
+ }
135
+ });
136
+
137
+ const byId = new Map<string, any>();
138
+ this.getRootLayerObjects(layerId).forEach((obj: any) => {
139
+ const id = obj?.data?.id;
140
+ if (typeof id === "string") byId.set(id, obj);
141
+ });
142
+
143
+ for (let index = 0; index < specs.length; index += 1) {
144
+ const spec = specs[index];
145
+ let current = byId.get(spec.id);
146
+ if (
147
+ current &&
148
+ spec.type === "image" &&
149
+ spec.src &&
150
+ current.getSrc &&
151
+ current.getSrc() !== spec.src
152
+ ) {
153
+ this.canvas.remove(current);
154
+ byId.delete(spec.id);
155
+ current = undefined;
156
+ }
157
+
158
+ if (!current) {
159
+ const created = await this.createFabricObject(spec);
160
+ if (!created) continue;
161
+ this.patchFabricObject(created as any, spec, { layerId });
162
+ this.canvas.add(created as any);
163
+ byId.set(spec.id, created);
164
+ continue;
165
+ }
166
+
167
+ this.patchFabricObject(current, spec, { layerId });
168
+ }
169
+
170
+ this.requestRenderAll();
171
+ }
172
+
173
+ private async applyObjectSpecsToContainer(
174
+ container: Group,
175
+ specs: RenderObjectSpec[],
176
+ ): Promise<void> {
177
+ const desiredIds = new Set(specs.map((s) => s.id));
178
+ const existing = container.getObjects() as any[];
179
+ existing.forEach((obj) => {
180
+ const id = obj?.data?.id;
181
+ if (typeof id === "string" && !desiredIds.has(id)) {
182
+ container.remove(obj);
183
+ }
184
+ });
185
+
186
+ const byId = new Map<string, any>();
187
+ (container.getObjects() as any[]).forEach((obj) => {
188
+ const id = obj?.data?.id;
189
+ if (typeof id === "string") byId.set(id, obj);
190
+ });
191
+
192
+ for (let index = 0; index < specs.length; index += 1) {
193
+ const spec = specs[index];
194
+ let current = byId.get(spec.id);
195
+ if (
196
+ current &&
197
+ spec.type === "image" &&
198
+ spec.src &&
199
+ current.getSrc &&
200
+ current.getSrc() !== spec.src
201
+ ) {
202
+ container.remove(current);
203
+ byId.delete(spec.id);
204
+ current = undefined;
205
+ }
206
+
207
+ if (!current) {
208
+ const created = await this.createFabricObject(spec);
209
+ if (!created) continue;
210
+ container.add(created as any);
211
+ current = created as any;
212
+ byId.set(spec.id, current);
213
+ } else {
214
+ this.patchFabricObject(current, spec);
215
+ }
216
+
217
+ this.moveObjectInContainer(container, current, index);
218
+ }
219
+
220
+ container.dirty = true;
221
+ this.requestRenderAll();
222
+ }
223
+
224
+ private patchFabricObject(
225
+ obj: any,
226
+ spec: RenderObjectSpec,
227
+ extraData?: Record<string, any>,
228
+ ) {
229
+ const nextData = {
230
+ ...(obj.data || {}),
231
+ ...(spec.data || {}),
232
+ ...(extraData || {}),
233
+ id: spec.id,
234
+ };
235
+ obj.set({ ...(spec.props || {}), data: nextData });
236
+ obj.setCoords();
237
+ }
238
+
239
+ private moveObjectInContainer(
240
+ container: Group | Canvas,
241
+ obj: any,
242
+ index: number,
243
+ ) {
244
+ if (!obj) return;
245
+
246
+ const moveObjectTo = (container as any).moveObjectTo;
247
+ if (typeof moveObjectTo === "function") {
248
+ moveObjectTo.call(container, obj, index);
249
+ return;
250
+ }
251
+
252
+ const list = (container as any)._objects as any[] | undefined;
253
+ if (!Array.isArray(list)) return;
254
+ const from = list.indexOf(obj);
255
+ if (from < 0 || from === index) return;
256
+ list.splice(from, 1);
257
+ const target = Math.max(0, Math.min(index, list.length));
258
+ list.splice(target, 0, obj);
259
+ if (typeof (container as any)._onStackOrderChanged === "function") {
260
+ (container as any)._onStackOrderChanged();
261
+ }
262
+ }
263
+
264
+ private async createFabricObject(
265
+ spec: RenderObjectSpec,
266
+ ): Promise<FabricObject | undefined> {
267
+ if (spec.type === "rect") {
268
+ const rect = new Rect({
269
+ ...(spec.props || {}),
270
+ data: { ...(spec.data || {}), id: spec.id },
271
+ } as any);
272
+ rect.setCoords();
273
+ return rect;
274
+ }
275
+
276
+ if (spec.type === "path") {
277
+ const pathData = (spec.props as any)?.path || (spec.props as any)?.pathData;
278
+ if (!pathData) return undefined;
279
+ const path = new Path(pathData, {
280
+ ...(spec.props || {}),
281
+ data: { ...(spec.data || {}), id: spec.id },
282
+ } as any);
283
+ path.setCoords();
284
+ return path;
285
+ }
286
+
287
+ if (spec.type === "image") {
288
+ if (!spec.src) return undefined;
289
+ const image = await Image.fromURL(spec.src, { crossOrigin: "anonymous" });
290
+ image.set({
291
+ ...(spec.props || {}),
292
+ data: { ...(spec.data || {}), id: spec.id },
293
+ } as any);
294
+ image.setCoords();
295
+ return image as any;
296
+ }
297
+
298
+ return undefined;
299
+ }
300
+ }
@@ -1,92 +1,92 @@
1
- import { Coordinate, Layout, Point, Size } from "./coordinate";
2
-
3
- export class ViewportSystem {
4
- private _containerSize: Size = { width: 0, height: 0 };
5
- private _physicalSize: Size = { width: 0, height: 0 };
6
- private _padding: number = 0;
7
- private _layout: Layout = {
8
- scale: 1,
9
- offsetX: 0,
10
- offsetY: 0,
11
- width: 0,
12
- height: 0,
13
- };
14
-
15
- constructor(
16
- containerSize: Size = { width: 0, height: 0 },
17
- physicalSize: Size = { width: 0, height: 0 },
18
- padding: number = 40,
19
- ) {
20
- this._containerSize = containerSize;
21
- this._physicalSize = physicalSize;
22
- this._padding = padding;
23
- this.updateLayout();
24
- }
25
-
26
- get layout(): Layout {
27
- return this._layout;
28
- }
29
-
30
- get scale(): number {
31
- return this._layout.scale;
32
- }
33
-
34
- get offset(): Point {
35
- return { x: this._layout.offsetX, y: this._layout.offsetY };
36
- }
37
-
38
- updateContainer(width: number, height: number) {
39
- if (
40
- this._containerSize.width === width &&
41
- this._containerSize.height === height
42
- )
43
- return;
44
- this._containerSize = { width, height };
45
- this.updateLayout();
46
- }
47
-
48
- updatePhysical(width: number, height: number) {
49
- if (this._physicalSize.width === width && this._physicalSize.height === height)
50
- return;
51
- this._physicalSize = { width, height };
52
- this.updateLayout();
53
- }
54
-
55
- setPadding(padding: number) {
56
- if (this._padding === padding) return;
57
- this._padding = padding;
58
- this.updateLayout();
59
- }
60
-
61
- private updateLayout() {
62
- this._layout = Coordinate.calculateLayout(
63
- this._containerSize,
64
- this._physicalSize,
65
- this._padding,
66
- );
67
- }
68
-
69
- toPixel(value: number): number {
70
- return value * this._layout.scale;
71
- }
72
-
73
- toPhysical(value: number): number {
74
- return this._layout.scale === 0 ? 0 : value / this._layout.scale;
75
- }
76
-
77
- toPixelPoint(point: Point): Point {
78
- return {
79
- x: point.x * this._layout.scale + this._layout.offsetX,
80
- y: point.y * this._layout.scale + this._layout.offsetY,
81
- };
82
- }
83
-
84
- // Convert screen coordinate (e.g. mouse event) to physical coordinate (relative to content origin)
85
- toPhysicalPoint(point: Point): Point {
86
- if (this._layout.scale === 0) return { x: 0, y: 0 };
87
- return {
88
- x: (point.x - this._layout.offsetX) / this._layout.scale,
89
- y: (point.y - this._layout.offsetY) / this._layout.scale,
90
- };
91
- }
92
- }
1
+ import { Coordinate, Layout, Point, Size } from "./coordinate";
2
+
3
+ export class ViewportSystem {
4
+ private _containerSize: Size = { width: 0, height: 0 };
5
+ private _physicalSize: Size = { width: 0, height: 0 };
6
+ private _padding: number = 0;
7
+ private _layout: Layout = {
8
+ scale: 1,
9
+ offsetX: 0,
10
+ offsetY: 0,
11
+ width: 0,
12
+ height: 0,
13
+ };
14
+
15
+ constructor(
16
+ containerSize: Size = { width: 0, height: 0 },
17
+ physicalSize: Size = { width: 0, height: 0 },
18
+ padding: number = 40,
19
+ ) {
20
+ this._containerSize = containerSize;
21
+ this._physicalSize = physicalSize;
22
+ this._padding = padding;
23
+ this.updateLayout();
24
+ }
25
+
26
+ get layout(): Layout {
27
+ return this._layout;
28
+ }
29
+
30
+ get scale(): number {
31
+ return this._layout.scale;
32
+ }
33
+
34
+ get offset(): Point {
35
+ return { x: this._layout.offsetX, y: this._layout.offsetY };
36
+ }
37
+
38
+ updateContainer(width: number, height: number) {
39
+ if (
40
+ this._containerSize.width === width &&
41
+ this._containerSize.height === height
42
+ )
43
+ return;
44
+ this._containerSize = { width, height };
45
+ this.updateLayout();
46
+ }
47
+
48
+ updatePhysical(width: number, height: number) {
49
+ if (this._physicalSize.width === width && this._physicalSize.height === height)
50
+ return;
51
+ this._physicalSize = { width, height };
52
+ this.updateLayout();
53
+ }
54
+
55
+ setPadding(padding: number) {
56
+ if (this._padding === padding) return;
57
+ this._padding = padding;
58
+ this.updateLayout();
59
+ }
60
+
61
+ private updateLayout() {
62
+ this._layout = Coordinate.calculateLayout(
63
+ this._containerSize,
64
+ this._physicalSize,
65
+ this._padding,
66
+ );
67
+ }
68
+
69
+ toPixel(value: number): number {
70
+ return value * this._layout.scale;
71
+ }
72
+
73
+ toPhysical(value: number): number {
74
+ return this._layout.scale === 0 ? 0 : value / this._layout.scale;
75
+ }
76
+
77
+ toPixelPoint(point: Point): Point {
78
+ return {
79
+ x: point.x * this._layout.scale + this._layout.offsetX,
80
+ y: point.y * this._layout.scale + this._layout.offsetY,
81
+ };
82
+ }
83
+
84
+ // Convert screen coordinate (e.g. mouse event) to physical coordinate (relative to content origin)
85
+ toPhysicalPoint(point: Point): Point {
86
+ if (this._layout.scale === 0) return { x: 0, y: 0 };
87
+ return {
88
+ x: (point.x - this._layout.offsetX) / this._layout.scale,
89
+ y: (point.y - this._layout.offsetY) / this._layout.scale,
90
+ };
91
+ }
92
+ }