@pooder/kit 4.3.1 → 5.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.
- package/.test-dist/src/CanvasService.js +249 -0
- package/.test-dist/src/ViewportSystem.js +75 -0
- package/.test-dist/src/background.js +203 -0
- package/.test-dist/src/bridgeSelection.js +20 -0
- package/.test-dist/src/constraints.js +237 -0
- package/.test-dist/src/coordinate.js +74 -0
- package/.test-dist/src/dieline.js +818 -0
- package/.test-dist/src/edgeScale.js +12 -0
- package/.test-dist/src/feature.js +754 -0
- package/.test-dist/src/featureComplete.js +32 -0
- package/.test-dist/src/film.js +167 -0
- package/.test-dist/src/geometry.js +506 -0
- package/.test-dist/src/image.js +1234 -0
- package/.test-dist/src/index.js +35 -0
- package/.test-dist/src/maskOps.js +270 -0
- package/.test-dist/src/mirror.js +104 -0
- package/.test-dist/src/renderSpec.js +2 -0
- package/.test-dist/src/ruler.js +343 -0
- package/.test-dist/src/sceneLayout.js +99 -0
- package/.test-dist/src/sceneLayoutModel.js +196 -0
- package/.test-dist/src/sceneView.js +40 -0
- package/.test-dist/src/sceneVisibility.js +42 -0
- package/.test-dist/src/size.js +332 -0
- package/.test-dist/src/tracer.js +544 -0
- package/.test-dist/src/units.js +30 -0
- package/.test-dist/src/white-ink.js +829 -0
- package/.test-dist/src/wrappedOffsets.js +33 -0
- package/.test-dist/tests/run.js +94 -0
- package/CHANGELOG.md +17 -0
- package/dist/index.d.mts +342 -37
- package/dist/index.d.ts +342 -37
- package/dist/index.js +3679 -865
- package/dist/index.mjs +3673 -868
- package/package.json +2 -2
- package/src/CanvasService.ts +300 -96
- package/src/ViewportSystem.ts +92 -92
- package/src/background.ts +230 -230
- package/src/bridgeSelection.ts +17 -0
- package/src/coordinate.ts +106 -106
- package/src/dieline.ts +1005 -973
- package/src/edgeScale.ts +19 -0
- package/src/feature.ts +83 -30
- package/src/film.ts +194 -194
- package/src/geometry.ts +242 -84
- package/src/image.ts +1582 -512
- package/src/index.ts +14 -10
- package/src/maskOps.ts +326 -0
- package/src/mirror.ts +128 -128
- package/src/renderSpec.ts +18 -0
- package/src/ruler.ts +449 -508
- package/src/sceneLayout.ts +121 -0
- package/src/sceneLayoutModel.ts +335 -0
- package/src/sceneVisibility.ts +49 -0
- package/src/size.ts +379 -0
- package/src/tracer.ts +719 -570
- package/src/units.ts +27 -27
- package/src/white-ink.ts +1018 -373
- package/src/wrappedOffsets.ts +33 -0
- package/tests/run.ts +118 -0
- package/tsconfig.test.json +15 -15
package/src/size.ts
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CommandContribution,
|
|
3
|
+
ConfigurationContribution,
|
|
4
|
+
ConfigurationService,
|
|
5
|
+
ContributionPointIds,
|
|
6
|
+
Extension,
|
|
7
|
+
ExtensionContext,
|
|
8
|
+
} from "@pooder/core";
|
|
9
|
+
import CanvasService from "./CanvasService";
|
|
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>("ConfigurationService");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private ensureDefaults(configService: ConfigurationService) {
|
|
201
|
+
const state = readSizeState(configService);
|
|
202
|
+
configService.update("size.unit", state.unit);
|
|
203
|
+
configService.update("size.actualWidthMm", state.actualWidthMm);
|
|
204
|
+
configService.update("size.actualHeightMm", state.actualHeightMm);
|
|
205
|
+
configService.update("size.constraintMode", state.constraintMode);
|
|
206
|
+
configService.update(
|
|
207
|
+
"size.aspectRatio",
|
|
208
|
+
state.actualWidthMm / Math.max(0.001, state.actualHeightMm),
|
|
209
|
+
);
|
|
210
|
+
configService.update("size.cutMode", state.cutMode);
|
|
211
|
+
configService.update("size.cutMarginMm", state.cutMarginMm);
|
|
212
|
+
configService.update("size.viewPadding", state.viewPadding);
|
|
213
|
+
configService.update("size.minMm", state.minMm);
|
|
214
|
+
configService.update("size.maxMm", state.maxMm);
|
|
215
|
+
configService.update("size.stepMm", state.stepMm);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private emitStateChanged() {
|
|
219
|
+
const state = this.getStateForUI();
|
|
220
|
+
if (!state) return;
|
|
221
|
+
this.context?.eventBus.emit("size:state:changed", state);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private getStateForUI() {
|
|
225
|
+
const configService = this.getConfigService();
|
|
226
|
+
if (!configService) return null;
|
|
227
|
+
const state = readSizeState(configService);
|
|
228
|
+
return {
|
|
229
|
+
...state,
|
|
230
|
+
actualWidth: fromMm(state.actualWidthMm, state.unit),
|
|
231
|
+
actualHeight: fromMm(state.actualHeightMm, state.unit),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private updateDimensions(input: UpdateSizeDimensionsInput) {
|
|
236
|
+
const configService = this.getConfigService();
|
|
237
|
+
if (!configService) return null;
|
|
238
|
+
|
|
239
|
+
const state = readSizeState(configService);
|
|
240
|
+
const inputUnit = normalizeUnit(input.unit ?? state.unit);
|
|
241
|
+
const changed: ChangedField = input.changed || "both";
|
|
242
|
+
|
|
243
|
+
const providedWidthMm = Number.isFinite(input.width as any)
|
|
244
|
+
? toMm(Number(input.width), inputUnit)
|
|
245
|
+
: undefined;
|
|
246
|
+
const providedHeightMm = Number.isFinite(input.height as any)
|
|
247
|
+
? toMm(Number(input.height), inputUnit)
|
|
248
|
+
: undefined;
|
|
249
|
+
|
|
250
|
+
const limits = {
|
|
251
|
+
minMm: state.minMm,
|
|
252
|
+
maxMm: state.maxMm,
|
|
253
|
+
stepMm: state.stepMm,
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
let nextWidthMm =
|
|
257
|
+
providedWidthMm !== undefined ? providedWidthMm : state.actualWidthMm;
|
|
258
|
+
let nextHeightMm =
|
|
259
|
+
providedHeightMm !== undefined ? providedHeightMm : state.actualHeightMm;
|
|
260
|
+
|
|
261
|
+
if (state.constraintMode === "equal") {
|
|
262
|
+
const anchor =
|
|
263
|
+
changed === "height"
|
|
264
|
+
? nextHeightMm
|
|
265
|
+
: changed === "width"
|
|
266
|
+
? nextWidthMm
|
|
267
|
+
: providedWidthMm ?? providedHeightMm ?? nextWidthMm;
|
|
268
|
+
nextWidthMm = anchor;
|
|
269
|
+
nextHeightMm = anchor;
|
|
270
|
+
} else if (state.constraintMode === "lockAspect") {
|
|
271
|
+
const ratio = Math.max(0.0001, state.aspectRatio);
|
|
272
|
+
if (changed === "height") {
|
|
273
|
+
nextWidthMm = nextHeightMm * ratio;
|
|
274
|
+
} else {
|
|
275
|
+
nextHeightMm = nextWidthMm / ratio;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
nextWidthMm = sanitizeMmValue(nextWidthMm, limits);
|
|
280
|
+
nextHeightMm = sanitizeMmValue(nextHeightMm, limits);
|
|
281
|
+
|
|
282
|
+
if (state.constraintMode === "equal") {
|
|
283
|
+
const value = Math.max(nextWidthMm, nextHeightMm);
|
|
284
|
+
nextWidthMm = value;
|
|
285
|
+
nextHeightMm = value;
|
|
286
|
+
} else if (state.constraintMode === "lockAspect") {
|
|
287
|
+
const ratio = Math.max(0.0001, state.aspectRatio);
|
|
288
|
+
if (changed === "height") {
|
|
289
|
+
nextWidthMm = sanitizeMmValue(nextHeightMm * ratio, limits);
|
|
290
|
+
} else {
|
|
291
|
+
nextHeightMm = sanitizeMmValue(nextWidthMm / ratio, limits);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
configService.update("size.actualWidthMm", nextWidthMm);
|
|
296
|
+
configService.update("size.actualHeightMm", nextHeightMm);
|
|
297
|
+
configService.update("size.unit", inputUnit);
|
|
298
|
+
this.emitStateChanged();
|
|
299
|
+
return this.getStateForUI();
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private setConstraintMode(modeRaw: string) {
|
|
303
|
+
const configService = this.getConfigService();
|
|
304
|
+
if (!configService) return null;
|
|
305
|
+
const state = readSizeState(configService);
|
|
306
|
+
const mode = normalizeConstraintMode(modeRaw);
|
|
307
|
+
|
|
308
|
+
configService.update("size.constraintMode", mode);
|
|
309
|
+
if (mode === "lockAspect") {
|
|
310
|
+
const ratio = state.actualWidthMm / Math.max(0.001, state.actualHeightMm);
|
|
311
|
+
configService.update("size.aspectRatio", ratio);
|
|
312
|
+
}
|
|
313
|
+
if (mode === "equal") {
|
|
314
|
+
const value = sanitizeMmValue(Math.max(state.actualWidthMm, state.actualHeightMm), {
|
|
315
|
+
minMm: state.minMm,
|
|
316
|
+
maxMm: state.maxMm,
|
|
317
|
+
stepMm: state.stepMm,
|
|
318
|
+
});
|
|
319
|
+
configService.update("size.actualWidthMm", value);
|
|
320
|
+
configService.update("size.actualHeightMm", value);
|
|
321
|
+
configService.update("size.aspectRatio", 1);
|
|
322
|
+
}
|
|
323
|
+
this.emitStateChanged();
|
|
324
|
+
return this.getStateForUI();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
private setUnit(unitRaw: string) {
|
|
328
|
+
const configService = this.getConfigService();
|
|
329
|
+
if (!configService) return null;
|
|
330
|
+
const unit = normalizeUnit(unitRaw);
|
|
331
|
+
configService.update("size.unit", unit);
|
|
332
|
+
this.emitStateChanged();
|
|
333
|
+
return this.getStateForUI();
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private setCut(cutModeRaw: string, cutMarginMm = 0) {
|
|
337
|
+
const configService = this.getConfigService();
|
|
338
|
+
if (!configService) return null;
|
|
339
|
+
const cutMode = normalizeCutMode(cutModeRaw);
|
|
340
|
+
const margin = Math.max(0, Number(cutMarginMm) || 0);
|
|
341
|
+
configService.update("size.cutMode", cutMode);
|
|
342
|
+
configService.update("size.cutMarginMm", margin);
|
|
343
|
+
this.emitStateChanged();
|
|
344
|
+
return this.getStateForUI();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
private getSelectedImageSize(id?: string) {
|
|
348
|
+
const configService = this.getConfigService();
|
|
349
|
+
if (!configService || !this.canvasService) return null;
|
|
350
|
+
const sizeState = readSizeState(configService);
|
|
351
|
+
const layout = computeSceneLayout(this.canvasService, sizeState);
|
|
352
|
+
if (!layout || layout.scale <= 0) return null;
|
|
353
|
+
|
|
354
|
+
const all = this.canvasService.canvas.getObjects() as any[];
|
|
355
|
+
const active = this.canvasService.canvas.getActiveObject() as any;
|
|
356
|
+
const activeId = active?.data?.layerId === "image.user" ? active?.data?.id : null;
|
|
357
|
+
const targetId = id || activeId;
|
|
358
|
+
const target =
|
|
359
|
+
all.find((obj) => obj?.data?.layerId === "image.user" && obj?.data?.id === targetId) ||
|
|
360
|
+
all.find((obj) => obj?.data?.layerId === "image.user");
|
|
361
|
+
if (!target) return null;
|
|
362
|
+
|
|
363
|
+
const objectWidthPx = Math.abs((target.width || 0) * (target.scaleX || 1));
|
|
364
|
+
const objectHeightPx = Math.abs((target.height || 0) * (target.scaleY || 1));
|
|
365
|
+
if (objectWidthPx <= 0 || objectHeightPx <= 0) return null;
|
|
366
|
+
|
|
367
|
+
const widthMm = objectWidthPx / layout.scale;
|
|
368
|
+
const heightMm = objectHeightPx / layout.scale;
|
|
369
|
+
|
|
370
|
+
return {
|
|
371
|
+
id: target?.data?.id || null,
|
|
372
|
+
widthMm,
|
|
373
|
+
heightMm,
|
|
374
|
+
width: fromMm(widthMm, sizeState.unit),
|
|
375
|
+
height: fromMm(heightMm, sizeState.unit),
|
|
376
|
+
unit: sizeState.unit,
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
}
|