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