@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.
Files changed (69) hide show
  1. package/.test-dist/src/coordinate.js +74 -0
  2. package/.test-dist/src/extensions/background.js +547 -0
  3. package/.test-dist/src/extensions/bridgeSelection.js +20 -0
  4. package/.test-dist/src/extensions/constraints.js +237 -0
  5. package/.test-dist/src/extensions/dieline.js +935 -0
  6. package/.test-dist/src/extensions/dielineShape.js +66 -0
  7. package/.test-dist/src/extensions/edgeScale.js +12 -0
  8. package/.test-dist/src/extensions/feature.js +910 -0
  9. package/.test-dist/src/extensions/featureComplete.js +32 -0
  10. package/.test-dist/src/extensions/film.js +226 -0
  11. package/.test-dist/src/extensions/geometry.js +609 -0
  12. package/.test-dist/src/extensions/image.js +1788 -0
  13. package/.test-dist/src/extensions/index.js +28 -0
  14. package/.test-dist/src/extensions/maskOps.js +334 -0
  15. package/.test-dist/src/extensions/mirror.js +104 -0
  16. package/.test-dist/src/extensions/ruler.js +442 -0
  17. package/.test-dist/src/extensions/sceneLayout.js +96 -0
  18. package/.test-dist/src/extensions/sceneLayoutModel.js +202 -0
  19. package/.test-dist/src/extensions/sceneVisibility.js +55 -0
  20. package/.test-dist/src/extensions/size.js +331 -0
  21. package/.test-dist/src/extensions/tracer.js +709 -0
  22. package/.test-dist/src/extensions/white-ink.js +1200 -0
  23. package/.test-dist/src/extensions/wrappedOffsets.js +33 -0
  24. package/.test-dist/src/index.js +18 -0
  25. package/.test-dist/src/services/CanvasService.js +1032 -0
  26. package/.test-dist/src/services/ViewportSystem.js +76 -0
  27. package/.test-dist/src/services/index.js +25 -0
  28. package/.test-dist/src/services/renderSpec.js +2 -0
  29. package/.test-dist/src/services/visibility.js +57 -0
  30. package/.test-dist/src/units.js +30 -0
  31. package/.test-dist/tests/run.js +150 -0
  32. package/CHANGELOG.md +12 -0
  33. package/dist/index.d.mts +164 -62
  34. package/dist/index.d.ts +164 -62
  35. package/dist/index.js +2433 -1719
  36. package/dist/index.mjs +2442 -1723
  37. package/package.json +1 -1
  38. package/src/coordinate.ts +106 -106
  39. package/src/extensions/background.ts +716 -323
  40. package/src/extensions/bridgeSelection.ts +17 -17
  41. package/src/extensions/constraints.ts +322 -322
  42. package/src/extensions/dieline.ts +1173 -1149
  43. package/src/extensions/dielineShape.ts +109 -109
  44. package/src/extensions/edgeScale.ts +19 -19
  45. package/src/extensions/feature.ts +1140 -1137
  46. package/src/extensions/featureComplete.ts +46 -46
  47. package/src/extensions/film.ts +270 -266
  48. package/src/extensions/geometry.ts +851 -885
  49. package/src/extensions/image.ts +2240 -2054
  50. package/src/extensions/index.ts +10 -11
  51. package/src/extensions/maskOps.ts +283 -283
  52. package/src/extensions/mirror.ts +128 -128
  53. package/src/extensions/ruler.ts +664 -654
  54. package/src/extensions/sceneLayout.ts +140 -140
  55. package/src/extensions/sceneLayoutModel.ts +364 -364
  56. package/src/extensions/size.ts +389 -389
  57. package/src/extensions/tracer.ts +1019 -1019
  58. package/src/extensions/white-ink.ts +1508 -1575
  59. package/src/extensions/wrappedOffsets.ts +33 -33
  60. package/src/index.ts +2 -2
  61. package/src/services/CanvasService.ts +1317 -832
  62. package/src/services/ViewportSystem.ts +95 -95
  63. package/src/services/index.ts +4 -3
  64. package/src/services/renderSpec.ts +85 -53
  65. package/src/services/visibility.ts +83 -0
  66. package/src/units.ts +27 -27
  67. package/tests/run.ts +258 -118
  68. package/tsconfig.test.json +15 -15
  69. package/src/extensions/sceneVisibility.ts +0 -64
@@ -1,389 +1,389 @@
1
- import {
2
- CommandContribution,
3
- ConfigurationContribution,
4
- ConfigurationService,
5
- ContributionPointIds,
6
- Extension,
7
- ExtensionContext,
8
- } from "@pooder/core";
9
- import { CanvasService } from "../services";
10
- import {
11
- fromMm,
12
- normalizeConstraintMode,
13
- normalizeCutMode,
14
- normalizeUnit,
15
- readSizeState,
16
- sanitizeMmValue,
17
- toMm,
18
- type SizeConstraintMode,
19
- computeSceneLayout,
20
- } from "./sceneLayoutModel";
21
- import type { Unit } from "../coordinate";
22
-
23
- type ChangedField = "width" | "height" | "both";
24
-
25
- interface UpdateSizeDimensionsInput {
26
- width?: number;
27
- height?: number;
28
- unit?: Unit;
29
- changed?: ChangedField;
30
- }
31
-
32
- export class SizeTool implements Extension {
33
- id = "pooder.kit.size";
34
- metadata = {
35
- name: "SizeTool",
36
- };
37
-
38
- private context?: ExtensionContext;
39
- private canvasService?: CanvasService;
40
-
41
- activate(context: ExtensionContext) {
42
- this.context = context;
43
- this.canvasService = context.services.get<CanvasService>("CanvasService");
44
- const configService = context.services.get<ConfigurationService>(
45
- "ConfigurationService",
46
- );
47
- if (!configService) return;
48
- this.ensureDefaults(configService);
49
- this.emitStateChanged();
50
- }
51
-
52
- deactivate(_context: ExtensionContext) {
53
- this.context = undefined;
54
- this.canvasService = undefined;
55
- }
56
-
57
- contribute() {
58
- return {
59
- [ContributionPointIds.TOOLS]: [
60
- {
61
- id: this.id,
62
- name: "Size",
63
- interaction: "instant",
64
- },
65
- ],
66
- [ContributionPointIds.CONFIGURATIONS]: [
67
- {
68
- id: "size.unit",
69
- type: "select",
70
- label: "Display Unit",
71
- options: ["mm", "cm", "in"],
72
- default: "mm",
73
- },
74
- {
75
- id: "size.actualWidthMm",
76
- type: "number",
77
- label: "Actual Width (mm)",
78
- min: 10,
79
- max: 2000,
80
- step: 0.1,
81
- default: 500,
82
- },
83
- {
84
- id: "size.actualHeightMm",
85
- type: "number",
86
- label: "Actual Height (mm)",
87
- min: 10,
88
- max: 2000,
89
- step: 0.1,
90
- default: 500,
91
- },
92
- {
93
- id: "size.constraintMode",
94
- type: "select",
95
- label: "Constraint Mode",
96
- options: ["free", "lockAspect", "equal"],
97
- default: "free",
98
- },
99
- {
100
- id: "size.aspectRatio",
101
- type: "number",
102
- label: "Aspect Ratio",
103
- min: 0.01,
104
- max: 100,
105
- step: 0.01,
106
- default: 1,
107
- },
108
- {
109
- id: "size.cutMode",
110
- type: "select",
111
- label: "Cut Mode",
112
- options: ["trim", "outset", "inset"],
113
- default: "trim",
114
- },
115
- {
116
- id: "size.cutMarginMm",
117
- type: "number",
118
- label: "Cut Margin (mm)",
119
- min: 0,
120
- max: 100,
121
- step: 0.1,
122
- default: 0,
123
- },
124
- {
125
- id: "size.viewPadding",
126
- type: "select",
127
- label: "View Padding",
128
- options: [0, 10, 20, 40, 60, 100, "2%", "5%", "10%", "15%", "20%"],
129
- default: 140,
130
- },
131
- {
132
- id: "size.minMm",
133
- type: "number",
134
- label: "Min Size (mm)",
135
- min: 0.1,
136
- max: 2000,
137
- step: 0.1,
138
- default: 10,
139
- },
140
- {
141
- id: "size.maxMm",
142
- type: "number",
143
- label: "Max Size (mm)",
144
- min: 1,
145
- max: 10000,
146
- step: 1,
147
- default: 2000,
148
- },
149
- {
150
- id: "size.stepMm",
151
- type: "number",
152
- label: "Size Step (mm)",
153
- min: 0.001,
154
- max: 100,
155
- step: 0.001,
156
- default: 0.1,
157
- },
158
- ] as ConfigurationContribution[],
159
- [ContributionPointIds.COMMANDS]: [
160
- {
161
- command: "getSizeState",
162
- title: "Get Size State",
163
- handler: () => this.getStateForUI(),
164
- },
165
- {
166
- command: "updateSizeDimensions",
167
- title: "Update Size Dimensions",
168
- handler: (input: UpdateSizeDimensionsInput = {}) =>
169
- this.updateDimensions(input),
170
- },
171
- {
172
- command: "setSizeConstraintMode",
173
- title: "Set Size Constraint Mode",
174
- handler: (mode: SizeConstraintMode) => this.setConstraintMode(mode),
175
- },
176
- {
177
- command: "setSizeDisplayUnit",
178
- title: "Set Size Display Unit",
179
- handler: (unit: Unit) => this.setUnit(unit),
180
- },
181
- {
182
- command: "setSizeCut",
183
- title: "Set Size Cut",
184
- handler: (cutMode: string, cutMarginMm: number = 0) =>
185
- this.setCut(cutMode, cutMarginMm),
186
- },
187
- {
188
- command: "getSelectedImageSize",
189
- title: "Get Selected Image Size",
190
- handler: (id?: string) => this.getSelectedImageSize(id),
191
- },
192
- ] as CommandContribution[],
193
- };
194
- }
195
-
196
- private getConfigService(): ConfigurationService | undefined {
197
- return this.context?.services.get<ConfigurationService>(
198
- "ConfigurationService",
199
- );
200
- }
201
-
202
- private ensureDefaults(configService: ConfigurationService) {
203
- const state = readSizeState(configService);
204
- configService.update("size.unit", state.unit);
205
- configService.update("size.actualWidthMm", state.actualWidthMm);
206
- configService.update("size.actualHeightMm", state.actualHeightMm);
207
- configService.update("size.constraintMode", state.constraintMode);
208
- configService.update(
209
- "size.aspectRatio",
210
- state.actualWidthMm / Math.max(0.001, state.actualHeightMm),
211
- );
212
- configService.update("size.cutMode", state.cutMode);
213
- configService.update("size.cutMarginMm", state.cutMarginMm);
214
- configService.update("size.viewPadding", state.viewPadding);
215
- configService.update("size.minMm", state.minMm);
216
- configService.update("size.maxMm", state.maxMm);
217
- configService.update("size.stepMm", state.stepMm);
218
- }
219
-
220
- private emitStateChanged() {
221
- const state = this.getStateForUI();
222
- if (!state) return;
223
- this.context?.eventBus.emit("size:state:changed", state);
224
- }
225
-
226
- private getStateForUI() {
227
- const configService = this.getConfigService();
228
- if (!configService) return null;
229
- const state = readSizeState(configService);
230
- return {
231
- ...state,
232
- actualWidth: fromMm(state.actualWidthMm, state.unit),
233
- actualHeight: fromMm(state.actualHeightMm, state.unit),
234
- };
235
- }
236
-
237
- private updateDimensions(input: UpdateSizeDimensionsInput) {
238
- const configService = this.getConfigService();
239
- if (!configService) return null;
240
-
241
- const state = readSizeState(configService);
242
- const inputUnit = normalizeUnit(input.unit ?? state.unit);
243
- const changed: ChangedField = input.changed || "both";
244
-
245
- const providedWidthMm = Number.isFinite(input.width as any)
246
- ? toMm(Number(input.width), inputUnit)
247
- : undefined;
248
- const providedHeightMm = Number.isFinite(input.height as any)
249
- ? toMm(Number(input.height), inputUnit)
250
- : undefined;
251
-
252
- const limits = {
253
- minMm: state.minMm,
254
- maxMm: state.maxMm,
255
- stepMm: state.stepMm,
256
- };
257
-
258
- let nextWidthMm =
259
- providedWidthMm !== undefined ? providedWidthMm : state.actualWidthMm;
260
- let nextHeightMm =
261
- providedHeightMm !== undefined ? providedHeightMm : state.actualHeightMm;
262
-
263
- if (state.constraintMode === "equal") {
264
- const anchor =
265
- changed === "height"
266
- ? nextHeightMm
267
- : changed === "width"
268
- ? nextWidthMm
269
- : (providedWidthMm ?? providedHeightMm ?? nextWidthMm);
270
- nextWidthMm = anchor;
271
- nextHeightMm = anchor;
272
- } else if (state.constraintMode === "lockAspect") {
273
- const ratio = Math.max(0.0001, state.aspectRatio);
274
- if (changed === "height") {
275
- nextWidthMm = nextHeightMm * ratio;
276
- } else {
277
- nextHeightMm = nextWidthMm / ratio;
278
- }
279
- }
280
-
281
- nextWidthMm = sanitizeMmValue(nextWidthMm, limits);
282
- nextHeightMm = sanitizeMmValue(nextHeightMm, limits);
283
-
284
- if (state.constraintMode === "equal") {
285
- const value = Math.max(nextWidthMm, nextHeightMm);
286
- nextWidthMm = value;
287
- nextHeightMm = value;
288
- } else if (state.constraintMode === "lockAspect") {
289
- const ratio = Math.max(0.0001, state.aspectRatio);
290
- if (changed === "height") {
291
- nextWidthMm = sanitizeMmValue(nextHeightMm * ratio, limits);
292
- } else {
293
- nextHeightMm = sanitizeMmValue(nextWidthMm / ratio, limits);
294
- }
295
- }
296
-
297
- configService.update("size.actualWidthMm", nextWidthMm);
298
- configService.update("size.actualHeightMm", nextHeightMm);
299
- configService.update("size.unit", inputUnit);
300
- this.emitStateChanged();
301
- return this.getStateForUI();
302
- }
303
-
304
- private setConstraintMode(modeRaw: string) {
305
- const configService = this.getConfigService();
306
- if (!configService) return null;
307
- const state = readSizeState(configService);
308
- const mode = normalizeConstraintMode(modeRaw);
309
-
310
- configService.update("size.constraintMode", mode);
311
- if (mode === "lockAspect") {
312
- const ratio = state.actualWidthMm / Math.max(0.001, state.actualHeightMm);
313
- configService.update("size.aspectRatio", ratio);
314
- }
315
- if (mode === "equal") {
316
- const value = sanitizeMmValue(
317
- Math.max(state.actualWidthMm, state.actualHeightMm),
318
- {
319
- minMm: state.minMm,
320
- maxMm: state.maxMm,
321
- stepMm: state.stepMm,
322
- },
323
- );
324
- configService.update("size.actualWidthMm", value);
325
- configService.update("size.actualHeightMm", value);
326
- configService.update("size.aspectRatio", 1);
327
- }
328
- this.emitStateChanged();
329
- return this.getStateForUI();
330
- }
331
-
332
- private setUnit(unitRaw: string) {
333
- const configService = this.getConfigService();
334
- if (!configService) return null;
335
- const unit = normalizeUnit(unitRaw);
336
- configService.update("size.unit", unit);
337
- this.emitStateChanged();
338
- return this.getStateForUI();
339
- }
340
-
341
- private setCut(cutModeRaw: string, cutMarginMm = 0) {
342
- const configService = this.getConfigService();
343
- if (!configService) return null;
344
- const cutMode = normalizeCutMode(cutModeRaw);
345
- const margin = Math.max(0, Number(cutMarginMm) || 0);
346
- configService.update("size.cutMode", cutMode);
347
- configService.update("size.cutMarginMm", margin);
348
- this.emitStateChanged();
349
- return this.getStateForUI();
350
- }
351
-
352
- private getSelectedImageSize(id?: string) {
353
- const configService = this.getConfigService();
354
- if (!configService || !this.canvasService) return null;
355
- const sizeState = readSizeState(configService);
356
- const layout = computeSceneLayout(this.canvasService, sizeState);
357
- if (!layout || layout.scale <= 0) return null;
358
-
359
- const all = this.canvasService.canvas.getObjects() as any[];
360
- const active = this.canvasService.canvas.getActiveObject() as any;
361
- const activeId =
362
- active?.data?.layerId === "image.user" ? active?.data?.id : null;
363
- const targetId = id || activeId;
364
- const target =
365
- all.find(
366
- (obj) =>
367
- obj?.data?.layerId === "image.user" && obj?.data?.id === targetId,
368
- ) || all.find((obj) => obj?.data?.layerId === "image.user");
369
- if (!target) return null;
370
-
371
- const objectWidthPx = Math.abs((target.width || 0) * (target.scaleX || 1));
372
- const objectHeightPx = Math.abs(
373
- (target.height || 0) * (target.scaleY || 1),
374
- );
375
- if (objectWidthPx <= 0 || objectHeightPx <= 0) return null;
376
-
377
- const widthMm = objectWidthPx / layout.scale;
378
- const heightMm = objectHeightPx / layout.scale;
379
-
380
- return {
381
- id: target?.data?.id || null,
382
- widthMm,
383
- heightMm,
384
- width: fromMm(widthMm, sizeState.unit),
385
- height: fromMm(heightMm, sizeState.unit),
386
- unit: sizeState.unit,
387
- };
388
- }
389
- }
1
+ import {
2
+ CommandContribution,
3
+ ConfigurationContribution,
4
+ ConfigurationService,
5
+ ContributionPointIds,
6
+ Extension,
7
+ ExtensionContext,
8
+ } from "@pooder/core";
9
+ import { CanvasService } from "../services";
10
+ import {
11
+ fromMm,
12
+ normalizeConstraintMode,
13
+ normalizeCutMode,
14
+ normalizeUnit,
15
+ readSizeState,
16
+ sanitizeMmValue,
17
+ toMm,
18
+ type SizeConstraintMode,
19
+ computeSceneLayout,
20
+ } from "./sceneLayoutModel";
21
+ import type { Unit } from "../coordinate";
22
+
23
+ type ChangedField = "width" | "height" | "both";
24
+
25
+ interface UpdateSizeDimensionsInput {
26
+ width?: number;
27
+ height?: number;
28
+ unit?: Unit;
29
+ changed?: ChangedField;
30
+ }
31
+
32
+ export class SizeTool implements Extension {
33
+ id = "pooder.kit.size";
34
+ metadata = {
35
+ name: "SizeTool",
36
+ };
37
+
38
+ private context?: ExtensionContext;
39
+ private canvasService?: CanvasService;
40
+
41
+ activate(context: ExtensionContext) {
42
+ this.context = context;
43
+ this.canvasService = context.services.get<CanvasService>("CanvasService");
44
+ const configService = context.services.get<ConfigurationService>(
45
+ "ConfigurationService",
46
+ );
47
+ if (!configService) return;
48
+ this.ensureDefaults(configService);
49
+ this.emitStateChanged();
50
+ }
51
+
52
+ deactivate(_context: ExtensionContext) {
53
+ this.context = undefined;
54
+ this.canvasService = undefined;
55
+ }
56
+
57
+ contribute() {
58
+ return {
59
+ [ContributionPointIds.TOOLS]: [
60
+ {
61
+ id: this.id,
62
+ name: "Size",
63
+ interaction: "instant",
64
+ },
65
+ ],
66
+ [ContributionPointIds.CONFIGURATIONS]: [
67
+ {
68
+ id: "size.unit",
69
+ type: "select",
70
+ label: "Display Unit",
71
+ options: ["mm", "cm", "in"],
72
+ default: "mm",
73
+ },
74
+ {
75
+ id: "size.actualWidthMm",
76
+ type: "number",
77
+ label: "Actual Width (mm)",
78
+ min: 10,
79
+ max: 2000,
80
+ step: 0.1,
81
+ default: 500,
82
+ },
83
+ {
84
+ id: "size.actualHeightMm",
85
+ type: "number",
86
+ label: "Actual Height (mm)",
87
+ min: 10,
88
+ max: 2000,
89
+ step: 0.1,
90
+ default: 500,
91
+ },
92
+ {
93
+ id: "size.constraintMode",
94
+ type: "select",
95
+ label: "Constraint Mode",
96
+ options: ["free", "lockAspect", "equal"],
97
+ default: "free",
98
+ },
99
+ {
100
+ id: "size.aspectRatio",
101
+ type: "number",
102
+ label: "Aspect Ratio",
103
+ min: 0.01,
104
+ max: 100,
105
+ step: 0.01,
106
+ default: 1,
107
+ },
108
+ {
109
+ id: "size.cutMode",
110
+ type: "select",
111
+ label: "Cut Mode",
112
+ options: ["trim", "outset", "inset"],
113
+ default: "trim",
114
+ },
115
+ {
116
+ id: "size.cutMarginMm",
117
+ type: "number",
118
+ label: "Cut Margin (mm)",
119
+ min: 0,
120
+ max: 100,
121
+ step: 0.1,
122
+ default: 0,
123
+ },
124
+ {
125
+ id: "size.viewPadding",
126
+ type: "select",
127
+ label: "View Padding",
128
+ options: [0, 10, 20, 40, 60, 100, "2%", "5%", "10%", "15%", "20%"],
129
+ default: 140,
130
+ },
131
+ {
132
+ id: "size.minMm",
133
+ type: "number",
134
+ label: "Min Size (mm)",
135
+ min: 0.1,
136
+ max: 2000,
137
+ step: 0.1,
138
+ default: 10,
139
+ },
140
+ {
141
+ id: "size.maxMm",
142
+ type: "number",
143
+ label: "Max Size (mm)",
144
+ min: 1,
145
+ max: 10000,
146
+ step: 1,
147
+ default: 2000,
148
+ },
149
+ {
150
+ id: "size.stepMm",
151
+ type: "number",
152
+ label: "Size Step (mm)",
153
+ min: 0.001,
154
+ max: 100,
155
+ step: 0.001,
156
+ default: 0.1,
157
+ },
158
+ ] as ConfigurationContribution[],
159
+ [ContributionPointIds.COMMANDS]: [
160
+ {
161
+ command: "getSizeState",
162
+ title: "Get Size State",
163
+ handler: () => this.getStateForUI(),
164
+ },
165
+ {
166
+ command: "updateSizeDimensions",
167
+ title: "Update Size Dimensions",
168
+ handler: (input: UpdateSizeDimensionsInput = {}) =>
169
+ this.updateDimensions(input),
170
+ },
171
+ {
172
+ command: "setSizeConstraintMode",
173
+ title: "Set Size Constraint Mode",
174
+ handler: (mode: SizeConstraintMode) => this.setConstraintMode(mode),
175
+ },
176
+ {
177
+ command: "setSizeDisplayUnit",
178
+ title: "Set Size Display Unit",
179
+ handler: (unit: Unit) => this.setUnit(unit),
180
+ },
181
+ {
182
+ command: "setSizeCut",
183
+ title: "Set Size Cut",
184
+ handler: (cutMode: string, cutMarginMm: number = 0) =>
185
+ this.setCut(cutMode, cutMarginMm),
186
+ },
187
+ {
188
+ command: "getSelectedImageSize",
189
+ title: "Get Selected Image Size",
190
+ handler: (id?: string) => this.getSelectedImageSize(id),
191
+ },
192
+ ] as CommandContribution[],
193
+ };
194
+ }
195
+
196
+ private getConfigService(): ConfigurationService | undefined {
197
+ return this.context?.services.get<ConfigurationService>(
198
+ "ConfigurationService",
199
+ );
200
+ }
201
+
202
+ private ensureDefaults(configService: ConfigurationService) {
203
+ const state = readSizeState(configService);
204
+ configService.update("size.unit", state.unit);
205
+ configService.update("size.actualWidthMm", state.actualWidthMm);
206
+ configService.update("size.actualHeightMm", state.actualHeightMm);
207
+ configService.update("size.constraintMode", state.constraintMode);
208
+ configService.update(
209
+ "size.aspectRatio",
210
+ state.actualWidthMm / Math.max(0.001, state.actualHeightMm),
211
+ );
212
+ configService.update("size.cutMode", state.cutMode);
213
+ configService.update("size.cutMarginMm", state.cutMarginMm);
214
+ configService.update("size.viewPadding", state.viewPadding);
215
+ configService.update("size.minMm", state.minMm);
216
+ configService.update("size.maxMm", state.maxMm);
217
+ configService.update("size.stepMm", state.stepMm);
218
+ }
219
+
220
+ private emitStateChanged() {
221
+ const state = this.getStateForUI();
222
+ if (!state) return;
223
+ this.context?.eventBus.emit("size:state:changed", state);
224
+ }
225
+
226
+ private getStateForUI() {
227
+ const configService = this.getConfigService();
228
+ if (!configService) return null;
229
+ const state = readSizeState(configService);
230
+ return {
231
+ ...state,
232
+ actualWidth: fromMm(state.actualWidthMm, state.unit),
233
+ actualHeight: fromMm(state.actualHeightMm, state.unit),
234
+ };
235
+ }
236
+
237
+ private updateDimensions(input: UpdateSizeDimensionsInput) {
238
+ const configService = this.getConfigService();
239
+ if (!configService) return null;
240
+
241
+ const state = readSizeState(configService);
242
+ const inputUnit = normalizeUnit(input.unit ?? state.unit);
243
+ const changed: ChangedField = input.changed || "both";
244
+
245
+ const providedWidthMm = Number.isFinite(input.width as any)
246
+ ? toMm(Number(input.width), inputUnit)
247
+ : undefined;
248
+ const providedHeightMm = Number.isFinite(input.height as any)
249
+ ? toMm(Number(input.height), inputUnit)
250
+ : undefined;
251
+
252
+ const limits = {
253
+ minMm: state.minMm,
254
+ maxMm: state.maxMm,
255
+ stepMm: state.stepMm,
256
+ };
257
+
258
+ let nextWidthMm =
259
+ providedWidthMm !== undefined ? providedWidthMm : state.actualWidthMm;
260
+ let nextHeightMm =
261
+ providedHeightMm !== undefined ? providedHeightMm : state.actualHeightMm;
262
+
263
+ if (state.constraintMode === "equal") {
264
+ const anchor =
265
+ changed === "height"
266
+ ? nextHeightMm
267
+ : changed === "width"
268
+ ? nextWidthMm
269
+ : (providedWidthMm ?? providedHeightMm ?? nextWidthMm);
270
+ nextWidthMm = anchor;
271
+ nextHeightMm = anchor;
272
+ } else if (state.constraintMode === "lockAspect") {
273
+ const ratio = Math.max(0.0001, state.aspectRatio);
274
+ if (changed === "height") {
275
+ nextWidthMm = nextHeightMm * ratio;
276
+ } else {
277
+ nextHeightMm = nextWidthMm / ratio;
278
+ }
279
+ }
280
+
281
+ nextWidthMm = sanitizeMmValue(nextWidthMm, limits);
282
+ nextHeightMm = sanitizeMmValue(nextHeightMm, limits);
283
+
284
+ if (state.constraintMode === "equal") {
285
+ const value = Math.max(nextWidthMm, nextHeightMm);
286
+ nextWidthMm = value;
287
+ nextHeightMm = value;
288
+ } else if (state.constraintMode === "lockAspect") {
289
+ const ratio = Math.max(0.0001, state.aspectRatio);
290
+ if (changed === "height") {
291
+ nextWidthMm = sanitizeMmValue(nextHeightMm * ratio, limits);
292
+ } else {
293
+ nextHeightMm = sanitizeMmValue(nextWidthMm / ratio, limits);
294
+ }
295
+ }
296
+
297
+ configService.update("size.actualWidthMm", nextWidthMm);
298
+ configService.update("size.actualHeightMm", nextHeightMm);
299
+ configService.update("size.unit", inputUnit);
300
+ this.emitStateChanged();
301
+ return this.getStateForUI();
302
+ }
303
+
304
+ private setConstraintMode(modeRaw: string) {
305
+ const configService = this.getConfigService();
306
+ if (!configService) return null;
307
+ const state = readSizeState(configService);
308
+ const mode = normalizeConstraintMode(modeRaw);
309
+
310
+ configService.update("size.constraintMode", mode);
311
+ if (mode === "lockAspect") {
312
+ const ratio = state.actualWidthMm / Math.max(0.001, state.actualHeightMm);
313
+ configService.update("size.aspectRatio", ratio);
314
+ }
315
+ if (mode === "equal") {
316
+ const value = sanitizeMmValue(
317
+ Math.max(state.actualWidthMm, state.actualHeightMm),
318
+ {
319
+ minMm: state.minMm,
320
+ maxMm: state.maxMm,
321
+ stepMm: state.stepMm,
322
+ },
323
+ );
324
+ configService.update("size.actualWidthMm", value);
325
+ configService.update("size.actualHeightMm", value);
326
+ configService.update("size.aspectRatio", 1);
327
+ }
328
+ this.emitStateChanged();
329
+ return this.getStateForUI();
330
+ }
331
+
332
+ private setUnit(unitRaw: string) {
333
+ const configService = this.getConfigService();
334
+ if (!configService) return null;
335
+ const unit = normalizeUnit(unitRaw);
336
+ configService.update("size.unit", unit);
337
+ this.emitStateChanged();
338
+ return this.getStateForUI();
339
+ }
340
+
341
+ private setCut(cutModeRaw: string, cutMarginMm = 0) {
342
+ const configService = this.getConfigService();
343
+ if (!configService) return null;
344
+ const cutMode = normalizeCutMode(cutModeRaw);
345
+ const margin = Math.max(0, Number(cutMarginMm) || 0);
346
+ configService.update("size.cutMode", cutMode);
347
+ configService.update("size.cutMarginMm", margin);
348
+ this.emitStateChanged();
349
+ return this.getStateForUI();
350
+ }
351
+
352
+ private getSelectedImageSize(id?: string) {
353
+ const configService = this.getConfigService();
354
+ if (!configService || !this.canvasService) return null;
355
+ const sizeState = readSizeState(configService);
356
+ const layout = computeSceneLayout(this.canvasService, sizeState);
357
+ if (!layout || layout.scale <= 0) return null;
358
+
359
+ const all = this.canvasService.canvas.getObjects() as any[];
360
+ const active = this.canvasService.canvas.getActiveObject() as any;
361
+ const activeId =
362
+ active?.data?.layerId === "image.user" ? active?.data?.id : null;
363
+ const targetId = id || activeId;
364
+ const target =
365
+ all.find(
366
+ (obj) =>
367
+ obj?.data?.layerId === "image.user" && obj?.data?.id === targetId,
368
+ ) || all.find((obj) => obj?.data?.layerId === "image.user");
369
+ if (!target) return null;
370
+
371
+ const objectWidthPx = Math.abs((target.width || 0) * (target.scaleX || 1));
372
+ const objectHeightPx = Math.abs(
373
+ (target.height || 0) * (target.scaleY || 1),
374
+ );
375
+ if (objectWidthPx <= 0 || objectHeightPx <= 0) return null;
376
+
377
+ const widthMm = objectWidthPx / layout.scale;
378
+ const heightMm = objectHeightPx / layout.scale;
379
+
380
+ return {
381
+ id: target?.data?.id || null,
382
+ widthMm,
383
+ heightMm,
384
+ width: fromMm(widthMm, sizeState.unit),
385
+ height: fromMm(heightMm, sizeState.unit),
386
+ unit: sizeState.unit,
387
+ };
388
+ }
389
+ }