@pooder/kit 4.3.1 → 5.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 (60) hide show
  1. package/.test-dist/src/CanvasService.js +249 -0
  2. package/.test-dist/src/ViewportSystem.js +75 -0
  3. package/.test-dist/src/background.js +203 -0
  4. package/.test-dist/src/bridgeSelection.js +20 -0
  5. package/.test-dist/src/constraints.js +237 -0
  6. package/.test-dist/src/coordinate.js +74 -0
  7. package/.test-dist/src/dieline.js +723 -0
  8. package/.test-dist/src/edgeScale.js +12 -0
  9. package/.test-dist/src/feature.js +752 -0
  10. package/.test-dist/src/featureComplete.js +32 -0
  11. package/.test-dist/src/film.js +167 -0
  12. package/.test-dist/src/geometry.js +506 -0
  13. package/.test-dist/src/image.js +1234 -0
  14. package/.test-dist/src/index.js +35 -0
  15. package/.test-dist/src/maskOps.js +270 -0
  16. package/.test-dist/src/mirror.js +104 -0
  17. package/.test-dist/src/renderSpec.js +2 -0
  18. package/.test-dist/src/ruler.js +343 -0
  19. package/.test-dist/src/sceneLayout.js +99 -0
  20. package/.test-dist/src/sceneLayoutModel.js +196 -0
  21. package/.test-dist/src/sceneView.js +40 -0
  22. package/.test-dist/src/sceneVisibility.js +42 -0
  23. package/.test-dist/src/size.js +332 -0
  24. package/.test-dist/src/tracer.js +544 -0
  25. package/.test-dist/src/units.js +30 -0
  26. package/.test-dist/src/white-ink.js +829 -0
  27. package/.test-dist/src/wrappedOffsets.js +33 -0
  28. package/.test-dist/tests/run.js +94 -0
  29. package/CHANGELOG.md +11 -0
  30. package/dist/index.d.mts +339 -36
  31. package/dist/index.d.ts +339 -36
  32. package/dist/index.js +3572 -850
  33. package/dist/index.mjs +3565 -852
  34. package/package.json +2 -2
  35. package/src/CanvasService.ts +300 -96
  36. package/src/ViewportSystem.ts +92 -92
  37. package/src/background.ts +230 -230
  38. package/src/bridgeSelection.ts +17 -0
  39. package/src/coordinate.ts +106 -106
  40. package/src/dieline.ts +897 -973
  41. package/src/edgeScale.ts +19 -0
  42. package/src/feature.ts +83 -30
  43. package/src/film.ts +194 -194
  44. package/src/geometry.ts +242 -84
  45. package/src/image.ts +1582 -512
  46. package/src/index.ts +14 -10
  47. package/src/maskOps.ts +326 -0
  48. package/src/mirror.ts +128 -128
  49. package/src/renderSpec.ts +18 -0
  50. package/src/ruler.ts +449 -508
  51. package/src/sceneLayout.ts +121 -0
  52. package/src/sceneLayoutModel.ts +335 -0
  53. package/src/sceneVisibility.ts +49 -0
  54. package/src/size.ts +379 -0
  55. package/src/tracer.ts +719 -570
  56. package/src/units.ts +27 -27
  57. package/src/white-ink.ts +1018 -373
  58. package/src/wrappedOffsets.ts +33 -0
  59. package/tests/run.ts +118 -0
  60. package/tsconfig.test.json +15 -15
@@ -0,0 +1,249 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fabric_1 = require("fabric");
4
+ const ViewportSystem_1 = require("./ViewportSystem");
5
+ class CanvasService {
6
+ constructor(el, options) {
7
+ if (el instanceof fabric_1.Canvas) {
8
+ this.canvas = el;
9
+ }
10
+ else {
11
+ this.canvas = new fabric_1.Canvas(el, {
12
+ preserveObjectStacking: true,
13
+ ...options,
14
+ });
15
+ }
16
+ this.viewport = new ViewportSystem_1.ViewportSystem();
17
+ if (this.canvas.width !== undefined && this.canvas.height !== undefined) {
18
+ this.viewport.updateContainer(this.canvas.width, this.canvas.height);
19
+ }
20
+ if (options?.eventBus) {
21
+ this.setEventBus(options.eventBus);
22
+ }
23
+ }
24
+ setEventBus(eventBus) {
25
+ this.eventBus = eventBus;
26
+ this.setupEvents();
27
+ }
28
+ setupEvents() {
29
+ if (!this.eventBus)
30
+ return;
31
+ const bus = this.eventBus;
32
+ const forward = (name) => (e) => bus.emit(name, e);
33
+ this.canvas.on("selection:created", forward("selection:created"));
34
+ this.canvas.on("selection:updated", forward("selection:updated"));
35
+ this.canvas.on("selection:cleared", forward("selection:cleared"));
36
+ this.canvas.on("object:modified", forward("object:modified"));
37
+ this.canvas.on("object:added", forward("object:added"));
38
+ this.canvas.on("object:removed", forward("object:removed"));
39
+ }
40
+ dispose() {
41
+ this.canvas.dispose();
42
+ }
43
+ /**
44
+ * Get a layer (Group) by its ID.
45
+ * We assume layers are Groups directly on the canvas with a data.id property.
46
+ */
47
+ getLayer(id) {
48
+ return this.canvas.getObjects().find((obj) => obj.data?.id === id);
49
+ }
50
+ /**
51
+ * Create a layer (Group) with the given ID if it doesn't exist.
52
+ */
53
+ createLayer(id, options = {}) {
54
+ let layer = this.getLayer(id);
55
+ if (!layer) {
56
+ const defaultOptions = {
57
+ selectable: false,
58
+ evented: false,
59
+ ...options,
60
+ data: { ...options.data, id },
61
+ };
62
+ layer = new fabric_1.Group([], defaultOptions);
63
+ this.canvas.add(layer);
64
+ }
65
+ return layer;
66
+ }
67
+ /**
68
+ * Find an object by ID, optionally within a specific layer.
69
+ */
70
+ getObject(id, layerId) {
71
+ if (layerId) {
72
+ const layer = this.getLayer(layerId);
73
+ if (!layer)
74
+ return undefined;
75
+ return layer.getObjects().find((obj) => obj.data?.id === id);
76
+ }
77
+ return this.canvas.getObjects().find((obj) => obj.data?.id === id);
78
+ }
79
+ requestRenderAll() {
80
+ this.canvas.requestRenderAll();
81
+ }
82
+ resize(width, height) {
83
+ this.canvas.setDimensions({ width, height });
84
+ this.viewport.updateContainer(width, height);
85
+ this.eventBus?.emit("canvas:resized", { width, height });
86
+ this.requestRenderAll();
87
+ }
88
+ async applyLayerSpec(spec) {
89
+ const layer = this.createLayer(spec.id, spec.props || {});
90
+ await this.applyObjectSpecsToContainer(layer, spec.objects);
91
+ }
92
+ async applyObjectSpecsToLayer(layerId, objects) {
93
+ const layer = this.createLayer(layerId, {});
94
+ await this.applyObjectSpecsToContainer(layer, objects);
95
+ }
96
+ getRootLayerObjects(layerId) {
97
+ return this.canvas
98
+ .getObjects()
99
+ .filter((obj) => obj?.data?.layerId === layerId);
100
+ }
101
+ async applyObjectSpecsToRootLayer(layerId, specs) {
102
+ const desiredIds = new Set(specs.map((s) => s.id));
103
+ const existing = this.getRootLayerObjects(layerId);
104
+ existing.forEach((obj) => {
105
+ const id = obj?.data?.id;
106
+ if (typeof id === "string" && !desiredIds.has(id)) {
107
+ this.canvas.remove(obj);
108
+ }
109
+ });
110
+ const byId = new Map();
111
+ this.getRootLayerObjects(layerId).forEach((obj) => {
112
+ const id = obj?.data?.id;
113
+ if (typeof id === "string")
114
+ byId.set(id, obj);
115
+ });
116
+ for (let index = 0; index < specs.length; index += 1) {
117
+ const spec = specs[index];
118
+ let current = byId.get(spec.id);
119
+ if (current &&
120
+ spec.type === "image" &&
121
+ spec.src &&
122
+ current.getSrc &&
123
+ current.getSrc() !== spec.src) {
124
+ this.canvas.remove(current);
125
+ byId.delete(spec.id);
126
+ current = undefined;
127
+ }
128
+ if (!current) {
129
+ const created = await this.createFabricObject(spec);
130
+ if (!created)
131
+ continue;
132
+ this.patchFabricObject(created, spec, { layerId });
133
+ this.canvas.add(created);
134
+ byId.set(spec.id, created);
135
+ continue;
136
+ }
137
+ this.patchFabricObject(current, spec, { layerId });
138
+ }
139
+ this.requestRenderAll();
140
+ }
141
+ async applyObjectSpecsToContainer(container, specs) {
142
+ const desiredIds = new Set(specs.map((s) => s.id));
143
+ const existing = container.getObjects();
144
+ existing.forEach((obj) => {
145
+ const id = obj?.data?.id;
146
+ if (typeof id === "string" && !desiredIds.has(id)) {
147
+ container.remove(obj);
148
+ }
149
+ });
150
+ const byId = new Map();
151
+ container.getObjects().forEach((obj) => {
152
+ const id = obj?.data?.id;
153
+ if (typeof id === "string")
154
+ byId.set(id, obj);
155
+ });
156
+ for (let index = 0; index < specs.length; index += 1) {
157
+ const spec = specs[index];
158
+ let current = byId.get(spec.id);
159
+ if (current &&
160
+ spec.type === "image" &&
161
+ spec.src &&
162
+ current.getSrc &&
163
+ current.getSrc() !== spec.src) {
164
+ container.remove(current);
165
+ byId.delete(spec.id);
166
+ current = undefined;
167
+ }
168
+ if (!current) {
169
+ const created = await this.createFabricObject(spec);
170
+ if (!created)
171
+ continue;
172
+ container.add(created);
173
+ current = created;
174
+ byId.set(spec.id, current);
175
+ }
176
+ else {
177
+ this.patchFabricObject(current, spec);
178
+ }
179
+ this.moveObjectInContainer(container, current, index);
180
+ }
181
+ container.dirty = true;
182
+ this.requestRenderAll();
183
+ }
184
+ patchFabricObject(obj, spec, extraData) {
185
+ const nextData = {
186
+ ...(obj.data || {}),
187
+ ...(spec.data || {}),
188
+ ...(extraData || {}),
189
+ id: spec.id,
190
+ };
191
+ obj.set({ ...(spec.props || {}), data: nextData });
192
+ obj.setCoords();
193
+ }
194
+ moveObjectInContainer(container, obj, index) {
195
+ if (!obj)
196
+ return;
197
+ const moveObjectTo = container.moveObjectTo;
198
+ if (typeof moveObjectTo === "function") {
199
+ moveObjectTo.call(container, obj, index);
200
+ return;
201
+ }
202
+ const list = container._objects;
203
+ if (!Array.isArray(list))
204
+ return;
205
+ const from = list.indexOf(obj);
206
+ if (from < 0 || from === index)
207
+ return;
208
+ list.splice(from, 1);
209
+ const target = Math.max(0, Math.min(index, list.length));
210
+ list.splice(target, 0, obj);
211
+ if (typeof container._onStackOrderChanged === "function") {
212
+ container._onStackOrderChanged();
213
+ }
214
+ }
215
+ async createFabricObject(spec) {
216
+ if (spec.type === "rect") {
217
+ const rect = new fabric_1.Rect({
218
+ ...(spec.props || {}),
219
+ data: { ...(spec.data || {}), id: spec.id },
220
+ });
221
+ rect.setCoords();
222
+ return rect;
223
+ }
224
+ if (spec.type === "path") {
225
+ const pathData = spec.props?.path || spec.props?.pathData;
226
+ if (!pathData)
227
+ return undefined;
228
+ const path = new fabric_1.Path(pathData, {
229
+ ...(spec.props || {}),
230
+ data: { ...(spec.data || {}), id: spec.id },
231
+ });
232
+ path.setCoords();
233
+ return path;
234
+ }
235
+ if (spec.type === "image") {
236
+ if (!spec.src)
237
+ return undefined;
238
+ const image = await fabric_1.Image.fromURL(spec.src, { crossOrigin: "anonymous" });
239
+ image.set({
240
+ ...(spec.props || {}),
241
+ data: { ...(spec.data || {}), id: spec.id },
242
+ });
243
+ image.setCoords();
244
+ return image;
245
+ }
246
+ return undefined;
247
+ }
248
+ }
249
+ exports.default = CanvasService;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ViewportSystem = void 0;
4
+ const coordinate_1 = require("./coordinate");
5
+ class ViewportSystem {
6
+ constructor(containerSize = { width: 0, height: 0 }, physicalSize = { width: 0, height: 0 }, padding = 40) {
7
+ this._containerSize = { width: 0, height: 0 };
8
+ this._physicalSize = { width: 0, height: 0 };
9
+ this._padding = 0;
10
+ this._layout = {
11
+ scale: 1,
12
+ offsetX: 0,
13
+ offsetY: 0,
14
+ width: 0,
15
+ height: 0,
16
+ };
17
+ this._containerSize = containerSize;
18
+ this._physicalSize = physicalSize;
19
+ this._padding = padding;
20
+ this.updateLayout();
21
+ }
22
+ get layout() {
23
+ return this._layout;
24
+ }
25
+ get scale() {
26
+ return this._layout.scale;
27
+ }
28
+ get offset() {
29
+ return { x: this._layout.offsetX, y: this._layout.offsetY };
30
+ }
31
+ updateContainer(width, height) {
32
+ if (this._containerSize.width === width &&
33
+ this._containerSize.height === height)
34
+ return;
35
+ this._containerSize = { width, height };
36
+ this.updateLayout();
37
+ }
38
+ updatePhysical(width, height) {
39
+ if (this._physicalSize.width === width && this._physicalSize.height === height)
40
+ return;
41
+ this._physicalSize = { width, height };
42
+ this.updateLayout();
43
+ }
44
+ setPadding(padding) {
45
+ if (this._padding === padding)
46
+ return;
47
+ this._padding = padding;
48
+ this.updateLayout();
49
+ }
50
+ updateLayout() {
51
+ this._layout = coordinate_1.Coordinate.calculateLayout(this._containerSize, this._physicalSize, this._padding);
52
+ }
53
+ toPixel(value) {
54
+ return value * this._layout.scale;
55
+ }
56
+ toPhysical(value) {
57
+ return this._layout.scale === 0 ? 0 : value / this._layout.scale;
58
+ }
59
+ toPixelPoint(point) {
60
+ return {
61
+ x: point.x * this._layout.scale + this._layout.offsetX,
62
+ y: point.y * this._layout.scale + this._layout.offsetY,
63
+ };
64
+ }
65
+ // Convert screen coordinate (e.g. mouse event) to physical coordinate (relative to content origin)
66
+ toPhysicalPoint(point) {
67
+ if (this._layout.scale === 0)
68
+ return { x: 0, y: 0 };
69
+ return {
70
+ x: (point.x - this._layout.offsetX) / this._layout.scale,
71
+ y: (point.y - this._layout.offsetY) / this._layout.scale,
72
+ };
73
+ }
74
+ }
75
+ exports.ViewportSystem = ViewportSystem;
@@ -0,0 +1,203 @@
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;
@@ -0,0 +1,20 @@
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
+ }