@pooder/kit 5.3.0 → 5.3.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 (60) hide show
  1. package/.test-dist/src/CanvasService.js +249 -249
  2. package/.test-dist/src/ViewportSystem.js +75 -75
  3. package/.test-dist/src/background.js +203 -203
  4. package/.test-dist/src/bridgeSelection.js +20 -20
  5. package/.test-dist/src/constraints.js +237 -237
  6. package/.test-dist/src/dieline.js +818 -818
  7. package/.test-dist/src/edgeScale.js +12 -12
  8. package/.test-dist/src/feature.js +826 -826
  9. package/.test-dist/src/featureComplete.js +32 -32
  10. package/.test-dist/src/film.js +167 -167
  11. package/.test-dist/src/geometry.js +506 -506
  12. package/.test-dist/src/image.js +1250 -1250
  13. package/.test-dist/src/maskOps.js +270 -270
  14. package/.test-dist/src/mirror.js +104 -104
  15. package/.test-dist/src/renderSpec.js +2 -2
  16. package/.test-dist/src/ruler.js +343 -343
  17. package/.test-dist/src/sceneLayout.js +99 -99
  18. package/.test-dist/src/sceneLayoutModel.js +196 -196
  19. package/.test-dist/src/sceneView.js +40 -40
  20. package/.test-dist/src/sceneVisibility.js +42 -42
  21. package/.test-dist/src/size.js +332 -332
  22. package/.test-dist/src/tracer.js +544 -544
  23. package/.test-dist/src/white-ink.js +829 -829
  24. package/.test-dist/src/wrappedOffsets.js +33 -33
  25. package/CHANGELOG.md +6 -0
  26. package/dist/index.d.mts +6 -0
  27. package/dist/index.d.ts +6 -0
  28. package/dist/index.js +108 -20
  29. package/dist/index.mjs +108 -20
  30. package/package.json +1 -1
  31. package/src/coordinate.ts +106 -106
  32. package/src/extensions/background.ts +230 -230
  33. package/src/extensions/bridgeSelection.ts +17 -17
  34. package/src/extensions/constraints.ts +322 -322
  35. package/src/extensions/dieline.ts +46 -0
  36. package/src/extensions/edgeScale.ts +19 -19
  37. package/src/extensions/feature.ts +1021 -1021
  38. package/src/extensions/featureComplete.ts +46 -46
  39. package/src/extensions/film.ts +194 -194
  40. package/src/extensions/geometry.ts +752 -719
  41. package/src/extensions/image.ts +1926 -1924
  42. package/src/extensions/index.ts +11 -11
  43. package/src/extensions/maskOps.ts +283 -283
  44. package/src/extensions/mirror.ts +128 -128
  45. package/src/extensions/ruler.ts +451 -451
  46. package/src/extensions/sceneLayout.ts +140 -140
  47. package/src/extensions/sceneLayoutModel.ts +352 -342
  48. package/src/extensions/sceneVisibility.ts +71 -71
  49. package/src/extensions/size.ts +389 -389
  50. package/src/extensions/tracer.ts +58 -19
  51. package/src/extensions/white-ink.ts +1400 -1400
  52. package/src/extensions/wrappedOffsets.ts +33 -33
  53. package/src/index.ts +2 -2
  54. package/src/services/CanvasService.ts +300 -300
  55. package/src/services/ViewportSystem.ts +95 -95
  56. package/src/services/index.ts +3 -3
  57. package/src/services/renderSpec.ts +18 -18
  58. package/src/units.ts +27 -27
  59. package/tests/run.ts +118 -118
  60. package/tsconfig.test.json +15 -15
@@ -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
+ }