@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,46 +1,46 @@
1
- import { ConstraintContext, ConstraintFeature, ConstraintRegistry } from "./constraints";
2
-
3
- export type FeatureCompleteIssue = {
4
- featureId: string;
5
- groupId?: string;
6
- reason: string;
7
- };
8
-
9
- export function validateFeaturesStrict(
10
- features: ConstraintFeature[],
11
- context: ConstraintContext,
12
- ): { ok: boolean; issues?: FeatureCompleteIssue[] } {
13
- const eps = 1e-6;
14
- const issues: FeatureCompleteIssue[] = [];
15
-
16
- for (const f of features) {
17
- if (!f.constraints || f.constraints.length === 0) continue;
18
- // Pass ALL constraints (including validateOnly) for strict validation check
19
- const constrained = ConstraintRegistry.apply(f.x, f.y, f, context, f.constraints);
20
- if (
21
- Math.abs(constrained.x - f.x) > eps ||
22
- Math.abs(constrained.y - f.y) > eps
23
- ) {
24
- issues.push({
25
- featureId: f.id,
26
- groupId: f.groupId,
27
- reason: "Position violates constraint strategy",
28
- });
29
- }
30
- }
31
-
32
- return { ok: issues.length === 0, issues: issues.length ? issues : undefined };
33
- }
34
-
35
- export function completeFeaturesStrict(
36
- features: ConstraintFeature[],
37
- context: ConstraintContext,
38
- update: (nextFeatures: ConstraintFeature[]) => void,
39
- ): { ok: boolean; issues?: FeatureCompleteIssue[] } {
40
- const validation = validateFeaturesStrict(features, context);
41
- if (!validation.ok) return validation;
42
- const next = JSON.parse(JSON.stringify(features || [])) as ConstraintFeature[];
43
- update(next);
44
- return { ok: true };
45
- }
46
-
1
+ import { ConstraintContext, ConstraintFeature, ConstraintRegistry } from "./constraints";
2
+
3
+ export type FeatureCompleteIssue = {
4
+ featureId: string;
5
+ groupId?: string;
6
+ reason: string;
7
+ };
8
+
9
+ export function validateFeaturesStrict(
10
+ features: ConstraintFeature[],
11
+ context: ConstraintContext,
12
+ ): { ok: boolean; issues?: FeatureCompleteIssue[] } {
13
+ const eps = 1e-6;
14
+ const issues: FeatureCompleteIssue[] = [];
15
+
16
+ for (const f of features) {
17
+ if (!f.constraints || f.constraints.length === 0) continue;
18
+ // Pass ALL constraints (including validateOnly) for strict validation check
19
+ const constrained = ConstraintRegistry.apply(f.x, f.y, f, context, f.constraints);
20
+ if (
21
+ Math.abs(constrained.x - f.x) > eps ||
22
+ Math.abs(constrained.y - f.y) > eps
23
+ ) {
24
+ issues.push({
25
+ featureId: f.id,
26
+ groupId: f.groupId,
27
+ reason: "Position violates constraint strategy",
28
+ });
29
+ }
30
+ }
31
+
32
+ return { ok: issues.length === 0, issues: issues.length ? issues : undefined };
33
+ }
34
+
35
+ export function completeFeaturesStrict(
36
+ features: ConstraintFeature[],
37
+ context: ConstraintContext,
38
+ update: (nextFeatures: ConstraintFeature[]) => void,
39
+ ): { ok: boolean; issues?: FeatureCompleteIssue[] } {
40
+ const validation = validateFeaturesStrict(features, context);
41
+ if (!validation.ok) return validation;
42
+ const next = JSON.parse(JSON.stringify(features || [])) as ConstraintFeature[];
43
+ update(next);
44
+ return { ok: true };
45
+ }
46
+
@@ -1,194 +1,266 @@
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 "../services";
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
+ 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 FILM_LAYER_ID = "overlay";
18
+ const FILM_IMAGE_ID = "film-image";
19
+ const DEFAULT_WIDTH = 800;
20
+ const DEFAULT_HEIGHT = 600;
21
+
22
+ export class FilmTool implements Extension {
23
+ id = "pooder.kit.film";
24
+
25
+ public metadata = {
26
+ name: "FilmTool",
27
+ };
28
+
29
+ private url: string = "";
30
+ private opacity: number = 0.5;
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.updateFilm();
41
+ };
42
+
43
+ constructor(
44
+ options?: Partial<{
45
+ url: string;
46
+ opacity: number;
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 FilmTool");
58
+ return;
59
+ }
60
+
61
+ this.renderProducerDisposable?.dispose();
62
+ this.renderProducerDisposable = this.canvasService.registerRenderProducer(
63
+ this.id,
64
+ () => ({
65
+ layerSpecs: {
66
+ [FILM_LAYER_ID]: this.specs,
67
+ },
68
+ }),
69
+ { priority: 500 },
70
+ );
71
+
72
+ const configService = context.services.get<ConfigurationService>(
73
+ "ConfigurationService",
74
+ );
75
+ if (configService) {
76
+ // Load initial config
77
+ this.url = configService.get("film.url", this.url);
78
+ this.opacity = configService.get("film.opacity", this.opacity);
79
+
80
+ // Listen for changes
81
+ configService.onAnyChange((e: { key: string; value: any }) => {
82
+ if (e.key.startsWith("film.")) {
83
+ const prop = e.key.split(".")[1];
84
+ console.log(
85
+ `[FilmTool] Config change detected: ${e.key} -> ${e.value}`,
86
+ );
87
+ if (prop && prop in this) {
88
+ (this as any)[prop] = e.value;
89
+ this.updateFilm();
90
+ }
91
+ }
92
+ });
93
+ }
94
+
95
+ context.eventBus.on("canvas:resized", this.onCanvasResized);
96
+ this.updateFilm();
97
+ }
98
+
99
+ deactivate(context: ExtensionContext) {
100
+ context.eventBus.off("canvas:resized", this.onCanvasResized);
101
+ this.renderSeq += 1;
102
+ this.specs = [];
103
+ this.renderImageUrl = "";
104
+ this.renderProducerDisposable?.dispose();
105
+ this.renderProducerDisposable = undefined;
106
+ if (!this.canvasService) return;
107
+ void this.canvasService.flushRenderFromProducers();
108
+ this.canvasService.requestRenderAll();
109
+ this.canvasService = undefined;
110
+ }
111
+
112
+ contribute() {
113
+ return {
114
+ [ContributionPointIds.CONFIGURATIONS]: [
115
+ {
116
+ id: "film.url",
117
+ type: "string",
118
+ label: "Film Image URL",
119
+ default: "",
120
+ },
121
+ {
122
+ id: "film.opacity",
123
+ type: "number",
124
+ label: "Opacity",
125
+ min: 0,
126
+ max: 1,
127
+ step: 0.1,
128
+ default: 0.5,
129
+ },
130
+ ] as ConfigurationContribution[],
131
+ [ContributionPointIds.COMMANDS]: [
132
+ {
133
+ command: "setFilmImage",
134
+ title: "Set Film Image",
135
+ handler: (url: string, opacity: number) => {
136
+ if (this.url === url && this.opacity === opacity) return true;
137
+
138
+ this.url = url;
139
+ this.opacity = opacity;
140
+
141
+ this.updateFilm();
142
+
143
+ return true;
144
+ },
145
+ },
146
+ ] as CommandContribution[],
147
+ };
148
+ }
149
+
150
+ private getViewportSize(): { width: number; height: number } {
151
+ const width = Number(this.canvasService?.canvas.width || 0);
152
+ const height = Number(this.canvasService?.canvas.height || 0);
153
+ return {
154
+ width: width > 0 ? width : DEFAULT_WIDTH,
155
+ height: height > 0 ? height : DEFAULT_HEIGHT,
156
+ };
157
+ }
158
+
159
+ private clampOpacity(value: number): number {
160
+ return Math.max(0, Math.min(1, Number(value)));
161
+ }
162
+
163
+ private buildFilmSpecs(
164
+ imageUrl: string,
165
+ opacity: number,
166
+ ): RenderObjectSpec[] {
167
+ if (!imageUrl) {
168
+ return [];
169
+ }
170
+ const { width, height } = this.getViewportSize();
171
+ const sourceSize = this.sourceSizeBySrc.get(imageUrl);
172
+ const sourceWidth = Math.max(1, Number(sourceSize?.width || width));
173
+ const sourceHeight = Math.max(1, Number(sourceSize?.height || height));
174
+ const coverScale = Math.max(width / sourceWidth, height / sourceHeight);
175
+ return [
176
+ {
177
+ id: FILM_IMAGE_ID,
178
+ type: "image",
179
+ src: imageUrl,
180
+ space: "screen",
181
+ data: {
182
+ id: FILM_IMAGE_ID,
183
+ layerId: FILM_LAYER_ID,
184
+ type: "film-image",
185
+ },
186
+ props: {
187
+ left: 0,
188
+ top: 0,
189
+ originX: "left",
190
+ originY: "top",
191
+ opacity: this.clampOpacity(opacity),
192
+ scaleX: coverScale,
193
+ scaleY: coverScale,
194
+ selectable: false,
195
+ evented: false,
196
+ excludeFromExport: true,
197
+ },
198
+ },
199
+ ];
200
+ }
201
+
202
+ private async ensureImageSize(src: string): Promise<SourceSize | null> {
203
+ if (!src) return null;
204
+ const cached = this.sourceSizeBySrc.get(src);
205
+ if (cached) return cached;
206
+
207
+ const pending = this.pendingSizeBySrc.get(src);
208
+ if (pending) {
209
+ return pending;
210
+ }
211
+
212
+ const task = this.loadImageSize(src);
213
+ this.pendingSizeBySrc.set(src, task);
214
+ try {
215
+ return await task;
216
+ } finally {
217
+ if (this.pendingSizeBySrc.get(src) === task) {
218
+ this.pendingSizeBySrc.delete(src);
219
+ }
220
+ }
221
+ }
222
+
223
+ private async loadImageSize(src: string): Promise<SourceSize | null> {
224
+ try {
225
+ const image = await FabricImage.fromURL(src, {
226
+ crossOrigin: "anonymous",
227
+ });
228
+ const width = Number(image?.width || 0);
229
+ const height = Number(image?.height || 0);
230
+ if (width > 0 && height > 0) {
231
+ const size = { width, height };
232
+ this.sourceSizeBySrc.set(src, size);
233
+ return size;
234
+ }
235
+ } catch (error) {
236
+ console.error("[FilmTool] Failed to load film image", src, error);
237
+ }
238
+ return null;
239
+ }
240
+
241
+ private updateFilm() {
242
+ void this.updateFilmAsync();
243
+ }
244
+
245
+ private async updateFilmAsync() {
246
+ if (!this.canvasService) return;
247
+ const seq = ++this.renderSeq;
248
+ const nextUrl = String(this.url || "").trim();
249
+
250
+ if (!nextUrl) {
251
+ this.renderImageUrl = "";
252
+ } else if (nextUrl !== this.renderImageUrl) {
253
+ const loaded = await this.ensureImageSize(nextUrl);
254
+ if (seq !== this.renderSeq) return;
255
+ if (loaded) {
256
+ this.renderImageUrl = nextUrl;
257
+ }
258
+ }
259
+
260
+ this.specs = this.buildFilmSpecs(this.renderImageUrl, this.opacity);
261
+ await this.canvasService.flushRenderFromProducers();
262
+ if (seq !== this.renderSeq) return;
263
+ this.canvasService.bringLayerToFront(FILM_LAYER_ID);
264
+ this.canvasService.requestRenderAll();
265
+ }
266
+ }