@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
@@ -0,0 +1,202 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sanitizeMmValue = sanitizeMmValue;
4
+ exports.normalizeUnit = normalizeUnit;
5
+ exports.normalizeConstraintMode = normalizeConstraintMode;
6
+ exports.normalizeCutMode = normalizeCutMode;
7
+ exports.toMm = toMm;
8
+ exports.fromMm = fromMm;
9
+ exports.resolvePaddingPx = resolvePaddingPx;
10
+ exports.readSizeState = readSizeState;
11
+ exports.computeSceneLayout = computeSceneLayout;
12
+ exports.buildSceneGeometry = buildSceneGeometry;
13
+ const coordinate_1 = require("../coordinate");
14
+ const units_1 = require("../units");
15
+ const dielineShape_1 = require("./dielineShape");
16
+ const DEFAULT_SIZE_STATE = {
17
+ unit: "mm",
18
+ actualWidthMm: 500,
19
+ actualHeightMm: 500,
20
+ constraintMode: "free",
21
+ aspectRatio: 1,
22
+ cutMode: "trim",
23
+ cutMarginMm: 0,
24
+ viewPadding: 140,
25
+ minMm: 10,
26
+ maxMm: 2000,
27
+ stepMm: 0.1,
28
+ };
29
+ function clamp(value, min, max) {
30
+ return Math.max(min, Math.min(max, value));
31
+ }
32
+ function roundToStep(value, step) {
33
+ if (!Number.isFinite(step) || step <= 0)
34
+ return value;
35
+ return Math.round(value / step) * step;
36
+ }
37
+ function sanitizeMmValue(valueMm, limits) {
38
+ if (!Number.isFinite(valueMm))
39
+ return limits.minMm;
40
+ const rounded = roundToStep(valueMm, limits.stepMm);
41
+ return clamp(rounded, limits.minMm, limits.maxMm);
42
+ }
43
+ function normalizeUnit(value) {
44
+ if (value === "cm" || value === "in")
45
+ return value;
46
+ return "mm";
47
+ }
48
+ function normalizeConstraintMode(value) {
49
+ if (value === "lockAspect" || value === "equal")
50
+ return value;
51
+ return "free";
52
+ }
53
+ function normalizeCutMode(value) {
54
+ if (value === "outset" || value === "inset")
55
+ return value;
56
+ return "trim";
57
+ }
58
+ function toMm(value, fromUnit) {
59
+ return coordinate_1.Coordinate.convertUnit(value, fromUnit, "mm");
60
+ }
61
+ function fromMm(valueMm, toUnit) {
62
+ return coordinate_1.Coordinate.convertUnit(valueMm, "mm", toUnit);
63
+ }
64
+ function resolvePaddingPx(raw, containerWidth, containerHeight) {
65
+ if (typeof raw === "number")
66
+ return Math.max(0, raw);
67
+ if (typeof raw === "string") {
68
+ if (raw.endsWith("%")) {
69
+ const percent = parseFloat(raw) / 100;
70
+ if (!Number.isFinite(percent))
71
+ return 0;
72
+ return Math.max(0, Math.min(containerWidth, containerHeight) * percent);
73
+ }
74
+ const fixed = parseFloat(raw);
75
+ return Number.isFinite(fixed) ? Math.max(0, fixed) : 0;
76
+ }
77
+ return 0;
78
+ }
79
+ function readSizeState(configService) {
80
+ const unit = normalizeUnit(configService.get("size.unit", DEFAULT_SIZE_STATE.unit));
81
+ const minMm = Math.max(0.1, Number(configService.get("size.minMm", DEFAULT_SIZE_STATE.minMm)));
82
+ const maxMm = Math.max(minMm, Number(configService.get("size.maxMm", DEFAULT_SIZE_STATE.maxMm)));
83
+ const stepMm = Math.max(0.001, Number(configService.get("size.stepMm", DEFAULT_SIZE_STATE.stepMm)));
84
+ const actualWidthMm = sanitizeMmValue((0, units_1.parseLengthToMm)(configService.get("size.actualWidthMm", DEFAULT_SIZE_STATE.actualWidthMm), "mm"), { minMm, maxMm, stepMm });
85
+ const actualHeightMm = sanitizeMmValue((0, units_1.parseLengthToMm)(configService.get("size.actualHeightMm", DEFAULT_SIZE_STATE.actualHeightMm), "mm"), { minMm, maxMm, stepMm });
86
+ const aspectRaw = Number(configService.get("size.aspectRatio", DEFAULT_SIZE_STATE.aspectRatio));
87
+ const aspectRatio = Number.isFinite(aspectRaw) && aspectRaw > 0
88
+ ? aspectRaw
89
+ : actualWidthMm / Math.max(0.001, actualHeightMm);
90
+ const cutMarginMm = Math.max(0, (0, units_1.parseLengthToMm)(configService.get("size.cutMarginMm", DEFAULT_SIZE_STATE.cutMarginMm), "mm"));
91
+ const viewPadding = configService.get("size.viewPadding", DEFAULT_SIZE_STATE.viewPadding);
92
+ return {
93
+ unit,
94
+ actualWidthMm,
95
+ actualHeightMm,
96
+ constraintMode: normalizeConstraintMode(configService.get("size.constraintMode", DEFAULT_SIZE_STATE.constraintMode)),
97
+ aspectRatio,
98
+ cutMode: normalizeCutMode(configService.get("size.cutMode", DEFAULT_SIZE_STATE.cutMode)),
99
+ cutMarginMm,
100
+ viewPadding,
101
+ minMm,
102
+ maxMm,
103
+ stepMm,
104
+ };
105
+ }
106
+ function rectByCenter(centerX, centerY, width, height) {
107
+ return {
108
+ left: centerX - width / 2,
109
+ top: centerY - height / 2,
110
+ width,
111
+ height,
112
+ centerX,
113
+ centerY,
114
+ };
115
+ }
116
+ function getCutSizeMm(size) {
117
+ if (size.cutMode === "trim") {
118
+ return { widthMm: size.actualWidthMm, heightMm: size.actualHeightMm };
119
+ }
120
+ const delta = size.cutMarginMm * 2;
121
+ if (size.cutMode === "outset") {
122
+ return {
123
+ widthMm: size.actualWidthMm + delta,
124
+ heightMm: size.actualHeightMm + delta,
125
+ };
126
+ }
127
+ return {
128
+ widthMm: Math.max(size.minMm, size.actualWidthMm - delta),
129
+ heightMm: Math.max(size.minMm, size.actualHeightMm - delta),
130
+ };
131
+ }
132
+ function computeSceneLayout(canvasService, size) {
133
+ const canvasWidth = canvasService.canvas.width || 0;
134
+ const canvasHeight = canvasService.canvas.height || 0;
135
+ if (canvasWidth <= 0 || canvasHeight <= 0)
136
+ return null;
137
+ const { widthMm: cutWidthMm, heightMm: cutHeightMm } = getCutSizeMm(size);
138
+ const viewWidthMm = Math.max(size.actualWidthMm, cutWidthMm);
139
+ const viewHeightMm = Math.max(size.actualHeightMm, cutHeightMm);
140
+ if (!Number.isFinite(viewWidthMm) ||
141
+ !Number.isFinite(viewHeightMm) ||
142
+ viewWidthMm <= 0 ||
143
+ viewHeightMm <= 0) {
144
+ return null;
145
+ }
146
+ const paddingPx = resolvePaddingPx(size.viewPadding, canvasWidth, canvasHeight);
147
+ canvasService.viewport.updateContainer(canvasWidth, canvasHeight);
148
+ canvasService.viewport.setPadding(paddingPx);
149
+ canvasService.viewport.updatePhysical(viewWidthMm, viewHeightMm);
150
+ const layout = canvasService.viewport.layout;
151
+ if (!Number.isFinite(layout.scale) ||
152
+ !Number.isFinite(layout.offsetX) ||
153
+ !Number.isFinite(layout.offsetY) ||
154
+ layout.scale <= 0) {
155
+ return null;
156
+ }
157
+ const centerX = layout.offsetX + layout.width / 2;
158
+ const centerY = layout.offsetY + layout.height / 2;
159
+ const trimWidthPx = size.actualWidthMm * layout.scale;
160
+ const trimHeightPx = size.actualHeightMm * layout.scale;
161
+ const cutWidthPx = cutWidthMm * layout.scale;
162
+ const cutHeightPx = cutHeightMm * layout.scale;
163
+ const trimRect = rectByCenter(centerX, centerY, trimWidthPx, trimHeightPx);
164
+ const cutRect = rectByCenter(centerX, centerY, cutWidthPx, cutHeightPx);
165
+ const bleedRect = rectByCenter(centerX, centerY, Math.max(trimWidthPx, cutWidthPx), Math.max(trimHeightPx, cutHeightPx));
166
+ return {
167
+ scale: layout.scale,
168
+ canvasWidth,
169
+ canvasHeight,
170
+ trimRect,
171
+ cutRect,
172
+ bleedRect,
173
+ trimWidthMm: size.actualWidthMm,
174
+ trimHeightMm: size.actualHeightMm,
175
+ cutWidthMm,
176
+ cutHeightMm,
177
+ cutMode: size.cutMode,
178
+ cutMarginMm: size.cutMarginMm,
179
+ };
180
+ }
181
+ function buildSceneGeometry(configService, layout) {
182
+ const radiusMm = (0, units_1.parseLengthToMm)(configService.get("dieline.radius", 0), "mm");
183
+ const offset = (layout.cutRect.width - layout.trimRect.width) / 2;
184
+ const sourceWidth = Number(configService.get("dieline.customSourceWidthPx", 0));
185
+ const sourceHeight = Number(configService.get("dieline.customSourceHeightPx", 0));
186
+ const shapeStyle = (0, dielineShape_1.normalizeShapeStyle)(configService.get("dieline.shapeStyle", dielineShape_1.DEFAULT_DIELINE_SHAPE_STYLE));
187
+ return {
188
+ shape: (0, dielineShape_1.normalizeDielineShape)(configService.get("dieline.shape", dielineShape_1.DEFAULT_DIELINE_SHAPE)),
189
+ shapeStyle,
190
+ unit: "px",
191
+ x: layout.trimRect.centerX,
192
+ y: layout.trimRect.centerY,
193
+ width: layout.trimRect.width,
194
+ height: layout.trimRect.height,
195
+ radius: radiusMm * layout.scale,
196
+ offset,
197
+ scale: layout.scale,
198
+ pathData: configService.get("dieline.pathData"),
199
+ customSourceWidthPx: Number.isFinite(sourceWidth) && sourceWidth > 0 ? sourceWidth : undefined,
200
+ customSourceHeightPx: Number.isFinite(sourceHeight) && sourceHeight > 0 ? sourceHeight : undefined,
201
+ };
202
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SceneVisibilityService = void 0;
4
+ const core_1 = require("@pooder/core");
5
+ const CANVAS_SERVICE_ID = "CanvasService";
6
+ const HIDDEN_DIELINE_TOOLS = new Set(["pooder.kit.image", "pooder.kit.white-ink"]);
7
+ const HIDDEN_RULER_TOOLS = new Set(["pooder.kit.white-ink"]);
8
+ class SceneVisibilityService {
9
+ constructor() {
10
+ this.activeToolId = null;
11
+ this.onToolActivated = (e) => {
12
+ this.activeToolId = e.id;
13
+ this.apply();
14
+ };
15
+ this.onObjectAdded = () => {
16
+ this.apply();
17
+ };
18
+ }
19
+ init(context) {
20
+ if (this.context) {
21
+ this.dispose(this.context);
22
+ }
23
+ const canvasService = context.get(CANVAS_SERVICE_ID);
24
+ if (!canvasService) {
25
+ throw new Error("[SceneVisibilityService] CanvasService is required.");
26
+ }
27
+ this.context = context;
28
+ this.canvasService = canvasService;
29
+ this.activeToolId = context.get(core_1.WORKBENCH_SERVICE)?.activeToolId ?? null;
30
+ context.eventBus.on("tool:activated", this.onToolActivated);
31
+ context.eventBus.on("object:added", this.onObjectAdded);
32
+ this.apply();
33
+ }
34
+ dispose(context) {
35
+ const activeContext = this.context ?? context;
36
+ activeContext.eventBus.off("tool:activated", this.onToolActivated);
37
+ activeContext.eventBus.off("object:added", this.onObjectAdded);
38
+ this.context = undefined;
39
+ this.activeToolId = null;
40
+ this.canvasService = undefined;
41
+ }
42
+ apply() {
43
+ if (!this.canvasService)
44
+ return;
45
+ const dielineLayer = this.canvasService.getLayer("dieline-overlay");
46
+ if (dielineLayer) {
47
+ const visible = !HIDDEN_DIELINE_TOOLS.has(this.activeToolId || "");
48
+ this.canvasService.setLayerVisibility("dieline-overlay", visible);
49
+ }
50
+ const rulerVisible = !HIDDEN_RULER_TOOLS.has(this.activeToolId || "");
51
+ this.canvasService.setLayerVisibility("ruler-overlay", rulerVisible);
52
+ this.canvasService.requestRenderAll();
53
+ }
54
+ }
55
+ exports.SceneVisibilityService = SceneVisibilityService;
@@ -0,0 +1,331 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SizeTool = void 0;
4
+ const core_1 = require("@pooder/core");
5
+ const sceneLayoutModel_1 = require("./sceneLayoutModel");
6
+ class SizeTool {
7
+ constructor() {
8
+ this.id = "pooder.kit.size";
9
+ this.metadata = {
10
+ name: "SizeTool",
11
+ };
12
+ }
13
+ activate(context) {
14
+ this.context = context;
15
+ this.canvasService = context.services.get("CanvasService");
16
+ const configService = context.services.get("ConfigurationService");
17
+ if (!configService)
18
+ return;
19
+ this.ensureDefaults(configService);
20
+ this.emitStateChanged();
21
+ }
22
+ deactivate(_context) {
23
+ this.context = undefined;
24
+ this.canvasService = undefined;
25
+ }
26
+ contribute() {
27
+ return {
28
+ [core_1.ContributionPointIds.TOOLS]: [
29
+ {
30
+ id: this.id,
31
+ name: "Size",
32
+ interaction: "instant",
33
+ },
34
+ ],
35
+ [core_1.ContributionPointIds.CONFIGURATIONS]: [
36
+ {
37
+ id: "size.unit",
38
+ type: "select",
39
+ label: "Display Unit",
40
+ options: ["mm", "cm", "in"],
41
+ default: "mm",
42
+ },
43
+ {
44
+ id: "size.actualWidthMm",
45
+ type: "number",
46
+ label: "Actual Width (mm)",
47
+ min: 10,
48
+ max: 2000,
49
+ step: 0.1,
50
+ default: 500,
51
+ },
52
+ {
53
+ id: "size.actualHeightMm",
54
+ type: "number",
55
+ label: "Actual Height (mm)",
56
+ min: 10,
57
+ max: 2000,
58
+ step: 0.1,
59
+ default: 500,
60
+ },
61
+ {
62
+ id: "size.constraintMode",
63
+ type: "select",
64
+ label: "Constraint Mode",
65
+ options: ["free", "lockAspect", "equal"],
66
+ default: "free",
67
+ },
68
+ {
69
+ id: "size.aspectRatio",
70
+ type: "number",
71
+ label: "Aspect Ratio",
72
+ min: 0.01,
73
+ max: 100,
74
+ step: 0.01,
75
+ default: 1,
76
+ },
77
+ {
78
+ id: "size.cutMode",
79
+ type: "select",
80
+ label: "Cut Mode",
81
+ options: ["trim", "outset", "inset"],
82
+ default: "trim",
83
+ },
84
+ {
85
+ id: "size.cutMarginMm",
86
+ type: "number",
87
+ label: "Cut Margin (mm)",
88
+ min: 0,
89
+ max: 100,
90
+ step: 0.1,
91
+ default: 0,
92
+ },
93
+ {
94
+ id: "size.viewPadding",
95
+ type: "select",
96
+ label: "View Padding",
97
+ options: [0, 10, 20, 40, 60, 100, "2%", "5%", "10%", "15%", "20%"],
98
+ default: 140,
99
+ },
100
+ {
101
+ id: "size.minMm",
102
+ type: "number",
103
+ label: "Min Size (mm)",
104
+ min: 0.1,
105
+ max: 2000,
106
+ step: 0.1,
107
+ default: 10,
108
+ },
109
+ {
110
+ id: "size.maxMm",
111
+ type: "number",
112
+ label: "Max Size (mm)",
113
+ min: 1,
114
+ max: 10000,
115
+ step: 1,
116
+ default: 2000,
117
+ },
118
+ {
119
+ id: "size.stepMm",
120
+ type: "number",
121
+ label: "Size Step (mm)",
122
+ min: 0.001,
123
+ max: 100,
124
+ step: 0.001,
125
+ default: 0.1,
126
+ },
127
+ ],
128
+ [core_1.ContributionPointIds.COMMANDS]: [
129
+ {
130
+ command: "getSizeState",
131
+ title: "Get Size State",
132
+ handler: () => this.getStateForUI(),
133
+ },
134
+ {
135
+ command: "updateSizeDimensions",
136
+ title: "Update Size Dimensions",
137
+ handler: (input = {}) => this.updateDimensions(input),
138
+ },
139
+ {
140
+ command: "setSizeConstraintMode",
141
+ title: "Set Size Constraint Mode",
142
+ handler: (mode) => this.setConstraintMode(mode),
143
+ },
144
+ {
145
+ command: "setSizeDisplayUnit",
146
+ title: "Set Size Display Unit",
147
+ handler: (unit) => this.setUnit(unit),
148
+ },
149
+ {
150
+ command: "setSizeCut",
151
+ title: "Set Size Cut",
152
+ handler: (cutMode, cutMarginMm = 0) => this.setCut(cutMode, cutMarginMm),
153
+ },
154
+ {
155
+ command: "getSelectedImageSize",
156
+ title: "Get Selected Image Size",
157
+ handler: (id) => this.getSelectedImageSize(id),
158
+ },
159
+ ],
160
+ };
161
+ }
162
+ getConfigService() {
163
+ return this.context?.services.get("ConfigurationService");
164
+ }
165
+ ensureDefaults(configService) {
166
+ const state = (0, sceneLayoutModel_1.readSizeState)(configService);
167
+ configService.update("size.unit", state.unit);
168
+ configService.update("size.actualWidthMm", state.actualWidthMm);
169
+ configService.update("size.actualHeightMm", state.actualHeightMm);
170
+ configService.update("size.constraintMode", state.constraintMode);
171
+ configService.update("size.aspectRatio", state.actualWidthMm / Math.max(0.001, state.actualHeightMm));
172
+ configService.update("size.cutMode", state.cutMode);
173
+ configService.update("size.cutMarginMm", state.cutMarginMm);
174
+ configService.update("size.viewPadding", state.viewPadding);
175
+ configService.update("size.minMm", state.minMm);
176
+ configService.update("size.maxMm", state.maxMm);
177
+ configService.update("size.stepMm", state.stepMm);
178
+ }
179
+ emitStateChanged() {
180
+ const state = this.getStateForUI();
181
+ if (!state)
182
+ return;
183
+ this.context?.eventBus.emit("size:state:changed", state);
184
+ }
185
+ getStateForUI() {
186
+ const configService = this.getConfigService();
187
+ if (!configService)
188
+ return null;
189
+ const state = (0, sceneLayoutModel_1.readSizeState)(configService);
190
+ return {
191
+ ...state,
192
+ actualWidth: (0, sceneLayoutModel_1.fromMm)(state.actualWidthMm, state.unit),
193
+ actualHeight: (0, sceneLayoutModel_1.fromMm)(state.actualHeightMm, state.unit),
194
+ };
195
+ }
196
+ updateDimensions(input) {
197
+ const configService = this.getConfigService();
198
+ if (!configService)
199
+ return null;
200
+ const state = (0, sceneLayoutModel_1.readSizeState)(configService);
201
+ const inputUnit = (0, sceneLayoutModel_1.normalizeUnit)(input.unit ?? state.unit);
202
+ const changed = input.changed || "both";
203
+ const providedWidthMm = Number.isFinite(input.width)
204
+ ? (0, sceneLayoutModel_1.toMm)(Number(input.width), inputUnit)
205
+ : undefined;
206
+ const providedHeightMm = Number.isFinite(input.height)
207
+ ? (0, sceneLayoutModel_1.toMm)(Number(input.height), inputUnit)
208
+ : undefined;
209
+ const limits = {
210
+ minMm: state.minMm,
211
+ maxMm: state.maxMm,
212
+ stepMm: state.stepMm,
213
+ };
214
+ let nextWidthMm = providedWidthMm !== undefined ? providedWidthMm : state.actualWidthMm;
215
+ let nextHeightMm = providedHeightMm !== undefined ? providedHeightMm : state.actualHeightMm;
216
+ if (state.constraintMode === "equal") {
217
+ const anchor = changed === "height"
218
+ ? nextHeightMm
219
+ : changed === "width"
220
+ ? nextWidthMm
221
+ : (providedWidthMm ?? providedHeightMm ?? nextWidthMm);
222
+ nextWidthMm = anchor;
223
+ nextHeightMm = anchor;
224
+ }
225
+ else if (state.constraintMode === "lockAspect") {
226
+ const ratio = Math.max(0.0001, state.aspectRatio);
227
+ if (changed === "height") {
228
+ nextWidthMm = nextHeightMm * ratio;
229
+ }
230
+ else {
231
+ nextHeightMm = nextWidthMm / ratio;
232
+ }
233
+ }
234
+ nextWidthMm = (0, sceneLayoutModel_1.sanitizeMmValue)(nextWidthMm, limits);
235
+ nextHeightMm = (0, sceneLayoutModel_1.sanitizeMmValue)(nextHeightMm, limits);
236
+ if (state.constraintMode === "equal") {
237
+ const value = Math.max(nextWidthMm, nextHeightMm);
238
+ nextWidthMm = value;
239
+ nextHeightMm = value;
240
+ }
241
+ else if (state.constraintMode === "lockAspect") {
242
+ const ratio = Math.max(0.0001, state.aspectRatio);
243
+ if (changed === "height") {
244
+ nextWidthMm = (0, sceneLayoutModel_1.sanitizeMmValue)(nextHeightMm * ratio, limits);
245
+ }
246
+ else {
247
+ nextHeightMm = (0, sceneLayoutModel_1.sanitizeMmValue)(nextWidthMm / ratio, limits);
248
+ }
249
+ }
250
+ configService.update("size.actualWidthMm", nextWidthMm);
251
+ configService.update("size.actualHeightMm", nextHeightMm);
252
+ configService.update("size.unit", inputUnit);
253
+ this.emitStateChanged();
254
+ return this.getStateForUI();
255
+ }
256
+ setConstraintMode(modeRaw) {
257
+ const configService = this.getConfigService();
258
+ if (!configService)
259
+ return null;
260
+ const state = (0, sceneLayoutModel_1.readSizeState)(configService);
261
+ const mode = (0, sceneLayoutModel_1.normalizeConstraintMode)(modeRaw);
262
+ configService.update("size.constraintMode", mode);
263
+ if (mode === "lockAspect") {
264
+ const ratio = state.actualWidthMm / Math.max(0.001, state.actualHeightMm);
265
+ configService.update("size.aspectRatio", ratio);
266
+ }
267
+ if (mode === "equal") {
268
+ const value = (0, sceneLayoutModel_1.sanitizeMmValue)(Math.max(state.actualWidthMm, state.actualHeightMm), {
269
+ minMm: state.minMm,
270
+ maxMm: state.maxMm,
271
+ stepMm: state.stepMm,
272
+ });
273
+ configService.update("size.actualWidthMm", value);
274
+ configService.update("size.actualHeightMm", value);
275
+ configService.update("size.aspectRatio", 1);
276
+ }
277
+ this.emitStateChanged();
278
+ return this.getStateForUI();
279
+ }
280
+ setUnit(unitRaw) {
281
+ const configService = this.getConfigService();
282
+ if (!configService)
283
+ return null;
284
+ const unit = (0, sceneLayoutModel_1.normalizeUnit)(unitRaw);
285
+ configService.update("size.unit", unit);
286
+ this.emitStateChanged();
287
+ return this.getStateForUI();
288
+ }
289
+ setCut(cutModeRaw, cutMarginMm = 0) {
290
+ const configService = this.getConfigService();
291
+ if (!configService)
292
+ return null;
293
+ const cutMode = (0, sceneLayoutModel_1.normalizeCutMode)(cutModeRaw);
294
+ const margin = Math.max(0, Number(cutMarginMm) || 0);
295
+ configService.update("size.cutMode", cutMode);
296
+ configService.update("size.cutMarginMm", margin);
297
+ this.emitStateChanged();
298
+ return this.getStateForUI();
299
+ }
300
+ getSelectedImageSize(id) {
301
+ const configService = this.getConfigService();
302
+ if (!configService || !this.canvasService)
303
+ return null;
304
+ const sizeState = (0, sceneLayoutModel_1.readSizeState)(configService);
305
+ const layout = (0, sceneLayoutModel_1.computeSceneLayout)(this.canvasService, sizeState);
306
+ if (!layout || layout.scale <= 0)
307
+ return null;
308
+ const all = this.canvasService.canvas.getObjects();
309
+ const active = this.canvasService.canvas.getActiveObject();
310
+ const activeId = active?.data?.layerId === "image.user" ? active?.data?.id : null;
311
+ const targetId = id || activeId;
312
+ const target = all.find((obj) => obj?.data?.layerId === "image.user" && obj?.data?.id === targetId) || all.find((obj) => obj?.data?.layerId === "image.user");
313
+ if (!target)
314
+ return null;
315
+ const objectWidthPx = Math.abs((target.width || 0) * (target.scaleX || 1));
316
+ const objectHeightPx = Math.abs((target.height || 0) * (target.scaleY || 1));
317
+ if (objectWidthPx <= 0 || objectHeightPx <= 0)
318
+ return null;
319
+ const widthMm = objectWidthPx / layout.scale;
320
+ const heightMm = objectHeightPx / layout.scale;
321
+ return {
322
+ id: target?.data?.id || null,
323
+ widthMm,
324
+ heightMm,
325
+ width: (0, sceneLayoutModel_1.fromMm)(widthMm, sizeState.unit),
326
+ height: (0, sceneLayoutModel_1.fromMm)(heightMm, sizeState.unit),
327
+ unit: sizeState.unit,
328
+ };
329
+ }
330
+ }
331
+ exports.SizeTool = SizeTool;