@pooder/kit 5.1.0 → 5.3.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 (86) hide show
  1. package/.test-dist/src/CanvasService.js +249 -249
  2. package/.test-dist/src/ViewportSystem.js +75 -75
  3. package/.test-dist/src/background.js +203 -203
  4. package/.test-dist/src/bridgeSelection.js +20 -20
  5. package/.test-dist/src/constraints.js +237 -237
  6. package/.test-dist/src/dieline.js +818 -818
  7. package/.test-dist/src/edgeScale.js +12 -12
  8. package/.test-dist/src/extensions/background.js +203 -0
  9. package/.test-dist/src/extensions/bridgeSelection.js +20 -0
  10. package/.test-dist/src/extensions/constraints.js +237 -0
  11. package/.test-dist/src/extensions/dieline.js +828 -0
  12. package/.test-dist/src/extensions/edgeScale.js +12 -0
  13. package/.test-dist/src/extensions/feature.js +825 -0
  14. package/.test-dist/src/extensions/featureComplete.js +32 -0
  15. package/.test-dist/src/extensions/film.js +167 -0
  16. package/.test-dist/src/extensions/geometry.js +545 -0
  17. package/.test-dist/src/extensions/image.js +1529 -0
  18. package/.test-dist/src/extensions/index.js +30 -0
  19. package/.test-dist/src/extensions/maskOps.js +279 -0
  20. package/.test-dist/src/extensions/mirror.js +104 -0
  21. package/.test-dist/src/extensions/ruler.js +345 -0
  22. package/.test-dist/src/extensions/sceneLayout.js +96 -0
  23. package/.test-dist/src/extensions/sceneLayoutModel.js +196 -0
  24. package/.test-dist/src/extensions/sceneVisibility.js +62 -0
  25. package/.test-dist/src/extensions/size.js +331 -0
  26. package/.test-dist/src/extensions/tracer.js +538 -0
  27. package/.test-dist/src/extensions/white-ink.js +1190 -0
  28. package/.test-dist/src/extensions/wrappedOffsets.js +33 -0
  29. package/.test-dist/src/feature.js +826 -826
  30. package/.test-dist/src/featureComplete.js +32 -32
  31. package/.test-dist/src/film.js +167 -167
  32. package/.test-dist/src/geometry.js +506 -506
  33. package/.test-dist/src/image.js +1250 -1250
  34. package/.test-dist/src/index.js +2 -19
  35. package/.test-dist/src/maskOps.js +270 -270
  36. package/.test-dist/src/mirror.js +104 -104
  37. package/.test-dist/src/renderSpec.js +2 -2
  38. package/.test-dist/src/ruler.js +343 -343
  39. package/.test-dist/src/sceneLayout.js +99 -99
  40. package/.test-dist/src/sceneLayoutModel.js +196 -196
  41. package/.test-dist/src/sceneView.js +40 -40
  42. package/.test-dist/src/sceneVisibility.js +42 -42
  43. package/.test-dist/src/services/CanvasService.js +249 -0
  44. package/.test-dist/src/services/ViewportSystem.js +76 -0
  45. package/.test-dist/src/services/index.js +24 -0
  46. package/.test-dist/src/services/renderSpec.js +2 -0
  47. package/.test-dist/src/size.js +332 -332
  48. package/.test-dist/src/tracer.js +544 -544
  49. package/.test-dist/src/white-ink.js +829 -829
  50. package/.test-dist/src/wrappedOffsets.js +33 -33
  51. package/CHANGELOG.md +12 -0
  52. package/dist/index.d.mts +14 -0
  53. package/dist/index.d.ts +14 -0
  54. package/dist/index.js +3521 -3220
  55. package/dist/index.mjs +3532 -3226
  56. package/package.json +1 -1
  57. package/src/coordinate.ts +106 -106
  58. package/src/extensions/background.ts +230 -230
  59. package/src/extensions/bridgeSelection.ts +17 -17
  60. package/src/extensions/constraints.ts +322 -322
  61. package/src/extensions/dieline.ts +20 -17
  62. package/src/extensions/edgeScale.ts +19 -19
  63. package/src/extensions/feature.ts +1021 -1021
  64. package/src/extensions/featureComplete.ts +46 -46
  65. package/src/extensions/film.ts +194 -194
  66. package/src/extensions/geometry.ts +719 -719
  67. package/src/extensions/image.ts +1924 -1594
  68. package/src/extensions/index.ts +11 -11
  69. package/src/extensions/maskOps.ts +365 -299
  70. package/src/extensions/mirror.ts +128 -128
  71. package/src/extensions/ruler.ts +451 -451
  72. package/src/extensions/sceneLayout.ts +140 -140
  73. package/src/extensions/sceneLayoutModel.ts +342 -342
  74. package/src/extensions/sceneVisibility.ts +71 -71
  75. package/src/extensions/size.ts +389 -389
  76. package/src/extensions/tracer.ts +302 -370
  77. package/src/extensions/white-ink.ts +1489 -1366
  78. package/src/extensions/wrappedOffsets.ts +33 -33
  79. package/src/index.ts +2 -2
  80. package/src/services/CanvasService.ts +300 -300
  81. package/src/services/ViewportSystem.ts +95 -95
  82. package/src/services/index.ts +3 -3
  83. package/src/services/renderSpec.ts +18 -18
  84. package/src/units.ts +27 -27
  85. package/tests/run.ts +118 -118
  86. package/tsconfig.test.json +15 -15
@@ -1,33 +1,33 @@
1
- export function wrappedDistance(total: number, start: number, end: number): number {
2
- if (!Number.isFinite(total) || total <= 0) return 0;
3
- if (!Number.isFinite(start) || !Number.isFinite(end)) return 0;
4
-
5
- const s = ((start % total) + total) % total;
6
- const e = ((end % total) + total) % total;
7
- return e >= s ? e - s : total - s + e;
8
- }
9
-
10
- export function sampleWrappedOffsets(
11
- total: number,
12
- start: number,
13
- end: number,
14
- count: number,
15
- ): number[] {
16
- if (!Number.isFinite(total) || total <= 0) return [];
17
- if (!Number.isFinite(start) || !Number.isFinite(end)) return [];
18
-
19
- const n = Math.max(0, Math.floor(count));
20
- if (n <= 0) return [];
21
-
22
- const dist = wrappedDistance(total, start, end);
23
- if (n === 1) return [((start % total) + total) % total];
24
-
25
- const step = dist / (n - 1);
26
- const offsets: number[] = [];
27
- for (let i = 0; i < n; i++) {
28
- const raw = start + step * i;
29
- const wrapped = ((raw % total) + total) % total;
30
- offsets.push(wrapped);
31
- }
32
- return offsets;
33
- }
1
+ export function wrappedDistance(total: number, start: number, end: number): number {
2
+ if (!Number.isFinite(total) || total <= 0) return 0;
3
+ if (!Number.isFinite(start) || !Number.isFinite(end)) return 0;
4
+
5
+ const s = ((start % total) + total) % total;
6
+ const e = ((end % total) + total) % total;
7
+ return e >= s ? e - s : total - s + e;
8
+ }
9
+
10
+ export function sampleWrappedOffsets(
11
+ total: number,
12
+ start: number,
13
+ end: number,
14
+ count: number,
15
+ ): number[] {
16
+ if (!Number.isFinite(total) || total <= 0) return [];
17
+ if (!Number.isFinite(start) || !Number.isFinite(end)) return [];
18
+
19
+ const n = Math.max(0, Math.floor(count));
20
+ if (n <= 0) return [];
21
+
22
+ const dist = wrappedDistance(total, start, end);
23
+ if (n === 1) return [((start % total) + total) % total];
24
+
25
+ const step = dist / (n - 1);
26
+ const offsets: number[] = [];
27
+ for (let i = 0; i < n; i++) {
28
+ const raw = start + step * i;
29
+ const wrapped = ((raw % total) + total) % total;
30
+ offsets.push(wrapped);
31
+ }
32
+ return offsets;
33
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./extensions";
2
- export * from "./services";
1
+ export * from "./extensions";
2
+ export * from "./services";
@@ -1,300 +1,300 @@
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
+ 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
+ }