@pooder/kit 2.0.0 → 3.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.
- package/CHANGELOG.md +11 -0
- package/dist/index.d.mts +246 -134
- package/dist/index.d.ts +246 -134
- package/dist/index.js +2051 -1045
- package/dist/index.mjs +2042 -1050
- package/package.json +3 -2
- package/src/CanvasService.ts +65 -0
- package/src/background.ts +156 -109
- package/src/coordinate.ts +49 -0
- package/src/dieline.ts +536 -336
- package/src/film.ts +120 -89
- package/src/geometry.ts +251 -38
- package/src/hole.ts +422 -286
- package/src/image.ts +374 -174
- package/src/index.ts +1 -0
- package/src/mirror.ts +86 -49
- package/src/ruler.ts +188 -118
- package/src/tracer.ts +372 -0
- package/src/white-ink.ts +186 -142
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pooder/kit",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Standard plugins for Pooder editor",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"paper": "^0.12.18",
|
|
21
|
-
"
|
|
21
|
+
"fabric": "^7.0.0",
|
|
22
|
+
"@pooder/core": "1.0.0"
|
|
22
23
|
},
|
|
23
24
|
"scripts": {
|
|
24
25
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Canvas, Group, FabricObject } from "fabric";
|
|
2
|
+
import { Service } from "@pooder/core";
|
|
3
|
+
|
|
4
|
+
export default class CanvasService implements Service {
|
|
5
|
+
public canvas: Canvas;
|
|
6
|
+
|
|
7
|
+
constructor(el: HTMLCanvasElement | string | Canvas, options?: any) {
|
|
8
|
+
if (el instanceof Canvas) {
|
|
9
|
+
this.canvas = el;
|
|
10
|
+
} else {
|
|
11
|
+
this.canvas = new Canvas(el, {
|
|
12
|
+
preserveObjectStacking: true,
|
|
13
|
+
...options,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
dispose() {
|
|
19
|
+
this.canvas.dispose();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Get a layer (Group) by its ID.
|
|
24
|
+
* We assume layers are Groups directly on the canvas with a data.id property.
|
|
25
|
+
*/
|
|
26
|
+
getLayer(id: string): Group | undefined {
|
|
27
|
+
return this.canvas.getObjects().find((obj: any) => obj.data?.id === id) as
|
|
28
|
+
| Group
|
|
29
|
+
| undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Create a layer (Group) with the given ID if it doesn't exist.
|
|
34
|
+
*/
|
|
35
|
+
createLayer(id: string, options: any = {}): Group {
|
|
36
|
+
let layer = this.getLayer(id);
|
|
37
|
+
if (!layer) {
|
|
38
|
+
const defaultOptions = {
|
|
39
|
+
selectable: false,
|
|
40
|
+
evented: false,
|
|
41
|
+
...options,
|
|
42
|
+
data: { ...options.data, id },
|
|
43
|
+
};
|
|
44
|
+
layer = new Group([], defaultOptions);
|
|
45
|
+
this.canvas.add(layer);
|
|
46
|
+
}
|
|
47
|
+
return layer;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Find an object by ID, optionally within a specific layer.
|
|
52
|
+
*/
|
|
53
|
+
getObject(id: string, layerId?: string): FabricObject | undefined {
|
|
54
|
+
if (layerId) {
|
|
55
|
+
const layer = this.getLayer(layerId);
|
|
56
|
+
if (!layer) return undefined;
|
|
57
|
+
return layer.getObjects().find((obj: any) => obj.data?.id === id);
|
|
58
|
+
}
|
|
59
|
+
return this.canvas.getObjects().find((obj: any) => obj.data?.id === id);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
requestRenderAll() {
|
|
63
|
+
this.canvas.requestRenderAll();
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/background.ts
CHANGED
|
@@ -1,83 +1,174 @@
|
|
|
1
1
|
import {
|
|
2
|
-
Command,
|
|
3
|
-
Editor,
|
|
4
|
-
EditorState,
|
|
5
2
|
Extension,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
ContributionPointIds,
|
|
5
|
+
CommandContribution,
|
|
6
|
+
ConfigurationContribution,
|
|
10
7
|
} from "@pooder/core";
|
|
8
|
+
import { Rect, FabricImage as Image } from "fabric";
|
|
9
|
+
import CanvasService from "./CanvasService";
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export class BackgroundTool implements Extension<BackgroundToolOptions> {
|
|
17
|
-
public name = "BackgroundTool";
|
|
18
|
-
public options: BackgroundToolOptions = {
|
|
19
|
-
color: "",
|
|
20
|
-
url: "",
|
|
21
|
-
};
|
|
22
|
-
public schema: Record<keyof BackgroundToolOptions, OptionSchema> = {
|
|
23
|
-
color: {
|
|
24
|
-
type: "color",
|
|
25
|
-
label: "Background Color",
|
|
26
|
-
},
|
|
27
|
-
url: {
|
|
28
|
-
type: "string",
|
|
29
|
-
label: "Image URL",
|
|
30
|
-
},
|
|
11
|
+
export class BackgroundTool implements Extension {
|
|
12
|
+
id = "pooder.kit.background";
|
|
13
|
+
public metadata = {
|
|
14
|
+
name: "BackgroundTool",
|
|
31
15
|
};
|
|
32
16
|
|
|
33
|
-
private
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
width: editor.canvas.width,
|
|
38
|
-
height: editor.canvas.height,
|
|
39
|
-
selectable: false,
|
|
40
|
-
evented: false,
|
|
41
|
-
data: {
|
|
42
|
-
id: "background",
|
|
43
|
-
},
|
|
44
|
-
});
|
|
17
|
+
private color: string = "";
|
|
18
|
+
private url: string = "";
|
|
19
|
+
|
|
20
|
+
private canvasService?: CanvasService;
|
|
45
21
|
|
|
46
|
-
|
|
47
|
-
|
|
22
|
+
constructor(
|
|
23
|
+
options?: Partial<{
|
|
24
|
+
color: string;
|
|
25
|
+
url: string;
|
|
26
|
+
}>,
|
|
27
|
+
) {
|
|
28
|
+
if (options) {
|
|
29
|
+
Object.assign(this, options);
|
|
48
30
|
}
|
|
49
31
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
this.
|
|
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();
|
|
53
70
|
}
|
|
54
71
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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;
|
|
59
79
|
}
|
|
60
80
|
}
|
|
61
81
|
|
|
62
|
-
|
|
63
|
-
|
|
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
|
+
};
|
|
64
139
|
}
|
|
65
140
|
|
|
66
|
-
private
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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");
|
|
71
158
|
if (!layer) {
|
|
72
159
|
console.warn("[BackgroundTool] Background layer not found");
|
|
73
160
|
return;
|
|
74
161
|
}
|
|
75
162
|
|
|
76
|
-
const { color, url } =
|
|
77
|
-
const width = editor.state.width;
|
|
78
|
-
const height = editor.state.height;
|
|
163
|
+
const { color, url } = this;
|
|
79
164
|
|
|
80
|
-
|
|
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;
|
|
81
172
|
if (rect) {
|
|
82
173
|
rect.set({
|
|
83
174
|
fill: color,
|
|
@@ -97,7 +188,10 @@ export class BackgroundTool implements Extension<BackgroundToolOptions> {
|
|
|
97
188
|
layer.sendObjectToBack(rect);
|
|
98
189
|
}
|
|
99
190
|
|
|
100
|
-
let img =
|
|
191
|
+
let img = this.canvasService.getObject(
|
|
192
|
+
"background-image",
|
|
193
|
+
"background",
|
|
194
|
+
) as Image;
|
|
101
195
|
try {
|
|
102
196
|
if (img) {
|
|
103
197
|
if (img.getSrc() !== url) {
|
|
@@ -126,58 +220,11 @@ export class BackgroundTool implements Extension<BackgroundToolOptions> {
|
|
|
126
220
|
layer.add(img);
|
|
127
221
|
}
|
|
128
222
|
}
|
|
129
|
-
|
|
223
|
+
this.canvasService.requestRenderAll();
|
|
130
224
|
} catch (e) {
|
|
131
225
|
console.error("[BackgroundTool] Failed to load image", e);
|
|
132
226
|
}
|
|
227
|
+
layer.dirty = true;
|
|
228
|
+
this.canvasService.requestRenderAll();
|
|
133
229
|
}
|
|
134
|
-
|
|
135
|
-
commands: Record<string, Command> = {
|
|
136
|
-
reset: {
|
|
137
|
-
execute: (editor: Editor) => {
|
|
138
|
-
this.updateBackground(editor, this.options);
|
|
139
|
-
return true;
|
|
140
|
-
},
|
|
141
|
-
},
|
|
142
|
-
clear: {
|
|
143
|
-
execute: (editor: Editor) => {
|
|
144
|
-
this.options = {
|
|
145
|
-
color: "transparent",
|
|
146
|
-
url: "",
|
|
147
|
-
};
|
|
148
|
-
this.updateBackground(editor, this.options);
|
|
149
|
-
return true;
|
|
150
|
-
},
|
|
151
|
-
},
|
|
152
|
-
setBackgroundColor: {
|
|
153
|
-
execute: (editor: Editor, color: string) => {
|
|
154
|
-
if (this.options.color === color) return true;
|
|
155
|
-
this.options.color = color;
|
|
156
|
-
this.updateBackground(editor, this.options);
|
|
157
|
-
return true;
|
|
158
|
-
},
|
|
159
|
-
schema: {
|
|
160
|
-
color: {
|
|
161
|
-
type: "string", // Should be 'color' if supported by CommandArgSchema, but using 'string' for now as per previous plan
|
|
162
|
-
label: "Background Color",
|
|
163
|
-
required: true,
|
|
164
|
-
},
|
|
165
|
-
},
|
|
166
|
-
},
|
|
167
|
-
setBackgroundImage: {
|
|
168
|
-
execute: (editor: Editor, url: string) => {
|
|
169
|
-
if (this.options.url === url) return true;
|
|
170
|
-
this.options.url = url;
|
|
171
|
-
this.updateBackground(editor, this.options);
|
|
172
|
-
return true;
|
|
173
|
-
},
|
|
174
|
-
schema: {
|
|
175
|
-
url: {
|
|
176
|
-
type: "string",
|
|
177
|
-
label: "Image URL",
|
|
178
|
-
required: true,
|
|
179
|
-
},
|
|
180
|
-
},
|
|
181
|
-
},
|
|
182
|
-
};
|
|
183
230
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
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 class Coordinate {
|
|
12
|
+
/**
|
|
13
|
+
* Convert an absolute value to a normalized value (0-1).
|
|
14
|
+
* @param value Absolute value (e.g., pixels)
|
|
15
|
+
* @param total Total dimension size (e.g., canvas width)
|
|
16
|
+
*/
|
|
17
|
+
static toNormalized(value: number, total: number): number {
|
|
18
|
+
return total === 0 ? 0 : value / total;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Convert a normalized value (0-1) to an absolute value.
|
|
23
|
+
* @param normalized Normalized value (0-1)
|
|
24
|
+
* @param total Total dimension size (e.g., canvas width)
|
|
25
|
+
*/
|
|
26
|
+
static toAbsolute(normalized: number, total: number): number {
|
|
27
|
+
return normalized * total;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Normalize a point's coordinates.
|
|
32
|
+
*/
|
|
33
|
+
static normalizePoint(point: Point, size: Size): Point {
|
|
34
|
+
return {
|
|
35
|
+
x: this.toNormalized(point.x, size.width),
|
|
36
|
+
y: this.toNormalized(point.y, size.height),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Denormalize a point's coordinates to absolute pixels.
|
|
42
|
+
*/
|
|
43
|
+
static denormalizePoint(point: Point, size: Size): Point {
|
|
44
|
+
return {
|
|
45
|
+
x: this.toAbsolute(point.x, size.width),
|
|
46
|
+
y: this.toAbsolute(point.y, size.height),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|