@pooder/kit 4.3.1 → 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.
- package/.test-dist/src/CanvasService.js +249 -0
- package/.test-dist/src/ViewportSystem.js +75 -0
- package/.test-dist/src/background.js +203 -0
- package/.test-dist/src/bridgeSelection.js +20 -0
- package/.test-dist/src/constraints.js +237 -0
- package/.test-dist/src/coordinate.js +74 -0
- package/.test-dist/src/dieline.js +723 -0
- package/.test-dist/src/edgeScale.js +12 -0
- package/.test-dist/src/feature.js +752 -0
- package/.test-dist/src/featureComplete.js +32 -0
- package/.test-dist/src/film.js +167 -0
- package/.test-dist/src/geometry.js +506 -0
- package/.test-dist/src/image.js +1234 -0
- package/.test-dist/src/index.js +35 -0
- package/.test-dist/src/maskOps.js +270 -0
- package/.test-dist/src/mirror.js +104 -0
- package/.test-dist/src/renderSpec.js +2 -0
- package/.test-dist/src/ruler.js +343 -0
- package/.test-dist/src/sceneLayout.js +99 -0
- package/.test-dist/src/sceneLayoutModel.js +196 -0
- package/.test-dist/src/sceneView.js +40 -0
- package/.test-dist/src/sceneVisibility.js +42 -0
- package/.test-dist/src/size.js +332 -0
- package/.test-dist/src/tracer.js +544 -0
- package/.test-dist/src/units.js +30 -0
- package/.test-dist/src/white-ink.js +829 -0
- package/.test-dist/src/wrappedOffsets.js +33 -0
- package/.test-dist/tests/run.js +94 -0
- package/CHANGELOG.md +11 -0
- package/dist/index.d.mts +339 -36
- package/dist/index.d.ts +339 -36
- package/dist/index.js +3572 -850
- package/dist/index.mjs +3565 -852
- package/package.json +2 -2
- package/src/CanvasService.ts +300 -96
- package/src/ViewportSystem.ts +92 -92
- package/src/background.ts +230 -230
- package/src/bridgeSelection.ts +17 -0
- package/src/coordinate.ts +106 -106
- package/src/dieline.ts +897 -973
- package/src/edgeScale.ts +19 -0
- package/src/feature.ts +83 -30
- package/src/film.ts +194 -194
- package/src/geometry.ts +242 -84
- package/src/image.ts +1582 -512
- package/src/index.ts +14 -10
- package/src/maskOps.ts +326 -0
- package/src/mirror.ts +128 -128
- package/src/renderSpec.ts +18 -0
- package/src/ruler.ts +449 -508
- package/src/sceneLayout.ts +121 -0
- package/src/sceneLayoutModel.ts +335 -0
- package/src/sceneVisibility.ts +49 -0
- package/src/size.ts +379 -0
- package/src/tracer.ts +719 -570
- package/src/units.ts +27 -27
- package/src/white-ink.ts +1018 -373
- package/src/wrappedOffsets.ts +33 -0
- package/tests/run.ts +118 -0
- package/tsconfig.test.json +15 -15
package/src/image.ts
CHANGED
|
@@ -1,512 +1,1582 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Extension,
|
|
3
|
-
ExtensionContext,
|
|
4
|
-
ContributionPointIds,
|
|
5
|
-
CommandContribution,
|
|
6
|
-
ConfigurationContribution,
|
|
7
|
-
ConfigurationService,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (!this.
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
private
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
1
|
+
import {
|
|
2
|
+
Extension,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
ContributionPointIds,
|
|
5
|
+
CommandContribution,
|
|
6
|
+
ConfigurationContribution,
|
|
7
|
+
ConfigurationService,
|
|
8
|
+
ToolSessionService,
|
|
9
|
+
WorkbenchService,
|
|
10
|
+
} from "@pooder/core";
|
|
11
|
+
import { Canvas as FabricCanvas, Image as FabricImage, Point } from "fabric";
|
|
12
|
+
import CanvasService from "./CanvasService";
|
|
13
|
+
import type { RenderObjectSpec } from "./renderSpec";
|
|
14
|
+
import { computeSceneLayout, readSizeState } from "./sceneLayoutModel";
|
|
15
|
+
|
|
16
|
+
export interface ImageItem {
|
|
17
|
+
id: string;
|
|
18
|
+
url: string;
|
|
19
|
+
opacity: number;
|
|
20
|
+
scale?: number;
|
|
21
|
+
angle?: number;
|
|
22
|
+
left?: number;
|
|
23
|
+
top?: number;
|
|
24
|
+
sourceUrl?: string;
|
|
25
|
+
committedUrl?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface FrameRect {
|
|
29
|
+
left: number;
|
|
30
|
+
top: number;
|
|
31
|
+
width: number;
|
|
32
|
+
height: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface SourceSize {
|
|
36
|
+
width: number;
|
|
37
|
+
height: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface RenderImageState {
|
|
41
|
+
src: string;
|
|
42
|
+
left: number;
|
|
43
|
+
top: number;
|
|
44
|
+
scale: number;
|
|
45
|
+
angle: number;
|
|
46
|
+
opacity: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface FrameVisualConfig {
|
|
50
|
+
strokeColor: string;
|
|
51
|
+
strokeWidth: number;
|
|
52
|
+
strokeStyle: "solid" | "dashed" | "hidden";
|
|
53
|
+
dashLength: number;
|
|
54
|
+
innerBackground: string;
|
|
55
|
+
outerBackground: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface UpsertImageOptions {
|
|
59
|
+
id?: string;
|
|
60
|
+
mode?: "auto" | "replace" | "add";
|
|
61
|
+
createIfMissing?: boolean;
|
|
62
|
+
addOptions?: Partial<ImageItem>;
|
|
63
|
+
fitOnAdd?: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface DielineFitArea {
|
|
67
|
+
width: number;
|
|
68
|
+
height: number;
|
|
69
|
+
left: number;
|
|
70
|
+
top: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
interface UpdateImageOptions {
|
|
74
|
+
target?: "auto" | "config" | "working";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
interface DetectBounds {
|
|
78
|
+
x: number;
|
|
79
|
+
y: number;
|
|
80
|
+
width: number;
|
|
81
|
+
height: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
interface DetectEdgeResult {
|
|
85
|
+
pathData: string;
|
|
86
|
+
rawBounds?: DetectBounds;
|
|
87
|
+
baseBounds?: DetectBounds;
|
|
88
|
+
imageWidth?: number;
|
|
89
|
+
imageHeight?: number;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
interface ImageRenderSnapshot {
|
|
93
|
+
id: string;
|
|
94
|
+
centerX: number;
|
|
95
|
+
centerY: number;
|
|
96
|
+
objectScale: number;
|
|
97
|
+
sourceWidth: number;
|
|
98
|
+
sourceHeight: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface DetectFromFrameOptions {
|
|
102
|
+
expand?: number;
|
|
103
|
+
smoothing?: boolean;
|
|
104
|
+
simplifyTolerance?: number;
|
|
105
|
+
multiplier?: number;
|
|
106
|
+
debug?: boolean;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const IMAGE_OBJECT_LAYER_ID = "image.user";
|
|
110
|
+
const IMAGE_OVERLAY_LAYER_ID = "image-overlay";
|
|
111
|
+
const IMAGE_REPLACE_GUARD_MS = 2500;
|
|
112
|
+
const IMAGE_DETECT_EXPAND_DEFAULT = 30;
|
|
113
|
+
const IMAGE_DETECT_SIMPLIFY_TOLERANCE_DEFAULT = 2;
|
|
114
|
+
const IMAGE_DETECT_MULTIPLIER_DEFAULT = 2;
|
|
115
|
+
|
|
116
|
+
export class ImageTool implements Extension {
|
|
117
|
+
id = "pooder.kit.image";
|
|
118
|
+
|
|
119
|
+
metadata = {
|
|
120
|
+
name: "ImageTool",
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
private items: ImageItem[] = [];
|
|
124
|
+
private workingItems: ImageItem[] = [];
|
|
125
|
+
private hasWorkingChanges = false;
|
|
126
|
+
private loadResolvers: Map<string, () => void> = new Map();
|
|
127
|
+
private sourceSizeBySrc: Map<string, SourceSize> = new Map();
|
|
128
|
+
private canvasService?: CanvasService;
|
|
129
|
+
private context?: ExtensionContext;
|
|
130
|
+
private isUpdatingConfig = false;
|
|
131
|
+
private isToolActive = false;
|
|
132
|
+
private isImageSelectionActive = false;
|
|
133
|
+
private focusedImageId: string | null = null;
|
|
134
|
+
private suppressSelectionClearUntil = 0;
|
|
135
|
+
private renderSeq = 0;
|
|
136
|
+
private dirtyTrackerDisposable?: { dispose(): void };
|
|
137
|
+
|
|
138
|
+
activate(context: ExtensionContext) {
|
|
139
|
+
this.context = context;
|
|
140
|
+
this.canvasService = context.services.get<CanvasService>("CanvasService");
|
|
141
|
+
if (!this.canvasService) {
|
|
142
|
+
console.warn("CanvasService not found for ImageTool");
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
context.eventBus.on("tool:activated", this.onToolActivated);
|
|
147
|
+
context.eventBus.on("object:modified", this.onObjectModified);
|
|
148
|
+
context.eventBus.on("selection:created", this.onSelectionChanged);
|
|
149
|
+
context.eventBus.on("selection:updated", this.onSelectionChanged);
|
|
150
|
+
context.eventBus.on("selection:cleared", this.onSelectionCleared);
|
|
151
|
+
context.eventBus.on(
|
|
152
|
+
"scene:layout:change",
|
|
153
|
+
this.onSceneLayoutChanged,
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const configService = context.services.get<ConfigurationService>(
|
|
157
|
+
"ConfigurationService",
|
|
158
|
+
);
|
|
159
|
+
if (configService) {
|
|
160
|
+
this.items = this.normalizeItems(
|
|
161
|
+
configService.get("image.items", []) || [],
|
|
162
|
+
);
|
|
163
|
+
this.workingItems = this.cloneItems(this.items);
|
|
164
|
+
this.hasWorkingChanges = false;
|
|
165
|
+
|
|
166
|
+
configService.onAnyChange((e: { key: string; value: any }) => {
|
|
167
|
+
if (this.isUpdatingConfig) return;
|
|
168
|
+
|
|
169
|
+
if (e.key === "image.items") {
|
|
170
|
+
this.items = this.normalizeItems(e.value || []);
|
|
171
|
+
if (!this.isToolActive || !this.hasWorkingChanges) {
|
|
172
|
+
this.workingItems = this.cloneItems(this.items);
|
|
173
|
+
this.hasWorkingChanges = false;
|
|
174
|
+
}
|
|
175
|
+
this.updateImages();
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (e.key.startsWith("size.") || e.key.startsWith("image.frame.")) {
|
|
180
|
+
this.updateImages();
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const toolSessionService =
|
|
186
|
+
context.services.get<ToolSessionService>("ToolSessionService");
|
|
187
|
+
this.dirtyTrackerDisposable = toolSessionService?.registerDirtyTracker(
|
|
188
|
+
this.id,
|
|
189
|
+
() => this.hasWorkingChanges,
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
this.updateImages();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
deactivate(context: ExtensionContext) {
|
|
196
|
+
context.eventBus.off("tool:activated", this.onToolActivated);
|
|
197
|
+
context.eventBus.off("object:modified", this.onObjectModified);
|
|
198
|
+
context.eventBus.off("selection:created", this.onSelectionChanged);
|
|
199
|
+
context.eventBus.off("selection:updated", this.onSelectionChanged);
|
|
200
|
+
context.eventBus.off("selection:cleared", this.onSelectionCleared);
|
|
201
|
+
context.eventBus.off(
|
|
202
|
+
"scene:layout:change",
|
|
203
|
+
this.onSceneLayoutChanged,
|
|
204
|
+
);
|
|
205
|
+
this.dirtyTrackerDisposable?.dispose();
|
|
206
|
+
this.dirtyTrackerDisposable = undefined;
|
|
207
|
+
|
|
208
|
+
this.clearRenderedImages();
|
|
209
|
+
if (this.canvasService) {
|
|
210
|
+
void this.canvasService.applyObjectSpecsToRootLayer(
|
|
211
|
+
IMAGE_OVERLAY_LAYER_ID,
|
|
212
|
+
[],
|
|
213
|
+
);
|
|
214
|
+
this.canvasService = undefined;
|
|
215
|
+
}
|
|
216
|
+
this.context = undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private onToolActivated = (event: {
|
|
220
|
+
id: string | null;
|
|
221
|
+
previous?: string | null;
|
|
222
|
+
reason?: string;
|
|
223
|
+
}) => {
|
|
224
|
+
const before = this.isToolActive;
|
|
225
|
+
this.syncToolActiveFromWorkbench(event.id);
|
|
226
|
+
if (!this.isToolActive) {
|
|
227
|
+
const now = Date.now();
|
|
228
|
+
const inGuardWindow =
|
|
229
|
+
now <= this.suppressSelectionClearUntil && !!this.focusedImageId;
|
|
230
|
+
if (!inGuardWindow) {
|
|
231
|
+
this.isImageSelectionActive = false;
|
|
232
|
+
this.focusedImageId = null;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
this.debug("tool:activated", {
|
|
236
|
+
id: event.id,
|
|
237
|
+
previous: event.previous,
|
|
238
|
+
reason: event.reason,
|
|
239
|
+
before,
|
|
240
|
+
isToolActive: this.isToolActive,
|
|
241
|
+
focusedImageId: this.focusedImageId,
|
|
242
|
+
suppressSelectionClearUntil: this.suppressSelectionClearUntil,
|
|
243
|
+
});
|
|
244
|
+
if (!this.isToolActive && this.isDebugEnabled()) {
|
|
245
|
+
console.trace("[ImageTool] tool deactivated trace");
|
|
246
|
+
}
|
|
247
|
+
this.updateImages();
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
private onSelectionChanged = (e: any) => {
|
|
251
|
+
const list: any[] = [];
|
|
252
|
+
if (Array.isArray(e?.selected)) {
|
|
253
|
+
list.push(...e.selected);
|
|
254
|
+
}
|
|
255
|
+
if (Array.isArray(e?.target?._objects)) {
|
|
256
|
+
list.push(...e.target._objects);
|
|
257
|
+
}
|
|
258
|
+
if (e?.target && !Array.isArray(e?.target?._objects)) {
|
|
259
|
+
list.push(e.target);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const selectedImage = list.find(
|
|
263
|
+
(obj: any) => obj?.data?.layerId === IMAGE_OBJECT_LAYER_ID,
|
|
264
|
+
);
|
|
265
|
+
this.isImageSelectionActive = !!selectedImage;
|
|
266
|
+
if (selectedImage?.data?.id) {
|
|
267
|
+
this.focusedImageId = selectedImage.data.id;
|
|
268
|
+
} else if (list.length > 0) {
|
|
269
|
+
this.focusedImageId = null;
|
|
270
|
+
}
|
|
271
|
+
this.debug("selection:changed", {
|
|
272
|
+
listSize: list.length,
|
|
273
|
+
isImageSelectionActive: this.isImageSelectionActive,
|
|
274
|
+
focusedImageId: this.focusedImageId,
|
|
275
|
+
});
|
|
276
|
+
this.updateImages();
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
private onSelectionCleared = () => {
|
|
280
|
+
const now = Date.now();
|
|
281
|
+
if (now <= this.suppressSelectionClearUntil && this.focusedImageId) {
|
|
282
|
+
this.debug("selection:cleared ignored", {
|
|
283
|
+
suppressUntil: this.suppressSelectionClearUntil,
|
|
284
|
+
focusedImageId: this.focusedImageId,
|
|
285
|
+
});
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
this.isImageSelectionActive = false;
|
|
289
|
+
this.focusedImageId = null;
|
|
290
|
+
this.debug("selection:cleared applied");
|
|
291
|
+
this.updateImages();
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
private onSceneLayoutChanged = () => {
|
|
295
|
+
this.updateImages();
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
private syncToolActiveFromWorkbench(fallbackId?: string | null) {
|
|
299
|
+
const wb = this.context?.services.get<WorkbenchService>("WorkbenchService");
|
|
300
|
+
const activeId = wb?.activeToolId;
|
|
301
|
+
if (typeof activeId === "string" || activeId === null) {
|
|
302
|
+
this.isToolActive = activeId === this.id;
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
this.isToolActive = fallbackId === this.id;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private isImageEditingVisible(): boolean {
|
|
309
|
+
return (
|
|
310
|
+
this.isToolActive || this.isImageSelectionActive || !!this.focusedImageId
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private isDebugEnabled(): boolean {
|
|
315
|
+
return !!this.getConfig<boolean>("image.debug", false);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
private debug(message: string, payload?: any) {
|
|
319
|
+
if (!this.isDebugEnabled()) return;
|
|
320
|
+
if (payload === undefined) {
|
|
321
|
+
console.log(`[ImageTool] ${message}`);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
console.log(`[ImageTool] ${message}`, payload);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
contribute() {
|
|
328
|
+
return {
|
|
329
|
+
[ContributionPointIds.TOOLS]: [
|
|
330
|
+
{
|
|
331
|
+
id: this.id,
|
|
332
|
+
name: "Image",
|
|
333
|
+
interaction: "session",
|
|
334
|
+
commands: {
|
|
335
|
+
begin: "resetWorkingImages",
|
|
336
|
+
commit: "completeImages",
|
|
337
|
+
rollback: "resetWorkingImages",
|
|
338
|
+
},
|
|
339
|
+
session: {
|
|
340
|
+
autoBegin: true,
|
|
341
|
+
leavePolicy: "block",
|
|
342
|
+
},
|
|
343
|
+
},
|
|
344
|
+
],
|
|
345
|
+
[ContributionPointIds.CONFIGURATIONS]: [
|
|
346
|
+
{
|
|
347
|
+
id: "image.items",
|
|
348
|
+
type: "array",
|
|
349
|
+
label: "Images",
|
|
350
|
+
default: [],
|
|
351
|
+
},
|
|
352
|
+
{
|
|
353
|
+
id: "image.debug",
|
|
354
|
+
type: "boolean",
|
|
355
|
+
label: "Image Debug Log",
|
|
356
|
+
default: false,
|
|
357
|
+
},
|
|
358
|
+
{
|
|
359
|
+
id: "image.frame.strokeColor",
|
|
360
|
+
type: "color",
|
|
361
|
+
label: "Image Frame Stroke Color",
|
|
362
|
+
default: "#FF0000",
|
|
363
|
+
},
|
|
364
|
+
{
|
|
365
|
+
id: "image.frame.strokeWidth",
|
|
366
|
+
type: "number",
|
|
367
|
+
label: "Image Frame Stroke Width",
|
|
368
|
+
min: 0,
|
|
369
|
+
max: 20,
|
|
370
|
+
step: 0.5,
|
|
371
|
+
default: 2,
|
|
372
|
+
},
|
|
373
|
+
{
|
|
374
|
+
id: "image.frame.strokeStyle",
|
|
375
|
+
type: "select",
|
|
376
|
+
label: "Image Frame Stroke Style",
|
|
377
|
+
options: ["solid", "dashed", "hidden"],
|
|
378
|
+
default: "solid",
|
|
379
|
+
},
|
|
380
|
+
{
|
|
381
|
+
id: "image.frame.dashLength",
|
|
382
|
+
type: "number",
|
|
383
|
+
label: "Image Frame Dash Length",
|
|
384
|
+
min: 1,
|
|
385
|
+
max: 40,
|
|
386
|
+
step: 1,
|
|
387
|
+
default: 8,
|
|
388
|
+
},
|
|
389
|
+
{
|
|
390
|
+
id: "image.frame.innerBackground",
|
|
391
|
+
type: "color",
|
|
392
|
+
label: "Image Frame Inner Background",
|
|
393
|
+
default: "rgba(0,0,0,0)",
|
|
394
|
+
},
|
|
395
|
+
{
|
|
396
|
+
id: "image.frame.outerBackground",
|
|
397
|
+
type: "color",
|
|
398
|
+
label: "Image Frame Outer Background",
|
|
399
|
+
default: "rgba(0,0,0,0.18)",
|
|
400
|
+
},
|
|
401
|
+
] as ConfigurationContribution[],
|
|
402
|
+
[ContributionPointIds.COMMANDS]: [
|
|
403
|
+
{
|
|
404
|
+
command: "addImage",
|
|
405
|
+
title: "Add Image",
|
|
406
|
+
handler: async (url: string, options?: Partial<ImageItem>) => {
|
|
407
|
+
const result = await this.upsertImageEntry(url, {
|
|
408
|
+
mode: "add",
|
|
409
|
+
addOptions: options,
|
|
410
|
+
});
|
|
411
|
+
return result.id;
|
|
412
|
+
},
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
command: "upsertImage",
|
|
416
|
+
title: "Upsert Image",
|
|
417
|
+
handler: async (url: string, options: UpsertImageOptions = {}) => {
|
|
418
|
+
return await this.upsertImageEntry(url, options);
|
|
419
|
+
},
|
|
420
|
+
},
|
|
421
|
+
{
|
|
422
|
+
command: "getWorkingImages",
|
|
423
|
+
title: "Get Working Images",
|
|
424
|
+
handler: () => {
|
|
425
|
+
return this.cloneItems(this.workingItems);
|
|
426
|
+
},
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
command: "setWorkingImage",
|
|
430
|
+
title: "Set Working Image",
|
|
431
|
+
handler: (id: string, updates: Partial<ImageItem>) => {
|
|
432
|
+
this.updateImageInWorking(id, updates);
|
|
433
|
+
},
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
command: "resetWorkingImages",
|
|
437
|
+
title: "Reset Working Images",
|
|
438
|
+
handler: () => {
|
|
439
|
+
this.workingItems = this.cloneItems(this.items);
|
|
440
|
+
this.hasWorkingChanges = false;
|
|
441
|
+
this.updateImages();
|
|
442
|
+
},
|
|
443
|
+
},
|
|
444
|
+
{
|
|
445
|
+
command: "completeImages",
|
|
446
|
+
title: "Complete Images",
|
|
447
|
+
handler: async () => {
|
|
448
|
+
return await this.commitWorkingImagesAsCropped();
|
|
449
|
+
},
|
|
450
|
+
},
|
|
451
|
+
{
|
|
452
|
+
command: "exportImageFrameUrl",
|
|
453
|
+
title: "Export Image Frame Url",
|
|
454
|
+
handler: async (
|
|
455
|
+
options: { multiplier?: number; format?: "png" | "jpeg" } = {},
|
|
456
|
+
) => {
|
|
457
|
+
return await this.exportImageFrameUrl(options);
|
|
458
|
+
},
|
|
459
|
+
},
|
|
460
|
+
{
|
|
461
|
+
command: "fitImageToArea",
|
|
462
|
+
title: "Fit Image to Area",
|
|
463
|
+
handler: async (
|
|
464
|
+
id: string,
|
|
465
|
+
area: {
|
|
466
|
+
width: number;
|
|
467
|
+
height: number;
|
|
468
|
+
left?: number;
|
|
469
|
+
top?: number;
|
|
470
|
+
},
|
|
471
|
+
) => {
|
|
472
|
+
await this.fitImageToArea(id, area);
|
|
473
|
+
},
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
command: "fitImageToDefaultArea",
|
|
477
|
+
title: "Fit Image to Default Area",
|
|
478
|
+
handler: async (id: string) => {
|
|
479
|
+
await this.fitImageToDefaultArea(id);
|
|
480
|
+
},
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
command: "removeImage",
|
|
484
|
+
title: "Remove Image",
|
|
485
|
+
handler: (id: string) => {
|
|
486
|
+
const removed = this.items.find((item) => item.id === id);
|
|
487
|
+
const next = this.items.filter((item) => item.id !== id);
|
|
488
|
+
if (next.length !== this.items.length) {
|
|
489
|
+
this.purgeSourceSizeCacheForItem(removed);
|
|
490
|
+
if (this.focusedImageId === id) {
|
|
491
|
+
this.focusedImageId = null;
|
|
492
|
+
this.isImageSelectionActive = false;
|
|
493
|
+
}
|
|
494
|
+
this.updateConfig(next);
|
|
495
|
+
}
|
|
496
|
+
},
|
|
497
|
+
},
|
|
498
|
+
{
|
|
499
|
+
command: "updateImage",
|
|
500
|
+
title: "Update Image",
|
|
501
|
+
handler: async (
|
|
502
|
+
id: string,
|
|
503
|
+
updates: Partial<ImageItem>,
|
|
504
|
+
options: UpdateImageOptions = {},
|
|
505
|
+
) => {
|
|
506
|
+
await this.updateImage(id, updates, options);
|
|
507
|
+
},
|
|
508
|
+
},
|
|
509
|
+
{
|
|
510
|
+
command: "clearImages",
|
|
511
|
+
title: "Clear Images",
|
|
512
|
+
handler: () => {
|
|
513
|
+
this.sourceSizeBySrc.clear();
|
|
514
|
+
this.focusedImageId = null;
|
|
515
|
+
this.isImageSelectionActive = false;
|
|
516
|
+
this.updateConfig([]);
|
|
517
|
+
},
|
|
518
|
+
},
|
|
519
|
+
{
|
|
520
|
+
command: "bringToFront",
|
|
521
|
+
title: "Bring Image to Front",
|
|
522
|
+
handler: (id: string) => {
|
|
523
|
+
const index = this.items.findIndex((item) => item.id === id);
|
|
524
|
+
if (index !== -1 && index < this.items.length - 1) {
|
|
525
|
+
const next = [...this.items];
|
|
526
|
+
const [item] = next.splice(index, 1);
|
|
527
|
+
next.push(item);
|
|
528
|
+
this.updateConfig(next);
|
|
529
|
+
}
|
|
530
|
+
},
|
|
531
|
+
},
|
|
532
|
+
{
|
|
533
|
+
command: "sendToBack",
|
|
534
|
+
title: "Send Image to Back",
|
|
535
|
+
handler: (id: string) => {
|
|
536
|
+
const index = this.items.findIndex((item) => item.id === id);
|
|
537
|
+
if (index > 0) {
|
|
538
|
+
const next = [...this.items];
|
|
539
|
+
const [item] = next.splice(index, 1);
|
|
540
|
+
next.unshift(item);
|
|
541
|
+
this.updateConfig(next);
|
|
542
|
+
}
|
|
543
|
+
},
|
|
544
|
+
},
|
|
545
|
+
] as CommandContribution[],
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
private normalizeItem(item: ImageItem): ImageItem {
|
|
550
|
+
const url = typeof item.url === "string" ? item.url : "";
|
|
551
|
+
const sourceUrl =
|
|
552
|
+
typeof item.sourceUrl === "string" && item.sourceUrl.length > 0
|
|
553
|
+
? item.sourceUrl
|
|
554
|
+
: url;
|
|
555
|
+
const committedUrl =
|
|
556
|
+
typeof item.committedUrl === "string" && item.committedUrl.length > 0
|
|
557
|
+
? item.committedUrl
|
|
558
|
+
: undefined;
|
|
559
|
+
|
|
560
|
+
return {
|
|
561
|
+
...item,
|
|
562
|
+
url: url || sourceUrl,
|
|
563
|
+
sourceUrl,
|
|
564
|
+
committedUrl,
|
|
565
|
+
opacity: Number.isFinite(item.opacity as any) ? item.opacity : 1,
|
|
566
|
+
scale: Number.isFinite(item.scale as any) ? item.scale : 1,
|
|
567
|
+
angle: Number.isFinite(item.angle as any) ? item.angle : 0,
|
|
568
|
+
left: Number.isFinite(item.left as any) ? item.left : 0.5,
|
|
569
|
+
top: Number.isFinite(item.top as any) ? item.top : 0.5,
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
private normalizeItems(items: ImageItem[]): ImageItem[] {
|
|
574
|
+
return (items || []).map((item) => this.normalizeItem(item));
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
private cloneItems(items: ImageItem[]): ImageItem[] {
|
|
578
|
+
return this.normalizeItems((items || []).map((i) => ({ ...i })));
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
private generateId(): string {
|
|
582
|
+
return Math.random().toString(36).substring(2, 9);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
private getImageIdFromActiveObject(): string | null {
|
|
586
|
+
const active = this.canvasService?.canvas.getActiveObject() as any;
|
|
587
|
+
if (
|
|
588
|
+
active?.data?.layerId === IMAGE_OBJECT_LAYER_ID &&
|
|
589
|
+
typeof active?.data?.id === "string"
|
|
590
|
+
) {
|
|
591
|
+
return active.data.id;
|
|
592
|
+
}
|
|
593
|
+
return null;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
private resolveReplaceTargetId(explicitId?: string | null): string | null {
|
|
597
|
+
const has = (id: string | null | undefined) =>
|
|
598
|
+
!!id && this.items.some((item) => item.id === id);
|
|
599
|
+
|
|
600
|
+
if (has(explicitId)) return explicitId as string;
|
|
601
|
+
if (has(this.focusedImageId)) return this.focusedImageId as string;
|
|
602
|
+
|
|
603
|
+
const activeId = this.getImageIdFromActiveObject();
|
|
604
|
+
if (has(activeId)) return activeId;
|
|
605
|
+
|
|
606
|
+
if (this.items.length === 1) return this.items[0].id;
|
|
607
|
+
return null;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
private async addImageEntry(
|
|
611
|
+
url: string,
|
|
612
|
+
options?: Partial<ImageItem>,
|
|
613
|
+
fitOnAdd = true,
|
|
614
|
+
): Promise<string> {
|
|
615
|
+
const id = this.generateId();
|
|
616
|
+
const newItem = this.normalizeItem({
|
|
617
|
+
id,
|
|
618
|
+
url,
|
|
619
|
+
opacity: 1,
|
|
620
|
+
...options,
|
|
621
|
+
} as ImageItem);
|
|
622
|
+
|
|
623
|
+
this.focusedImageId = id;
|
|
624
|
+
this.isImageSelectionActive = true;
|
|
625
|
+
this.suppressSelectionClearUntil = Date.now() + IMAGE_REPLACE_GUARD_MS;
|
|
626
|
+
const sessionDirtyBeforeAdd = this.isToolActive && this.hasWorkingChanges;
|
|
627
|
+
const waitLoaded = this.waitImageLoaded(id, true);
|
|
628
|
+
this.updateConfig([...this.items, newItem]);
|
|
629
|
+
this.addItemToWorkingSessionIfNeeded(newItem, sessionDirtyBeforeAdd);
|
|
630
|
+
const loaded = await waitLoaded;
|
|
631
|
+
if (loaded && fitOnAdd) {
|
|
632
|
+
await this.fitImageToDefaultArea(id);
|
|
633
|
+
}
|
|
634
|
+
if (loaded) {
|
|
635
|
+
this.focusImageSelection(id);
|
|
636
|
+
}
|
|
637
|
+
return id;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
private async upsertImageEntry(
|
|
641
|
+
url: string,
|
|
642
|
+
options: UpsertImageOptions = {},
|
|
643
|
+
): Promise<{ id: string; mode: "replace" | "add" }> {
|
|
644
|
+
const mode = options.mode || "auto";
|
|
645
|
+
const fitOnAdd = options.fitOnAdd !== false;
|
|
646
|
+
if (mode === "add") {
|
|
647
|
+
const id = await this.addImageEntry(url, options.addOptions, fitOnAdd);
|
|
648
|
+
return { id, mode: "add" };
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
const targetId = this.resolveReplaceTargetId(options.id ?? null);
|
|
652
|
+
if (targetId) {
|
|
653
|
+
await this.updateImageInConfig(targetId, { url });
|
|
654
|
+
return { id: targetId, mode: "replace" };
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
if (mode === "replace" || options.createIfMissing === false) {
|
|
658
|
+
throw new Error("replace-target-not-found");
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
const id = await this.addImageEntry(url, options.addOptions, fitOnAdd);
|
|
662
|
+
return { id, mode: "add" };
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
private addItemToWorkingSessionIfNeeded(
|
|
666
|
+
item: ImageItem,
|
|
667
|
+
sessionDirtyBeforeAdd: boolean,
|
|
668
|
+
) {
|
|
669
|
+
if (!sessionDirtyBeforeAdd || !this.isToolActive) return;
|
|
670
|
+
if (this.workingItems.some((existing) => existing.id === item.id)) return;
|
|
671
|
+
this.workingItems = this.cloneItems([...this.workingItems, item]);
|
|
672
|
+
this.updateImages();
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
private async updateImage(
|
|
676
|
+
id: string,
|
|
677
|
+
updates: Partial<ImageItem>,
|
|
678
|
+
options: UpdateImageOptions = {},
|
|
679
|
+
) {
|
|
680
|
+
this.syncToolActiveFromWorkbench();
|
|
681
|
+
const target = options.target || "auto";
|
|
682
|
+
|
|
683
|
+
if (target === "working" || (target === "auto" && this.isToolActive)) {
|
|
684
|
+
this.updateImageInWorking(id, updates);
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
await this.updateImageInConfig(id, updates);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
private getConfig<T>(key: string, fallback?: T): T | undefined {
|
|
692
|
+
if (!this.context) return fallback;
|
|
693
|
+
const configService = this.context.services.get<ConfigurationService>(
|
|
694
|
+
"ConfigurationService",
|
|
695
|
+
);
|
|
696
|
+
if (!configService) return fallback;
|
|
697
|
+
return (configService.get(key, fallback) as T) ?? fallback;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
private updateConfig(newItems: ImageItem[], skipCanvasUpdate = false) {
|
|
701
|
+
if (!this.context) return;
|
|
702
|
+
|
|
703
|
+
this.isUpdatingConfig = true;
|
|
704
|
+
this.items = this.normalizeItems(newItems);
|
|
705
|
+
if (!this.isToolActive || !this.hasWorkingChanges) {
|
|
706
|
+
this.workingItems = this.cloneItems(this.items);
|
|
707
|
+
this.hasWorkingChanges = false;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
const configService = this.context.services.get<ConfigurationService>(
|
|
711
|
+
"ConfigurationService",
|
|
712
|
+
);
|
|
713
|
+
configService?.update("image.items", this.items);
|
|
714
|
+
|
|
715
|
+
if (!skipCanvasUpdate) {
|
|
716
|
+
this.updateImages();
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
setTimeout(() => {
|
|
720
|
+
this.isUpdatingConfig = false;
|
|
721
|
+
}, 50);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
private getFrameRect(): FrameRect {
|
|
725
|
+
if (!this.canvasService) {
|
|
726
|
+
return { left: 0, top: 0, width: 0, height: 0 };
|
|
727
|
+
}
|
|
728
|
+
const configService = this.context?.services.get<ConfigurationService>(
|
|
729
|
+
"ConfigurationService",
|
|
730
|
+
);
|
|
731
|
+
if (!configService) {
|
|
732
|
+
return { left: 0, top: 0, width: 0, height: 0 };
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
const sizeState = readSizeState(configService);
|
|
736
|
+
const layout = computeSceneLayout(this.canvasService, sizeState);
|
|
737
|
+
if (!layout) {
|
|
738
|
+
return { left: 0, top: 0, width: 0, height: 0 };
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
return {
|
|
742
|
+
left: layout.cutRect.left,
|
|
743
|
+
top: layout.cutRect.top,
|
|
744
|
+
width: layout.cutRect.width,
|
|
745
|
+
height: layout.cutRect.height,
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
private async resolveDefaultFitArea(): Promise<DielineFitArea | null> {
|
|
750
|
+
if (!this.context || !this.canvasService) return null;
|
|
751
|
+
const commandService = this.context.services.get<any>("CommandService");
|
|
752
|
+
if (!commandService) return null;
|
|
753
|
+
|
|
754
|
+
try {
|
|
755
|
+
const layout = await Promise.resolve(
|
|
756
|
+
commandService.executeCommand("getSceneLayout"),
|
|
757
|
+
);
|
|
758
|
+
const cutRect = layout?.cutRect;
|
|
759
|
+
const width = Number(cutRect?.width);
|
|
760
|
+
const height = Number(cutRect?.height);
|
|
761
|
+
const left = Number(cutRect?.left);
|
|
762
|
+
const top = Number(cutRect?.top);
|
|
763
|
+
|
|
764
|
+
if (
|
|
765
|
+
!Number.isFinite(width) ||
|
|
766
|
+
!Number.isFinite(height) ||
|
|
767
|
+
!Number.isFinite(left) ||
|
|
768
|
+
!Number.isFinite(top)
|
|
769
|
+
) {
|
|
770
|
+
return null;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
return {
|
|
774
|
+
width: Math.max(1, width),
|
|
775
|
+
height: Math.max(1, height),
|
|
776
|
+
left: left + width / 2,
|
|
777
|
+
top: top + height / 2,
|
|
778
|
+
};
|
|
779
|
+
} catch {
|
|
780
|
+
return null;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
private async fitImageToDefaultArea(id: string) {
|
|
785
|
+
if (!this.canvasService) return;
|
|
786
|
+
const area = await this.resolveDefaultFitArea();
|
|
787
|
+
|
|
788
|
+
if (area) {
|
|
789
|
+
await this.fitImageToArea(id, area);
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
const canvasW = Math.max(1, this.canvasService.canvas.width || 0);
|
|
794
|
+
const canvasH = Math.max(1, this.canvasService.canvas.height || 0);
|
|
795
|
+
await this.fitImageToArea(id, {
|
|
796
|
+
width: canvasW,
|
|
797
|
+
height: canvasH,
|
|
798
|
+
left: canvasW / 2,
|
|
799
|
+
top: canvasH / 2,
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
private getImageObjects(): any[] {
|
|
804
|
+
if (!this.canvasService) return [];
|
|
805
|
+
return this.canvasService.canvas.getObjects().filter((obj: any) => {
|
|
806
|
+
return obj?.data?.layerId === IMAGE_OBJECT_LAYER_ID;
|
|
807
|
+
}) as any[];
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
private getOverlayObjects(): any[] {
|
|
811
|
+
if (!this.canvasService) return [];
|
|
812
|
+
return this.canvasService.getRootLayerObjects(
|
|
813
|
+
IMAGE_OVERLAY_LAYER_ID,
|
|
814
|
+
) as any[];
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
private getImageObject(id: string): any | undefined {
|
|
818
|
+
return this.getImageObjects().find((obj: any) => obj?.data?.id === id);
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
private clearRenderedImages() {
|
|
822
|
+
if (!this.canvasService) return;
|
|
823
|
+
const canvas = this.canvasService.canvas;
|
|
824
|
+
this.getImageObjects().forEach((obj) => canvas.remove(obj));
|
|
825
|
+
this.canvasService.requestRenderAll();
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
private purgeSourceSizeCacheForItem(item?: ImageItem) {
|
|
829
|
+
if (!item) return;
|
|
830
|
+
const sources = [item.url, item.sourceUrl, item.committedUrl].filter(
|
|
831
|
+
(value): value is string => typeof value === "string" && value.length > 0,
|
|
832
|
+
);
|
|
833
|
+
sources.forEach((src) => this.sourceSizeBySrc.delete(src));
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
private rememberSourceSize(src: string, obj: any) {
|
|
837
|
+
const width = Number(obj?.width || 0);
|
|
838
|
+
const height = Number(obj?.height || 0);
|
|
839
|
+
if (src && width > 0 && height > 0) {
|
|
840
|
+
this.sourceSizeBySrc.set(src, { width, height });
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
private getSourceSize(src: string, obj?: any): SourceSize {
|
|
845
|
+
const cached = src ? this.sourceSizeBySrc.get(src) : undefined;
|
|
846
|
+
if (cached) return cached;
|
|
847
|
+
|
|
848
|
+
const width = Number(obj?.width || 0);
|
|
849
|
+
const height = Number(obj?.height || 0);
|
|
850
|
+
if (src && width > 0 && height > 0) {
|
|
851
|
+
const size = { width, height };
|
|
852
|
+
this.sourceSizeBySrc.set(src, size);
|
|
853
|
+
return size;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
return { width: 1, height: 1 };
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
private getCoverScale(frame: FrameRect, size: SourceSize): number {
|
|
860
|
+
const sw = Math.max(1, size.width);
|
|
861
|
+
const sh = Math.max(1, size.height);
|
|
862
|
+
const fw = Math.max(1, frame.width);
|
|
863
|
+
const fh = Math.max(1, frame.height);
|
|
864
|
+
return Math.max(fw / sw, fh / sh);
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
private getFrameVisualConfig(): FrameVisualConfig {
|
|
868
|
+
const strokeStyleRaw = (this.getConfig<string>(
|
|
869
|
+
"image.frame.strokeStyle",
|
|
870
|
+
"solid",
|
|
871
|
+
) || "solid") as string;
|
|
872
|
+
const strokeStyle: "solid" | "dashed" | "hidden" =
|
|
873
|
+
strokeStyleRaw === "dashed" || strokeStyleRaw === "hidden"
|
|
874
|
+
? strokeStyleRaw
|
|
875
|
+
: "solid";
|
|
876
|
+
|
|
877
|
+
const strokeWidth = Number(
|
|
878
|
+
this.getConfig<number>("image.frame.strokeWidth", 2) ?? 2,
|
|
879
|
+
);
|
|
880
|
+
const dashLength = Number(
|
|
881
|
+
this.getConfig<number>("image.frame.dashLength", 8) ?? 8,
|
|
882
|
+
);
|
|
883
|
+
|
|
884
|
+
return {
|
|
885
|
+
strokeColor:
|
|
886
|
+
this.getConfig<string>("image.frame.strokeColor", "#FF0000") ||
|
|
887
|
+
"#FF0000",
|
|
888
|
+
strokeWidth: Number.isFinite(strokeWidth) ? Math.max(0, strokeWidth) : 2,
|
|
889
|
+
strokeStyle,
|
|
890
|
+
dashLength: Number.isFinite(dashLength) ? Math.max(1, dashLength) : 8,
|
|
891
|
+
innerBackground:
|
|
892
|
+
this.getConfig<string>(
|
|
893
|
+
"image.frame.innerBackground",
|
|
894
|
+
"rgba(0,0,0,0)",
|
|
895
|
+
) || "rgba(0,0,0,0)",
|
|
896
|
+
outerBackground:
|
|
897
|
+
this.getConfig<string>(
|
|
898
|
+
"image.frame.outerBackground",
|
|
899
|
+
"rgba(0,0,0,0.18)",
|
|
900
|
+
) || "rgba(0,0,0,0.18)",
|
|
901
|
+
};
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
private resolveRenderImageState(item: ImageItem): RenderImageState {
|
|
905
|
+
const active = this.isToolActive;
|
|
906
|
+
const sourceUrl = item.sourceUrl || item.url;
|
|
907
|
+
const committedUrl = item.committedUrl;
|
|
908
|
+
|
|
909
|
+
if (!active && committedUrl) {
|
|
910
|
+
return {
|
|
911
|
+
src: committedUrl,
|
|
912
|
+
left: 0.5,
|
|
913
|
+
top: 0.5,
|
|
914
|
+
scale: 1,
|
|
915
|
+
angle: 0,
|
|
916
|
+
opacity: item.opacity,
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
return {
|
|
921
|
+
src: sourceUrl || item.url,
|
|
922
|
+
left: Number.isFinite(item.left as any) ? (item.left as number) : 0.5,
|
|
923
|
+
top: Number.isFinite(item.top as any) ? (item.top as number) : 0.5,
|
|
924
|
+
scale: Math.max(0.05, item.scale ?? 1),
|
|
925
|
+
angle: Number.isFinite(item.angle as any) ? (item.angle as number) : 0,
|
|
926
|
+
opacity: item.opacity,
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
private computeCanvasProps(
|
|
931
|
+
render: RenderImageState,
|
|
932
|
+
size: SourceSize,
|
|
933
|
+
frame: FrameRect,
|
|
934
|
+
) {
|
|
935
|
+
const left = render.left;
|
|
936
|
+
const top = render.top;
|
|
937
|
+
const zoom = render.scale;
|
|
938
|
+
const angle = render.angle;
|
|
939
|
+
|
|
940
|
+
const centerX = frame.left + left * frame.width;
|
|
941
|
+
const centerY = frame.top + top * frame.height;
|
|
942
|
+
const scale = this.getCoverScale(frame, size) * zoom;
|
|
943
|
+
|
|
944
|
+
return {
|
|
945
|
+
left: centerX,
|
|
946
|
+
top: centerY,
|
|
947
|
+
scaleX: scale,
|
|
948
|
+
scaleY: scale,
|
|
949
|
+
angle,
|
|
950
|
+
originX: "center" as const,
|
|
951
|
+
originY: "center" as const,
|
|
952
|
+
uniformScaling: true,
|
|
953
|
+
lockScalingFlip: true,
|
|
954
|
+
selectable: this.isImageEditingVisible(),
|
|
955
|
+
evented: this.isImageEditingVisible(),
|
|
956
|
+
hasControls: this.isImageEditingVisible(),
|
|
957
|
+
hasBorders: this.isImageEditingVisible(),
|
|
958
|
+
opacity: render.opacity,
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
private getCurrentSrc(obj: any): string | undefined {
|
|
963
|
+
if (!obj) return undefined;
|
|
964
|
+
if (typeof obj.getSrc === "function") return obj.getSrc();
|
|
965
|
+
return obj?._originalElement?.src;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
private async upsertImageObject(
|
|
969
|
+
item: ImageItem,
|
|
970
|
+
frame: FrameRect,
|
|
971
|
+
seq: number,
|
|
972
|
+
) {
|
|
973
|
+
if (!this.canvasService) return;
|
|
974
|
+
const canvas = this.canvasService.canvas;
|
|
975
|
+
const render = this.resolveRenderImageState(item);
|
|
976
|
+
if (!render.src) return;
|
|
977
|
+
|
|
978
|
+
let obj = this.getImageObject(item.id);
|
|
979
|
+
const currentSrc = this.getCurrentSrc(obj);
|
|
980
|
+
|
|
981
|
+
if (obj && currentSrc && currentSrc !== render.src) {
|
|
982
|
+
canvas.remove(obj);
|
|
983
|
+
obj = undefined;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
if (!obj) {
|
|
987
|
+
const created = await FabricImage.fromURL(render.src, {
|
|
988
|
+
crossOrigin: "anonymous",
|
|
989
|
+
});
|
|
990
|
+
if (seq !== this.renderSeq) return;
|
|
991
|
+
|
|
992
|
+
created.set({
|
|
993
|
+
data: {
|
|
994
|
+
id: item.id,
|
|
995
|
+
layerId: IMAGE_OBJECT_LAYER_ID,
|
|
996
|
+
type: "image-item",
|
|
997
|
+
},
|
|
998
|
+
} as any);
|
|
999
|
+
canvas.add(created as any);
|
|
1000
|
+
obj = created as any;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
this.rememberSourceSize(render.src, obj);
|
|
1004
|
+
const sourceSize = this.getSourceSize(render.src, obj);
|
|
1005
|
+
const props = this.computeCanvasProps(render, sourceSize, frame);
|
|
1006
|
+
|
|
1007
|
+
obj.set({
|
|
1008
|
+
...props,
|
|
1009
|
+
data: {
|
|
1010
|
+
...(obj.data || {}),
|
|
1011
|
+
id: item.id,
|
|
1012
|
+
layerId: IMAGE_OBJECT_LAYER_ID,
|
|
1013
|
+
type: "image-item",
|
|
1014
|
+
},
|
|
1015
|
+
});
|
|
1016
|
+
obj.setCoords();
|
|
1017
|
+
|
|
1018
|
+
const resolver = this.loadResolvers.get(item.id);
|
|
1019
|
+
if (resolver) {
|
|
1020
|
+
resolver();
|
|
1021
|
+
this.loadResolvers.delete(item.id);
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
private syncImageZOrder(items: ImageItem[]) {
|
|
1026
|
+
if (!this.canvasService) return;
|
|
1027
|
+
const canvas = this.canvasService.canvas;
|
|
1028
|
+
|
|
1029
|
+
const objects = canvas.getObjects();
|
|
1030
|
+
let insertIndex = 0;
|
|
1031
|
+
|
|
1032
|
+
const backgroundLayer = this.canvasService.getLayer("background");
|
|
1033
|
+
if (backgroundLayer) {
|
|
1034
|
+
const bgIndex = objects.indexOf(backgroundLayer as any);
|
|
1035
|
+
if (bgIndex >= 0) insertIndex = bgIndex + 1;
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
items.forEach((item) => {
|
|
1039
|
+
const obj = this.getImageObject(item.id);
|
|
1040
|
+
if (!obj) return;
|
|
1041
|
+
canvas.moveObjectTo(obj, insertIndex);
|
|
1042
|
+
insertIndex += 1;
|
|
1043
|
+
});
|
|
1044
|
+
|
|
1045
|
+
const overlayObjects = this.getOverlayObjects().sort((a: any, b: any) => {
|
|
1046
|
+
const az = Number(a?.data?.zIndex ?? 0);
|
|
1047
|
+
const bz = Number(b?.data?.zIndex ?? 0);
|
|
1048
|
+
return az - bz;
|
|
1049
|
+
});
|
|
1050
|
+
overlayObjects.forEach((obj) => {
|
|
1051
|
+
canvas.bringObjectToFront(obj);
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
private buildOverlaySpecs(frame: FrameRect): RenderObjectSpec[] {
|
|
1056
|
+
const visible = this.isImageEditingVisible();
|
|
1057
|
+
if (
|
|
1058
|
+
!visible ||
|
|
1059
|
+
frame.width <= 0 ||
|
|
1060
|
+
frame.height <= 0 ||
|
|
1061
|
+
!this.canvasService
|
|
1062
|
+
) {
|
|
1063
|
+
this.debug("overlay:hidden", {
|
|
1064
|
+
visible,
|
|
1065
|
+
frame,
|
|
1066
|
+
isToolActive: this.isToolActive,
|
|
1067
|
+
isImageSelectionActive: this.isImageSelectionActive,
|
|
1068
|
+
focusedImageId: this.focusedImageId,
|
|
1069
|
+
});
|
|
1070
|
+
return [];
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
const canvasW = this.canvasService.canvas.width || 0;
|
|
1074
|
+
const canvasH = this.canvasService.canvas.height || 0;
|
|
1075
|
+
const visual = this.getFrameVisualConfig();
|
|
1076
|
+
|
|
1077
|
+
const topH = Math.max(0, frame.top);
|
|
1078
|
+
const bottomH = Math.max(0, canvasH - (frame.top + frame.height));
|
|
1079
|
+
const leftW = Math.max(0, frame.left);
|
|
1080
|
+
const rightW = Math.max(0, canvasW - (frame.left + frame.width));
|
|
1081
|
+
|
|
1082
|
+
const mask: RenderObjectSpec[] = [
|
|
1083
|
+
{
|
|
1084
|
+
id: "image.cropMask.top",
|
|
1085
|
+
type: "rect",
|
|
1086
|
+
data: { id: "image.cropMask.top", zIndex: 1 },
|
|
1087
|
+
props: {
|
|
1088
|
+
left: canvasW / 2,
|
|
1089
|
+
top: topH / 2,
|
|
1090
|
+
width: canvasW,
|
|
1091
|
+
height: topH,
|
|
1092
|
+
originX: "center",
|
|
1093
|
+
originY: "center",
|
|
1094
|
+
fill: visual.outerBackground,
|
|
1095
|
+
selectable: false,
|
|
1096
|
+
evented: false,
|
|
1097
|
+
},
|
|
1098
|
+
},
|
|
1099
|
+
{
|
|
1100
|
+
id: "image.cropMask.bottom",
|
|
1101
|
+
type: "rect",
|
|
1102
|
+
data: { id: "image.cropMask.bottom", zIndex: 2 },
|
|
1103
|
+
props: {
|
|
1104
|
+
left: canvasW / 2,
|
|
1105
|
+
top: frame.top + frame.height + bottomH / 2,
|
|
1106
|
+
width: canvasW,
|
|
1107
|
+
height: bottomH,
|
|
1108
|
+
originX: "center",
|
|
1109
|
+
originY: "center",
|
|
1110
|
+
fill: visual.outerBackground,
|
|
1111
|
+
selectable: false,
|
|
1112
|
+
evented: false,
|
|
1113
|
+
},
|
|
1114
|
+
},
|
|
1115
|
+
{
|
|
1116
|
+
id: "image.cropMask.left",
|
|
1117
|
+
type: "rect",
|
|
1118
|
+
data: { id: "image.cropMask.left", zIndex: 3 },
|
|
1119
|
+
props: {
|
|
1120
|
+
left: leftW / 2,
|
|
1121
|
+
top: frame.top + frame.height / 2,
|
|
1122
|
+
width: leftW,
|
|
1123
|
+
height: frame.height,
|
|
1124
|
+
originX: "center",
|
|
1125
|
+
originY: "center",
|
|
1126
|
+
fill: visual.outerBackground,
|
|
1127
|
+
selectable: false,
|
|
1128
|
+
evented: false,
|
|
1129
|
+
},
|
|
1130
|
+
},
|
|
1131
|
+
{
|
|
1132
|
+
id: "image.cropMask.right",
|
|
1133
|
+
type: "rect",
|
|
1134
|
+
data: { id: "image.cropMask.right", zIndex: 4 },
|
|
1135
|
+
props: {
|
|
1136
|
+
left: frame.left + frame.width + rightW / 2,
|
|
1137
|
+
top: frame.top + frame.height / 2,
|
|
1138
|
+
width: rightW,
|
|
1139
|
+
height: frame.height,
|
|
1140
|
+
originX: "center",
|
|
1141
|
+
originY: "center",
|
|
1142
|
+
fill: visual.outerBackground,
|
|
1143
|
+
selectable: false,
|
|
1144
|
+
evented: false,
|
|
1145
|
+
},
|
|
1146
|
+
},
|
|
1147
|
+
];
|
|
1148
|
+
|
|
1149
|
+
const frameSpec: RenderObjectSpec = {
|
|
1150
|
+
id: "image.cropFrame",
|
|
1151
|
+
type: "rect",
|
|
1152
|
+
data: { id: "image.cropFrame", zIndex: 5 },
|
|
1153
|
+
props: {
|
|
1154
|
+
left: frame.left + frame.width / 2,
|
|
1155
|
+
top: frame.top + frame.height / 2,
|
|
1156
|
+
width: frame.width,
|
|
1157
|
+
height: frame.height,
|
|
1158
|
+
originX: "center",
|
|
1159
|
+
originY: "center",
|
|
1160
|
+
fill: visual.innerBackground,
|
|
1161
|
+
stroke:
|
|
1162
|
+
visual.strokeStyle === "hidden"
|
|
1163
|
+
? "rgba(0,0,0,0)"
|
|
1164
|
+
: visual.strokeColor,
|
|
1165
|
+
strokeWidth: visual.strokeStyle === "hidden" ? 0 : visual.strokeWidth,
|
|
1166
|
+
strokeDashArray:
|
|
1167
|
+
visual.strokeStyle === "dashed"
|
|
1168
|
+
? [visual.dashLength, visual.dashLength]
|
|
1169
|
+
: undefined,
|
|
1170
|
+
selectable: false,
|
|
1171
|
+
evented: false,
|
|
1172
|
+
},
|
|
1173
|
+
};
|
|
1174
|
+
|
|
1175
|
+
return [...mask, frameSpec];
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
private updateImages() {
|
|
1179
|
+
void this.updateImagesAsync();
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
private async updateImagesAsync() {
|
|
1183
|
+
if (!this.canvasService) return;
|
|
1184
|
+
this.syncToolActiveFromWorkbench();
|
|
1185
|
+
const seq = ++this.renderSeq;
|
|
1186
|
+
|
|
1187
|
+
const renderItems = this.isToolActive ? this.workingItems : this.items;
|
|
1188
|
+
const frame = this.getFrameRect();
|
|
1189
|
+
const desiredIds = new Set(renderItems.map((item) => item.id));
|
|
1190
|
+
if (this.focusedImageId && !desiredIds.has(this.focusedImageId)) {
|
|
1191
|
+
this.focusedImageId = null;
|
|
1192
|
+
this.isImageSelectionActive = false;
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
this.getImageObjects().forEach((obj: any) => {
|
|
1196
|
+
const id = obj?.data?.id;
|
|
1197
|
+
if (typeof id === "string" && !desiredIds.has(id)) {
|
|
1198
|
+
this.canvasService?.canvas.remove(obj);
|
|
1199
|
+
}
|
|
1200
|
+
});
|
|
1201
|
+
|
|
1202
|
+
for (const item of renderItems) {
|
|
1203
|
+
if (seq !== this.renderSeq) return;
|
|
1204
|
+
await this.upsertImageObject(item, frame, seq);
|
|
1205
|
+
}
|
|
1206
|
+
if (seq !== this.renderSeq) return;
|
|
1207
|
+
|
|
1208
|
+
this.syncImageZOrder(renderItems);
|
|
1209
|
+
const overlaySpecs = this.buildOverlaySpecs(frame);
|
|
1210
|
+
await this.canvasService.applyObjectSpecsToRootLayer(
|
|
1211
|
+
IMAGE_OVERLAY_LAYER_ID,
|
|
1212
|
+
overlaySpecs,
|
|
1213
|
+
);
|
|
1214
|
+
this.syncImageZOrder(renderItems);
|
|
1215
|
+
const overlayCanvasCount = this.getOverlayObjects().length;
|
|
1216
|
+
|
|
1217
|
+
this.debug("render:done", {
|
|
1218
|
+
seq,
|
|
1219
|
+
renderCount: renderItems.length,
|
|
1220
|
+
overlayCount: overlaySpecs.length,
|
|
1221
|
+
overlayCanvasCount,
|
|
1222
|
+
isToolActive: this.isToolActive,
|
|
1223
|
+
isImageSelectionActive: this.isImageSelectionActive,
|
|
1224
|
+
focusedImageId: this.focusedImageId,
|
|
1225
|
+
});
|
|
1226
|
+
this.canvasService.requestRenderAll();
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
private clampNormalized(value: number): number {
|
|
1230
|
+
return Math.max(-1, Math.min(2, value));
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
private onObjectModified = (e: any) => {
|
|
1234
|
+
if (!this.isToolActive) return;
|
|
1235
|
+
const target = e?.target;
|
|
1236
|
+
const id = target?.data?.id;
|
|
1237
|
+
const layerId = target?.data?.layerId;
|
|
1238
|
+
if (typeof id !== "string" || layerId !== IMAGE_OBJECT_LAYER_ID) return;
|
|
1239
|
+
|
|
1240
|
+
const frame = this.getFrameRect();
|
|
1241
|
+
if (!frame.width || !frame.height) return;
|
|
1242
|
+
|
|
1243
|
+
const center = target.getCenterPoint
|
|
1244
|
+
? target.getCenterPoint()
|
|
1245
|
+
: new Point(target.left ?? 0, target.top ?? 0);
|
|
1246
|
+
|
|
1247
|
+
const objectScale = Number.isFinite(target?.scaleX) ? target.scaleX : 1;
|
|
1248
|
+
|
|
1249
|
+
const workingItem = this.workingItems.find((item) => item.id === id);
|
|
1250
|
+
const sourceKey = workingItem?.sourceUrl || workingItem?.url || "";
|
|
1251
|
+
const sourceSize = this.getSourceSize(sourceKey, target);
|
|
1252
|
+
const coverScale = this.getCoverScale(frame, sourceSize);
|
|
1253
|
+
|
|
1254
|
+
const updates: Partial<ImageItem> = {
|
|
1255
|
+
left: this.clampNormalized((center.x - frame.left) / frame.width),
|
|
1256
|
+
top: this.clampNormalized((center.y - frame.top) / frame.height),
|
|
1257
|
+
angle: Number.isFinite(target.angle) ? target.angle : 0,
|
|
1258
|
+
scale: Math.max(0.05, (objectScale || 1) / coverScale),
|
|
1259
|
+
};
|
|
1260
|
+
|
|
1261
|
+
this.focusedImageId = id;
|
|
1262
|
+
this.updateImageInWorking(id, updates);
|
|
1263
|
+
};
|
|
1264
|
+
|
|
1265
|
+
private updateImageInWorking(id: string, updates: Partial<ImageItem>) {
|
|
1266
|
+
const index = this.workingItems.findIndex((item) => item.id === id);
|
|
1267
|
+
if (index < 0) return;
|
|
1268
|
+
|
|
1269
|
+
const next = [...this.workingItems];
|
|
1270
|
+
next[index] = this.normalizeItem({ ...next[index], ...updates });
|
|
1271
|
+
this.workingItems = next;
|
|
1272
|
+
this.hasWorkingChanges = true;
|
|
1273
|
+
this.isImageSelectionActive = true;
|
|
1274
|
+
this.focusedImageId = id;
|
|
1275
|
+
if (this.isToolActive) {
|
|
1276
|
+
this.updateImages();
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
private async updateImageInConfig(id: string, updates: Partial<ImageItem>) {
|
|
1281
|
+
const index = this.items.findIndex((item) => item.id === id);
|
|
1282
|
+
if (index < 0) return;
|
|
1283
|
+
|
|
1284
|
+
const replacingSource =
|
|
1285
|
+
typeof updates.url === "string" && updates.url.length > 0;
|
|
1286
|
+
const next = [...this.items];
|
|
1287
|
+
const base = next[index];
|
|
1288
|
+
const replacingUrl = replacingSource ? (updates.url as string) : undefined;
|
|
1289
|
+
|
|
1290
|
+
next[index] = this.normalizeItem({
|
|
1291
|
+
...base,
|
|
1292
|
+
...updates,
|
|
1293
|
+
...(replacingSource
|
|
1294
|
+
? {
|
|
1295
|
+
url: replacingUrl,
|
|
1296
|
+
sourceUrl: replacingUrl,
|
|
1297
|
+
committedUrl: undefined,
|
|
1298
|
+
scale: updates.scale ?? 1,
|
|
1299
|
+
angle: updates.angle ?? 0,
|
|
1300
|
+
left: updates.left ?? 0.5,
|
|
1301
|
+
top: updates.top ?? 0.5,
|
|
1302
|
+
}
|
|
1303
|
+
: {}),
|
|
1304
|
+
});
|
|
1305
|
+
|
|
1306
|
+
this.updateConfig(next);
|
|
1307
|
+
|
|
1308
|
+
if (replacingSource) {
|
|
1309
|
+
this.focusedImageId = id;
|
|
1310
|
+
this.isImageSelectionActive = true;
|
|
1311
|
+
this.suppressSelectionClearUntil = Date.now() + IMAGE_REPLACE_GUARD_MS;
|
|
1312
|
+
this.debug("replace:image:begin", { id, replacingUrl });
|
|
1313
|
+
this.purgeSourceSizeCacheForItem(base);
|
|
1314
|
+
const loaded = await this.waitImageLoaded(id, true);
|
|
1315
|
+
this.debug("replace:image:loaded", { id, loaded });
|
|
1316
|
+
if (loaded) {
|
|
1317
|
+
await this.refitImageToFrame(id);
|
|
1318
|
+
this.focusImageSelection(id);
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
private waitImageLoaded(id: string, forceWait = false): Promise<boolean> {
|
|
1324
|
+
if (!forceWait && this.getImageObject(id)) {
|
|
1325
|
+
return Promise.resolve(true);
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
return new Promise<boolean>((resolve) => {
|
|
1329
|
+
const timeout = setTimeout(() => {
|
|
1330
|
+
this.loadResolvers.delete(id);
|
|
1331
|
+
resolve(false);
|
|
1332
|
+
}, 4000);
|
|
1333
|
+
|
|
1334
|
+
this.loadResolvers.set(id, () => {
|
|
1335
|
+
clearTimeout(timeout);
|
|
1336
|
+
resolve(true);
|
|
1337
|
+
});
|
|
1338
|
+
});
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
private async refitImageToFrame(id: string) {
|
|
1342
|
+
const obj = this.getImageObject(id);
|
|
1343
|
+
if (!obj || !this.canvasService) return;
|
|
1344
|
+
const current = this.items.find((item) => item.id === id);
|
|
1345
|
+
if (!current) return;
|
|
1346
|
+
const render = this.resolveRenderImageState(current);
|
|
1347
|
+
|
|
1348
|
+
this.rememberSourceSize(render.src, obj);
|
|
1349
|
+
const source = this.getSourceSize(render.src, obj);
|
|
1350
|
+
const frame = this.getFrameRect();
|
|
1351
|
+
const coverScale = this.getCoverScale(frame, source);
|
|
1352
|
+
|
|
1353
|
+
const currentScale = obj.scaleX || 1;
|
|
1354
|
+
const zoom = Math.max(0.05, currentScale / coverScale);
|
|
1355
|
+
|
|
1356
|
+
const updated: Partial<ImageItem> = {
|
|
1357
|
+
scale: Number.isFinite(zoom) ? zoom : 1,
|
|
1358
|
+
angle: 0,
|
|
1359
|
+
left: 0.5,
|
|
1360
|
+
top: 0.5,
|
|
1361
|
+
};
|
|
1362
|
+
|
|
1363
|
+
const index = this.items.findIndex((item) => item.id === id);
|
|
1364
|
+
if (index < 0) return;
|
|
1365
|
+
|
|
1366
|
+
const next = [...this.items];
|
|
1367
|
+
next[index] = this.normalizeItem({ ...next[index], ...updated });
|
|
1368
|
+
this.updateConfig(next);
|
|
1369
|
+
this.workingItems = this.cloneItems(next);
|
|
1370
|
+
this.hasWorkingChanges = false;
|
|
1371
|
+
this.isImageSelectionActive = true;
|
|
1372
|
+
this.focusedImageId = id;
|
|
1373
|
+
this.updateImages();
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
private focusImageSelection(id: string) {
|
|
1377
|
+
if (!this.canvasService) return;
|
|
1378
|
+
const obj = this.getImageObject(id);
|
|
1379
|
+
if (!obj) return;
|
|
1380
|
+
|
|
1381
|
+
this.isImageSelectionActive = true;
|
|
1382
|
+
this.focusedImageId = id;
|
|
1383
|
+
this.suppressSelectionClearUntil = Date.now() + 700;
|
|
1384
|
+
obj.set({
|
|
1385
|
+
selectable: true,
|
|
1386
|
+
evented: true,
|
|
1387
|
+
hasControls: true,
|
|
1388
|
+
hasBorders: true,
|
|
1389
|
+
});
|
|
1390
|
+
this.canvasService.canvas.setActiveObject(obj);
|
|
1391
|
+
this.debug("focus:image", { id });
|
|
1392
|
+
this.canvasService.requestRenderAll();
|
|
1393
|
+
this.updateImages();
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
private async fitImageToArea(
|
|
1397
|
+
id: string,
|
|
1398
|
+
area: { width: number; height: number; left?: number; top?: number },
|
|
1399
|
+
) {
|
|
1400
|
+
if (!this.canvasService) return;
|
|
1401
|
+
|
|
1402
|
+
const loaded = await this.waitImageLoaded(id, false);
|
|
1403
|
+
if (!loaded) return;
|
|
1404
|
+
|
|
1405
|
+
const obj = this.getImageObject(id);
|
|
1406
|
+
if (!obj) return;
|
|
1407
|
+
const renderItems = this.isToolActive ? this.workingItems : this.items;
|
|
1408
|
+
const current = renderItems.find((item) => item.id === id);
|
|
1409
|
+
if (!current) return;
|
|
1410
|
+
const render = this.resolveRenderImageState(current);
|
|
1411
|
+
|
|
1412
|
+
this.rememberSourceSize(render.src, obj);
|
|
1413
|
+
const source = this.getSourceSize(render.src, obj);
|
|
1414
|
+
const frame = this.getFrameRect();
|
|
1415
|
+
const baseCover = this.getCoverScale(frame, source);
|
|
1416
|
+
|
|
1417
|
+
const desiredScale = Math.max(
|
|
1418
|
+
Math.max(1, area.width) / Math.max(1, source.width),
|
|
1419
|
+
Math.max(1, area.height) / Math.max(1, source.height),
|
|
1420
|
+
);
|
|
1421
|
+
|
|
1422
|
+
const canvasW = this.canvasService.canvas.width || 1;
|
|
1423
|
+
const canvasH = this.canvasService.canvas.height || 1;
|
|
1424
|
+
|
|
1425
|
+
const areaLeftInput = area.left ?? 0.5;
|
|
1426
|
+
const areaTopInput = area.top ?? 0.5;
|
|
1427
|
+
|
|
1428
|
+
const areaLeftPx =
|
|
1429
|
+
areaLeftInput <= 1.5 ? areaLeftInput * canvasW : areaLeftInput;
|
|
1430
|
+
const areaTopPx =
|
|
1431
|
+
areaTopInput <= 1.5 ? areaTopInput * canvasH : areaTopInput;
|
|
1432
|
+
|
|
1433
|
+
const updates: Partial<ImageItem> = {
|
|
1434
|
+
scale: Math.max(0.05, desiredScale / baseCover),
|
|
1435
|
+
left: this.clampNormalized(
|
|
1436
|
+
(areaLeftPx - frame.left) / Math.max(1, frame.width),
|
|
1437
|
+
),
|
|
1438
|
+
top: this.clampNormalized(
|
|
1439
|
+
(areaTopPx - frame.top) / Math.max(1, frame.height),
|
|
1440
|
+
),
|
|
1441
|
+
};
|
|
1442
|
+
|
|
1443
|
+
if (this.isToolActive) {
|
|
1444
|
+
this.updateImageInWorking(id, updates);
|
|
1445
|
+
return;
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
await this.updateImageInConfig(id, updates);
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
private async commitWorkingImagesAsCropped() {
|
|
1452
|
+
if (!this.canvasService) {
|
|
1453
|
+
return { ok: false, reason: "canvas-not-ready" };
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
await this.updateImagesAsync();
|
|
1457
|
+
|
|
1458
|
+
const frame = this.getFrameRect();
|
|
1459
|
+
if (!frame.width || !frame.height) {
|
|
1460
|
+
return { ok: false, reason: "frame-not-ready" };
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
const focusId =
|
|
1464
|
+
this.resolveReplaceTargetId(this.focusedImageId) ||
|
|
1465
|
+
(this.workingItems.length === 1 ? this.workingItems[0].id : null);
|
|
1466
|
+
|
|
1467
|
+
const next: ImageItem[] = [];
|
|
1468
|
+
for (const item of this.workingItems) {
|
|
1469
|
+
const url = await this.exportCroppedImageByIds([item.id], {
|
|
1470
|
+
multiplier: 2,
|
|
1471
|
+
format: "png",
|
|
1472
|
+
});
|
|
1473
|
+
|
|
1474
|
+
const sourceUrl = item.sourceUrl || item.url;
|
|
1475
|
+
const previousCommitted = item.committedUrl;
|
|
1476
|
+
next.push(
|
|
1477
|
+
this.normalizeItem({
|
|
1478
|
+
...item,
|
|
1479
|
+
url,
|
|
1480
|
+
sourceUrl,
|
|
1481
|
+
committedUrl: url,
|
|
1482
|
+
}),
|
|
1483
|
+
);
|
|
1484
|
+
if (previousCommitted && previousCommitted !== url) {
|
|
1485
|
+
this.sourceSizeBySrc.delete(previousCommitted);
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
this.hasWorkingChanges = false;
|
|
1490
|
+
this.workingItems = this.cloneItems(next);
|
|
1491
|
+
this.updateConfig(next);
|
|
1492
|
+
if (focusId) {
|
|
1493
|
+
this.focusedImageId = focusId;
|
|
1494
|
+
this.isImageSelectionActive = true;
|
|
1495
|
+
this.suppressSelectionClearUntil = Date.now() + IMAGE_REPLACE_GUARD_MS;
|
|
1496
|
+
this.focusImageSelection(focusId);
|
|
1497
|
+
}
|
|
1498
|
+
return { ok: true };
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
private async exportCroppedImageByIds(
|
|
1502
|
+
imageIds: string[],
|
|
1503
|
+
options: { multiplier?: number; format?: "png" | "jpeg" },
|
|
1504
|
+
): Promise<string> {
|
|
1505
|
+
if (!this.canvasService) {
|
|
1506
|
+
throw new Error("CanvasService not initialized");
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
const frame = this.getFrameRect();
|
|
1510
|
+
const multiplier = Math.max(1, options.multiplier ?? 2);
|
|
1511
|
+
const format = options.format ?? "png";
|
|
1512
|
+
|
|
1513
|
+
const width = Math.max(1, Math.round(frame.width * multiplier));
|
|
1514
|
+
const height = Math.max(1, Math.round(frame.height * multiplier));
|
|
1515
|
+
|
|
1516
|
+
const el = document.createElement("canvas");
|
|
1517
|
+
const tempCanvas = new FabricCanvas(el, {
|
|
1518
|
+
renderOnAddRemove: false,
|
|
1519
|
+
selection: false,
|
|
1520
|
+
enableRetinaScaling: false,
|
|
1521
|
+
preserveObjectStacking: true,
|
|
1522
|
+
} as any);
|
|
1523
|
+
tempCanvas.setDimensions({ width, height });
|
|
1524
|
+
|
|
1525
|
+
const idSet = new Set(imageIds);
|
|
1526
|
+
const sourceObjects = this.canvasService.canvas
|
|
1527
|
+
.getObjects()
|
|
1528
|
+
.filter((obj: any) => {
|
|
1529
|
+
return (
|
|
1530
|
+
obj?.data?.layerId === IMAGE_OBJECT_LAYER_ID &&
|
|
1531
|
+
typeof obj?.data?.id === "string" &&
|
|
1532
|
+
idSet.has(obj.data.id)
|
|
1533
|
+
);
|
|
1534
|
+
});
|
|
1535
|
+
|
|
1536
|
+
for (const source of sourceObjects as any[]) {
|
|
1537
|
+
const clone = await source.clone();
|
|
1538
|
+
const center = source.getCenterPoint
|
|
1539
|
+
? source.getCenterPoint()
|
|
1540
|
+
: new Point(source.left ?? 0, source.top ?? 0);
|
|
1541
|
+
|
|
1542
|
+
clone.set({
|
|
1543
|
+
originX: "center",
|
|
1544
|
+
originY: "center",
|
|
1545
|
+
left: (center.x - frame.left) * multiplier,
|
|
1546
|
+
top: (center.y - frame.top) * multiplier,
|
|
1547
|
+
scaleX: (source.scaleX || 1) * multiplier,
|
|
1548
|
+
scaleY: (source.scaleY || 1) * multiplier,
|
|
1549
|
+
angle: source.angle || 0,
|
|
1550
|
+
selectable: false,
|
|
1551
|
+
evented: false,
|
|
1552
|
+
});
|
|
1553
|
+
clone.setCoords();
|
|
1554
|
+
tempCanvas.add(clone);
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
tempCanvas.renderAll();
|
|
1558
|
+
const dataUrl = tempCanvas.toDataURL({ format, multiplier: 1 });
|
|
1559
|
+
tempCanvas.dispose();
|
|
1560
|
+
|
|
1561
|
+
const blob = await (await fetch(dataUrl)).blob();
|
|
1562
|
+
return URL.createObjectURL(blob);
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
private async exportImageFrameUrl(
|
|
1566
|
+
options: { multiplier?: number; format?: "png" | "jpeg" } = {},
|
|
1567
|
+
): Promise<{ url: string }> {
|
|
1568
|
+
if (!this.canvasService) {
|
|
1569
|
+
throw new Error("CanvasService not initialized");
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
const imageIds = this.getImageObjects()
|
|
1573
|
+
.map((obj: any) => obj?.data?.id)
|
|
1574
|
+
.filter((id: any) => typeof id === "string");
|
|
1575
|
+
|
|
1576
|
+
const url = await this.exportCroppedImageByIds(
|
|
1577
|
+
imageIds as string[],
|
|
1578
|
+
options,
|
|
1579
|
+
);
|
|
1580
|
+
return { url };
|
|
1581
|
+
}
|
|
1582
|
+
}
|