@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.
Files changed (90) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/index.d.mts +243 -36
  3. package/dist/index.d.ts +243 -36
  4. package/dist/index.js +2278 -1041
  5. package/dist/index.mjs +2278 -1041
  6. package/package.json +1 -1
  7. package/src/coordinate.ts +106 -106
  8. package/src/extensions/background.ts +323 -230
  9. package/src/extensions/bridgeSelection.ts +17 -17
  10. package/src/extensions/constraints.ts +322 -322
  11. package/src/extensions/dieline.ts +1149 -1076
  12. package/src/extensions/dielineShape.ts +109 -0
  13. package/src/extensions/edgeScale.ts +19 -19
  14. package/src/extensions/feature.ts +1137 -1021
  15. package/src/extensions/featureComplete.ts +46 -46
  16. package/src/extensions/film.ts +266 -194
  17. package/src/extensions/geometry.ts +885 -752
  18. package/src/extensions/image.ts +2054 -1926
  19. package/src/extensions/index.ts +11 -11
  20. package/src/extensions/maskOps.ts +283 -283
  21. package/src/extensions/mirror.ts +128 -128
  22. package/src/extensions/ruler.ts +654 -451
  23. package/src/extensions/sceneLayout.ts +140 -140
  24. package/src/extensions/sceneLayoutModel.ts +364 -352
  25. package/src/extensions/sceneVisibility.ts +64 -71
  26. package/src/extensions/size.ts +389 -389
  27. package/src/extensions/tracer.ts +1019 -1019
  28. package/src/extensions/white-ink.ts +1567 -1514
  29. package/src/extensions/wrappedOffsets.ts +33 -33
  30. package/src/index.ts +2 -2
  31. package/src/services/CanvasService.ts +832 -300
  32. package/src/services/ViewportSystem.ts +95 -95
  33. package/src/services/index.ts +3 -3
  34. package/src/services/renderSpec.ts +53 -18
  35. package/src/units.ts +27 -27
  36. package/tests/run.ts +118 -118
  37. package/tsconfig.test.json +15 -15
  38. package/.test-dist/src/CanvasService.js +0 -249
  39. package/.test-dist/src/ViewportSystem.js +0 -75
  40. package/.test-dist/src/background.js +0 -203
  41. package/.test-dist/src/bridgeSelection.js +0 -20
  42. package/.test-dist/src/constraints.js +0 -237
  43. package/.test-dist/src/coordinate.js +0 -74
  44. package/.test-dist/src/dieline.js +0 -818
  45. package/.test-dist/src/edgeScale.js +0 -12
  46. package/.test-dist/src/extensions/background.js +0 -203
  47. package/.test-dist/src/extensions/bridgeSelection.js +0 -20
  48. package/.test-dist/src/extensions/constraints.js +0 -237
  49. package/.test-dist/src/extensions/dieline.js +0 -828
  50. package/.test-dist/src/extensions/edgeScale.js +0 -12
  51. package/.test-dist/src/extensions/feature.js +0 -825
  52. package/.test-dist/src/extensions/featureComplete.js +0 -32
  53. package/.test-dist/src/extensions/film.js +0 -167
  54. package/.test-dist/src/extensions/geometry.js +0 -545
  55. package/.test-dist/src/extensions/image.js +0 -1529
  56. package/.test-dist/src/extensions/index.js +0 -30
  57. package/.test-dist/src/extensions/maskOps.js +0 -279
  58. package/.test-dist/src/extensions/mirror.js +0 -104
  59. package/.test-dist/src/extensions/ruler.js +0 -345
  60. package/.test-dist/src/extensions/sceneLayout.js +0 -96
  61. package/.test-dist/src/extensions/sceneLayoutModel.js +0 -196
  62. package/.test-dist/src/extensions/sceneVisibility.js +0 -62
  63. package/.test-dist/src/extensions/size.js +0 -331
  64. package/.test-dist/src/extensions/tracer.js +0 -538
  65. package/.test-dist/src/extensions/white-ink.js +0 -1190
  66. package/.test-dist/src/extensions/wrappedOffsets.js +0 -33
  67. package/.test-dist/src/feature.js +0 -826
  68. package/.test-dist/src/featureComplete.js +0 -32
  69. package/.test-dist/src/film.js +0 -167
  70. package/.test-dist/src/geometry.js +0 -506
  71. package/.test-dist/src/image.js +0 -1250
  72. package/.test-dist/src/index.js +0 -18
  73. package/.test-dist/src/maskOps.js +0 -270
  74. package/.test-dist/src/mirror.js +0 -104
  75. package/.test-dist/src/renderSpec.js +0 -2
  76. package/.test-dist/src/ruler.js +0 -343
  77. package/.test-dist/src/sceneLayout.js +0 -99
  78. package/.test-dist/src/sceneLayoutModel.js +0 -196
  79. package/.test-dist/src/sceneView.js +0 -40
  80. package/.test-dist/src/sceneVisibility.js +0 -42
  81. package/.test-dist/src/services/CanvasService.js +0 -249
  82. package/.test-dist/src/services/ViewportSystem.js +0 -76
  83. package/.test-dist/src/services/index.js +0 -24
  84. package/.test-dist/src/services/renderSpec.js +0 -2
  85. package/.test-dist/src/size.js +0 -332
  86. package/.test-dist/src/tracer.js +0 -544
  87. package/.test-dist/src/units.js +0 -30
  88. package/.test-dist/src/white-ink.js +0 -829
  89. package/.test-dist/src/wrappedOffsets.js +0 -33
  90. package/.test-dist/tests/run.js +0 -94
@@ -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
+ }