@pooder/kit 5.4.0 → 6.0.1
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/coordinate.js +74 -0
- package/.test-dist/src/extensions/background.js +547 -0
- package/.test-dist/src/extensions/bridgeSelection.js +20 -0
- package/.test-dist/src/extensions/constraints.js +237 -0
- package/.test-dist/src/extensions/dieline.js +935 -0
- package/.test-dist/src/extensions/dielineShape.js +66 -0
- package/.test-dist/src/extensions/edgeScale.js +12 -0
- package/.test-dist/src/extensions/feature.js +910 -0
- package/.test-dist/src/extensions/featureComplete.js +32 -0
- package/.test-dist/src/extensions/film.js +226 -0
- package/.test-dist/src/extensions/geometry.js +609 -0
- package/.test-dist/src/extensions/image.js +1788 -0
- package/.test-dist/src/extensions/index.js +28 -0
- package/.test-dist/src/extensions/maskOps.js +334 -0
- package/.test-dist/src/extensions/mirror.js +104 -0
- package/.test-dist/src/extensions/ruler.js +442 -0
- package/.test-dist/src/extensions/sceneLayout.js +96 -0
- package/.test-dist/src/extensions/sceneLayoutModel.js +202 -0
- package/.test-dist/src/extensions/sceneVisibility.js +55 -0
- package/.test-dist/src/extensions/size.js +331 -0
- package/.test-dist/src/extensions/tracer.js +709 -0
- package/.test-dist/src/extensions/white-ink.js +1200 -0
- package/.test-dist/src/extensions/wrappedOffsets.js +33 -0
- package/.test-dist/src/index.js +18 -0
- package/.test-dist/src/services/CanvasService.js +1032 -0
- package/.test-dist/src/services/ViewportSystem.js +76 -0
- package/.test-dist/src/services/index.js +25 -0
- package/.test-dist/src/services/renderSpec.js +2 -0
- package/.test-dist/src/services/visibility.js +57 -0
- package/.test-dist/src/units.js +30 -0
- package/.test-dist/tests/run.js +150 -0
- package/CHANGELOG.md +12 -0
- package/dist/index.d.mts +164 -62
- package/dist/index.d.ts +164 -62
- package/dist/index.js +2433 -1719
- package/dist/index.mjs +2442 -1723
- package/package.json +1 -1
- package/src/coordinate.ts +106 -106
- package/src/extensions/background.ts +716 -323
- package/src/extensions/bridgeSelection.ts +17 -17
- package/src/extensions/constraints.ts +322 -322
- package/src/extensions/dieline.ts +1173 -1149
- package/src/extensions/dielineShape.ts +109 -109
- package/src/extensions/edgeScale.ts +19 -19
- package/src/extensions/feature.ts +1140 -1137
- package/src/extensions/featureComplete.ts +46 -46
- package/src/extensions/film.ts +270 -266
- package/src/extensions/geometry.ts +851 -885
- package/src/extensions/image.ts +2240 -2054
- package/src/extensions/index.ts +10 -11
- package/src/extensions/maskOps.ts +283 -283
- package/src/extensions/mirror.ts +128 -128
- package/src/extensions/ruler.ts +664 -654
- package/src/extensions/sceneLayout.ts +140 -140
- package/src/extensions/sceneLayoutModel.ts +364 -364
- package/src/extensions/size.ts +389 -389
- package/src/extensions/tracer.ts +1019 -1019
- package/src/extensions/white-ink.ts +1508 -1575
- package/src/extensions/wrappedOffsets.ts +33 -33
- package/src/index.ts +2 -2
- package/src/services/CanvasService.ts +1317 -832
- package/src/services/ViewportSystem.ts +95 -95
- package/src/services/index.ts +4 -3
- package/src/services/renderSpec.ts +85 -53
- package/src/services/visibility.ts +83 -0
- package/src/units.ts +27 -27
- package/tests/run.ts +258 -118
- package/tsconfig.test.json +15 -15
- package/src/extensions/sceneVisibility.ts +0 -64
|
@@ -0,0 +1,1788 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ImageTool = void 0;
|
|
4
|
+
const core_1 = require("@pooder/core");
|
|
5
|
+
const fabric_1 = require("fabric");
|
|
6
|
+
const dielineShape_1 = require("./dielineShape");
|
|
7
|
+
const geometry_1 = require("./geometry");
|
|
8
|
+
const sceneLayoutModel_1 = require("./sceneLayoutModel");
|
|
9
|
+
const IMAGE_OBJECT_LAYER_ID = "image.user";
|
|
10
|
+
const IMAGE_OVERLAY_LAYER_ID = "image-overlay";
|
|
11
|
+
const IMAGE_DEFAULT_CONTROL_CAPABILITIES = [
|
|
12
|
+
"rotate",
|
|
13
|
+
"scale",
|
|
14
|
+
];
|
|
15
|
+
const IMAGE_CONTROL_DESCRIPTORS = [
|
|
16
|
+
{
|
|
17
|
+
key: "tl",
|
|
18
|
+
capability: "rotate",
|
|
19
|
+
create: () => new fabric_1.Control({
|
|
20
|
+
x: -0.5,
|
|
21
|
+
y: -0.5,
|
|
22
|
+
actionName: "rotate",
|
|
23
|
+
actionHandler: fabric_1.controlsUtils.rotationWithSnapping,
|
|
24
|
+
cursorStyleHandler: fabric_1.controlsUtils.rotationStyleHandler,
|
|
25
|
+
}),
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
key: "br",
|
|
29
|
+
capability: "scale",
|
|
30
|
+
create: () => new fabric_1.Control({
|
|
31
|
+
x: 0.5,
|
|
32
|
+
y: 0.5,
|
|
33
|
+
actionName: "scale",
|
|
34
|
+
actionHandler: fabric_1.controlsUtils.scalingEqually,
|
|
35
|
+
cursorStyleHandler: fabric_1.controlsUtils.scaleCursorStyleHandler,
|
|
36
|
+
}),
|
|
37
|
+
},
|
|
38
|
+
];
|
|
39
|
+
class ImageTool {
|
|
40
|
+
constructor() {
|
|
41
|
+
this.id = "pooder.kit.image";
|
|
42
|
+
this.metadata = {
|
|
43
|
+
name: "ImageTool",
|
|
44
|
+
};
|
|
45
|
+
this.items = [];
|
|
46
|
+
this.workingItems = [];
|
|
47
|
+
this.hasWorkingChanges = false;
|
|
48
|
+
this.loadResolvers = new Map();
|
|
49
|
+
this.sourceSizeBySrc = new Map();
|
|
50
|
+
this.isUpdatingConfig = false;
|
|
51
|
+
this.isToolActive = false;
|
|
52
|
+
this.isImageSelectionActive = false;
|
|
53
|
+
this.focusedImageId = null;
|
|
54
|
+
this.renderSeq = 0;
|
|
55
|
+
this.imageSpecs = [];
|
|
56
|
+
this.overlaySpecs = [];
|
|
57
|
+
this.imageControlsByCapabilityKey = new Map();
|
|
58
|
+
this.onToolActivated = (event) => {
|
|
59
|
+
const before = this.isToolActive;
|
|
60
|
+
this.syncToolActiveFromWorkbench(event.id);
|
|
61
|
+
if (!this.isToolActive) {
|
|
62
|
+
this.setImageFocus(null, {
|
|
63
|
+
syncCanvasSelection: true,
|
|
64
|
+
skipRender: true,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
this.debug("tool:activated", {
|
|
68
|
+
id: event.id,
|
|
69
|
+
previous: event.previous,
|
|
70
|
+
reason: event.reason,
|
|
71
|
+
before,
|
|
72
|
+
isToolActive: this.isToolActive,
|
|
73
|
+
focusedImageId: this.focusedImageId,
|
|
74
|
+
});
|
|
75
|
+
if (!this.isToolActive && this.isDebugEnabled()) {
|
|
76
|
+
console.trace("[ImageTool] tool deactivated trace");
|
|
77
|
+
}
|
|
78
|
+
this.updateImages();
|
|
79
|
+
};
|
|
80
|
+
this.onSelectionChanged = (e) => {
|
|
81
|
+
const list = [];
|
|
82
|
+
if (Array.isArray(e?.selected)) {
|
|
83
|
+
list.push(...e.selected);
|
|
84
|
+
}
|
|
85
|
+
if (Array.isArray(e?.target?._objects)) {
|
|
86
|
+
list.push(...e.target._objects);
|
|
87
|
+
}
|
|
88
|
+
if (e?.target && !Array.isArray(e?.target?._objects)) {
|
|
89
|
+
list.push(e.target);
|
|
90
|
+
}
|
|
91
|
+
const selectedImage = list.find((obj) => obj?.data?.layerId === IMAGE_OBJECT_LAYER_ID);
|
|
92
|
+
this.isImageSelectionActive = !!selectedImage;
|
|
93
|
+
if (selectedImage?.data?.id) {
|
|
94
|
+
this.focusedImageId = selectedImage.data.id;
|
|
95
|
+
}
|
|
96
|
+
else if (list.length > 0) {
|
|
97
|
+
this.focusedImageId = null;
|
|
98
|
+
}
|
|
99
|
+
this.debug("selection:changed", {
|
|
100
|
+
listSize: list.length,
|
|
101
|
+
isImageSelectionActive: this.isImageSelectionActive,
|
|
102
|
+
focusedImageId: this.focusedImageId,
|
|
103
|
+
});
|
|
104
|
+
this.updateImages();
|
|
105
|
+
};
|
|
106
|
+
this.onSelectionCleared = () => {
|
|
107
|
+
this.setImageFocus(null, {
|
|
108
|
+
syncCanvasSelection: false,
|
|
109
|
+
skipRender: true,
|
|
110
|
+
});
|
|
111
|
+
this.debug("selection:cleared applied");
|
|
112
|
+
this.updateImages();
|
|
113
|
+
};
|
|
114
|
+
this.onSceneLayoutChanged = () => {
|
|
115
|
+
this.updateImages();
|
|
116
|
+
};
|
|
117
|
+
this.onSceneGeometryChanged = () => {
|
|
118
|
+
this.updateImages();
|
|
119
|
+
};
|
|
120
|
+
this.onObjectModified = (e) => {
|
|
121
|
+
if (!this.isToolActive)
|
|
122
|
+
return;
|
|
123
|
+
const target = e?.target;
|
|
124
|
+
const id = target?.data?.id;
|
|
125
|
+
const layerId = target?.data?.layerId;
|
|
126
|
+
if (typeof id !== "string" || layerId !== IMAGE_OBJECT_LAYER_ID)
|
|
127
|
+
return;
|
|
128
|
+
const frame = this.getFrameRect();
|
|
129
|
+
if (!frame.width || !frame.height)
|
|
130
|
+
return;
|
|
131
|
+
const center = target.getCenterPoint
|
|
132
|
+
? target.getCenterPoint()
|
|
133
|
+
: new fabric_1.Point(target.left ?? 0, target.top ?? 0);
|
|
134
|
+
const centerScene = this.canvasService
|
|
135
|
+
? this.canvasService.toScenePoint({ x: center.x, y: center.y })
|
|
136
|
+
: { x: center.x, y: center.y };
|
|
137
|
+
const objectScale = Number.isFinite(target?.scaleX) ? target.scaleX : 1;
|
|
138
|
+
const objectScaleScene = this.toSceneObjectScale(objectScale || 1);
|
|
139
|
+
const workingItem = this.workingItems.find((item) => item.id === id);
|
|
140
|
+
const sourceKey = workingItem?.sourceUrl || workingItem?.url || "";
|
|
141
|
+
const sourceSize = this.getSourceSize(sourceKey, target);
|
|
142
|
+
const coverScale = this.getCoverScale(frame, sourceSize);
|
|
143
|
+
const updates = {
|
|
144
|
+
left: this.clampNormalized((centerScene.x - frame.left) / frame.width),
|
|
145
|
+
top: this.clampNormalized((centerScene.y - frame.top) / frame.height),
|
|
146
|
+
angle: Number.isFinite(target.angle) ? target.angle : 0,
|
|
147
|
+
scale: Math.max(0.05, objectScaleScene / coverScale),
|
|
148
|
+
};
|
|
149
|
+
this.focusedImageId = id;
|
|
150
|
+
this.updateImageInWorking(id, updates);
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
activate(context) {
|
|
154
|
+
this.context = context;
|
|
155
|
+
this.canvasService = context.services.get("CanvasService");
|
|
156
|
+
if (!this.canvasService) {
|
|
157
|
+
console.warn("CanvasService not found for ImageTool");
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
this.renderProducerDisposable?.dispose();
|
|
161
|
+
this.renderProducerDisposable = this.canvasService.registerRenderProducer(this.id, () => ({
|
|
162
|
+
passes: [
|
|
163
|
+
{
|
|
164
|
+
id: IMAGE_OBJECT_LAYER_ID,
|
|
165
|
+
stack: 500,
|
|
166
|
+
order: 0,
|
|
167
|
+
visibility: {
|
|
168
|
+
op: "not",
|
|
169
|
+
expr: {
|
|
170
|
+
op: "sessionActive",
|
|
171
|
+
toolId: "pooder.kit.white-ink",
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
objects: this.imageSpecs,
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
id: IMAGE_OVERLAY_LAYER_ID,
|
|
178
|
+
stack: 800,
|
|
179
|
+
order: 0,
|
|
180
|
+
visibility: {
|
|
181
|
+
op: "not",
|
|
182
|
+
expr: {
|
|
183
|
+
op: "sessionActive",
|
|
184
|
+
toolId: "pooder.kit.white-ink",
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
objects: this.overlaySpecs,
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
}), { priority: 300 });
|
|
191
|
+
context.eventBus.on("tool:activated", this.onToolActivated);
|
|
192
|
+
context.eventBus.on("object:modified", this.onObjectModified);
|
|
193
|
+
context.eventBus.on("selection:created", this.onSelectionChanged);
|
|
194
|
+
context.eventBus.on("selection:updated", this.onSelectionChanged);
|
|
195
|
+
context.eventBus.on("selection:cleared", this.onSelectionCleared);
|
|
196
|
+
context.eventBus.on("scene:layout:change", this.onSceneLayoutChanged);
|
|
197
|
+
context.eventBus.on("scene:geometry:change", this.onSceneGeometryChanged);
|
|
198
|
+
const configService = context.services.get("ConfigurationService");
|
|
199
|
+
if (configService) {
|
|
200
|
+
this.items = this.normalizeItems(configService.get("image.items", []) || []);
|
|
201
|
+
this.workingItems = this.cloneItems(this.items);
|
|
202
|
+
this.hasWorkingChanges = false;
|
|
203
|
+
configService.onAnyChange((e) => {
|
|
204
|
+
if (this.isUpdatingConfig)
|
|
205
|
+
return;
|
|
206
|
+
if (e.key === "image.items") {
|
|
207
|
+
this.items = this.normalizeItems(e.value || []);
|
|
208
|
+
if (!this.isToolActive || !this.hasWorkingChanges) {
|
|
209
|
+
this.workingItems = this.cloneItems(this.items);
|
|
210
|
+
this.hasWorkingChanges = false;
|
|
211
|
+
}
|
|
212
|
+
this.updateImages();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (e.key.startsWith("size.") ||
|
|
216
|
+
e.key.startsWith("image.frame.") ||
|
|
217
|
+
e.key.startsWith("image.control.")) {
|
|
218
|
+
if (e.key.startsWith("image.control.")) {
|
|
219
|
+
this.imageControlsByCapabilityKey.clear();
|
|
220
|
+
}
|
|
221
|
+
this.updateImages();
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
const toolSessionService = context.services.get("ToolSessionService");
|
|
226
|
+
this.dirtyTrackerDisposable = toolSessionService?.registerDirtyTracker(this.id, () => this.hasWorkingChanges);
|
|
227
|
+
this.updateImages();
|
|
228
|
+
}
|
|
229
|
+
deactivate(context) {
|
|
230
|
+
context.eventBus.off("tool:activated", this.onToolActivated);
|
|
231
|
+
context.eventBus.off("object:modified", this.onObjectModified);
|
|
232
|
+
context.eventBus.off("selection:created", this.onSelectionChanged);
|
|
233
|
+
context.eventBus.off("selection:updated", this.onSelectionChanged);
|
|
234
|
+
context.eventBus.off("selection:cleared", this.onSelectionCleared);
|
|
235
|
+
context.eventBus.off("scene:layout:change", this.onSceneLayoutChanged);
|
|
236
|
+
context.eventBus.off("scene:geometry:change", this.onSceneGeometryChanged);
|
|
237
|
+
this.dirtyTrackerDisposable?.dispose();
|
|
238
|
+
this.dirtyTrackerDisposable = undefined;
|
|
239
|
+
this.cropShapeHatchPattern = undefined;
|
|
240
|
+
this.cropShapeHatchPatternColor = undefined;
|
|
241
|
+
this.cropShapeHatchPatternKey = undefined;
|
|
242
|
+
this.imageSpecs = [];
|
|
243
|
+
this.overlaySpecs = [];
|
|
244
|
+
this.imageControlsByCapabilityKey.clear();
|
|
245
|
+
this.clearRenderedImages();
|
|
246
|
+
this.renderProducerDisposable?.dispose();
|
|
247
|
+
this.renderProducerDisposable = undefined;
|
|
248
|
+
if (this.canvasService) {
|
|
249
|
+
void this.canvasService.flushRenderFromProducers();
|
|
250
|
+
this.canvasService = undefined;
|
|
251
|
+
}
|
|
252
|
+
this.context = undefined;
|
|
253
|
+
}
|
|
254
|
+
syncToolActiveFromWorkbench(fallbackId) {
|
|
255
|
+
const wb = this.context?.services.get("WorkbenchService");
|
|
256
|
+
const activeId = wb?.activeToolId;
|
|
257
|
+
if (typeof activeId === "string" || activeId === null) {
|
|
258
|
+
this.isToolActive = activeId === this.id;
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
this.isToolActive = fallbackId === this.id;
|
|
262
|
+
}
|
|
263
|
+
isImageEditingVisible() {
|
|
264
|
+
return (this.isToolActive || this.isImageSelectionActive || !!this.focusedImageId);
|
|
265
|
+
}
|
|
266
|
+
getEnabledImageControlCapabilities() {
|
|
267
|
+
return IMAGE_DEFAULT_CONTROL_CAPABILITIES;
|
|
268
|
+
}
|
|
269
|
+
getImageControls(capabilities) {
|
|
270
|
+
const normalized = [...new Set(capabilities)].sort();
|
|
271
|
+
const cacheKey = normalized.join("|");
|
|
272
|
+
const cached = this.imageControlsByCapabilityKey.get(cacheKey);
|
|
273
|
+
if (cached) {
|
|
274
|
+
return cached;
|
|
275
|
+
}
|
|
276
|
+
const enabled = new Set(normalized);
|
|
277
|
+
const controls = {};
|
|
278
|
+
IMAGE_CONTROL_DESCRIPTORS.forEach((descriptor) => {
|
|
279
|
+
if (!enabled.has(descriptor.capability))
|
|
280
|
+
return;
|
|
281
|
+
controls[descriptor.key] = descriptor.create();
|
|
282
|
+
});
|
|
283
|
+
this.imageControlsByCapabilityKey.set(cacheKey, controls);
|
|
284
|
+
return controls;
|
|
285
|
+
}
|
|
286
|
+
getImageControlVisualConfig() {
|
|
287
|
+
const cornerSizeRaw = Number(this.getConfig("image.control.cornerSize", 14) ?? 14);
|
|
288
|
+
const touchCornerSizeRaw = Number(this.getConfig("image.control.touchCornerSize", 24) ?? 24);
|
|
289
|
+
const borderScaleFactorRaw = Number(this.getConfig("image.control.borderScaleFactor", 1.5) ?? 1.5);
|
|
290
|
+
const paddingRaw = Number(this.getConfig("image.control.padding", 0) ?? 0);
|
|
291
|
+
const cornerStyleRaw = (this.getConfig("image.control.cornerStyle", "circle") || "circle");
|
|
292
|
+
const cornerStyle = cornerStyleRaw === "rect" ? "rect" : "circle";
|
|
293
|
+
return {
|
|
294
|
+
cornerSize: Number.isFinite(cornerSizeRaw)
|
|
295
|
+
? Math.max(4, Math.min(64, cornerSizeRaw))
|
|
296
|
+
: 14,
|
|
297
|
+
touchCornerSize: Number.isFinite(touchCornerSizeRaw)
|
|
298
|
+
? Math.max(8, Math.min(96, touchCornerSizeRaw))
|
|
299
|
+
: 24,
|
|
300
|
+
cornerStyle,
|
|
301
|
+
cornerColor: this.getConfig("image.control.cornerColor", "#ffffff") ||
|
|
302
|
+
"#ffffff",
|
|
303
|
+
cornerStrokeColor: this.getConfig("image.control.cornerStrokeColor", "#1677ff") ||
|
|
304
|
+
"#1677ff",
|
|
305
|
+
transparentCorners: !!this.getConfig("image.control.transparentCorners", false),
|
|
306
|
+
borderColor: this.getConfig("image.control.borderColor", "#1677ff") ||
|
|
307
|
+
"#1677ff",
|
|
308
|
+
borderScaleFactor: Number.isFinite(borderScaleFactorRaw)
|
|
309
|
+
? Math.max(0.5, Math.min(8, borderScaleFactorRaw))
|
|
310
|
+
: 1.5,
|
|
311
|
+
padding: Number.isFinite(paddingRaw)
|
|
312
|
+
? Math.max(0, Math.min(64, paddingRaw))
|
|
313
|
+
: 0,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
applyImageObjectInteractionState(obj) {
|
|
317
|
+
if (!obj)
|
|
318
|
+
return;
|
|
319
|
+
const visible = this.isImageEditingVisible();
|
|
320
|
+
const visual = this.getImageControlVisualConfig();
|
|
321
|
+
obj.set({
|
|
322
|
+
selectable: visible,
|
|
323
|
+
evented: visible,
|
|
324
|
+
hasControls: visible,
|
|
325
|
+
hasBorders: visible,
|
|
326
|
+
lockScalingFlip: true,
|
|
327
|
+
cornerSize: visual.cornerSize,
|
|
328
|
+
touchCornerSize: visual.touchCornerSize,
|
|
329
|
+
cornerStyle: visual.cornerStyle,
|
|
330
|
+
cornerColor: visual.cornerColor,
|
|
331
|
+
cornerStrokeColor: visual.cornerStrokeColor,
|
|
332
|
+
transparentCorners: visual.transparentCorners,
|
|
333
|
+
borderColor: visual.borderColor,
|
|
334
|
+
borderScaleFactor: visual.borderScaleFactor,
|
|
335
|
+
padding: visual.padding,
|
|
336
|
+
});
|
|
337
|
+
obj.controls = this.getImageControls(this.getEnabledImageControlCapabilities());
|
|
338
|
+
obj.setCoords?.();
|
|
339
|
+
}
|
|
340
|
+
refreshImageObjectInteractionState() {
|
|
341
|
+
this.getImageObjects().forEach((obj) => this.applyImageObjectInteractionState(obj));
|
|
342
|
+
}
|
|
343
|
+
isDebugEnabled() {
|
|
344
|
+
return !!this.getConfig("image.debug", false);
|
|
345
|
+
}
|
|
346
|
+
debug(message, payload) {
|
|
347
|
+
if (!this.isDebugEnabled())
|
|
348
|
+
return;
|
|
349
|
+
if (payload === undefined) {
|
|
350
|
+
console.log(`[ImageTool] ${message}`);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
console.log(`[ImageTool] ${message}`, payload);
|
|
354
|
+
}
|
|
355
|
+
contribute() {
|
|
356
|
+
return {
|
|
357
|
+
[core_1.ContributionPointIds.TOOLS]: [
|
|
358
|
+
{
|
|
359
|
+
id: this.id,
|
|
360
|
+
name: "Image",
|
|
361
|
+
interaction: "session",
|
|
362
|
+
commands: {
|
|
363
|
+
begin: "resetWorkingImages",
|
|
364
|
+
commit: "completeImages",
|
|
365
|
+
rollback: "resetWorkingImages",
|
|
366
|
+
},
|
|
367
|
+
session: {
|
|
368
|
+
autoBegin: true,
|
|
369
|
+
leavePolicy: "block",
|
|
370
|
+
},
|
|
371
|
+
},
|
|
372
|
+
],
|
|
373
|
+
[core_1.ContributionPointIds.CONFIGURATIONS]: [
|
|
374
|
+
{
|
|
375
|
+
id: "image.items",
|
|
376
|
+
type: "array",
|
|
377
|
+
label: "Images",
|
|
378
|
+
default: [],
|
|
379
|
+
},
|
|
380
|
+
{
|
|
381
|
+
id: "image.debug",
|
|
382
|
+
type: "boolean",
|
|
383
|
+
label: "Image Debug Log",
|
|
384
|
+
default: false,
|
|
385
|
+
},
|
|
386
|
+
{
|
|
387
|
+
id: "image.control.cornerSize",
|
|
388
|
+
type: "number",
|
|
389
|
+
label: "Image Control Corner Size",
|
|
390
|
+
min: 4,
|
|
391
|
+
max: 64,
|
|
392
|
+
step: 1,
|
|
393
|
+
default: 14,
|
|
394
|
+
},
|
|
395
|
+
{
|
|
396
|
+
id: "image.control.touchCornerSize",
|
|
397
|
+
type: "number",
|
|
398
|
+
label: "Image Control Touch Corner Size",
|
|
399
|
+
min: 8,
|
|
400
|
+
max: 96,
|
|
401
|
+
step: 1,
|
|
402
|
+
default: 24,
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
id: "image.control.cornerStyle",
|
|
406
|
+
type: "select",
|
|
407
|
+
label: "Image Control Corner Style",
|
|
408
|
+
options: ["circle", "rect"],
|
|
409
|
+
default: "circle",
|
|
410
|
+
},
|
|
411
|
+
{
|
|
412
|
+
id: "image.control.cornerColor",
|
|
413
|
+
type: "color",
|
|
414
|
+
label: "Image Control Corner Color",
|
|
415
|
+
default: "#ffffff",
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
id: "image.control.cornerStrokeColor",
|
|
419
|
+
type: "color",
|
|
420
|
+
label: "Image Control Corner Stroke Color",
|
|
421
|
+
default: "#1677ff",
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
id: "image.control.transparentCorners",
|
|
425
|
+
type: "boolean",
|
|
426
|
+
label: "Image Control Transparent Corners",
|
|
427
|
+
default: false,
|
|
428
|
+
},
|
|
429
|
+
{
|
|
430
|
+
id: "image.control.borderColor",
|
|
431
|
+
type: "color",
|
|
432
|
+
label: "Image Control Border Color",
|
|
433
|
+
default: "#1677ff",
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
id: "image.control.borderScaleFactor",
|
|
437
|
+
type: "number",
|
|
438
|
+
label: "Image Control Border Width",
|
|
439
|
+
min: 0.5,
|
|
440
|
+
max: 8,
|
|
441
|
+
step: 0.1,
|
|
442
|
+
default: 1.5,
|
|
443
|
+
},
|
|
444
|
+
{
|
|
445
|
+
id: "image.control.padding",
|
|
446
|
+
type: "number",
|
|
447
|
+
label: "Image Control Padding",
|
|
448
|
+
min: 0,
|
|
449
|
+
max: 64,
|
|
450
|
+
step: 1,
|
|
451
|
+
default: 0,
|
|
452
|
+
},
|
|
453
|
+
{
|
|
454
|
+
id: "image.frame.strokeColor",
|
|
455
|
+
type: "color",
|
|
456
|
+
label: "Image Frame Stroke Color",
|
|
457
|
+
default: "#808080",
|
|
458
|
+
},
|
|
459
|
+
{
|
|
460
|
+
id: "image.frame.strokeWidth",
|
|
461
|
+
type: "number",
|
|
462
|
+
label: "Image Frame Stroke Width",
|
|
463
|
+
min: 0,
|
|
464
|
+
max: 20,
|
|
465
|
+
step: 0.5,
|
|
466
|
+
default: 2,
|
|
467
|
+
},
|
|
468
|
+
{
|
|
469
|
+
id: "image.frame.strokeStyle",
|
|
470
|
+
type: "select",
|
|
471
|
+
label: "Image Frame Stroke Style",
|
|
472
|
+
options: ["solid", "dashed", "hidden"],
|
|
473
|
+
default: "dashed",
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
id: "image.frame.dashLength",
|
|
477
|
+
type: "number",
|
|
478
|
+
label: "Image Frame Dash Length",
|
|
479
|
+
min: 1,
|
|
480
|
+
max: 40,
|
|
481
|
+
step: 1,
|
|
482
|
+
default: 8,
|
|
483
|
+
},
|
|
484
|
+
{
|
|
485
|
+
id: "image.frame.innerBackground",
|
|
486
|
+
type: "color",
|
|
487
|
+
label: "Image Frame Inner Background",
|
|
488
|
+
default: "rgba(0,0,0,0)",
|
|
489
|
+
},
|
|
490
|
+
{
|
|
491
|
+
id: "image.frame.outerBackground",
|
|
492
|
+
type: "color",
|
|
493
|
+
label: "Image Frame Outer Background",
|
|
494
|
+
default: "#f5f5f5",
|
|
495
|
+
},
|
|
496
|
+
],
|
|
497
|
+
[core_1.ContributionPointIds.COMMANDS]: [
|
|
498
|
+
{
|
|
499
|
+
command: "addImage",
|
|
500
|
+
title: "Add Image",
|
|
501
|
+
handler: async (url, options) => {
|
|
502
|
+
const result = await this.upsertImageEntry(url, {
|
|
503
|
+
mode: "add",
|
|
504
|
+
addOptions: options,
|
|
505
|
+
});
|
|
506
|
+
return result.id;
|
|
507
|
+
},
|
|
508
|
+
},
|
|
509
|
+
{
|
|
510
|
+
command: "upsertImage",
|
|
511
|
+
title: "Upsert Image",
|
|
512
|
+
handler: async (url, options = {}) => {
|
|
513
|
+
return await this.upsertImageEntry(url, options);
|
|
514
|
+
},
|
|
515
|
+
},
|
|
516
|
+
{
|
|
517
|
+
command: "getWorkingImages",
|
|
518
|
+
title: "Get Working Images",
|
|
519
|
+
handler: () => {
|
|
520
|
+
return this.cloneItems(this.workingItems);
|
|
521
|
+
},
|
|
522
|
+
},
|
|
523
|
+
{
|
|
524
|
+
command: "setWorkingImage",
|
|
525
|
+
title: "Set Working Image",
|
|
526
|
+
handler: (id, updates) => {
|
|
527
|
+
this.updateImageInWorking(id, updates);
|
|
528
|
+
},
|
|
529
|
+
},
|
|
530
|
+
{
|
|
531
|
+
command: "resetWorkingImages",
|
|
532
|
+
title: "Reset Working Images",
|
|
533
|
+
handler: () => {
|
|
534
|
+
this.workingItems = this.cloneItems(this.items);
|
|
535
|
+
this.hasWorkingChanges = false;
|
|
536
|
+
this.updateImages();
|
|
537
|
+
this.emitWorkingChange();
|
|
538
|
+
},
|
|
539
|
+
},
|
|
540
|
+
{
|
|
541
|
+
command: "completeImages",
|
|
542
|
+
title: "Complete Images",
|
|
543
|
+
handler: async () => {
|
|
544
|
+
return await this.commitWorkingImagesAsCropped();
|
|
545
|
+
},
|
|
546
|
+
},
|
|
547
|
+
{
|
|
548
|
+
command: "exportUserCroppedImage",
|
|
549
|
+
title: "Export User Cropped Image",
|
|
550
|
+
handler: async (options = {}) => {
|
|
551
|
+
return await this.exportUserCroppedImage(options);
|
|
552
|
+
},
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
command: "fitImageToArea",
|
|
556
|
+
title: "Fit Image to Area",
|
|
557
|
+
handler: async (id, area) => {
|
|
558
|
+
await this.fitImageToArea(id, area);
|
|
559
|
+
},
|
|
560
|
+
},
|
|
561
|
+
{
|
|
562
|
+
command: "fitImageToDefaultArea",
|
|
563
|
+
title: "Fit Image to Default Area",
|
|
564
|
+
handler: async (id) => {
|
|
565
|
+
await this.fitImageToDefaultArea(id);
|
|
566
|
+
},
|
|
567
|
+
},
|
|
568
|
+
{
|
|
569
|
+
command: "focusImage",
|
|
570
|
+
title: "Focus Image",
|
|
571
|
+
handler: (id, options = {}) => {
|
|
572
|
+
return this.setImageFocus(id, options);
|
|
573
|
+
},
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
command: "removeImage",
|
|
577
|
+
title: "Remove Image",
|
|
578
|
+
handler: (id) => {
|
|
579
|
+
const removed = this.items.find((item) => item.id === id);
|
|
580
|
+
const next = this.items.filter((item) => item.id !== id);
|
|
581
|
+
if (next.length !== this.items.length) {
|
|
582
|
+
this.purgeSourceSizeCacheForItem(removed);
|
|
583
|
+
if (this.focusedImageId === id) {
|
|
584
|
+
this.setImageFocus(null, {
|
|
585
|
+
syncCanvasSelection: true,
|
|
586
|
+
skipRender: true,
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
this.updateConfig(next);
|
|
590
|
+
}
|
|
591
|
+
},
|
|
592
|
+
},
|
|
593
|
+
{
|
|
594
|
+
command: "updateImage",
|
|
595
|
+
title: "Update Image",
|
|
596
|
+
handler: async (id, updates, options = {}) => {
|
|
597
|
+
await this.updateImage(id, updates, options);
|
|
598
|
+
},
|
|
599
|
+
},
|
|
600
|
+
{
|
|
601
|
+
command: "clearImages",
|
|
602
|
+
title: "Clear Images",
|
|
603
|
+
handler: () => {
|
|
604
|
+
this.sourceSizeBySrc.clear();
|
|
605
|
+
this.setImageFocus(null, {
|
|
606
|
+
syncCanvasSelection: true,
|
|
607
|
+
skipRender: true,
|
|
608
|
+
});
|
|
609
|
+
this.updateConfig([]);
|
|
610
|
+
},
|
|
611
|
+
},
|
|
612
|
+
{
|
|
613
|
+
command: "bringToFront",
|
|
614
|
+
title: "Bring Image to Front",
|
|
615
|
+
handler: (id) => {
|
|
616
|
+
const index = this.items.findIndex((item) => item.id === id);
|
|
617
|
+
if (index !== -1 && index < this.items.length - 1) {
|
|
618
|
+
const next = [...this.items];
|
|
619
|
+
const [item] = next.splice(index, 1);
|
|
620
|
+
next.push(item);
|
|
621
|
+
this.updateConfig(next);
|
|
622
|
+
}
|
|
623
|
+
},
|
|
624
|
+
},
|
|
625
|
+
{
|
|
626
|
+
command: "sendToBack",
|
|
627
|
+
title: "Send Image to Back",
|
|
628
|
+
handler: (id) => {
|
|
629
|
+
const index = this.items.findIndex((item) => item.id === id);
|
|
630
|
+
if (index > 0) {
|
|
631
|
+
const next = [...this.items];
|
|
632
|
+
const [item] = next.splice(index, 1);
|
|
633
|
+
next.unshift(item);
|
|
634
|
+
this.updateConfig(next);
|
|
635
|
+
}
|
|
636
|
+
},
|
|
637
|
+
},
|
|
638
|
+
],
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
normalizeItem(item) {
|
|
642
|
+
const url = typeof item.url === "string" ? item.url : "";
|
|
643
|
+
const sourceUrl = typeof item.sourceUrl === "string" && item.sourceUrl.length > 0
|
|
644
|
+
? item.sourceUrl
|
|
645
|
+
: url;
|
|
646
|
+
const committedUrl = typeof item.committedUrl === "string" && item.committedUrl.length > 0
|
|
647
|
+
? item.committedUrl
|
|
648
|
+
: undefined;
|
|
649
|
+
return {
|
|
650
|
+
...item,
|
|
651
|
+
url: url || sourceUrl,
|
|
652
|
+
sourceUrl,
|
|
653
|
+
committedUrl,
|
|
654
|
+
opacity: Number.isFinite(item.opacity) ? item.opacity : 1,
|
|
655
|
+
scale: Number.isFinite(item.scale) ? item.scale : 1,
|
|
656
|
+
angle: Number.isFinite(item.angle) ? item.angle : 0,
|
|
657
|
+
left: Number.isFinite(item.left) ? item.left : 0.5,
|
|
658
|
+
top: Number.isFinite(item.top) ? item.top : 0.5,
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
normalizeItems(items) {
|
|
662
|
+
return (items || []).map((item) => this.normalizeItem(item));
|
|
663
|
+
}
|
|
664
|
+
cloneItems(items) {
|
|
665
|
+
return this.normalizeItems((items || []).map((i) => ({ ...i })));
|
|
666
|
+
}
|
|
667
|
+
emitWorkingChange(changedId = null) {
|
|
668
|
+
this.context?.eventBus.emit("image:working:change", {
|
|
669
|
+
changedId,
|
|
670
|
+
items: this.cloneItems(this.workingItems),
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
generateId() {
|
|
674
|
+
return Math.random().toString(36).substring(2, 9);
|
|
675
|
+
}
|
|
676
|
+
hasImageItem(id) {
|
|
677
|
+
return (this.items.some((item) => item.id === id) ||
|
|
678
|
+
this.workingItems.some((item) => item.id === id));
|
|
679
|
+
}
|
|
680
|
+
setImageFocus(id, options = {}) {
|
|
681
|
+
const syncCanvasSelection = options.syncCanvasSelection !== false;
|
|
682
|
+
if (id && !this.hasImageItem(id)) {
|
|
683
|
+
return { ok: false, reason: "image-not-found" };
|
|
684
|
+
}
|
|
685
|
+
this.focusedImageId = id;
|
|
686
|
+
this.isImageSelectionActive = !!id;
|
|
687
|
+
if (syncCanvasSelection && this.canvasService) {
|
|
688
|
+
const canvas = this.canvasService.canvas;
|
|
689
|
+
if (!id) {
|
|
690
|
+
canvas.discardActiveObject();
|
|
691
|
+
}
|
|
692
|
+
else {
|
|
693
|
+
const obj = this.getImageObject(id);
|
|
694
|
+
if (obj) {
|
|
695
|
+
this.applyImageObjectInteractionState(obj);
|
|
696
|
+
canvas.setActiveObject(obj);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
this.canvasService.requestRenderAll();
|
|
700
|
+
}
|
|
701
|
+
if (!options.skipRender) {
|
|
702
|
+
this.updateImages();
|
|
703
|
+
}
|
|
704
|
+
return { ok: true, id };
|
|
705
|
+
}
|
|
706
|
+
async addImageEntry(url, options, fitOnAdd = true) {
|
|
707
|
+
const id = this.generateId();
|
|
708
|
+
const newItem = this.normalizeItem({
|
|
709
|
+
id,
|
|
710
|
+
url,
|
|
711
|
+
opacity: 1,
|
|
712
|
+
...options,
|
|
713
|
+
});
|
|
714
|
+
const sessionDirtyBeforeAdd = this.isToolActive && this.hasWorkingChanges;
|
|
715
|
+
const waitLoaded = this.waitImageLoaded(id, true);
|
|
716
|
+
this.updateConfig([...this.items, newItem]);
|
|
717
|
+
this.addItemToWorkingSessionIfNeeded(newItem, sessionDirtyBeforeAdd);
|
|
718
|
+
const loaded = await waitLoaded;
|
|
719
|
+
if (loaded && fitOnAdd) {
|
|
720
|
+
await this.fitImageToDefaultArea(id);
|
|
721
|
+
}
|
|
722
|
+
if (loaded) {
|
|
723
|
+
this.setImageFocus(id);
|
|
724
|
+
}
|
|
725
|
+
return id;
|
|
726
|
+
}
|
|
727
|
+
async upsertImageEntry(url, options = {}) {
|
|
728
|
+
const mode = options.mode || (options.id ? "replace" : "add");
|
|
729
|
+
const fitOnAdd = options.fitOnAdd !== false;
|
|
730
|
+
if (mode === "replace") {
|
|
731
|
+
if (!options.id) {
|
|
732
|
+
throw new Error("replace-target-id-required");
|
|
733
|
+
}
|
|
734
|
+
const targetId = options.id;
|
|
735
|
+
if (!this.hasImageItem(targetId)) {
|
|
736
|
+
throw new Error("replace-target-not-found");
|
|
737
|
+
}
|
|
738
|
+
await this.updateImageInConfig(targetId, { url });
|
|
739
|
+
return { id: targetId, mode: "replace" };
|
|
740
|
+
}
|
|
741
|
+
const id = await this.addImageEntry(url, options.addOptions, fitOnAdd);
|
|
742
|
+
return { id, mode: "add" };
|
|
743
|
+
}
|
|
744
|
+
addItemToWorkingSessionIfNeeded(item, sessionDirtyBeforeAdd) {
|
|
745
|
+
if (!sessionDirtyBeforeAdd || !this.isToolActive)
|
|
746
|
+
return;
|
|
747
|
+
if (this.workingItems.some((existing) => existing.id === item.id))
|
|
748
|
+
return;
|
|
749
|
+
this.workingItems = this.cloneItems([...this.workingItems, item]);
|
|
750
|
+
this.updateImages();
|
|
751
|
+
this.emitWorkingChange(item.id);
|
|
752
|
+
}
|
|
753
|
+
async updateImage(id, updates, options = {}) {
|
|
754
|
+
this.syncToolActiveFromWorkbench();
|
|
755
|
+
const target = options.target || "auto";
|
|
756
|
+
if (target === "working" || (target === "auto" && this.isToolActive)) {
|
|
757
|
+
this.updateImageInWorking(id, updates);
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
await this.updateImageInConfig(id, updates);
|
|
761
|
+
}
|
|
762
|
+
getConfig(key, fallback) {
|
|
763
|
+
if (!this.context)
|
|
764
|
+
return fallback;
|
|
765
|
+
const configService = this.context.services.get("ConfigurationService");
|
|
766
|
+
if (!configService)
|
|
767
|
+
return fallback;
|
|
768
|
+
return configService.get(key, fallback) ?? fallback;
|
|
769
|
+
}
|
|
770
|
+
updateConfig(newItems, skipCanvasUpdate = false) {
|
|
771
|
+
if (!this.context)
|
|
772
|
+
return;
|
|
773
|
+
this.isUpdatingConfig = true;
|
|
774
|
+
this.items = this.normalizeItems(newItems);
|
|
775
|
+
if (!this.isToolActive || !this.hasWorkingChanges) {
|
|
776
|
+
this.workingItems = this.cloneItems(this.items);
|
|
777
|
+
this.hasWorkingChanges = false;
|
|
778
|
+
}
|
|
779
|
+
const configService = this.context.services.get("ConfigurationService");
|
|
780
|
+
configService?.update("image.items", this.items);
|
|
781
|
+
if (!skipCanvasUpdate) {
|
|
782
|
+
this.updateImages();
|
|
783
|
+
}
|
|
784
|
+
setTimeout(() => {
|
|
785
|
+
this.isUpdatingConfig = false;
|
|
786
|
+
}, 50);
|
|
787
|
+
}
|
|
788
|
+
getFrameRect() {
|
|
789
|
+
if (!this.canvasService) {
|
|
790
|
+
return { left: 0, top: 0, width: 0, height: 0 };
|
|
791
|
+
}
|
|
792
|
+
const configService = this.context?.services.get("ConfigurationService");
|
|
793
|
+
if (!configService) {
|
|
794
|
+
return { left: 0, top: 0, width: 0, height: 0 };
|
|
795
|
+
}
|
|
796
|
+
const sizeState = (0, sceneLayoutModel_1.readSizeState)(configService);
|
|
797
|
+
const layout = (0, sceneLayoutModel_1.computeSceneLayout)(this.canvasService, sizeState);
|
|
798
|
+
if (!layout) {
|
|
799
|
+
return { left: 0, top: 0, width: 0, height: 0 };
|
|
800
|
+
}
|
|
801
|
+
return this.canvasService.toSceneRect({
|
|
802
|
+
left: layout.cutRect.left,
|
|
803
|
+
top: layout.cutRect.top,
|
|
804
|
+
width: layout.cutRect.width,
|
|
805
|
+
height: layout.cutRect.height,
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
getFrameRectScreen(frame) {
|
|
809
|
+
if (!this.canvasService) {
|
|
810
|
+
return { left: 0, top: 0, width: 0, height: 0 };
|
|
811
|
+
}
|
|
812
|
+
return this.canvasService.toScreenRect(frame || this.getFrameRect());
|
|
813
|
+
}
|
|
814
|
+
toLayoutSceneRect(rect) {
|
|
815
|
+
return {
|
|
816
|
+
left: rect.left,
|
|
817
|
+
top: rect.top,
|
|
818
|
+
width: rect.width,
|
|
819
|
+
height: rect.height,
|
|
820
|
+
space: "scene",
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
async resolveDefaultFitArea() {
|
|
824
|
+
if (!this.canvasService)
|
|
825
|
+
return null;
|
|
826
|
+
const frame = this.getFrameRect();
|
|
827
|
+
if (frame.width <= 0 || frame.height <= 0)
|
|
828
|
+
return null;
|
|
829
|
+
return {
|
|
830
|
+
width: Math.max(1, frame.width),
|
|
831
|
+
height: Math.max(1, frame.height),
|
|
832
|
+
left: frame.left + frame.width / 2,
|
|
833
|
+
top: frame.top + frame.height / 2,
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
async fitImageToDefaultArea(id) {
|
|
837
|
+
if (!this.canvasService)
|
|
838
|
+
return;
|
|
839
|
+
const area = await this.resolveDefaultFitArea();
|
|
840
|
+
if (area) {
|
|
841
|
+
await this.fitImageToArea(id, area);
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
const viewport = this.canvasService.getSceneViewportRect();
|
|
845
|
+
const canvasW = Math.max(1, viewport.width || 0);
|
|
846
|
+
const canvasH = Math.max(1, viewport.height || 0);
|
|
847
|
+
await this.fitImageToArea(id, {
|
|
848
|
+
width: canvasW,
|
|
849
|
+
height: canvasH,
|
|
850
|
+
left: viewport.left + canvasW / 2,
|
|
851
|
+
top: viewport.top + canvasH / 2,
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
getImageObjects() {
|
|
855
|
+
if (!this.canvasService)
|
|
856
|
+
return [];
|
|
857
|
+
return this.canvasService.canvas.getObjects().filter((obj) => {
|
|
858
|
+
return obj?.data?.layerId === IMAGE_OBJECT_LAYER_ID;
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
getOverlayObjects() {
|
|
862
|
+
if (!this.canvasService)
|
|
863
|
+
return [];
|
|
864
|
+
return this.canvasService.getPassObjects(IMAGE_OVERLAY_LAYER_ID);
|
|
865
|
+
}
|
|
866
|
+
getImageObject(id) {
|
|
867
|
+
return this.getImageObjects().find((obj) => obj?.data?.id === id);
|
|
868
|
+
}
|
|
869
|
+
clearRenderedImages() {
|
|
870
|
+
if (!this.canvasService)
|
|
871
|
+
return;
|
|
872
|
+
this.imageSpecs = [];
|
|
873
|
+
this.overlaySpecs = [];
|
|
874
|
+
this.canvasService.requestRenderFromProducers();
|
|
875
|
+
}
|
|
876
|
+
purgeSourceSizeCacheForItem(item) {
|
|
877
|
+
if (!item)
|
|
878
|
+
return;
|
|
879
|
+
const sources = [item.url, item.sourceUrl, item.committedUrl].filter((value) => typeof value === "string" && value.length > 0);
|
|
880
|
+
sources.forEach((src) => this.sourceSizeBySrc.delete(src));
|
|
881
|
+
}
|
|
882
|
+
rememberSourceSize(src, obj) {
|
|
883
|
+
const width = Number(obj?.width || 0);
|
|
884
|
+
const height = Number(obj?.height || 0);
|
|
885
|
+
if (src && width > 0 && height > 0) {
|
|
886
|
+
this.sourceSizeBySrc.set(src, { width, height });
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
getSourceSize(src, obj) {
|
|
890
|
+
const cached = src ? this.sourceSizeBySrc.get(src) : undefined;
|
|
891
|
+
if (cached)
|
|
892
|
+
return cached;
|
|
893
|
+
const width = Number(obj?.width || 0);
|
|
894
|
+
const height = Number(obj?.height || 0);
|
|
895
|
+
if (src && width > 0 && height > 0) {
|
|
896
|
+
const size = { width, height };
|
|
897
|
+
this.sourceSizeBySrc.set(src, size);
|
|
898
|
+
return size;
|
|
899
|
+
}
|
|
900
|
+
return { width: 1, height: 1 };
|
|
901
|
+
}
|
|
902
|
+
async ensureSourceSize(src) {
|
|
903
|
+
if (!src)
|
|
904
|
+
return null;
|
|
905
|
+
const cached = this.sourceSizeBySrc.get(src);
|
|
906
|
+
if (cached)
|
|
907
|
+
return cached;
|
|
908
|
+
try {
|
|
909
|
+
const image = await fabric_1.Image.fromURL(src, {
|
|
910
|
+
crossOrigin: "anonymous",
|
|
911
|
+
});
|
|
912
|
+
const width = Number(image?.width || 0);
|
|
913
|
+
const height = Number(image?.height || 0);
|
|
914
|
+
if (width > 0 && height > 0) {
|
|
915
|
+
const size = { width, height };
|
|
916
|
+
this.sourceSizeBySrc.set(src, size);
|
|
917
|
+
return size;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
catch (error) {
|
|
921
|
+
this.debug("image:size:load-failed", {
|
|
922
|
+
src,
|
|
923
|
+
error: error instanceof Error ? error.message : String(error),
|
|
924
|
+
});
|
|
925
|
+
}
|
|
926
|
+
return null;
|
|
927
|
+
}
|
|
928
|
+
getCoverScale(frame, size) {
|
|
929
|
+
const sw = Math.max(1, size.width);
|
|
930
|
+
const sh = Math.max(1, size.height);
|
|
931
|
+
const fw = Math.max(1, frame.width);
|
|
932
|
+
const fh = Math.max(1, frame.height);
|
|
933
|
+
return Math.max(fw / sw, fh / sh);
|
|
934
|
+
}
|
|
935
|
+
getFrameVisualConfig() {
|
|
936
|
+
const strokeStyleRaw = (this.getConfig("image.frame.strokeStyle", "dashed") || "dashed");
|
|
937
|
+
const strokeStyle = strokeStyleRaw === "dashed" || strokeStyleRaw === "hidden"
|
|
938
|
+
? strokeStyleRaw
|
|
939
|
+
: "dashed";
|
|
940
|
+
const strokeWidth = Number(this.getConfig("image.frame.strokeWidth", 2) ?? 2);
|
|
941
|
+
const dashLength = Number(this.getConfig("image.frame.dashLength", 8) ?? 8);
|
|
942
|
+
return {
|
|
943
|
+
strokeColor: this.getConfig("image.frame.strokeColor", "#808080") ||
|
|
944
|
+
"#808080",
|
|
945
|
+
strokeWidth: Number.isFinite(strokeWidth) ? Math.max(0, strokeWidth) : 2,
|
|
946
|
+
strokeStyle,
|
|
947
|
+
dashLength: Number.isFinite(dashLength) ? Math.max(1, dashLength) : 8,
|
|
948
|
+
innerBackground: this.getConfig("image.frame.innerBackground", "rgba(0,0,0,0)") || "rgba(0,0,0,0)",
|
|
949
|
+
outerBackground: this.getConfig("image.frame.outerBackground", "#f5f5f5") ||
|
|
950
|
+
"#f5f5f5",
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
toSceneGeometryLike(raw) {
|
|
954
|
+
const shape = raw?.shape;
|
|
955
|
+
if (!(0, dielineShape_1.isDielineShape)(shape)) {
|
|
956
|
+
return null;
|
|
957
|
+
}
|
|
958
|
+
const radiusRaw = Number(raw?.radius);
|
|
959
|
+
const offsetRaw = Number(raw?.offset);
|
|
960
|
+
const unit = typeof raw?.unit === "string" ? raw.unit : "px";
|
|
961
|
+
const radius = unit === "scene" || !this.canvasService
|
|
962
|
+
? radiusRaw
|
|
963
|
+
: this.canvasService.toSceneLength(radiusRaw);
|
|
964
|
+
const offset = unit === "scene" || !this.canvasService
|
|
965
|
+
? offsetRaw
|
|
966
|
+
: this.canvasService.toSceneLength(offsetRaw);
|
|
967
|
+
return {
|
|
968
|
+
shape,
|
|
969
|
+
shapeStyle: (0, dielineShape_1.normalizeShapeStyle)(raw?.shapeStyle),
|
|
970
|
+
radius: Number.isFinite(radius) ? radius : 0,
|
|
971
|
+
offset: Number.isFinite(offset) ? offset : 0,
|
|
972
|
+
};
|
|
973
|
+
}
|
|
974
|
+
async resolveSceneGeometryForOverlay() {
|
|
975
|
+
if (!this.context)
|
|
976
|
+
return null;
|
|
977
|
+
const commandService = this.context.services.get("CommandService");
|
|
978
|
+
if (commandService) {
|
|
979
|
+
try {
|
|
980
|
+
const raw = await Promise.resolve(commandService.executeCommand("getSceneGeometry"));
|
|
981
|
+
const geometry = this.toSceneGeometryLike(raw);
|
|
982
|
+
if (geometry) {
|
|
983
|
+
this.debug("overlay:sceneGeometry:command", geometry);
|
|
984
|
+
return geometry;
|
|
985
|
+
}
|
|
986
|
+
this.debug("overlay:sceneGeometry:command:invalid", { raw });
|
|
987
|
+
}
|
|
988
|
+
catch (error) {
|
|
989
|
+
this.debug("overlay:sceneGeometry:command:error", {
|
|
990
|
+
error: error instanceof Error ? error.message : String(error),
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
if (!this.canvasService)
|
|
995
|
+
return null;
|
|
996
|
+
const configService = this.context.services.get("ConfigurationService");
|
|
997
|
+
if (!configService)
|
|
998
|
+
return null;
|
|
999
|
+
const sizeState = (0, sceneLayoutModel_1.readSizeState)(configService);
|
|
1000
|
+
const layout = (0, sceneLayoutModel_1.computeSceneLayout)(this.canvasService, sizeState);
|
|
1001
|
+
if (!layout) {
|
|
1002
|
+
this.debug("overlay:sceneGeometry:fallback:missing-layout");
|
|
1003
|
+
return null;
|
|
1004
|
+
}
|
|
1005
|
+
const geometry = this.toSceneGeometryLike((0, sceneLayoutModel_1.buildSceneGeometry)(configService, layout));
|
|
1006
|
+
if (geometry) {
|
|
1007
|
+
this.debug("overlay:sceneGeometry:fallback", geometry);
|
|
1008
|
+
}
|
|
1009
|
+
return geometry;
|
|
1010
|
+
}
|
|
1011
|
+
resolveCutShapeRadius(geometry, frame) {
|
|
1012
|
+
const visualRadius = Number.isFinite(geometry.radius)
|
|
1013
|
+
? Math.max(0, geometry.radius)
|
|
1014
|
+
: 0;
|
|
1015
|
+
const visualOffset = Number.isFinite(geometry.offset) ? geometry.offset : 0;
|
|
1016
|
+
const rawCutRadius = visualRadius === 0 ? 0 : Math.max(0, visualRadius + visualOffset);
|
|
1017
|
+
const maxRadius = Math.max(0, Math.min(frame.width, frame.height) / 2);
|
|
1018
|
+
return Math.max(0, Math.min(maxRadius, rawCutRadius));
|
|
1019
|
+
}
|
|
1020
|
+
getCropShapeHatchPattern(color = "rgba(255, 0, 0, 0.6)") {
|
|
1021
|
+
if (typeof document === "undefined")
|
|
1022
|
+
return undefined;
|
|
1023
|
+
const sceneScale = this.canvasService?.getSceneScale() || 1;
|
|
1024
|
+
const cacheKey = `${color}::${sceneScale.toFixed(6)}`;
|
|
1025
|
+
if (this.cropShapeHatchPattern &&
|
|
1026
|
+
this.cropShapeHatchPatternColor === color &&
|
|
1027
|
+
this.cropShapeHatchPatternKey === cacheKey) {
|
|
1028
|
+
return this.cropShapeHatchPattern;
|
|
1029
|
+
}
|
|
1030
|
+
const size = 16;
|
|
1031
|
+
const patternCanvas = document.createElement("canvas");
|
|
1032
|
+
patternCanvas.width = size;
|
|
1033
|
+
patternCanvas.height = size;
|
|
1034
|
+
const ctx = patternCanvas.getContext("2d");
|
|
1035
|
+
if (!ctx)
|
|
1036
|
+
return undefined;
|
|
1037
|
+
ctx.clearRect(0, 0, size, size);
|
|
1038
|
+
ctx.fillStyle = "rgba(255, 0, 0, 0.08)";
|
|
1039
|
+
ctx.fillRect(0, 0, size, size);
|
|
1040
|
+
ctx.strokeStyle = color;
|
|
1041
|
+
ctx.lineWidth = 1.5;
|
|
1042
|
+
ctx.beginPath();
|
|
1043
|
+
ctx.moveTo(-size, size);
|
|
1044
|
+
ctx.lineTo(size, -size);
|
|
1045
|
+
ctx.moveTo(-size / 2, size + size / 2);
|
|
1046
|
+
ctx.lineTo(size + size / 2, -size / 2);
|
|
1047
|
+
ctx.moveTo(0, size);
|
|
1048
|
+
ctx.lineTo(size, 0);
|
|
1049
|
+
ctx.moveTo(size / 2, size + size / 2);
|
|
1050
|
+
ctx.lineTo(size + size + size / 2, -size / 2);
|
|
1051
|
+
ctx.stroke();
|
|
1052
|
+
const pattern = new fabric_1.Pattern({
|
|
1053
|
+
source: patternCanvas,
|
|
1054
|
+
// @ts-ignore: Fabric Pattern accepts canvas source here.
|
|
1055
|
+
repetition: "repeat",
|
|
1056
|
+
});
|
|
1057
|
+
// Scene specs are scaled to screen by CanvasService; keep hatch density in screen pixels.
|
|
1058
|
+
pattern.patternTransform = [
|
|
1059
|
+
1 / sceneScale,
|
|
1060
|
+
0,
|
|
1061
|
+
0,
|
|
1062
|
+
1 / sceneScale,
|
|
1063
|
+
0,
|
|
1064
|
+
0,
|
|
1065
|
+
];
|
|
1066
|
+
this.cropShapeHatchPattern = pattern;
|
|
1067
|
+
this.cropShapeHatchPatternColor = color;
|
|
1068
|
+
this.cropShapeHatchPatternKey = cacheKey;
|
|
1069
|
+
return pattern;
|
|
1070
|
+
}
|
|
1071
|
+
buildCropShapeOverlaySpecs(frame, sceneGeometry) {
|
|
1072
|
+
if (!sceneGeometry) {
|
|
1073
|
+
this.debug("overlay:shape:skip", { reason: "scene-geometry-missing" });
|
|
1074
|
+
return [];
|
|
1075
|
+
}
|
|
1076
|
+
if (sceneGeometry.shape === "custom") {
|
|
1077
|
+
this.debug("overlay:shape:skip", { reason: "shape-custom" });
|
|
1078
|
+
return [];
|
|
1079
|
+
}
|
|
1080
|
+
const shape = sceneGeometry.shape;
|
|
1081
|
+
const shapeStyle = sceneGeometry.shapeStyle;
|
|
1082
|
+
const inset = 0;
|
|
1083
|
+
const shapeWidth = Math.max(1, frame.width);
|
|
1084
|
+
const shapeHeight = Math.max(1, frame.height);
|
|
1085
|
+
const radius = this.resolveCutShapeRadius(sceneGeometry, frame);
|
|
1086
|
+
this.debug("overlay:shape:geometry", {
|
|
1087
|
+
shape,
|
|
1088
|
+
frameWidth: frame.width,
|
|
1089
|
+
frameHeight: frame.height,
|
|
1090
|
+
offset: sceneGeometry.offset,
|
|
1091
|
+
shapeStyle,
|
|
1092
|
+
inset,
|
|
1093
|
+
shapeWidth,
|
|
1094
|
+
shapeHeight,
|
|
1095
|
+
baseRadius: sceneGeometry.radius,
|
|
1096
|
+
radius,
|
|
1097
|
+
});
|
|
1098
|
+
const isSameAsFrame = Math.abs(shapeWidth - frame.width) <= 0.0001 &&
|
|
1099
|
+
Math.abs(shapeHeight - frame.height) <= 0.0001;
|
|
1100
|
+
if (shape === "rect" && radius <= 0.0001 && isSameAsFrame) {
|
|
1101
|
+
this.debug("overlay:shape:skip", {
|
|
1102
|
+
reason: "shape-rect-no-radius",
|
|
1103
|
+
});
|
|
1104
|
+
return [];
|
|
1105
|
+
}
|
|
1106
|
+
const baseOptions = {
|
|
1107
|
+
shape,
|
|
1108
|
+
width: shapeWidth,
|
|
1109
|
+
height: shapeHeight,
|
|
1110
|
+
radius,
|
|
1111
|
+
x: frame.width / 2,
|
|
1112
|
+
y: frame.height / 2,
|
|
1113
|
+
features: [],
|
|
1114
|
+
shapeStyle,
|
|
1115
|
+
canvasWidth: frame.width,
|
|
1116
|
+
canvasHeight: frame.height,
|
|
1117
|
+
};
|
|
1118
|
+
try {
|
|
1119
|
+
const shapePathData = (0, geometry_1.generateDielinePath)(baseOptions);
|
|
1120
|
+
const outerRectPathData = `M 0 0 L ${frame.width} 0 L ${frame.width} ${frame.height} L 0 ${frame.height} Z`;
|
|
1121
|
+
const hatchPathData = `${outerRectPathData} ${shapePathData}`;
|
|
1122
|
+
if (!shapePathData || !hatchPathData) {
|
|
1123
|
+
this.debug("overlay:shape:skip", {
|
|
1124
|
+
reason: "path-generation-empty",
|
|
1125
|
+
shape,
|
|
1126
|
+
radius,
|
|
1127
|
+
});
|
|
1128
|
+
return [];
|
|
1129
|
+
}
|
|
1130
|
+
const patternFill = this.getCropShapeHatchPattern();
|
|
1131
|
+
const hatchFill = patternFill || "rgba(255, 0, 0, 0.22)";
|
|
1132
|
+
const shapeBounds = (0, geometry_1.getPathBounds)(shapePathData);
|
|
1133
|
+
const hatchBounds = (0, geometry_1.getPathBounds)(hatchPathData);
|
|
1134
|
+
const frameRect = this.toLayoutSceneRect(frame);
|
|
1135
|
+
const hatchPathLength = hatchPathData.length;
|
|
1136
|
+
const shapePathLength = shapePathData.length;
|
|
1137
|
+
const specs = [
|
|
1138
|
+
{
|
|
1139
|
+
id: "image.cropShapeHatch",
|
|
1140
|
+
type: "path",
|
|
1141
|
+
data: { id: "image.cropShapeHatch", zIndex: 5 },
|
|
1142
|
+
layout: {
|
|
1143
|
+
reference: "custom",
|
|
1144
|
+
referenceRect: frameRect,
|
|
1145
|
+
alignX: "start",
|
|
1146
|
+
alignY: "start",
|
|
1147
|
+
offsetX: hatchBounds.x,
|
|
1148
|
+
offsetY: hatchBounds.y,
|
|
1149
|
+
},
|
|
1150
|
+
props: {
|
|
1151
|
+
pathData: hatchPathData,
|
|
1152
|
+
originX: "left",
|
|
1153
|
+
originY: "top",
|
|
1154
|
+
fill: hatchFill,
|
|
1155
|
+
opacity: patternFill ? 1 : 0.8,
|
|
1156
|
+
stroke: null,
|
|
1157
|
+
fillRule: "evenodd",
|
|
1158
|
+
selectable: false,
|
|
1159
|
+
evented: false,
|
|
1160
|
+
excludeFromExport: true,
|
|
1161
|
+
objectCaching: false,
|
|
1162
|
+
},
|
|
1163
|
+
},
|
|
1164
|
+
{
|
|
1165
|
+
id: "image.cropShapePath",
|
|
1166
|
+
type: "path",
|
|
1167
|
+
data: { id: "image.cropShapePath", zIndex: 6 },
|
|
1168
|
+
layout: {
|
|
1169
|
+
reference: "custom",
|
|
1170
|
+
referenceRect: frameRect,
|
|
1171
|
+
alignX: "start",
|
|
1172
|
+
alignY: "start",
|
|
1173
|
+
offsetX: shapeBounds.x,
|
|
1174
|
+
offsetY: shapeBounds.y,
|
|
1175
|
+
},
|
|
1176
|
+
props: {
|
|
1177
|
+
pathData: shapePathData,
|
|
1178
|
+
originX: "left",
|
|
1179
|
+
originY: "top",
|
|
1180
|
+
fill: "rgba(0,0,0,0)",
|
|
1181
|
+
stroke: "rgba(255, 0, 0, 0.9)",
|
|
1182
|
+
strokeWidth: this.canvasService?.toSceneLength(1) ?? 1,
|
|
1183
|
+
selectable: false,
|
|
1184
|
+
evented: false,
|
|
1185
|
+
excludeFromExport: true,
|
|
1186
|
+
objectCaching: false,
|
|
1187
|
+
},
|
|
1188
|
+
},
|
|
1189
|
+
];
|
|
1190
|
+
this.debug("overlay:shape:built", {
|
|
1191
|
+
shape,
|
|
1192
|
+
radius,
|
|
1193
|
+
inset,
|
|
1194
|
+
shapeWidth,
|
|
1195
|
+
shapeHeight,
|
|
1196
|
+
fillRule: "evenodd",
|
|
1197
|
+
shapePathLength,
|
|
1198
|
+
hatchPathLength,
|
|
1199
|
+
shapeBounds,
|
|
1200
|
+
hatchBounds,
|
|
1201
|
+
hatchFillType: hatchFill && typeof hatchFill === "object" ? "pattern" : "color",
|
|
1202
|
+
ids: specs.map((spec) => spec.id),
|
|
1203
|
+
});
|
|
1204
|
+
return specs;
|
|
1205
|
+
}
|
|
1206
|
+
catch (error) {
|
|
1207
|
+
this.debug("overlay:shape:error", {
|
|
1208
|
+
shape,
|
|
1209
|
+
radius,
|
|
1210
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1211
|
+
});
|
|
1212
|
+
return [];
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
resolveRenderImageState(item) {
|
|
1216
|
+
const active = this.isToolActive;
|
|
1217
|
+
const sourceUrl = item.sourceUrl || item.url;
|
|
1218
|
+
const committedUrl = item.committedUrl;
|
|
1219
|
+
if (!active && committedUrl) {
|
|
1220
|
+
return {
|
|
1221
|
+
src: committedUrl,
|
|
1222
|
+
left: 0.5,
|
|
1223
|
+
top: 0.5,
|
|
1224
|
+
scale: 1,
|
|
1225
|
+
angle: 0,
|
|
1226
|
+
opacity: item.opacity,
|
|
1227
|
+
};
|
|
1228
|
+
}
|
|
1229
|
+
return {
|
|
1230
|
+
src: sourceUrl || item.url,
|
|
1231
|
+
left: Number.isFinite(item.left) ? item.left : 0.5,
|
|
1232
|
+
top: Number.isFinite(item.top) ? item.top : 0.5,
|
|
1233
|
+
scale: Math.max(0.05, item.scale ?? 1),
|
|
1234
|
+
angle: Number.isFinite(item.angle) ? item.angle : 0,
|
|
1235
|
+
opacity: item.opacity,
|
|
1236
|
+
};
|
|
1237
|
+
}
|
|
1238
|
+
computeCanvasProps(render, size, frame) {
|
|
1239
|
+
const left = render.left;
|
|
1240
|
+
const top = render.top;
|
|
1241
|
+
const zoom = render.scale;
|
|
1242
|
+
const angle = render.angle;
|
|
1243
|
+
const centerX = frame.left + left * frame.width;
|
|
1244
|
+
const centerY = frame.top + top * frame.height;
|
|
1245
|
+
const scale = this.getCoverScale(frame, size) * zoom;
|
|
1246
|
+
return {
|
|
1247
|
+
left: centerX,
|
|
1248
|
+
top: centerY,
|
|
1249
|
+
scaleX: scale,
|
|
1250
|
+
scaleY: scale,
|
|
1251
|
+
angle,
|
|
1252
|
+
originX: "center",
|
|
1253
|
+
originY: "center",
|
|
1254
|
+
uniformScaling: true,
|
|
1255
|
+
lockScalingFlip: true,
|
|
1256
|
+
selectable: this.isImageEditingVisible(),
|
|
1257
|
+
evented: this.isImageEditingVisible(),
|
|
1258
|
+
hasControls: this.isImageEditingVisible(),
|
|
1259
|
+
hasBorders: this.isImageEditingVisible(),
|
|
1260
|
+
opacity: render.opacity,
|
|
1261
|
+
};
|
|
1262
|
+
}
|
|
1263
|
+
toSceneObjectScale(value) {
|
|
1264
|
+
if (!this.canvasService)
|
|
1265
|
+
return value;
|
|
1266
|
+
return value / this.canvasService.getSceneScale();
|
|
1267
|
+
}
|
|
1268
|
+
getCurrentSrc(obj) {
|
|
1269
|
+
if (!obj)
|
|
1270
|
+
return undefined;
|
|
1271
|
+
if (typeof obj.getSrc === "function")
|
|
1272
|
+
return obj.getSrc();
|
|
1273
|
+
return obj?._originalElement?.src;
|
|
1274
|
+
}
|
|
1275
|
+
async buildImageSpecs(items, frame) {
|
|
1276
|
+
const specs = [];
|
|
1277
|
+
for (const item of items) {
|
|
1278
|
+
const render = this.resolveRenderImageState(item);
|
|
1279
|
+
if (!render.src)
|
|
1280
|
+
continue;
|
|
1281
|
+
const ensured = await this.ensureSourceSize(render.src);
|
|
1282
|
+
const sourceSize = ensured || this.getSourceSize(render.src);
|
|
1283
|
+
const props = this.computeCanvasProps(render, sourceSize, frame);
|
|
1284
|
+
specs.push({
|
|
1285
|
+
id: item.id,
|
|
1286
|
+
type: "image",
|
|
1287
|
+
src: render.src,
|
|
1288
|
+
data: {
|
|
1289
|
+
id: item.id,
|
|
1290
|
+
layerId: IMAGE_OBJECT_LAYER_ID,
|
|
1291
|
+
type: "image-item",
|
|
1292
|
+
},
|
|
1293
|
+
props,
|
|
1294
|
+
});
|
|
1295
|
+
}
|
|
1296
|
+
return specs;
|
|
1297
|
+
}
|
|
1298
|
+
buildOverlaySpecs(frame, sceneGeometry) {
|
|
1299
|
+
const visible = this.isImageEditingVisible();
|
|
1300
|
+
if (!visible ||
|
|
1301
|
+
frame.width <= 0 ||
|
|
1302
|
+
frame.height <= 0 ||
|
|
1303
|
+
!this.canvasService) {
|
|
1304
|
+
this.debug("overlay:hidden", {
|
|
1305
|
+
visible,
|
|
1306
|
+
frame,
|
|
1307
|
+
isToolActive: this.isToolActive,
|
|
1308
|
+
isImageSelectionActive: this.isImageSelectionActive,
|
|
1309
|
+
focusedImageId: this.focusedImageId,
|
|
1310
|
+
});
|
|
1311
|
+
return [];
|
|
1312
|
+
}
|
|
1313
|
+
const viewport = this.canvasService.getSceneViewportRect();
|
|
1314
|
+
const canvasW = viewport.width || 0;
|
|
1315
|
+
const canvasH = viewport.height || 0;
|
|
1316
|
+
const canvasLeft = viewport.left || 0;
|
|
1317
|
+
const canvasTop = viewport.top || 0;
|
|
1318
|
+
const visual = this.getFrameVisualConfig();
|
|
1319
|
+
const strokeWidthScene = this.canvasService.toSceneLength(visual.strokeWidth);
|
|
1320
|
+
const dashLengthScene = this.canvasService.toSceneLength(visual.dashLength);
|
|
1321
|
+
const frameLeft = Math.max(canvasLeft, Math.min(canvasLeft + canvasW, frame.left));
|
|
1322
|
+
const frameTop = Math.max(canvasTop, Math.min(canvasTop + canvasH, frame.top));
|
|
1323
|
+
const frameRight = Math.max(frameLeft, Math.min(canvasLeft + canvasW, frame.left + frame.width));
|
|
1324
|
+
const frameBottom = Math.max(frameTop, Math.min(canvasTop + canvasH, frame.top + frame.height));
|
|
1325
|
+
const visibleFrameH = Math.max(0, frameBottom - frameTop);
|
|
1326
|
+
const topH = Math.max(0, frameTop - canvasTop);
|
|
1327
|
+
const bottomH = Math.max(0, canvasTop + canvasH - frameBottom);
|
|
1328
|
+
const leftW = Math.max(0, frameLeft - canvasLeft);
|
|
1329
|
+
const rightW = Math.max(0, canvasLeft + canvasW - frameRight);
|
|
1330
|
+
const viewportRect = this.toLayoutSceneRect({
|
|
1331
|
+
left: canvasLeft,
|
|
1332
|
+
top: canvasTop,
|
|
1333
|
+
width: canvasW,
|
|
1334
|
+
height: canvasH,
|
|
1335
|
+
});
|
|
1336
|
+
const visibleFrameBandRect = this.toLayoutSceneRect({
|
|
1337
|
+
left: canvasLeft,
|
|
1338
|
+
top: frameTop,
|
|
1339
|
+
width: canvasW,
|
|
1340
|
+
height: visibleFrameH,
|
|
1341
|
+
});
|
|
1342
|
+
const frameRect = this.toLayoutSceneRect(frame);
|
|
1343
|
+
const shapeOverlay = this.buildCropShapeOverlaySpecs(frame, sceneGeometry);
|
|
1344
|
+
const mask = [
|
|
1345
|
+
{
|
|
1346
|
+
id: "image.cropMask.top",
|
|
1347
|
+
type: "rect",
|
|
1348
|
+
data: { id: "image.cropMask.top", zIndex: 1 },
|
|
1349
|
+
layout: {
|
|
1350
|
+
reference: "custom",
|
|
1351
|
+
referenceRect: viewportRect,
|
|
1352
|
+
alignX: "start",
|
|
1353
|
+
alignY: "start",
|
|
1354
|
+
width: "100%",
|
|
1355
|
+
height: topH,
|
|
1356
|
+
},
|
|
1357
|
+
props: {
|
|
1358
|
+
originX: "left",
|
|
1359
|
+
originY: "top",
|
|
1360
|
+
fill: visual.outerBackground,
|
|
1361
|
+
selectable: false,
|
|
1362
|
+
evented: false,
|
|
1363
|
+
},
|
|
1364
|
+
},
|
|
1365
|
+
{
|
|
1366
|
+
id: "image.cropMask.bottom",
|
|
1367
|
+
type: "rect",
|
|
1368
|
+
data: { id: "image.cropMask.bottom", zIndex: 2 },
|
|
1369
|
+
layout: {
|
|
1370
|
+
reference: "custom",
|
|
1371
|
+
referenceRect: viewportRect,
|
|
1372
|
+
alignX: "start",
|
|
1373
|
+
alignY: "end",
|
|
1374
|
+
width: "100%",
|
|
1375
|
+
height: bottomH,
|
|
1376
|
+
},
|
|
1377
|
+
props: {
|
|
1378
|
+
originX: "left",
|
|
1379
|
+
originY: "top",
|
|
1380
|
+
fill: visual.outerBackground,
|
|
1381
|
+
selectable: false,
|
|
1382
|
+
evented: false,
|
|
1383
|
+
},
|
|
1384
|
+
},
|
|
1385
|
+
{
|
|
1386
|
+
id: "image.cropMask.left",
|
|
1387
|
+
type: "rect",
|
|
1388
|
+
data: { id: "image.cropMask.left", zIndex: 3 },
|
|
1389
|
+
layout: {
|
|
1390
|
+
reference: "custom",
|
|
1391
|
+
referenceRect: visibleFrameBandRect,
|
|
1392
|
+
alignX: "start",
|
|
1393
|
+
alignY: "start",
|
|
1394
|
+
width: leftW,
|
|
1395
|
+
height: "100%",
|
|
1396
|
+
},
|
|
1397
|
+
props: {
|
|
1398
|
+
originX: "left",
|
|
1399
|
+
originY: "top",
|
|
1400
|
+
fill: visual.outerBackground,
|
|
1401
|
+
selectable: false,
|
|
1402
|
+
evented: false,
|
|
1403
|
+
},
|
|
1404
|
+
},
|
|
1405
|
+
{
|
|
1406
|
+
id: "image.cropMask.right",
|
|
1407
|
+
type: "rect",
|
|
1408
|
+
data: { id: "image.cropMask.right", zIndex: 4 },
|
|
1409
|
+
layout: {
|
|
1410
|
+
reference: "custom",
|
|
1411
|
+
referenceRect: visibleFrameBandRect,
|
|
1412
|
+
alignX: "end",
|
|
1413
|
+
alignY: "start",
|
|
1414
|
+
width: rightW,
|
|
1415
|
+
height: "100%",
|
|
1416
|
+
},
|
|
1417
|
+
props: {
|
|
1418
|
+
originX: "left",
|
|
1419
|
+
originY: "top",
|
|
1420
|
+
fill: visual.outerBackground,
|
|
1421
|
+
selectable: false,
|
|
1422
|
+
evented: false,
|
|
1423
|
+
},
|
|
1424
|
+
},
|
|
1425
|
+
];
|
|
1426
|
+
const frameSpec = {
|
|
1427
|
+
id: "image.cropFrame",
|
|
1428
|
+
type: "rect",
|
|
1429
|
+
data: { id: "image.cropFrame", zIndex: 7 },
|
|
1430
|
+
layout: {
|
|
1431
|
+
reference: "custom",
|
|
1432
|
+
referenceRect: frameRect,
|
|
1433
|
+
alignX: "start",
|
|
1434
|
+
alignY: "start",
|
|
1435
|
+
width: "100%",
|
|
1436
|
+
height: "100%",
|
|
1437
|
+
},
|
|
1438
|
+
props: {
|
|
1439
|
+
originX: "left",
|
|
1440
|
+
originY: "top",
|
|
1441
|
+
fill: visual.innerBackground,
|
|
1442
|
+
stroke: visual.strokeStyle === "hidden"
|
|
1443
|
+
? "rgba(0,0,0,0)"
|
|
1444
|
+
: visual.strokeColor,
|
|
1445
|
+
strokeWidth: visual.strokeStyle === "hidden" ? 0 : strokeWidthScene,
|
|
1446
|
+
strokeDashArray: visual.strokeStyle === "dashed"
|
|
1447
|
+
? [dashLengthScene, dashLengthScene]
|
|
1448
|
+
: undefined,
|
|
1449
|
+
selectable: false,
|
|
1450
|
+
evented: false,
|
|
1451
|
+
},
|
|
1452
|
+
};
|
|
1453
|
+
const specs = shapeOverlay.length > 0
|
|
1454
|
+
? [...mask, ...shapeOverlay]
|
|
1455
|
+
: [...mask, ...shapeOverlay, frameSpec];
|
|
1456
|
+
this.debug("overlay:built", {
|
|
1457
|
+
frame,
|
|
1458
|
+
shape: sceneGeometry?.shape,
|
|
1459
|
+
overlayIds: specs.map((spec) => ({
|
|
1460
|
+
id: spec.id,
|
|
1461
|
+
zIndex: spec.data?.zIndex,
|
|
1462
|
+
})),
|
|
1463
|
+
});
|
|
1464
|
+
return specs;
|
|
1465
|
+
}
|
|
1466
|
+
updateImages() {
|
|
1467
|
+
void this.updateImagesAsync();
|
|
1468
|
+
}
|
|
1469
|
+
async updateImagesAsync() {
|
|
1470
|
+
if (!this.canvasService)
|
|
1471
|
+
return;
|
|
1472
|
+
this.syncToolActiveFromWorkbench();
|
|
1473
|
+
const seq = ++this.renderSeq;
|
|
1474
|
+
const renderItems = this.isToolActive ? this.workingItems : this.items;
|
|
1475
|
+
const frame = this.getFrameRect();
|
|
1476
|
+
const desiredIds = new Set(renderItems.map((item) => item.id));
|
|
1477
|
+
if (this.focusedImageId && !desiredIds.has(this.focusedImageId)) {
|
|
1478
|
+
this.setImageFocus(null, {
|
|
1479
|
+
syncCanvasSelection: false,
|
|
1480
|
+
skipRender: true,
|
|
1481
|
+
});
|
|
1482
|
+
}
|
|
1483
|
+
const imageSpecs = await this.buildImageSpecs(renderItems, frame);
|
|
1484
|
+
if (seq !== this.renderSeq)
|
|
1485
|
+
return;
|
|
1486
|
+
const sceneGeometry = await this.resolveSceneGeometryForOverlay();
|
|
1487
|
+
if (seq !== this.renderSeq)
|
|
1488
|
+
return;
|
|
1489
|
+
this.imageSpecs = imageSpecs;
|
|
1490
|
+
this.overlaySpecs = this.buildOverlaySpecs(frame, sceneGeometry);
|
|
1491
|
+
await this.canvasService.flushRenderFromProducers();
|
|
1492
|
+
if (seq !== this.renderSeq)
|
|
1493
|
+
return;
|
|
1494
|
+
this.refreshImageObjectInteractionState();
|
|
1495
|
+
renderItems.forEach((item) => {
|
|
1496
|
+
if (!this.getImageObject(item.id))
|
|
1497
|
+
return;
|
|
1498
|
+
const resolver = this.loadResolvers.get(item.id);
|
|
1499
|
+
if (!resolver)
|
|
1500
|
+
return;
|
|
1501
|
+
resolver();
|
|
1502
|
+
this.loadResolvers.delete(item.id);
|
|
1503
|
+
});
|
|
1504
|
+
if (this.focusedImageId && this.isToolActive) {
|
|
1505
|
+
this.setImageFocus(this.focusedImageId, {
|
|
1506
|
+
syncCanvasSelection: true,
|
|
1507
|
+
skipRender: true,
|
|
1508
|
+
});
|
|
1509
|
+
}
|
|
1510
|
+
const overlayCanvasCount = this.getOverlayObjects().length;
|
|
1511
|
+
this.debug("render:done", {
|
|
1512
|
+
seq,
|
|
1513
|
+
renderCount: renderItems.length,
|
|
1514
|
+
overlayCount: this.overlaySpecs.length,
|
|
1515
|
+
overlayCanvasCount,
|
|
1516
|
+
isToolActive: this.isToolActive,
|
|
1517
|
+
isImageSelectionActive: this.isImageSelectionActive,
|
|
1518
|
+
focusedImageId: this.focusedImageId,
|
|
1519
|
+
});
|
|
1520
|
+
this.canvasService.requestRenderAll();
|
|
1521
|
+
}
|
|
1522
|
+
clampNormalized(value) {
|
|
1523
|
+
return Math.max(-1, Math.min(2, value));
|
|
1524
|
+
}
|
|
1525
|
+
updateImageInWorking(id, updates) {
|
|
1526
|
+
const index = this.workingItems.findIndex((item) => item.id === id);
|
|
1527
|
+
if (index < 0)
|
|
1528
|
+
return;
|
|
1529
|
+
const next = [...this.workingItems];
|
|
1530
|
+
next[index] = this.normalizeItem({ ...next[index], ...updates });
|
|
1531
|
+
this.workingItems = next;
|
|
1532
|
+
this.hasWorkingChanges = true;
|
|
1533
|
+
this.setImageFocus(id, {
|
|
1534
|
+
syncCanvasSelection: false,
|
|
1535
|
+
skipRender: true,
|
|
1536
|
+
});
|
|
1537
|
+
if (this.isToolActive) {
|
|
1538
|
+
this.updateImages();
|
|
1539
|
+
}
|
|
1540
|
+
this.emitWorkingChange(id);
|
|
1541
|
+
}
|
|
1542
|
+
async updateImageInConfig(id, updates) {
|
|
1543
|
+
const index = this.items.findIndex((item) => item.id === id);
|
|
1544
|
+
if (index < 0)
|
|
1545
|
+
return;
|
|
1546
|
+
const replacingSource = typeof updates.url === "string" && updates.url.length > 0;
|
|
1547
|
+
const next = [...this.items];
|
|
1548
|
+
const base = next[index];
|
|
1549
|
+
const replacingUrl = replacingSource ? updates.url : undefined;
|
|
1550
|
+
next[index] = this.normalizeItem({
|
|
1551
|
+
...base,
|
|
1552
|
+
...updates,
|
|
1553
|
+
...(replacingSource
|
|
1554
|
+
? {
|
|
1555
|
+
url: replacingUrl,
|
|
1556
|
+
sourceUrl: replacingUrl,
|
|
1557
|
+
committedUrl: undefined,
|
|
1558
|
+
scale: updates.scale ?? 1,
|
|
1559
|
+
angle: updates.angle ?? 0,
|
|
1560
|
+
left: updates.left ?? 0.5,
|
|
1561
|
+
top: updates.top ?? 0.5,
|
|
1562
|
+
}
|
|
1563
|
+
: {}),
|
|
1564
|
+
});
|
|
1565
|
+
this.updateConfig(next);
|
|
1566
|
+
if (replacingSource) {
|
|
1567
|
+
this.debug("replace:image:begin", { id, replacingUrl });
|
|
1568
|
+
this.purgeSourceSizeCacheForItem(base);
|
|
1569
|
+
const loaded = await this.waitImageLoaded(id, true);
|
|
1570
|
+
this.debug("replace:image:loaded", { id, loaded });
|
|
1571
|
+
if (loaded) {
|
|
1572
|
+
await this.refitImageToFrame(id);
|
|
1573
|
+
this.setImageFocus(id);
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
waitImageLoaded(id, forceWait = false) {
|
|
1578
|
+
if (!forceWait && this.getImageObject(id)) {
|
|
1579
|
+
return Promise.resolve(true);
|
|
1580
|
+
}
|
|
1581
|
+
return new Promise((resolve) => {
|
|
1582
|
+
const timeout = setTimeout(() => {
|
|
1583
|
+
this.loadResolvers.delete(id);
|
|
1584
|
+
resolve(false);
|
|
1585
|
+
}, 4000);
|
|
1586
|
+
this.loadResolvers.set(id, () => {
|
|
1587
|
+
clearTimeout(timeout);
|
|
1588
|
+
resolve(true);
|
|
1589
|
+
});
|
|
1590
|
+
});
|
|
1591
|
+
}
|
|
1592
|
+
async refitImageToFrame(id) {
|
|
1593
|
+
const obj = this.getImageObject(id);
|
|
1594
|
+
if (!obj || !this.canvasService)
|
|
1595
|
+
return;
|
|
1596
|
+
const current = this.items.find((item) => item.id === id);
|
|
1597
|
+
if (!current)
|
|
1598
|
+
return;
|
|
1599
|
+
const render = this.resolveRenderImageState(current);
|
|
1600
|
+
this.rememberSourceSize(render.src, obj);
|
|
1601
|
+
const source = this.getSourceSize(render.src, obj);
|
|
1602
|
+
const frame = this.getFrameRect();
|
|
1603
|
+
const coverScale = this.getCoverScale(frame, source);
|
|
1604
|
+
const currentScale = this.toSceneObjectScale(obj.scaleX || 1);
|
|
1605
|
+
const zoom = Math.max(0.05, currentScale / coverScale);
|
|
1606
|
+
const updated = {
|
|
1607
|
+
scale: Number.isFinite(zoom) ? zoom : 1,
|
|
1608
|
+
angle: 0,
|
|
1609
|
+
left: 0.5,
|
|
1610
|
+
top: 0.5,
|
|
1611
|
+
};
|
|
1612
|
+
const index = this.items.findIndex((item) => item.id === id);
|
|
1613
|
+
if (index < 0)
|
|
1614
|
+
return;
|
|
1615
|
+
const next = [...this.items];
|
|
1616
|
+
next[index] = this.normalizeItem({ ...next[index], ...updated });
|
|
1617
|
+
this.updateConfig(next);
|
|
1618
|
+
this.workingItems = this.cloneItems(next);
|
|
1619
|
+
this.hasWorkingChanges = false;
|
|
1620
|
+
this.updateImages();
|
|
1621
|
+
this.emitWorkingChange(id);
|
|
1622
|
+
}
|
|
1623
|
+
async fitImageToArea(id, area) {
|
|
1624
|
+
if (!this.canvasService)
|
|
1625
|
+
return;
|
|
1626
|
+
const loaded = await this.waitImageLoaded(id, false);
|
|
1627
|
+
if (!loaded)
|
|
1628
|
+
return;
|
|
1629
|
+
const obj = this.getImageObject(id);
|
|
1630
|
+
if (!obj)
|
|
1631
|
+
return;
|
|
1632
|
+
const renderItems = this.isToolActive ? this.workingItems : this.items;
|
|
1633
|
+
const current = renderItems.find((item) => item.id === id);
|
|
1634
|
+
if (!current)
|
|
1635
|
+
return;
|
|
1636
|
+
const render = this.resolveRenderImageState(current);
|
|
1637
|
+
this.rememberSourceSize(render.src, obj);
|
|
1638
|
+
const source = this.getSourceSize(render.src, obj);
|
|
1639
|
+
const frame = this.getFrameRect();
|
|
1640
|
+
const baseCover = this.getCoverScale(frame, source);
|
|
1641
|
+
const desiredScale = Math.max(Math.max(1, area.width) / Math.max(1, source.width), Math.max(1, area.height) / Math.max(1, source.height));
|
|
1642
|
+
const viewport = this.canvasService.getSceneViewportRect();
|
|
1643
|
+
const canvasW = viewport.width || 1;
|
|
1644
|
+
const canvasH = viewport.height || 1;
|
|
1645
|
+
const areaLeftInput = area.left ?? 0.5;
|
|
1646
|
+
const areaTopInput = area.top ?? 0.5;
|
|
1647
|
+
const areaLeftPx = areaLeftInput <= 1.5
|
|
1648
|
+
? viewport.left + areaLeftInput * canvasW
|
|
1649
|
+
: areaLeftInput;
|
|
1650
|
+
const areaTopPx = areaTopInput <= 1.5
|
|
1651
|
+
? viewport.top + areaTopInput * canvasH
|
|
1652
|
+
: areaTopInput;
|
|
1653
|
+
const updates = {
|
|
1654
|
+
scale: Math.max(0.05, desiredScale / baseCover),
|
|
1655
|
+
left: this.clampNormalized((areaLeftPx - frame.left) / Math.max(1, frame.width)),
|
|
1656
|
+
top: this.clampNormalized((areaTopPx - frame.top) / Math.max(1, frame.height)),
|
|
1657
|
+
};
|
|
1658
|
+
if (this.isToolActive) {
|
|
1659
|
+
this.updateImageInWorking(id, updates);
|
|
1660
|
+
return;
|
|
1661
|
+
}
|
|
1662
|
+
await this.updateImageInConfig(id, updates);
|
|
1663
|
+
}
|
|
1664
|
+
async commitWorkingImagesAsCropped() {
|
|
1665
|
+
if (!this.canvasService) {
|
|
1666
|
+
return { ok: false, reason: "canvas-not-ready" };
|
|
1667
|
+
}
|
|
1668
|
+
await this.updateImagesAsync();
|
|
1669
|
+
const frame = this.getFrameRect();
|
|
1670
|
+
if (!frame.width || !frame.height) {
|
|
1671
|
+
return { ok: false, reason: "frame-not-ready" };
|
|
1672
|
+
}
|
|
1673
|
+
const next = [];
|
|
1674
|
+
for (const item of this.workingItems) {
|
|
1675
|
+
const exported = await this.exportCroppedImageByIds([item.id], {
|
|
1676
|
+
multiplier: 2,
|
|
1677
|
+
format: "png",
|
|
1678
|
+
});
|
|
1679
|
+
const url = exported.url;
|
|
1680
|
+
const sourceUrl = item.sourceUrl || item.url;
|
|
1681
|
+
const previousCommitted = item.committedUrl;
|
|
1682
|
+
next.push(this.normalizeItem({
|
|
1683
|
+
...item,
|
|
1684
|
+
url,
|
|
1685
|
+
// Keep original source for next image-tool session editing,
|
|
1686
|
+
// and use committedUrl as non-image-tools render source.
|
|
1687
|
+
sourceUrl,
|
|
1688
|
+
committedUrl: url,
|
|
1689
|
+
}));
|
|
1690
|
+
if (previousCommitted && previousCommitted !== url) {
|
|
1691
|
+
this.sourceSizeBySrc.delete(previousCommitted);
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
this.hasWorkingChanges = false;
|
|
1695
|
+
this.workingItems = this.cloneItems(next);
|
|
1696
|
+
this.updateConfig(next);
|
|
1697
|
+
this.emitWorkingChange(this.focusedImageId);
|
|
1698
|
+
return { ok: true };
|
|
1699
|
+
}
|
|
1700
|
+
async exportCroppedImageByIds(imageIds, options) {
|
|
1701
|
+
if (!this.canvasService) {
|
|
1702
|
+
throw new Error("CanvasService not initialized");
|
|
1703
|
+
}
|
|
1704
|
+
const normalizedIds = [...new Set(imageIds)].filter((id) => typeof id === "string" && id.length > 0);
|
|
1705
|
+
if (!normalizedIds.length) {
|
|
1706
|
+
throw new Error("image-ids-required");
|
|
1707
|
+
}
|
|
1708
|
+
const frameScene = this.getFrameRect();
|
|
1709
|
+
const frame = this.getFrameRectScreen(frameScene);
|
|
1710
|
+
const multiplier = Math.max(1, options.multiplier ?? 2);
|
|
1711
|
+
const format = options.format === "jpeg" ? "jpeg" : "png";
|
|
1712
|
+
const width = Math.max(1, Math.round(frame.width * multiplier));
|
|
1713
|
+
const height = Math.max(1, Math.round(frame.height * multiplier));
|
|
1714
|
+
const el = document.createElement("canvas");
|
|
1715
|
+
const tempCanvas = new fabric_1.Canvas(el, {
|
|
1716
|
+
renderOnAddRemove: false,
|
|
1717
|
+
selection: false,
|
|
1718
|
+
enableRetinaScaling: false,
|
|
1719
|
+
preserveObjectStacking: true,
|
|
1720
|
+
});
|
|
1721
|
+
tempCanvas.setDimensions({ width, height });
|
|
1722
|
+
try {
|
|
1723
|
+
const idSet = new Set(normalizedIds);
|
|
1724
|
+
const sourceObjects = this.canvasService.canvas
|
|
1725
|
+
.getObjects()
|
|
1726
|
+
.filter((obj) => {
|
|
1727
|
+
return (obj?.data?.layerId === IMAGE_OBJECT_LAYER_ID &&
|
|
1728
|
+
typeof obj?.data?.id === "string" &&
|
|
1729
|
+
idSet.has(obj.data.id));
|
|
1730
|
+
});
|
|
1731
|
+
if (!sourceObjects.length) {
|
|
1732
|
+
throw new Error("image-objects-not-found");
|
|
1733
|
+
}
|
|
1734
|
+
for (const source of sourceObjects) {
|
|
1735
|
+
const clone = await source.clone();
|
|
1736
|
+
const center = source.getCenterPoint
|
|
1737
|
+
? source.getCenterPoint()
|
|
1738
|
+
: new fabric_1.Point(source.left ?? 0, source.top ?? 0);
|
|
1739
|
+
clone.set({
|
|
1740
|
+
originX: "center",
|
|
1741
|
+
originY: "center",
|
|
1742
|
+
left: (center.x - frame.left) * multiplier,
|
|
1743
|
+
top: (center.y - frame.top) * multiplier,
|
|
1744
|
+
scaleX: (source.scaleX || 1) * multiplier,
|
|
1745
|
+
scaleY: (source.scaleY || 1) * multiplier,
|
|
1746
|
+
angle: source.angle || 0,
|
|
1747
|
+
selectable: false,
|
|
1748
|
+
evented: false,
|
|
1749
|
+
});
|
|
1750
|
+
clone.setCoords();
|
|
1751
|
+
tempCanvas.add(clone);
|
|
1752
|
+
}
|
|
1753
|
+
tempCanvas.renderAll();
|
|
1754
|
+
const blob = await tempCanvas.toBlob({ format, multiplier: 1 });
|
|
1755
|
+
if (!blob) {
|
|
1756
|
+
throw new Error("image-export-failed");
|
|
1757
|
+
}
|
|
1758
|
+
return {
|
|
1759
|
+
url: URL.createObjectURL(blob),
|
|
1760
|
+
width,
|
|
1761
|
+
height,
|
|
1762
|
+
multiplier,
|
|
1763
|
+
format,
|
|
1764
|
+
imageIds: sourceObjects
|
|
1765
|
+
.map((obj) => obj?.data?.id)
|
|
1766
|
+
.filter((id) => typeof id === "string"),
|
|
1767
|
+
};
|
|
1768
|
+
}
|
|
1769
|
+
finally {
|
|
1770
|
+
tempCanvas.dispose();
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
async exportUserCroppedImage(options = {}) {
|
|
1774
|
+
if (!this.canvasService) {
|
|
1775
|
+
throw new Error("CanvasService not initialized");
|
|
1776
|
+
}
|
|
1777
|
+
await this.updateImagesAsync();
|
|
1778
|
+
this.syncToolActiveFromWorkbench();
|
|
1779
|
+
const imageIds = options.imageIds && options.imageIds.length > 0
|
|
1780
|
+
? options.imageIds
|
|
1781
|
+
: (this.isToolActive ? this.workingItems : this.items).map((item) => item.id);
|
|
1782
|
+
if (!imageIds.length) {
|
|
1783
|
+
throw new Error("no-images-to-export");
|
|
1784
|
+
}
|
|
1785
|
+
return await this.exportCroppedImageByIds(imageIds, options);
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
exports.ImageTool = ImageTool;
|