@pooder/kit 5.3.1 → 5.4.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 (90) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/index.d.mts +243 -36
  3. package/dist/index.d.ts +243 -36
  4. package/dist/index.js +2278 -1041
  5. package/dist/index.mjs +2278 -1041
  6. package/package.json +1 -1
  7. package/src/coordinate.ts +106 -106
  8. package/src/extensions/background.ts +323 -230
  9. package/src/extensions/bridgeSelection.ts +17 -17
  10. package/src/extensions/constraints.ts +322 -322
  11. package/src/extensions/dieline.ts +1149 -1076
  12. package/src/extensions/dielineShape.ts +109 -0
  13. package/src/extensions/edgeScale.ts +19 -19
  14. package/src/extensions/feature.ts +1137 -1021
  15. package/src/extensions/featureComplete.ts +46 -46
  16. package/src/extensions/film.ts +266 -194
  17. package/src/extensions/geometry.ts +885 -752
  18. package/src/extensions/image.ts +2054 -1926
  19. package/src/extensions/index.ts +11 -11
  20. package/src/extensions/maskOps.ts +283 -283
  21. package/src/extensions/mirror.ts +128 -128
  22. package/src/extensions/ruler.ts +654 -451
  23. package/src/extensions/sceneLayout.ts +140 -140
  24. package/src/extensions/sceneLayoutModel.ts +364 -352
  25. package/src/extensions/sceneVisibility.ts +64 -71
  26. package/src/extensions/size.ts +389 -389
  27. package/src/extensions/tracer.ts +1019 -1019
  28. package/src/extensions/white-ink.ts +1567 -1514
  29. package/src/extensions/wrappedOffsets.ts +33 -33
  30. package/src/index.ts +2 -2
  31. package/src/services/CanvasService.ts +832 -300
  32. package/src/services/ViewportSystem.ts +95 -95
  33. package/src/services/index.ts +3 -3
  34. package/src/services/renderSpec.ts +53 -18
  35. package/src/units.ts +27 -27
  36. package/tests/run.ts +118 -118
  37. package/tsconfig.test.json +15 -15
  38. package/.test-dist/src/CanvasService.js +0 -249
  39. package/.test-dist/src/ViewportSystem.js +0 -75
  40. package/.test-dist/src/background.js +0 -203
  41. package/.test-dist/src/bridgeSelection.js +0 -20
  42. package/.test-dist/src/constraints.js +0 -237
  43. package/.test-dist/src/coordinate.js +0 -74
  44. package/.test-dist/src/dieline.js +0 -818
  45. package/.test-dist/src/edgeScale.js +0 -12
  46. package/.test-dist/src/extensions/background.js +0 -203
  47. package/.test-dist/src/extensions/bridgeSelection.js +0 -20
  48. package/.test-dist/src/extensions/constraints.js +0 -237
  49. package/.test-dist/src/extensions/dieline.js +0 -828
  50. package/.test-dist/src/extensions/edgeScale.js +0 -12
  51. package/.test-dist/src/extensions/feature.js +0 -825
  52. package/.test-dist/src/extensions/featureComplete.js +0 -32
  53. package/.test-dist/src/extensions/film.js +0 -167
  54. package/.test-dist/src/extensions/geometry.js +0 -545
  55. package/.test-dist/src/extensions/image.js +0 -1529
  56. package/.test-dist/src/extensions/index.js +0 -30
  57. package/.test-dist/src/extensions/maskOps.js +0 -279
  58. package/.test-dist/src/extensions/mirror.js +0 -104
  59. package/.test-dist/src/extensions/ruler.js +0 -345
  60. package/.test-dist/src/extensions/sceneLayout.js +0 -96
  61. package/.test-dist/src/extensions/sceneLayoutModel.js +0 -196
  62. package/.test-dist/src/extensions/sceneVisibility.js +0 -62
  63. package/.test-dist/src/extensions/size.js +0 -331
  64. package/.test-dist/src/extensions/tracer.js +0 -538
  65. package/.test-dist/src/extensions/white-ink.js +0 -1190
  66. package/.test-dist/src/extensions/wrappedOffsets.js +0 -33
  67. package/.test-dist/src/feature.js +0 -826
  68. package/.test-dist/src/featureComplete.js +0 -32
  69. package/.test-dist/src/film.js +0 -167
  70. package/.test-dist/src/geometry.js +0 -506
  71. package/.test-dist/src/image.js +0 -1250
  72. package/.test-dist/src/index.js +0 -18
  73. package/.test-dist/src/maskOps.js +0 -270
  74. package/.test-dist/src/mirror.js +0 -104
  75. package/.test-dist/src/renderSpec.js +0 -2
  76. package/.test-dist/src/ruler.js +0 -343
  77. package/.test-dist/src/sceneLayout.js +0 -99
  78. package/.test-dist/src/sceneLayoutModel.js +0 -196
  79. package/.test-dist/src/sceneView.js +0 -40
  80. package/.test-dist/src/sceneVisibility.js +0 -42
  81. package/.test-dist/src/services/CanvasService.js +0 -249
  82. package/.test-dist/src/services/ViewportSystem.js +0 -76
  83. package/.test-dist/src/services/index.js +0 -24
  84. package/.test-dist/src/services/renderSpec.js +0 -2
  85. package/.test-dist/src/size.js +0 -332
  86. package/.test-dist/src/tracer.js +0 -544
  87. package/.test-dist/src/units.js +0 -30
  88. package/.test-dist/src/white-ink.js +0 -829
  89. package/.test-dist/src/wrappedOffsets.js +0 -33
  90. package/.test-dist/tests/run.js +0 -94
@@ -1,230 +1,323 @@
1
- import {
2
- Extension,
3
- ExtensionContext,
4
- ContributionPointIds,
5
- CommandContribution,
6
- ConfigurationContribution,
7
- } from "@pooder/core";
8
- import { Rect, FabricImage as Image } from "fabric";
9
- import { CanvasService } from "../services";
10
-
11
- export class BackgroundTool implements Extension {
12
- id = "pooder.kit.background";
13
- public metadata = {
14
- name: "BackgroundTool",
15
- };
16
-
17
- private color: string = "";
18
- private url: string = "";
19
-
20
- private canvasService?: CanvasService;
21
-
22
- constructor(
23
- options?: Partial<{
24
- color: string;
25
- url: string;
26
- }>,
27
- ) {
28
- if (options) {
29
- Object.assign(this, options);
30
- }
31
- }
32
-
33
- activate(context: ExtensionContext) {
34
- this.canvasService = context.services.get<CanvasService>("CanvasService");
35
- if (!this.canvasService) {
36
- console.warn("CanvasService not found for BackgroundTool");
37
- return;
38
- }
39
-
40
- const configService = context.services.get<any>("ConfigurationService");
41
- if (configService) {
42
- // Load initial config
43
- this.color = configService.get("background.color", this.color);
44
- this.url = configService.get("background.url", this.url);
45
-
46
- // Listen for changes
47
- configService.onAnyChange((e: { key: string; value: any }) => {
48
- if (e.key.startsWith("background.")) {
49
- const prop = e.key.split(".")[1];
50
- console.log(
51
- `[BackgroundTool] Config change detected: ${e.key} -> ${e.value}, prop: ${prop}`,
52
- );
53
- if (prop && prop in this) {
54
- console.log(
55
- `[BackgroundTool] Updating option ${prop} to ${e.value}`,
56
- );
57
- (this as any)[prop] = e.value;
58
- this.updateBackground();
59
- } else {
60
- console.warn(
61
- `[BackgroundTool] Property ${prop} not found in options`,
62
- );
63
- }
64
- }
65
- });
66
- }
67
-
68
- this.initLayer();
69
- this.updateBackground();
70
- }
71
-
72
- deactivate(context: ExtensionContext) {
73
- if (this.canvasService) {
74
- const layer = this.canvasService.getLayer("background");
75
- if (layer) {
76
- this.canvasService.canvas.remove(layer);
77
- }
78
- this.canvasService = undefined;
79
- }
80
- }
81
-
82
- contribute() {
83
- return {
84
- [ContributionPointIds.CONFIGURATIONS]: [
85
- {
86
- id: "background.color",
87
- type: "color",
88
- label: "Background Color",
89
- default: "",
90
- },
91
- {
92
- id: "background.url",
93
- type: "string",
94
- label: "Image URL",
95
- default: "",
96
- },
97
- ] as ConfigurationContribution[],
98
- [ContributionPointIds.COMMANDS]: [
99
- {
100
- command: "reset",
101
- title: "Reset Background",
102
- handler: () => {
103
- this.updateBackground();
104
- return true;
105
- },
106
- },
107
- {
108
- command: "clear",
109
- title: "Clear Background",
110
- handler: () => {
111
- this.color = "transparent";
112
- this.url = "";
113
- this.updateBackground();
114
- return true;
115
- },
116
- },
117
- {
118
- command: "setBackgroundColor",
119
- title: "Set Background Color",
120
- handler: (color: string) => {
121
- if (this.color === color) return true;
122
- this.color = color;
123
- this.updateBackground();
124
- return true;
125
- },
126
- },
127
- {
128
- command: "setBackgroundImage",
129
- title: "Set Background Image",
130
- handler: (url: string) => {
131
- if (this.url === url) return true;
132
- this.url = url;
133
- this.updateBackground();
134
- return true;
135
- },
136
- },
137
- ] as CommandContribution[],
138
- };
139
- }
140
-
141
- private initLayer() {
142
- if (!this.canvasService) return;
143
- let backgroundLayer = this.canvasService.getLayer("background");
144
- if (!backgroundLayer) {
145
- backgroundLayer = this.canvasService.createLayer("background", {
146
- width: this.canvasService.canvas.width,
147
- height: this.canvasService.canvas.height,
148
- selectable: false,
149
- evented: false,
150
- });
151
- this.canvasService.canvas.sendObjectToBack(backgroundLayer);
152
- }
153
- }
154
-
155
- private async updateBackground() {
156
- if (!this.canvasService) return;
157
- const layer = this.canvasService.getLayer("background");
158
- if (!layer) {
159
- console.warn("[BackgroundTool] Background layer not found");
160
- return;
161
- }
162
-
163
- const { color, url } = this;
164
-
165
- const width = this.canvasService.canvas.width || 800;
166
- const height = this.canvasService.canvas.height || 600;
167
-
168
- let rect = this.canvasService.getObject(
169
- "background-color-rect",
170
- "background",
171
- ) as Rect;
172
- if (rect) {
173
- rect.set({
174
- fill: color,
175
- });
176
- } else {
177
- rect = new Rect({
178
- width,
179
- height,
180
- fill: color,
181
- selectable: false,
182
- evented: false,
183
- data: {
184
- id: "background-color-rect",
185
- },
186
- });
187
- layer.add(rect);
188
- layer.sendObjectToBack(rect);
189
- }
190
-
191
- let img = this.canvasService.getObject(
192
- "background-image",
193
- "background",
194
- ) as Image;
195
- try {
196
- if (img) {
197
- if (img.getSrc() !== url) {
198
- if (url) {
199
- await img.setSrc(url);
200
- } else {
201
- layer.remove(img);
202
- }
203
- }
204
- } else {
205
- if (url) {
206
- img = await Image.fromURL(url, { crossOrigin: "anonymous" });
207
- img.set({
208
- originX: "left",
209
- originY: "top",
210
- left: 0,
211
- top: 0,
212
- selectable: false,
213
- evented: false,
214
- data: {
215
- id: "background-image",
216
- },
217
- });
218
- img.scaleToWidth(width);
219
- if (img.getScaledHeight() < height) img.scaleToHeight(height);
220
- layer.add(img);
221
- }
222
- }
223
- this.canvasService.requestRenderAll();
224
- } catch (e) {
225
- console.error("[BackgroundTool] Failed to load image", e);
226
- }
227
- layer.dirty = true;
228
- this.canvasService.requestRenderAll();
229
- }
230
- }
1
+ import {
2
+ Extension,
3
+ ExtensionContext,
4
+ ContributionPointIds,
5
+ CommandContribution,
6
+ ConfigurationContribution,
7
+ ConfigurationService,
8
+ } from "@pooder/core";
9
+ import { FabricImage } from "fabric";
10
+ import { CanvasService, RenderObjectSpec } from "../services";
11
+
12
+ interface SourceSize {
13
+ width: number;
14
+ height: number;
15
+ }
16
+
17
+ const BACKGROUND_LAYER_ID = "background";
18
+ const BACKGROUND_RECT_ID = "background-color-rect";
19
+ const BACKGROUND_IMAGE_ID = "background-image";
20
+ const DEFAULT_WIDTH = 800;
21
+ const DEFAULT_HEIGHT = 600;
22
+
23
+ export class BackgroundTool implements Extension {
24
+ id = "pooder.kit.background";
25
+ public metadata = {
26
+ name: "BackgroundTool",
27
+ };
28
+
29
+ private color: string = "";
30
+ private url: string = "";
31
+
32
+ private canvasService?: CanvasService;
33
+ private specs: RenderObjectSpec[] = [];
34
+ private renderProducerDisposable?: { dispose: () => void };
35
+ private renderSeq = 0;
36
+ private renderImageUrl = "";
37
+ private sourceSizeBySrc: Map<string, SourceSize> = new Map();
38
+ private pendingSizeBySrc: Map<string, Promise<SourceSize | null>> = new Map();
39
+ private onCanvasResized = () => {
40
+ this.updateBackground();
41
+ };
42
+
43
+ constructor(
44
+ options?: Partial<{
45
+ color: string;
46
+ url: string;
47
+ }>,
48
+ ) {
49
+ if (options) {
50
+ Object.assign(this, options);
51
+ }
52
+ }
53
+
54
+ activate(context: ExtensionContext) {
55
+ this.canvasService = context.services.get<CanvasService>("CanvasService");
56
+ if (!this.canvasService) {
57
+ console.warn("CanvasService not found for BackgroundTool");
58
+ return;
59
+ }
60
+
61
+ this.renderProducerDisposable?.dispose();
62
+ this.renderProducerDisposable = this.canvasService.registerRenderProducer(
63
+ this.id,
64
+ () => ({
65
+ layerSpecs: {
66
+ [BACKGROUND_LAYER_ID]: this.specs,
67
+ },
68
+ }),
69
+ { priority: 0 },
70
+ );
71
+
72
+ const configService = context.services.get<ConfigurationService>(
73
+ "ConfigurationService",
74
+ );
75
+ if (configService) {
76
+ // Load initial config
77
+ this.color = configService.get("background.color", this.color);
78
+ this.url = configService.get("background.url", this.url);
79
+
80
+ // Listen for changes
81
+ configService.onAnyChange((e: { key: string; value: any }) => {
82
+ if (e.key.startsWith("background.")) {
83
+ const prop = e.key.split(".")[1];
84
+ console.log(
85
+ `[BackgroundTool] Config change detected: ${e.key} -> ${e.value}, prop: ${prop}`,
86
+ );
87
+ if (prop && prop in this) {
88
+ console.log(
89
+ `[BackgroundTool] Updating option ${prop} to ${e.value}`,
90
+ );
91
+ (this as any)[prop] = e.value;
92
+ this.updateBackground();
93
+ } else {
94
+ console.warn(
95
+ `[BackgroundTool] Property ${prop} not found in options`,
96
+ );
97
+ }
98
+ }
99
+ });
100
+ }
101
+
102
+ context.eventBus.on("canvas:resized", this.onCanvasResized);
103
+ this.updateBackground();
104
+ }
105
+
106
+ deactivate(context: ExtensionContext) {
107
+ context.eventBus.off("canvas:resized", this.onCanvasResized);
108
+ this.renderSeq += 1;
109
+ this.specs = [];
110
+ this.renderImageUrl = "";
111
+ this.renderProducerDisposable?.dispose();
112
+ this.renderProducerDisposable = undefined;
113
+ if (!this.canvasService) return;
114
+ const layer = this.canvasService.getLayer(BACKGROUND_LAYER_ID);
115
+ if (layer) {
116
+ this.canvasService.canvas.remove(layer);
117
+ }
118
+ void this.canvasService.flushRenderFromProducers();
119
+ this.canvasService.requestRenderAll();
120
+ this.canvasService = undefined;
121
+ }
122
+
123
+ contribute() {
124
+ return {
125
+ [ContributionPointIds.CONFIGURATIONS]: [
126
+ {
127
+ id: "background.color",
128
+ type: "color",
129
+ label: "Background Color",
130
+ default: "",
131
+ },
132
+ {
133
+ id: "background.url",
134
+ type: "string",
135
+ label: "Image URL",
136
+ default: "",
137
+ },
138
+ ] as ConfigurationContribution[],
139
+ [ContributionPointIds.COMMANDS]: [
140
+ {
141
+ command: "reset",
142
+ title: "Reset Background",
143
+ handler: () => {
144
+ this.updateBackground();
145
+ return true;
146
+ },
147
+ },
148
+ {
149
+ command: "clear",
150
+ title: "Clear Background",
151
+ handler: () => {
152
+ this.color = "transparent";
153
+ this.url = "";
154
+ this.updateBackground();
155
+ return true;
156
+ },
157
+ },
158
+ {
159
+ command: "setBackgroundColor",
160
+ title: "Set Background Color",
161
+ handler: (color: string) => {
162
+ if (this.color === color) return true;
163
+ this.color = color;
164
+ this.updateBackground();
165
+ return true;
166
+ },
167
+ },
168
+ {
169
+ command: "setBackgroundImage",
170
+ title: "Set Background Image",
171
+ handler: (url: string) => {
172
+ if (this.url === url) return true;
173
+ this.url = url;
174
+ this.updateBackground();
175
+ return true;
176
+ },
177
+ },
178
+ ] as CommandContribution[],
179
+ };
180
+ }
181
+
182
+ private getViewportSize(): { width: number; height: number } {
183
+ const width = Number(this.canvasService?.canvas.width || 0);
184
+ const height = Number(this.canvasService?.canvas.height || 0);
185
+ return {
186
+ width: width > 0 ? width : DEFAULT_WIDTH,
187
+ height: height > 0 ? height : DEFAULT_HEIGHT,
188
+ };
189
+ }
190
+
191
+ private buildBackgroundSpecs(
192
+ color: string,
193
+ imageUrl: string,
194
+ ): RenderObjectSpec[] {
195
+ const { width, height } = this.getViewportSize();
196
+ const specs: RenderObjectSpec[] = [
197
+ {
198
+ id: BACKGROUND_RECT_ID,
199
+ type: "rect",
200
+ space: "screen",
201
+ data: {
202
+ id: BACKGROUND_RECT_ID,
203
+ layerId: BACKGROUND_LAYER_ID,
204
+ type: "background-color",
205
+ },
206
+ props: {
207
+ left: 0,
208
+ top: 0,
209
+ width,
210
+ height,
211
+ originX: "left",
212
+ originY: "top",
213
+ fill: color,
214
+ selectable: false,
215
+ evented: false,
216
+ excludeFromExport: true,
217
+ },
218
+ },
219
+ ];
220
+
221
+ if (!imageUrl) {
222
+ return specs;
223
+ }
224
+
225
+ const sourceSize = this.sourceSizeBySrc.get(imageUrl);
226
+ const sourceWidth = Math.max(1, Number(sourceSize?.width || width));
227
+ const sourceHeight = Math.max(1, Number(sourceSize?.height || height));
228
+ const coverScale = Math.max(width / sourceWidth, height / sourceHeight);
229
+ specs.push({
230
+ id: BACKGROUND_IMAGE_ID,
231
+ type: "image",
232
+ src: imageUrl,
233
+ space: "screen",
234
+ data: {
235
+ id: BACKGROUND_IMAGE_ID,
236
+ layerId: BACKGROUND_LAYER_ID,
237
+ type: "background-image",
238
+ },
239
+ props: {
240
+ left: 0,
241
+ top: 0,
242
+ originX: "left",
243
+ originY: "top",
244
+ scaleX: coverScale,
245
+ scaleY: coverScale,
246
+ selectable: false,
247
+ evented: false,
248
+ excludeFromExport: true,
249
+ },
250
+ });
251
+
252
+ return specs;
253
+ }
254
+
255
+ private async ensureImageSize(src: string): Promise<SourceSize | null> {
256
+ if (!src) return null;
257
+ const cached = this.sourceSizeBySrc.get(src);
258
+ if (cached) return cached;
259
+
260
+ const pending = this.pendingSizeBySrc.get(src);
261
+ if (pending) {
262
+ return pending;
263
+ }
264
+
265
+ const task = this.loadImageSize(src);
266
+ this.pendingSizeBySrc.set(src, task);
267
+ try {
268
+ return await task;
269
+ } finally {
270
+ if (this.pendingSizeBySrc.get(src) === task) {
271
+ this.pendingSizeBySrc.delete(src);
272
+ }
273
+ }
274
+ }
275
+
276
+ private async loadImageSize(src: string): Promise<SourceSize | null> {
277
+ try {
278
+ const image = await FabricImage.fromURL(src, {
279
+ crossOrigin: "anonymous",
280
+ });
281
+ const width = Number(image?.width || 0);
282
+ const height = Number(image?.height || 0);
283
+ if (width > 0 && height > 0) {
284
+ const size = { width, height };
285
+ this.sourceSizeBySrc.set(src, size);
286
+ return size;
287
+ }
288
+ } catch (error) {
289
+ console.error("[BackgroundTool] Failed to load image", src, error);
290
+ }
291
+ return null;
292
+ }
293
+
294
+ private updateBackground() {
295
+ void this.updateBackgroundAsync();
296
+ }
297
+
298
+ private async updateBackgroundAsync() {
299
+ if (!this.canvasService) return;
300
+ const seq = ++this.renderSeq;
301
+ const color = this.color;
302
+ const nextUrl = String(this.url || "").trim();
303
+
304
+ if (!nextUrl) {
305
+ this.renderImageUrl = "";
306
+ } else if (nextUrl !== this.renderImageUrl) {
307
+ const loaded = await this.ensureImageSize(nextUrl);
308
+ if (seq !== this.renderSeq) return;
309
+ if (loaded) {
310
+ this.renderImageUrl = nextUrl;
311
+ }
312
+ }
313
+
314
+ this.specs = this.buildBackgroundSpecs(color, this.renderImageUrl);
315
+ await this.canvasService.flushRenderFromProducers();
316
+ if (seq !== this.renderSeq) return;
317
+ const layer = this.canvasService.getLayer(BACKGROUND_LAYER_ID);
318
+ if (layer) {
319
+ this.canvasService.canvas.sendObjectToBack(layer);
320
+ }
321
+ this.canvasService.requestRenderAll();
322
+ }
323
+ }
@@ -1,17 +1,17 @@
1
- export function pickExitIndex(
2
- hits: Array<{ insideAbove: boolean; insideBelow: boolean }>,
3
- ): number {
4
- for (let i = 0; i < hits.length; i++) {
5
- const h = hits[i];
6
- if (h.insideBelow && !h.insideAbove) return i;
7
- }
8
- return -1;
9
- }
10
-
11
- export function scoreOutsideAbove(samples: Array<{ outsideAbove: boolean }>): number {
12
- let score = 0;
13
- for (const s of samples) {
14
- if (s.outsideAbove) score++;
15
- }
16
- return score;
17
- }
1
+ export function pickExitIndex(
2
+ hits: Array<{ insideAbove: boolean; insideBelow: boolean }>,
3
+ ): number {
4
+ for (let i = 0; i < hits.length; i++) {
5
+ const h = hits[i];
6
+ if (h.insideBelow && !h.insideAbove) return i;
7
+ }
8
+ return -1;
9
+ }
10
+
11
+ export function scoreOutsideAbove(samples: Array<{ outsideAbove: boolean }>): number {
12
+ let score = 0;
13
+ for (const s of samples) {
14
+ if (s.outsideAbove) score++;
15
+ }
16
+ return score;
17
+ }