@pooder/kit 4.0.0 → 4.1.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.
package/src/coordinate.ts CHANGED
@@ -1,106 +1,106 @@
1
- export interface Point {
2
- x: number;
3
- y: number;
4
- }
5
-
6
- export interface Size {
7
- width: number;
8
- height: number;
9
- }
10
-
11
- export type Unit = "px" | "mm" | "cm" | "in";
12
-
13
- export interface Layout {
14
- scale: number;
15
- offsetX: number;
16
- offsetY: number;
17
- width: number;
18
- height: number;
19
- }
20
-
21
- export class Coordinate {
22
- /**
23
- * Calculate layout to fit content within container while preserving aspect ratio.
24
- */
25
- static calculateLayout(
26
- container: Size,
27
- content: Size,
28
- padding: number = 0,
29
- ): Layout {
30
- const availableWidth = Math.max(0, container.width - padding * 2);
31
- const availableHeight = Math.max(0, container.height - padding * 2);
32
-
33
- if (content.width === 0 || content.height === 0) {
34
- return { scale: 1, offsetX: 0, offsetY: 0, width: 0, height: 0 };
35
- }
36
-
37
- const scaleX = availableWidth / content.width;
38
- const scaleY = availableHeight / content.height;
39
- const scale = Math.min(scaleX, scaleY);
40
-
41
- const width = content.width * scale;
42
- const height = content.height * scale;
43
-
44
- const offsetX = (container.width - width) / 2;
45
- const offsetY = (container.height - height) / 2;
46
-
47
- return { scale, offsetX, offsetY, width, height };
48
- }
49
-
50
- /**
51
- * Convert an absolute value to a normalized value (0-1).
52
- * @param value Absolute value (e.g., pixels)
53
- * @param total Total dimension size (e.g., canvas width)
54
- */
55
- static toNormalized(value: number, total: number): number {
56
- return total === 0 ? 0 : value / total;
57
- }
58
-
59
- /**
60
- * Convert a normalized value (0-1) to an absolute value.
61
- * @param normalized Normalized value (0-1)
62
- * @param total Total dimension size (e.g., canvas width)
63
- */
64
- static toAbsolute(normalized: number, total: number): number {
65
- return normalized * total;
66
- }
67
-
68
- /**
69
- * Normalize a point's coordinates.
70
- */
71
- static normalizePoint(point: Point, size: Size): Point {
72
- return {
73
- x: this.toNormalized(point.x, size.width),
74
- y: this.toNormalized(point.y, size.height),
75
- };
76
- }
77
-
78
- /**
79
- * Denormalize a point's coordinates to absolute pixels.
80
- */
81
- static denormalizePoint(point: Point, size: Size): Point {
82
- return {
83
- x: this.toAbsolute(point.x, size.width),
84
- y: this.toAbsolute(point.y, size.height),
85
- };
86
- }
87
-
88
- static convertUnit(value: number, from: Unit, to: Unit): number {
89
- if (from === to) return value;
90
-
91
- // Base unit: mm
92
- const toMM: Record<Unit, number> = {
93
- px: 0.264583, // 1px = 0.264583mm (96 DPI)
94
- mm: 1,
95
- cm: 10,
96
- in: 25.4
97
- };
98
-
99
- const mmValue = value * (from === 'px' ? toMM.px : toMM[from] || 1);
100
-
101
- if (to === 'px') {
102
- return mmValue / toMM.px;
103
- }
104
- return mmValue / (toMM[to] || 1);
105
- }
106
- }
1
+ export interface Point {
2
+ x: number;
3
+ y: number;
4
+ }
5
+
6
+ export interface Size {
7
+ width: number;
8
+ height: number;
9
+ }
10
+
11
+ export type Unit = "px" | "mm" | "cm" | "in";
12
+
13
+ export interface Layout {
14
+ scale: number;
15
+ offsetX: number;
16
+ offsetY: number;
17
+ width: number;
18
+ height: number;
19
+ }
20
+
21
+ export class Coordinate {
22
+ /**
23
+ * Calculate layout to fit content within container while preserving aspect ratio.
24
+ */
25
+ static calculateLayout(
26
+ container: Size,
27
+ content: Size,
28
+ padding: number = 0,
29
+ ): Layout {
30
+ const availableWidth = Math.max(0, container.width - padding * 2);
31
+ const availableHeight = Math.max(0, container.height - padding * 2);
32
+
33
+ if (content.width === 0 || content.height === 0) {
34
+ return { scale: 1, offsetX: 0, offsetY: 0, width: 0, height: 0 };
35
+ }
36
+
37
+ const scaleX = availableWidth / content.width;
38
+ const scaleY = availableHeight / content.height;
39
+ const scale = Math.min(scaleX, scaleY);
40
+
41
+ const width = content.width * scale;
42
+ const height = content.height * scale;
43
+
44
+ const offsetX = (container.width - width) / 2;
45
+ const offsetY = (container.height - height) / 2;
46
+
47
+ return { scale, offsetX, offsetY, width, height };
48
+ }
49
+
50
+ /**
51
+ * Convert an absolute value to a normalized value (0-1).
52
+ * @param value Absolute value (e.g., pixels)
53
+ * @param total Total dimension size (e.g., canvas width)
54
+ */
55
+ static toNormalized(value: number, total: number): number {
56
+ return total === 0 ? 0 : value / total;
57
+ }
58
+
59
+ /**
60
+ * Convert a normalized value (0-1) to an absolute value.
61
+ * @param normalized Normalized value (0-1)
62
+ * @param total Total dimension size (e.g., canvas width)
63
+ */
64
+ static toAbsolute(normalized: number, total: number): number {
65
+ return normalized * total;
66
+ }
67
+
68
+ /**
69
+ * Normalize a point's coordinates.
70
+ */
71
+ static normalizePoint(point: Point, size: Size): Point {
72
+ return {
73
+ x: this.toNormalized(point.x, size.width),
74
+ y: this.toNormalized(point.y, size.height),
75
+ };
76
+ }
77
+
78
+ /**
79
+ * Denormalize a point's coordinates to absolute pixels.
80
+ */
81
+ static denormalizePoint(point: Point, size: Size): Point {
82
+ return {
83
+ x: this.toAbsolute(point.x, size.width),
84
+ y: this.toAbsolute(point.y, size.height),
85
+ };
86
+ }
87
+
88
+ static convertUnit(value: number, from: Unit, to: Unit): number {
89
+ if (from === to) return value;
90
+
91
+ // Base unit: mm
92
+ const toMM: Record<Unit, number> = {
93
+ px: 0.264583, // 1px = 0.264583mm (96 DPI)
94
+ mm: 1,
95
+ cm: 10,
96
+ in: 25.4
97
+ };
98
+
99
+ const mmValue = value * (from === 'px' ? toMM.px : toMM[from] || 1);
100
+
101
+ if (to === 'px') {
102
+ return mmValue / toMM.px;
103
+ }
104
+ return mmValue / (toMM[to] || 1);
105
+ }
106
+ }
package/src/dieline.ts CHANGED
@@ -16,6 +16,7 @@ import {
16
16
  getPathBounds,
17
17
  DielineFeature,
18
18
  } from "./geometry";
19
+ import { ConstraintRegistry } from "./constraints";
19
20
 
20
21
  export interface DielineGeometry {
21
22
  shape: "rect" | "circle" | "ellipse" | "custom";
@@ -332,6 +333,40 @@ export class DielineTool implements Extension {
332
333
  },
333
334
  ] as ConfigurationContribution[],
334
335
  [ContributionPointIds.COMMANDS]: [
336
+ {
337
+ command: "updateFeaturePosition",
338
+ title: "Update Feature Position",
339
+ handler: (groupId: string, x: number, y: number) => {
340
+ const configService = this.context?.services.get<any>(
341
+ "ConfigurationService",
342
+ );
343
+ if (!configService) return;
344
+
345
+ const features = configService.get("dieline.features") || [];
346
+ const dielineWidth = configService.get("dieline.width") || 500;
347
+ const dielineHeight = configService.get("dieline.height") || 500;
348
+
349
+ let changed = false;
350
+ const newFeatures = features.map((f: any) => {
351
+ if (f.groupId === groupId) {
352
+ const constrained = ConstraintRegistry.apply(x, y, f, {
353
+ dielineWidth,
354
+ dielineHeight,
355
+ });
356
+
357
+ if (f.x !== constrained.x || f.y !== constrained.y) {
358
+ changed = true;
359
+ return { ...f, x: constrained.x, y: constrained.y };
360
+ }
361
+ }
362
+ return f;
363
+ });
364
+
365
+ if (changed) {
366
+ configService.update("dieline.features", newFeatures);
367
+ }
368
+ },
369
+ },
335
370
  {
336
371
  command: "getGeometry",
337
372
  title: "Get Geometry",
package/src/feature.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  resolveFeaturePosition,
16
16
  } from "./geometry";
17
17
  import { Coordinate } from "./coordinate";
18
+ import { ConstraintRegistry } from "./constraints";
18
19
 
19
20
  export class FeatureTool implements Extension {
20
21
  id = "pooder.kit.feature";
@@ -435,6 +436,30 @@ export class FeatureTool implements Extension {
435
436
  limit: number,
436
437
  feature?: DielineFeature
437
438
  ): { x: number; y: number } {
439
+ if (feature && feature.constraints) {
440
+ // Use Constraint Registry
441
+ // Convert to normalized coordinates
442
+ const minX = geometry.x - geometry.width / 2;
443
+ const minY = geometry.y - geometry.height / 2;
444
+
445
+ const nx = geometry.width > 0 ? (p.x - minX) / geometry.width : 0.5;
446
+ const ny = geometry.height > 0 ? (p.y - minY) / geometry.height : 0.5;
447
+
448
+ const scale = geometry.scale || 1;
449
+ const dielineWidth = geometry.width / scale;
450
+ const dielineHeight = geometry.height / scale;
451
+
452
+ const constrained = ConstraintRegistry.apply(nx, ny, feature, {
453
+ dielineWidth,
454
+ dielineHeight,
455
+ });
456
+
457
+ return {
458
+ x: minX + constrained.x * geometry.width,
459
+ y: minY + constrained.y * geometry.height,
460
+ };
461
+ }
462
+
438
463
  if (feature && feature.placement === "internal") {
439
464
  // Constrain to bounds
440
465
  // geometry.x/y is center
package/src/film.ts CHANGED
@@ -1,194 +1,194 @@
1
- import {
2
- Extension,
3
- ExtensionContext,
4
- ContributionPointIds,
5
- CommandContribution,
6
- ConfigurationContribution,
7
- } from "@pooder/core";
8
- import { FabricImage as Image } from "fabric";
9
- import CanvasService from "./CanvasService";
10
-
11
- export class FilmTool implements Extension {
12
- id = "pooder.kit.film";
13
-
14
- public metadata = {
15
- name: "FilmTool",
16
- };
17
-
18
- private url: string = "";
19
- private opacity: number = 0.5;
20
-
21
- private canvasService?: CanvasService;
22
-
23
- constructor(
24
- options?: Partial<{
25
- url: string;
26
- opacity: number;
27
- }>,
28
- ) {
29
- if (options) {
30
- Object.assign(this, options);
31
- }
32
- }
33
-
34
- activate(context: ExtensionContext) {
35
- this.canvasService = context.services.get<CanvasService>("CanvasService");
36
- if (!this.canvasService) {
37
- console.warn("CanvasService not found for FilmTool");
38
- return;
39
- }
40
-
41
- const configService = context.services.get<any>("ConfigurationService");
42
- if (configService) {
43
- // Load initial config
44
- this.url = configService.get("film.url", this.url);
45
- this.opacity = configService.get("film.opacity", this.opacity);
46
-
47
- // Listen for changes
48
- configService.onAnyChange((e: { key: string; value: any }) => {
49
- if (e.key.startsWith("film.")) {
50
- const prop = e.key.split(".")[1];
51
- console.log(
52
- `[FilmTool] Config change detected: ${e.key} -> ${e.value}`,
53
- );
54
- if (prop && prop in this) {
55
- (this as any)[prop] = e.value;
56
- this.updateFilm();
57
- }
58
- }
59
- });
60
- }
61
-
62
- this.initLayer();
63
- this.updateFilm();
64
- }
65
-
66
- deactivate(context: ExtensionContext) {
67
- if (this.canvasService) {
68
- const layer = this.canvasService.getLayer("overlay");
69
- if (layer) {
70
- const img = this.canvasService.getObject("film-image", "overlay");
71
- if (img) {
72
- layer.remove(img);
73
- this.canvasService.requestRenderAll();
74
- }
75
- }
76
- this.canvasService = undefined;
77
- }
78
- }
79
-
80
- contribute() {
81
- return {
82
- [ContributionPointIds.CONFIGURATIONS]: [
83
- {
84
- id: "film.url",
85
- type: "string",
86
- label: "Film Image URL",
87
- default: "",
88
- },
89
- {
90
- id: "film.opacity",
91
- type: "number",
92
- label: "Opacity",
93
- min: 0,
94
- max: 1,
95
- step: 0.1,
96
- default: 0.5,
97
- },
98
- ] as ConfigurationContribution[],
99
- [ContributionPointIds.COMMANDS]: [
100
- {
101
- command: "setFilmImage",
102
- title: "Set Film Image",
103
- handler: (url: string, opacity: number) => {
104
- if (this.url === url && this.opacity === opacity) return true;
105
-
106
- this.url = url;
107
- this.opacity = opacity;
108
-
109
- this.updateFilm();
110
-
111
- return true;
112
- },
113
- },
114
- ] as CommandContribution[],
115
- };
116
- }
117
-
118
- private initLayer() {
119
- if (!this.canvasService) return;
120
- let overlayLayer = this.canvasService.getLayer("overlay");
121
- if (!overlayLayer) {
122
- const width = this.canvasService.canvas.width || 800;
123
- const height = this.canvasService.canvas.height || 600;
124
-
125
- const layer = this.canvasService.createLayer("overlay", {
126
- width,
127
- height,
128
- left: 0,
129
- top: 0,
130
- originX: "left",
131
- originY: "top",
132
- selectable: false,
133
- evented: false,
134
- subTargetCheck: false,
135
- interactive: false,
136
- });
137
-
138
- this.canvasService.canvas.bringObjectToFront(layer);
139
- }
140
- }
141
-
142
- private async updateFilm() {
143
- if (!this.canvasService) return;
144
- const layer = this.canvasService.getLayer("overlay");
145
- if (!layer) {
146
- console.warn("[FilmTool] Overlay layer not found");
147
- return;
148
- }
149
-
150
- const { url, opacity } = this;
151
-
152
- if (!url) {
153
- const img = this.canvasService.getObject("film-image", "overlay");
154
- if (img) {
155
- layer.remove(img);
156
- this.canvasService.requestRenderAll();
157
- }
158
- return;
159
- }
160
-
161
- const width = this.canvasService.canvas.width || 800;
162
- const height = this.canvasService.canvas.height || 600;
163
-
164
- let img = this.canvasService.getObject("film-image", "overlay") as Image;
165
- try {
166
- if (img) {
167
- if (img.getSrc() !== url) {
168
- await img.setSrc(url);
169
- }
170
- img.set({ opacity });
171
- } else {
172
- img = await Image.fromURL(url, { crossOrigin: "anonymous" });
173
- img.scaleToWidth(width);
174
- if (img.getScaledHeight() < height) img.scaleToHeight(height);
175
- img.set({
176
- originX: "left",
177
- originY: "top",
178
- left: 0,
179
- top: 0,
180
- opacity,
181
- selectable: false,
182
- evented: false,
183
- data: { id: "film-image" },
184
- });
185
- layer.add(img);
186
- }
187
- this.canvasService.requestRenderAll();
188
- } catch (error) {
189
- console.error("[FilmTool] Failed to load film image", url, error);
190
- }
191
- layer.dirty = true;
192
- this.canvasService.requestRenderAll();
193
- }
194
- }
1
+ import {
2
+ Extension,
3
+ ExtensionContext,
4
+ ContributionPointIds,
5
+ CommandContribution,
6
+ ConfigurationContribution,
7
+ } from "@pooder/core";
8
+ import { FabricImage as Image } from "fabric";
9
+ import CanvasService from "./CanvasService";
10
+
11
+ export class FilmTool implements Extension {
12
+ id = "pooder.kit.film";
13
+
14
+ public metadata = {
15
+ name: "FilmTool",
16
+ };
17
+
18
+ private url: string = "";
19
+ private opacity: number = 0.5;
20
+
21
+ private canvasService?: CanvasService;
22
+
23
+ constructor(
24
+ options?: Partial<{
25
+ url: string;
26
+ opacity: number;
27
+ }>,
28
+ ) {
29
+ if (options) {
30
+ Object.assign(this, options);
31
+ }
32
+ }
33
+
34
+ activate(context: ExtensionContext) {
35
+ this.canvasService = context.services.get<CanvasService>("CanvasService");
36
+ if (!this.canvasService) {
37
+ console.warn("CanvasService not found for FilmTool");
38
+ return;
39
+ }
40
+
41
+ const configService = context.services.get<any>("ConfigurationService");
42
+ if (configService) {
43
+ // Load initial config
44
+ this.url = configService.get("film.url", this.url);
45
+ this.opacity = configService.get("film.opacity", this.opacity);
46
+
47
+ // Listen for changes
48
+ configService.onAnyChange((e: { key: string; value: any }) => {
49
+ if (e.key.startsWith("film.")) {
50
+ const prop = e.key.split(".")[1];
51
+ console.log(
52
+ `[FilmTool] Config change detected: ${e.key} -> ${e.value}`,
53
+ );
54
+ if (prop && prop in this) {
55
+ (this as any)[prop] = e.value;
56
+ this.updateFilm();
57
+ }
58
+ }
59
+ });
60
+ }
61
+
62
+ this.initLayer();
63
+ this.updateFilm();
64
+ }
65
+
66
+ deactivate(context: ExtensionContext) {
67
+ if (this.canvasService) {
68
+ const layer = this.canvasService.getLayer("overlay");
69
+ if (layer) {
70
+ const img = this.canvasService.getObject("film-image", "overlay");
71
+ if (img) {
72
+ layer.remove(img);
73
+ this.canvasService.requestRenderAll();
74
+ }
75
+ }
76
+ this.canvasService = undefined;
77
+ }
78
+ }
79
+
80
+ contribute() {
81
+ return {
82
+ [ContributionPointIds.CONFIGURATIONS]: [
83
+ {
84
+ id: "film.url",
85
+ type: "string",
86
+ label: "Film Image URL",
87
+ default: "",
88
+ },
89
+ {
90
+ id: "film.opacity",
91
+ type: "number",
92
+ label: "Opacity",
93
+ min: 0,
94
+ max: 1,
95
+ step: 0.1,
96
+ default: 0.5,
97
+ },
98
+ ] as ConfigurationContribution[],
99
+ [ContributionPointIds.COMMANDS]: [
100
+ {
101
+ command: "setFilmImage",
102
+ title: "Set Film Image",
103
+ handler: (url: string, opacity: number) => {
104
+ if (this.url === url && this.opacity === opacity) return true;
105
+
106
+ this.url = url;
107
+ this.opacity = opacity;
108
+
109
+ this.updateFilm();
110
+
111
+ return true;
112
+ },
113
+ },
114
+ ] as CommandContribution[],
115
+ };
116
+ }
117
+
118
+ private initLayer() {
119
+ if (!this.canvasService) return;
120
+ let overlayLayer = this.canvasService.getLayer("overlay");
121
+ if (!overlayLayer) {
122
+ const width = this.canvasService.canvas.width || 800;
123
+ const height = this.canvasService.canvas.height || 600;
124
+
125
+ const layer = this.canvasService.createLayer("overlay", {
126
+ width,
127
+ height,
128
+ left: 0,
129
+ top: 0,
130
+ originX: "left",
131
+ originY: "top",
132
+ selectable: false,
133
+ evented: false,
134
+ subTargetCheck: false,
135
+ interactive: false,
136
+ });
137
+
138
+ this.canvasService.canvas.bringObjectToFront(layer);
139
+ }
140
+ }
141
+
142
+ private async updateFilm() {
143
+ if (!this.canvasService) return;
144
+ const layer = this.canvasService.getLayer("overlay");
145
+ if (!layer) {
146
+ console.warn("[FilmTool] Overlay layer not found");
147
+ return;
148
+ }
149
+
150
+ const { url, opacity } = this;
151
+
152
+ if (!url) {
153
+ const img = this.canvasService.getObject("film-image", "overlay");
154
+ if (img) {
155
+ layer.remove(img);
156
+ this.canvasService.requestRenderAll();
157
+ }
158
+ return;
159
+ }
160
+
161
+ const width = this.canvasService.canvas.width || 800;
162
+ const height = this.canvasService.canvas.height || 600;
163
+
164
+ let img = this.canvasService.getObject("film-image", "overlay") as Image;
165
+ try {
166
+ if (img) {
167
+ if (img.getSrc() !== url) {
168
+ await img.setSrc(url);
169
+ }
170
+ img.set({ opacity });
171
+ } else {
172
+ img = await Image.fromURL(url, { crossOrigin: "anonymous" });
173
+ img.scaleToWidth(width);
174
+ if (img.getScaledHeight() < height) img.scaleToHeight(height);
175
+ img.set({
176
+ originX: "left",
177
+ originY: "top",
178
+ left: 0,
179
+ top: 0,
180
+ opacity,
181
+ selectable: false,
182
+ evented: false,
183
+ data: { id: "film-image" },
184
+ });
185
+ layer.add(img);
186
+ }
187
+ this.canvasService.requestRenderAll();
188
+ } catch (error) {
189
+ console.error("[FilmTool] Failed to load film image", url, error);
190
+ }
191
+ layer.dirty = true;
192
+ this.canvasService.requestRenderAll();
193
+ }
194
+ }