@pooder/kit 5.3.1 → 6.0.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 (65) hide show
  1. package/.test-dist/src/extensions/background.js +475 -131
  2. package/.test-dist/src/extensions/dieline.js +283 -180
  3. package/.test-dist/src/extensions/dielineShape.js +66 -0
  4. package/.test-dist/src/extensions/feature.js +388 -303
  5. package/.test-dist/src/extensions/film.js +133 -74
  6. package/.test-dist/src/extensions/geometry.js +120 -56
  7. package/.test-dist/src/extensions/image.js +296 -212
  8. package/.test-dist/src/extensions/index.js +1 -3
  9. package/.test-dist/src/extensions/maskOps.js +75 -20
  10. package/.test-dist/src/extensions/ruler.js +312 -215
  11. package/.test-dist/src/extensions/sceneLayoutModel.js +9 -3
  12. package/.test-dist/src/extensions/sceneVisibility.js +3 -10
  13. package/.test-dist/src/extensions/tracer.js +229 -58
  14. package/.test-dist/src/extensions/white-ink.js +139 -129
  15. package/.test-dist/src/services/CanvasService.js +888 -126
  16. package/.test-dist/src/services/index.js +1 -0
  17. package/.test-dist/src/services/visibility.js +54 -0
  18. package/.test-dist/tests/run.js +58 -4
  19. package/CHANGELOG.md +12 -0
  20. package/dist/index.d.mts +377 -82
  21. package/dist/index.d.ts +377 -82
  22. package/dist/index.js +3920 -2178
  23. package/dist/index.mjs +3992 -2247
  24. package/package.json +1 -1
  25. package/src/extensions/background.ts +631 -145
  26. package/src/extensions/dieline.ts +280 -187
  27. package/src/extensions/dielineShape.ts +109 -0
  28. package/src/extensions/feature.ts +485 -366
  29. package/src/extensions/film.ts +152 -76
  30. package/src/extensions/geometry.ts +203 -104
  31. package/src/extensions/image.ts +319 -238
  32. package/src/extensions/index.ts +0 -1
  33. package/src/extensions/ruler.ts +481 -268
  34. package/src/extensions/sceneLayoutModel.ts +18 -6
  35. package/src/extensions/white-ink.ts +157 -171
  36. package/src/services/CanvasService.ts +1126 -140
  37. package/src/services/index.ts +1 -0
  38. package/src/services/renderSpec.ts +69 -4
  39. package/src/services/visibility.ts +78 -0
  40. package/tests/run.ts +139 -4
  41. package/.test-dist/src/CanvasService.js +0 -249
  42. package/.test-dist/src/ViewportSystem.js +0 -75
  43. package/.test-dist/src/background.js +0 -203
  44. package/.test-dist/src/bridgeSelection.js +0 -20
  45. package/.test-dist/src/constraints.js +0 -237
  46. package/.test-dist/src/dieline.js +0 -818
  47. package/.test-dist/src/edgeScale.js +0 -12
  48. package/.test-dist/src/feature.js +0 -826
  49. package/.test-dist/src/featureComplete.js +0 -32
  50. package/.test-dist/src/film.js +0 -167
  51. package/.test-dist/src/geometry.js +0 -506
  52. package/.test-dist/src/image.js +0 -1250
  53. package/.test-dist/src/maskOps.js +0 -270
  54. package/.test-dist/src/mirror.js +0 -104
  55. package/.test-dist/src/renderSpec.js +0 -2
  56. package/.test-dist/src/ruler.js +0 -343
  57. package/.test-dist/src/sceneLayout.js +0 -99
  58. package/.test-dist/src/sceneLayoutModel.js +0 -196
  59. package/.test-dist/src/sceneView.js +0 -40
  60. package/.test-dist/src/sceneVisibility.js +0 -42
  61. package/.test-dist/src/size.js +0 -332
  62. package/.test-dist/src/tracer.js +0 -544
  63. package/.test-dist/src/white-ink.js +0 -829
  64. package/.test-dist/src/wrappedOffsets.js +0 -33
  65. package/src/extensions/sceneVisibility.ts +0 -71
@@ -1,203 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BackgroundTool = void 0;
4
- const core_1 = require("@pooder/core");
5
- const fabric_1 = require("fabric");
6
- class BackgroundTool {
7
- constructor(options) {
8
- this.id = "pooder.kit.background";
9
- this.metadata = {
10
- name: "BackgroundTool",
11
- };
12
- this.color = "";
13
- this.url = "";
14
- if (options) {
15
- Object.assign(this, options);
16
- }
17
- }
18
- activate(context) {
19
- this.canvasService = context.services.get("CanvasService");
20
- if (!this.canvasService) {
21
- console.warn("CanvasService not found for BackgroundTool");
22
- return;
23
- }
24
- const configService = context.services.get("ConfigurationService");
25
- if (configService) {
26
- // Load initial config
27
- this.color = configService.get("background.color", this.color);
28
- this.url = configService.get("background.url", this.url);
29
- // Listen for changes
30
- configService.onAnyChange((e) => {
31
- if (e.key.startsWith("background.")) {
32
- const prop = e.key.split(".")[1];
33
- console.log(`[BackgroundTool] Config change detected: ${e.key} -> ${e.value}, prop: ${prop}`);
34
- if (prop && prop in this) {
35
- console.log(`[BackgroundTool] Updating option ${prop} to ${e.value}`);
36
- this[prop] = e.value;
37
- this.updateBackground();
38
- }
39
- else {
40
- console.warn(`[BackgroundTool] Property ${prop} not found in options`);
41
- }
42
- }
43
- });
44
- }
45
- this.initLayer();
46
- this.updateBackground();
47
- }
48
- deactivate(context) {
49
- if (this.canvasService) {
50
- const layer = this.canvasService.getLayer("background");
51
- if (layer) {
52
- this.canvasService.canvas.remove(layer);
53
- }
54
- this.canvasService = undefined;
55
- }
56
- }
57
- contribute() {
58
- return {
59
- [core_1.ContributionPointIds.CONFIGURATIONS]: [
60
- {
61
- id: "background.color",
62
- type: "color",
63
- label: "Background Color",
64
- default: "",
65
- },
66
- {
67
- id: "background.url",
68
- type: "string",
69
- label: "Image URL",
70
- default: "",
71
- },
72
- ],
73
- [core_1.ContributionPointIds.COMMANDS]: [
74
- {
75
- command: "reset",
76
- title: "Reset Background",
77
- handler: () => {
78
- this.updateBackground();
79
- return true;
80
- },
81
- },
82
- {
83
- command: "clear",
84
- title: "Clear Background",
85
- handler: () => {
86
- this.color = "transparent";
87
- this.url = "";
88
- this.updateBackground();
89
- return true;
90
- },
91
- },
92
- {
93
- command: "setBackgroundColor",
94
- title: "Set Background Color",
95
- handler: (color) => {
96
- if (this.color === color)
97
- return true;
98
- this.color = color;
99
- this.updateBackground();
100
- return true;
101
- },
102
- },
103
- {
104
- command: "setBackgroundImage",
105
- title: "Set Background Image",
106
- handler: (url) => {
107
- if (this.url === url)
108
- return true;
109
- this.url = url;
110
- this.updateBackground();
111
- return true;
112
- },
113
- },
114
- ],
115
- };
116
- }
117
- initLayer() {
118
- if (!this.canvasService)
119
- return;
120
- let backgroundLayer = this.canvasService.getLayer("background");
121
- if (!backgroundLayer) {
122
- backgroundLayer = this.canvasService.createLayer("background", {
123
- width: this.canvasService.canvas.width,
124
- height: this.canvasService.canvas.height,
125
- selectable: false,
126
- evented: false,
127
- });
128
- this.canvasService.canvas.sendObjectToBack(backgroundLayer);
129
- }
130
- }
131
- async updateBackground() {
132
- if (!this.canvasService)
133
- return;
134
- const layer = this.canvasService.getLayer("background");
135
- if (!layer) {
136
- console.warn("[BackgroundTool] Background layer not found");
137
- return;
138
- }
139
- const { color, url } = this;
140
- const width = this.canvasService.canvas.width || 800;
141
- const height = this.canvasService.canvas.height || 600;
142
- let rect = this.canvasService.getObject("background-color-rect", "background");
143
- if (rect) {
144
- rect.set({
145
- fill: color,
146
- });
147
- }
148
- else {
149
- rect = new fabric_1.Rect({
150
- width,
151
- height,
152
- fill: color,
153
- selectable: false,
154
- evented: false,
155
- data: {
156
- id: "background-color-rect",
157
- },
158
- });
159
- layer.add(rect);
160
- layer.sendObjectToBack(rect);
161
- }
162
- let img = this.canvasService.getObject("background-image", "background");
163
- try {
164
- if (img) {
165
- if (img.getSrc() !== url) {
166
- if (url) {
167
- await img.setSrc(url);
168
- }
169
- else {
170
- layer.remove(img);
171
- }
172
- }
173
- }
174
- else {
175
- if (url) {
176
- img = await fabric_1.FabricImage.fromURL(url, { crossOrigin: "anonymous" });
177
- img.set({
178
- originX: "left",
179
- originY: "top",
180
- left: 0,
181
- top: 0,
182
- selectable: false,
183
- evented: false,
184
- data: {
185
- id: "background-image",
186
- },
187
- });
188
- img.scaleToWidth(width);
189
- if (img.getScaledHeight() < height)
190
- img.scaleToHeight(height);
191
- layer.add(img);
192
- }
193
- }
194
- this.canvasService.requestRenderAll();
195
- }
196
- catch (e) {
197
- console.error("[BackgroundTool] Failed to load image", e);
198
- }
199
- layer.dirty = true;
200
- this.canvasService.requestRenderAll();
201
- }
202
- }
203
- exports.BackgroundTool = BackgroundTool;
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.pickExitIndex = pickExitIndex;
4
- exports.scoreOutsideAbove = scoreOutsideAbove;
5
- function pickExitIndex(hits) {
6
- for (let i = 0; i < hits.length; i++) {
7
- const h = hits[i];
8
- if (h.insideBelow && !h.insideAbove)
9
- return i;
10
- }
11
- return -1;
12
- }
13
- function scoreOutsideAbove(samples) {
14
- let score = 0;
15
- for (const s of samples) {
16
- if (s.outsideAbove)
17
- score++;
18
- }
19
- return score;
20
- }
@@ -1,237 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConstraintRegistry = void 0;
4
- const geometry_1 = require("./geometry");
5
- class ConstraintRegistry {
6
- static register(type, handler) {
7
- this.handlers.set(type, handler);
8
- }
9
- static apply(x, y, feature, context, constraints // Optional override, defaults to feature.constraints
10
- ) {
11
- const list = constraints || feature.constraints;
12
- if (!list || list.length === 0) {
13
- return { x, y };
14
- }
15
- let currentX = x;
16
- let currentY = y;
17
- for (const constraint of list) {
18
- const handler = this.handlers.get(constraint.type);
19
- if (handler) {
20
- const result = handler(currentX, currentY, feature, context, constraint.params || {});
21
- currentX = result.x;
22
- currentY = result.y;
23
- }
24
- }
25
- return { x: currentX, y: currentY };
26
- }
27
- }
28
- exports.ConstraintRegistry = ConstraintRegistry;
29
- ConstraintRegistry.handlers = new Map();
30
- // --- Built-in Strategies ---
31
- /**
32
- * Path Constraint Strategy (formerly placement='edge')
33
- * Snaps the feature to the nearest point on the Dieline Path.
34
- */
35
- const pathConstraint = (x, y, feature, context, params) => {
36
- // We need to denormalize, find nearest, then normalize back
37
- // This is expensive but accurate.
38
- const { dielineWidth, dielineHeight, geometry } = context;
39
- if (!geometry)
40
- return { x, y }; // Cannot snap without geometry
41
- // Geometry is centered at (cx, cy)
42
- // x, y are normalized (0-1) relative to bounding box
43
- const minX = geometry.x - geometry.width / 2;
44
- const minY = geometry.y - geometry.height / 2;
45
- const absX = minX + x * geometry.width;
46
- const absY = minY + y * geometry.height;
47
- // Use geometry helper
48
- // Note: getNearestPointOnDieline creates a fresh paper scope each time.
49
- // Optimization: geometry object passed in context could be a reusable paper path?
50
- // For now, keep it simple as per existing logic.
51
- const nearest = (0, geometry_1.getNearestPointOnDieline)({ x: absX, y: absY }, geometry);
52
- let finalX = nearest.x;
53
- let finalY = nearest.y;
54
- // Only allow vertical offset if explicit offset limits are provided
55
- // Otherwise, we snap strictly to the path (offset = 0)
56
- const hasOffsetParams = params.minOffset !== undefined || params.maxOffset !== undefined;
57
- if (hasOffsetParams && nearest.normal) {
58
- // Project the cursor vector onto the normal vector
59
- // This ensures the feature stays on the "normal line" of the nearest path point
60
- const dx = absX - nearest.x;
61
- const dy = absY - nearest.y;
62
- const nx = nearest.normal.x;
63
- const ny = nearest.normal.y;
64
- // Dot product to get scalar projection
65
- const dist = dx * nx + dy * ny;
66
- // Limit the offset
67
- // geometry.width is in pixels, dielineWidth is in physical units (e.g. mm)
68
- // We assume dielineWidth corresponds to geometry.width
69
- const scale = dielineWidth > 0 ? geometry.width / dielineWidth : 1;
70
- // If one is provided but the other is not, default the other to 0.
71
- // If neither is provided (shouldn't happen due to hasOffsetParams check), default to 0.
72
- const rawMin = params.minOffset !== undefined ? params.minOffset : 0;
73
- const rawMax = params.maxOffset !== undefined ? params.maxOffset : 0;
74
- // However, if we want to allow one-sided infinity, user must explicitly provide Infinity?
75
- // Wait, user requirement: "If only one is passed, the other defaults to 0."
76
- // This implies:
77
- // { minOffset: -5 } -> maxOffset = 0 (range: -5 to 0)
78
- // { maxOffset: 5 } -> minOffset = 0 (range: 0 to 5)
79
- // { minOffset: -5, maxOffset: 5 } -> (range: -5 to 5)
80
- const minOffset = rawMin * scale;
81
- const maxOffset = rawMax * scale;
82
- const clampedDist = Math.max(minOffset, Math.min(dist, maxOffset));
83
- finalX = nearest.x + nx * clampedDist;
84
- finalY = nearest.y + ny * clampedDist;
85
- }
86
- // Re-normalize
87
- const nx = geometry.width > 0 ? (finalX - minX) / geometry.width : 0.5;
88
- const ny = geometry.height > 0 ? (finalY - minY) / geometry.height : 0.5;
89
- return { x: nx, y: ny };
90
- };
91
- /**
92
- * Edge Constraint Strategy (Box Edge)
93
- * Snaps the feature to the nearest allowed edge of the BOUNDING BOX.
94
- */
95
- const edgeConstraint = (x, y, feature, context, params) => {
96
- const { dielineWidth, dielineHeight } = context;
97
- const allowedEdges = params.allowedEdges || [
98
- "top",
99
- "bottom",
100
- "left",
101
- "right",
102
- ];
103
- const confine = params.confine || false;
104
- const offset = params.offset || 0;
105
- // Calculate physical distances to allowed edges
106
- const distances = [];
107
- if (allowedEdges.includes("top"))
108
- distances.push({ edge: "top", dist: y * dielineHeight });
109
- if (allowedEdges.includes("bottom"))
110
- distances.push({ edge: "bottom", dist: (1 - y) * dielineHeight });
111
- if (allowedEdges.includes("left"))
112
- distances.push({ edge: "left", dist: x * dielineWidth });
113
- if (allowedEdges.includes("right"))
114
- distances.push({ edge: "right", dist: (1 - x) * dielineWidth });
115
- if (distances.length === 0)
116
- return { x, y };
117
- // Find nearest
118
- distances.sort((a, b) => a.dist - b.dist);
119
- const nearest = distances[0].edge;
120
- let newX = x;
121
- let newY = y;
122
- const fw = feature.width || 0;
123
- const fh = feature.height || 0;
124
- // Snap to edge
125
- switch (nearest) {
126
- case "top":
127
- newY = 0 + offset / dielineHeight;
128
- if (confine) {
129
- const minX = (fw / 2) / dielineWidth;
130
- const maxX = 1 - minX;
131
- newX = Math.max(minX, Math.min(newX, maxX));
132
- }
133
- break;
134
- case "bottom":
135
- newY = 1 - offset / dielineHeight;
136
- if (confine) {
137
- const minX = (fw / 2) / dielineWidth;
138
- const maxX = 1 - minX;
139
- newX = Math.max(minX, Math.min(newX, maxX));
140
- }
141
- break;
142
- case "left":
143
- newX = 0 + offset / dielineWidth;
144
- if (confine) {
145
- const minY = (fh / 2) / dielineHeight;
146
- const maxY = 1 - minY;
147
- newY = Math.max(minY, Math.min(newY, maxY));
148
- }
149
- break;
150
- case "right":
151
- newX = 1 - offset / dielineWidth;
152
- if (confine) {
153
- const minY = (fh / 2) / dielineHeight;
154
- const maxY = 1 - minY;
155
- newY = Math.max(minY, Math.min(newY, maxY));
156
- }
157
- break;
158
- }
159
- return { x: newX, y: newY };
160
- };
161
- /**
162
- * Internal Constraint Strategy
163
- * Keeps the feature strictly inside the dieline bounds with optional margin.
164
- */
165
- const internalConstraint = (x, y, feature, context, params) => {
166
- const { dielineWidth, dielineHeight } = context;
167
- const margin = params.margin || 0;
168
- const fw = feature.width || 0;
169
- const fh = feature.height || 0;
170
- const minX = (margin + fw / 2) / dielineWidth;
171
- const maxX = 1 - (margin + fw / 2) / dielineWidth;
172
- const minY = (margin + fh / 2) / dielineHeight;
173
- const maxY = 1 - (margin + fh / 2) / dielineHeight;
174
- // Handle case where feature is larger than container
175
- const clampedX = minX > maxX ? 0.5 : Math.max(minX, Math.min(x, maxX));
176
- const clampedY = minY > maxY ? 0.5 : Math.max(minY, Math.min(y, maxY));
177
- return { x: clampedX, y: clampedY };
178
- };
179
- /**
180
- * Bottom Tangent Strategy (stand protrusion)
181
- * Forces a feature to be tangent to the dieline bottom edge from outside (below).
182
- */
183
- const tangentBottomConstraint = (x, y, feature, context, params) => {
184
- const { dielineWidth, dielineHeight } = context;
185
- const gap = params.gap || 0;
186
- const confineX = params.confineX !== false;
187
- const extentY = feature.shape === "circle"
188
- ? feature.radius || 0
189
- : (feature.height || 0) / 2;
190
- const newY = 1 + (extentY + gap) / dielineHeight;
191
- let newX = x;
192
- if (confineX) {
193
- const extentX = feature.shape === "circle"
194
- ? feature.radius || 0
195
- : (feature.width || 0) / 2;
196
- const minX = extentX / dielineWidth;
197
- const maxX = 1 - extentX / dielineWidth;
198
- newX = minX > maxX ? 0.5 : Math.max(minX, Math.min(newX, maxX));
199
- }
200
- return { x: newX, y: newY };
201
- };
202
- /**
203
- * Lowest Tangent Strategy (Lowest Point Lock)
204
- * Finds the lowest point of the Dieline geometry and locks the feature's Y to that level.
205
- * Allows horizontal movement (X).
206
- */
207
- const lowestTangentConstraint = (x, y, feature, context, params) => {
208
- const { dielineWidth, dielineHeight, geometry } = context;
209
- if (!geometry)
210
- return { x, y };
211
- const lowest = (0, geometry_1.getLowestPointOnDieline)(geometry);
212
- // Calculate normalized Y of the lowest point
213
- const minY = geometry.y - geometry.height / 2;
214
- const normY = (lowest.y - minY) / geometry.height;
215
- const gap = params.gap || 0;
216
- const confineX = params.confineX !== false;
217
- const extentY = feature.shape === "circle"
218
- ? feature.radius || 0
219
- : (feature.height || 0) / 2;
220
- const newY = normY + (extentY + gap) / dielineHeight;
221
- let newX = x;
222
- if (confineX) {
223
- const extentX = feature.shape === "circle"
224
- ? feature.radius || 0
225
- : (feature.width || 0) / 2;
226
- const minX = extentX / dielineWidth;
227
- const maxX = 1 - extentX / dielineWidth;
228
- newX = minX > maxX ? 0.5 : Math.max(minX, Math.min(newX, maxX));
229
- }
230
- return { x: newX, y: newY };
231
- };
232
- // Register built-ins
233
- ConstraintRegistry.register("path", pathConstraint);
234
- ConstraintRegistry.register("edge", edgeConstraint);
235
- ConstraintRegistry.register("internal", internalConstraint);
236
- ConstraintRegistry.register("tangent-bottom", tangentBottomConstraint);
237
- ConstraintRegistry.register("lowest-tangent", lowestTangentConstraint);