@pooder/kit 1.0.0 → 2.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/src/white-ink.ts CHANGED
@@ -1,301 +1,329 @@
1
- import {
2
- Command,
3
- Editor,
4
- EditorState,
5
- EventHandler,
6
- Extension,
7
- OptionSchema,
8
- Image,
9
- filters,
10
- PooderObject,
11
- PooderLayer
12
- } from '@pooder/core';
13
-
14
- interface WhiteInkToolOptions {
15
- customMask: string;
16
- opacity: number;
17
- enableClip: boolean;
18
- }
19
- export class WhiteInkTool implements Extension<WhiteInkToolOptions> {
20
- public name = 'WhiteInkTool';
21
- public options: WhiteInkToolOptions = {
22
- customMask: '',
23
- opacity: 1,
24
- enableClip: false
25
- };
26
-
27
- public schema: Record<keyof WhiteInkToolOptions, OptionSchema> = {
28
- customMask: { type: 'string', label: 'Custom Mask URL' },
29
- opacity: { type: 'number', min: 0, max: 1, step: 0.01, label: 'Opacity' },
30
- enableClip: { type: 'boolean', label: 'Enable Clip' }
31
- };
32
-
33
- private syncHandler: EventHandler | undefined;
34
-
35
- onMount(editor: Editor) {
36
- this.setup(editor);
37
- this.updateWhiteInk(editor, this.options);
38
- }
39
-
40
- onUnmount(editor: Editor) {
41
- this.teardown(editor);
42
- }
43
-
44
- onDestroy(editor: Editor) {
45
- this.teardown(editor);
46
- }
47
-
48
- private setup(editor: Editor) {
49
- let userLayer = editor.getLayer("user");
50
- if (!userLayer) {
51
- userLayer = new PooderLayer([], {
52
- width: editor.state.width,
53
- height: editor.state.height,
54
- left: 0,
55
- top: 0,
56
- originX: 'left',
57
- originY: 'top',
58
- selectable: false,
59
- evented: true,
60
- subTargetCheck: true,
61
- interactive: true,
62
- data: {
63
- id: 'user'
64
- }
65
- });
66
- editor.canvas.add(userLayer);
67
- }
68
-
69
- if (!this.syncHandler) {
70
- this.syncHandler = (e: any) => {
71
- const target = e.target;
72
- if (target && target.data?.id === 'user-image') {
73
- this.syncWithUserImage(editor);
74
- }
75
- };
76
-
77
- editor.canvas.on('object:moving', this.syncHandler);
78
- editor.canvas.on('object:scaling', this.syncHandler);
79
- editor.canvas.on('object:rotating', this.syncHandler);
80
- editor.canvas.on('object:modified', this.syncHandler);
81
- }
82
- }
83
-
84
- private teardown(editor: Editor) {
85
- if (this.syncHandler) {
86
- editor.canvas.off('object:moving', this.syncHandler);
87
- editor.canvas.off('object:scaling', this.syncHandler);
88
- editor.canvas.off('object:rotating', this.syncHandler);
89
- editor.canvas.off('object:modified', this.syncHandler);
90
- this.syncHandler = undefined;
91
- }
92
-
93
- const layer = editor.getLayer("user");
94
- if (layer) {
95
- const whiteInk = editor.getObject("white-ink", "user");
96
- if (whiteInk) {
97
- layer.remove(whiteInk);
98
- }
99
- }
100
-
101
- const userImage = editor.getObject("user-image", "user") as any;
102
- if (userImage && userImage.clipPath) {
103
- userImage.set({ clipPath: undefined });
104
- }
105
-
106
- editor.canvas.requestRenderAll();
107
- }
108
-
109
- onUpdate(editor: Editor, state: EditorState) {
110
- this.updateWhiteInk(editor, this.options);
111
- }
112
-
113
- commands: Record<string, Command> = {
114
- setWhiteInkImage: {
115
- execute: (editor: Editor, customMask: string, opacity: number, enableClip: boolean = true) => {
116
- if (this.options.customMask === customMask &&
117
- this.options.opacity === opacity &&
118
- this.options.enableClip === enableClip) return true;
119
-
120
- this.options.customMask = customMask;
121
- this.options.opacity = opacity;
122
- this.options.enableClip = enableClip;
123
-
124
- this.updateWhiteInk(editor, this.options);
125
-
126
- return true;
127
- },
128
- schema: {
129
- customMask: {
130
- type: 'string',
131
- label: 'Custom Mask URL',
132
- required: true
133
- },
134
- opacity: {
135
- type: 'number',
136
- label: 'Opacity',
137
- min: 0,
138
- max: 1,
139
- required: true
140
- },
141
- enableClip: {
142
- type: 'boolean',
143
- label: 'Enable Clip',
144
- default: true,
145
- required: false
146
- }
147
- }
148
- }
149
- };
150
-
151
- private updateWhiteInk(editor: Editor, opts: WhiteInkToolOptions) {
152
- const { customMask, opacity, enableClip } = opts;
153
-
154
- const layer = editor.getLayer("user");
155
- if (!layer) {
156
- console.warn('[WhiteInkTool] User layer not found');
157
- return;
158
- }
159
-
160
- const whiteInk = editor.getObject("white-ink", "user") as any;
161
- const userImage = editor.getObject("user-image", "user") as any;
162
-
163
- if (!customMask) {
164
- if (whiteInk) {
165
- layer.remove(whiteInk);
166
- }
167
- if (userImage && userImage.clipPath) {
168
- userImage.set({ clipPath: undefined });
169
- }
170
- editor.canvas.requestRenderAll();
171
- return;
172
- }
173
-
174
- // Check if we need to load/reload white ink backing
175
- if (whiteInk) {
176
- const currentSrc = whiteInk.getSrc?.() || whiteInk._element?.src;
177
- if (currentSrc !== customMask) {
178
- this.loadWhiteInk(editor, layer, customMask, opacity, enableClip, whiteInk);
179
- } else {
180
- if (whiteInk.opacity !== opacity) {
181
- whiteInk.set({ opacity });
182
- editor.canvas.requestRenderAll();
183
- }
184
- }
185
- } else {
186
- this.loadWhiteInk(editor, layer, customMask, opacity, enableClip);
187
- }
188
-
189
- // Handle Clip Path Toggle
190
- if (userImage) {
191
- if (enableClip) {
192
- // If enabled but missing, or mask changed (handled by re-load above, but good to ensure), apply it
193
- // We check if clipPath is present. Ideally we should check if it matches current mask,
194
- // but re-applying is safe.
195
- if (!userImage.clipPath) {
196
- this.applyClipPath(editor, customMask);
197
- }
198
- } else {
199
- // If disabled but present, remove it
200
- if (userImage.clipPath) {
201
- userImage.set({ clipPath: undefined });
202
- editor.canvas.requestRenderAll();
203
- }
204
- }
205
- }
206
- }
207
-
208
- private loadWhiteInk(editor: Editor, layer: PooderLayer, url: string, opacity: number, enableClip: boolean, oldImage?: any) {
209
- Image.fromURL(url, { crossOrigin: 'anonymous' }).then(image => {
210
- if (oldImage) {
211
- // Remove old image but don't copy properties yet, we'll sync with user-image
212
- layer.remove(oldImage);
213
- }
214
-
215
- image.filters?.push(new filters.BlendColor({
216
- color: '#FFFFFF',
217
- mode: 'add'
218
- }));
219
- image.applyFilters();
220
-
221
- image.set({
222
- opacity,
223
- selectable: false,
224
- evented: false,
225
- data: {
226
- id: 'white-ink'
227
- }
228
- });
229
-
230
- // Add to layer
231
- layer.add(image);
232
-
233
- // Ensure white-ink is behind user-image
234
- const userImage = editor.getObject("user-image", "user");
235
- if (userImage) {
236
- // Re-adding moves it to the top of the stack
237
- layer.remove(userImage);
238
- layer.add(userImage);
239
- }
240
-
241
- // Apply clip path to user-image if enabled
242
- if (enableClip) {
243
- this.applyClipPath(editor, url);
244
- } else if (userImage) {
245
- userImage.set({ clipPath: undefined });
246
- }
247
-
248
- // Sync position immediately
249
- this.syncWithUserImage(editor);
250
-
251
- editor.canvas.requestRenderAll();
252
- }).catch(err => {
253
- console.error("Failed to load white ink mask", url, err);
254
- });
255
- }
256
-
257
- private applyClipPath(editor: Editor, url: string) {
258
- const userImage = editor.getObject("user-image", "user") as any;
259
- if (!userImage) return;
260
-
261
- Image.fromURL(url, { crossOrigin: 'anonymous' }).then(maskImage => {
262
- // Configure clipPath
263
- // It needs to be relative to the object center
264
- maskImage.set({
265
- originX: 'center',
266
- originY: 'center',
267
- left: 0,
268
- top: 0,
269
- // Scale to fit userImage if dimensions differ
270
- scaleX: userImage.width / maskImage.width,
271
- scaleY: userImage.height / maskImage.height
272
- });
273
-
274
- userImage.set({ clipPath: maskImage });
275
- editor.canvas.requestRenderAll();
276
- }).catch(err => {
277
- console.error("Failed to load clip path", url, err);
278
- });
279
- }
280
-
281
- private syncWithUserImage(editor: Editor) {
282
- const userImage = editor.getObject("user-image", "user");
283
- const whiteInk = editor.getObject("white-ink", "user");
284
-
285
- if (userImage && whiteInk) {
286
- whiteInk.set({
287
- left: userImage.left,
288
- top: userImage.top,
289
- scaleX: userImage.scaleX,
290
- scaleY: userImage.scaleY,
291
- angle: userImage.angle,
292
- skewX: userImage.skewX,
293
- skewY: userImage.skewY,
294
- flipX: userImage.flipX,
295
- flipY: userImage.flipY,
296
- originX: userImage.originX,
297
- originY: userImage.originY
298
- });
299
- }
300
- }
301
- }
1
+ import {
2
+ Command,
3
+ Editor,
4
+ EditorState,
5
+ EventHandler,
6
+ Extension,
7
+ OptionSchema,
8
+ Image,
9
+ filters,
10
+ PooderObject,
11
+ PooderLayer,
12
+ } from "@pooder/core";
13
+
14
+ interface WhiteInkToolOptions {
15
+ customMask: string;
16
+ opacity: number;
17
+ enableClip: boolean;
18
+ }
19
+ export class WhiteInkTool implements Extension<WhiteInkToolOptions> {
20
+ public name = "WhiteInkTool";
21
+ public options: WhiteInkToolOptions = {
22
+ customMask: "",
23
+ opacity: 1,
24
+ enableClip: false,
25
+ };
26
+
27
+ public schema: Record<keyof WhiteInkToolOptions, OptionSchema> = {
28
+ customMask: { type: "string", label: "Custom Mask URL" },
29
+ opacity: { type: "number", min: 0, max: 1, step: 0.01, label: "Opacity" },
30
+ enableClip: { type: "boolean", label: "Enable Clip" },
31
+ };
32
+
33
+ private syncHandler: EventHandler | undefined;
34
+
35
+ onMount(editor: Editor) {
36
+ this.setup(editor);
37
+ this.updateWhiteInk(editor, this.options);
38
+ }
39
+
40
+ onUnmount(editor: Editor) {
41
+ this.teardown(editor);
42
+ }
43
+
44
+ onDestroy(editor: Editor) {
45
+ this.teardown(editor);
46
+ }
47
+
48
+ private setup(editor: Editor) {
49
+ let userLayer = editor.getLayer("user");
50
+ if (!userLayer) {
51
+ userLayer = new PooderLayer([], {
52
+ width: editor.state.width,
53
+ height: editor.state.height,
54
+ left: 0,
55
+ top: 0,
56
+ originX: "left",
57
+ originY: "top",
58
+ selectable: false,
59
+ evented: true,
60
+ subTargetCheck: true,
61
+ interactive: true,
62
+ data: {
63
+ id: "user",
64
+ },
65
+ });
66
+ editor.canvas.add(userLayer);
67
+ }
68
+
69
+ if (!this.syncHandler) {
70
+ this.syncHandler = (e: any) => {
71
+ const target = e.target;
72
+ if (target && target.data?.id === "user-image") {
73
+ this.syncWithUserImage(editor);
74
+ }
75
+ };
76
+
77
+ editor.canvas.on("object:moving", this.syncHandler);
78
+ editor.canvas.on("object:scaling", this.syncHandler);
79
+ editor.canvas.on("object:rotating", this.syncHandler);
80
+ editor.canvas.on("object:modified", this.syncHandler);
81
+ }
82
+ }
83
+
84
+ private teardown(editor: Editor) {
85
+ if (this.syncHandler) {
86
+ editor.canvas.off("object:moving", this.syncHandler);
87
+ editor.canvas.off("object:scaling", this.syncHandler);
88
+ editor.canvas.off("object:rotating", this.syncHandler);
89
+ editor.canvas.off("object:modified", this.syncHandler);
90
+ this.syncHandler = undefined;
91
+ }
92
+
93
+ const layer = editor.getLayer("user");
94
+ if (layer) {
95
+ const whiteInk = editor.getObject("white-ink", "user");
96
+ if (whiteInk) {
97
+ layer.remove(whiteInk);
98
+ }
99
+ }
100
+
101
+ const userImage = editor.getObject("user-image", "user") as any;
102
+ if (userImage && userImage.clipPath) {
103
+ userImage.set({ clipPath: undefined });
104
+ }
105
+
106
+ editor.canvas.requestRenderAll();
107
+ }
108
+
109
+ onUpdate(editor: Editor, state: EditorState) {
110
+ this.updateWhiteInk(editor, this.options);
111
+ }
112
+
113
+ commands: Record<string, Command> = {
114
+ setWhiteInkImage: {
115
+ execute: (
116
+ editor: Editor,
117
+ customMask: string,
118
+ opacity: number,
119
+ enableClip: boolean = true,
120
+ ) => {
121
+ if (
122
+ this.options.customMask === customMask &&
123
+ this.options.opacity === opacity &&
124
+ this.options.enableClip === enableClip
125
+ )
126
+ return true;
127
+
128
+ this.options.customMask = customMask;
129
+ this.options.opacity = opacity;
130
+ this.options.enableClip = enableClip;
131
+
132
+ this.updateWhiteInk(editor, this.options);
133
+
134
+ return true;
135
+ },
136
+ schema: {
137
+ customMask: {
138
+ type: "string",
139
+ label: "Custom Mask URL",
140
+ required: true,
141
+ },
142
+ opacity: {
143
+ type: "number",
144
+ label: "Opacity",
145
+ min: 0,
146
+ max: 1,
147
+ required: true,
148
+ },
149
+ enableClip: {
150
+ type: "boolean",
151
+ label: "Enable Clip",
152
+ default: true,
153
+ required: false,
154
+ },
155
+ },
156
+ },
157
+ };
158
+
159
+ private updateWhiteInk(editor: Editor, opts: WhiteInkToolOptions) {
160
+ const { customMask, opacity, enableClip } = opts;
161
+
162
+ const layer = editor.getLayer("user");
163
+ if (!layer) {
164
+ console.warn("[WhiteInkTool] User layer not found");
165
+ return;
166
+ }
167
+
168
+ const whiteInk = editor.getObject("white-ink", "user") as any;
169
+ const userImage = editor.getObject("user-image", "user") as any;
170
+
171
+ if (!customMask) {
172
+ if (whiteInk) {
173
+ layer.remove(whiteInk);
174
+ }
175
+ if (userImage && userImage.clipPath) {
176
+ userImage.set({ clipPath: undefined });
177
+ }
178
+ editor.canvas.requestRenderAll();
179
+ return;
180
+ }
181
+
182
+ // Check if we need to load/reload white ink backing
183
+ if (whiteInk) {
184
+ const currentSrc = whiteInk.getSrc?.() || whiteInk._element?.src;
185
+ if (currentSrc !== customMask) {
186
+ this.loadWhiteInk(
187
+ editor,
188
+ layer,
189
+ customMask,
190
+ opacity,
191
+ enableClip,
192
+ whiteInk,
193
+ );
194
+ } else {
195
+ if (whiteInk.opacity !== opacity) {
196
+ whiteInk.set({ opacity });
197
+ editor.canvas.requestRenderAll();
198
+ }
199
+ }
200
+ } else {
201
+ this.loadWhiteInk(editor, layer, customMask, opacity, enableClip);
202
+ }
203
+
204
+ // Handle Clip Path Toggle
205
+ if (userImage) {
206
+ if (enableClip) {
207
+ // If enabled but missing, or mask changed (handled by re-load above, but good to ensure), apply it
208
+ // We check if clipPath is present. Ideally we should check if it matches current mask,
209
+ // but re-applying is safe.
210
+ if (!userImage.clipPath) {
211
+ this.applyClipPath(editor, customMask);
212
+ }
213
+ } else {
214
+ // If disabled but present, remove it
215
+ if (userImage.clipPath) {
216
+ userImage.set({ clipPath: undefined });
217
+ editor.canvas.requestRenderAll();
218
+ }
219
+ }
220
+ }
221
+ }
222
+
223
+ private loadWhiteInk(
224
+ editor: Editor,
225
+ layer: PooderLayer,
226
+ url: string,
227
+ opacity: number,
228
+ enableClip: boolean,
229
+ oldImage?: any,
230
+ ) {
231
+ Image.fromURL(url, { crossOrigin: "anonymous" })
232
+ .then((image) => {
233
+ if (oldImage) {
234
+ // Remove old image but don't copy properties yet, we'll sync with user-image
235
+ layer.remove(oldImage);
236
+ }
237
+
238
+ image.filters?.push(
239
+ new filters.BlendColor({
240
+ color: "#FFFFFF",
241
+ mode: "add",
242
+ }),
243
+ );
244
+ image.applyFilters();
245
+
246
+ image.set({
247
+ opacity,
248
+ selectable: false,
249
+ evented: false,
250
+ data: {
251
+ id: "white-ink",
252
+ },
253
+ });
254
+
255
+ // Add to layer
256
+ layer.add(image);
257
+
258
+ // Ensure white-ink is behind user-image
259
+ const userImage = editor.getObject("user-image", "user");
260
+ if (userImage) {
261
+ // Re-adding moves it to the top of the stack
262
+ layer.remove(userImage);
263
+ layer.add(userImage);
264
+ }
265
+
266
+ // Apply clip path to user-image if enabled
267
+ if (enableClip) {
268
+ this.applyClipPath(editor, url);
269
+ } else if (userImage) {
270
+ userImage.set({ clipPath: undefined });
271
+ }
272
+
273
+ // Sync position immediately
274
+ this.syncWithUserImage(editor);
275
+
276
+ editor.canvas.requestRenderAll();
277
+ })
278
+ .catch((err) => {
279
+ console.error("Failed to load white ink mask", url, err);
280
+ });
281
+ }
282
+
283
+ private applyClipPath(editor: Editor, url: string) {
284
+ const userImage = editor.getObject("user-image", "user") as any;
285
+ if (!userImage) return;
286
+
287
+ Image.fromURL(url, { crossOrigin: "anonymous" })
288
+ .then((maskImage) => {
289
+ // Configure clipPath
290
+ // It needs to be relative to the object center
291
+ maskImage.set({
292
+ originX: "center",
293
+ originY: "center",
294
+ left: 0,
295
+ top: 0,
296
+ // Scale to fit userImage if dimensions differ
297
+ scaleX: userImage.width / maskImage.width,
298
+ scaleY: userImage.height / maskImage.height,
299
+ });
300
+
301
+ userImage.set({ clipPath: maskImage });
302
+ editor.canvas.requestRenderAll();
303
+ })
304
+ .catch((err) => {
305
+ console.error("Failed to load clip path", url, err);
306
+ });
307
+ }
308
+
309
+ private syncWithUserImage(editor: Editor) {
310
+ const userImage = editor.getObject("user-image", "user");
311
+ const whiteInk = editor.getObject("white-ink", "user");
312
+
313
+ if (userImage && whiteInk) {
314
+ whiteInk.set({
315
+ left: userImage.left,
316
+ top: userImage.top,
317
+ scaleX: userImage.scaleX,
318
+ scaleY: userImage.scaleY,
319
+ angle: userImage.angle,
320
+ skewX: userImage.skewX,
321
+ skewY: userImage.skewY,
322
+ flipX: userImage.flipX,
323
+ flipY: userImage.flipY,
324
+ originX: userImage.originX,
325
+ originY: userImage.originY,
326
+ });
327
+ }
328
+ }
329
+ }
package/tsconfig.json CHANGED
@@ -1,13 +1,13 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es2018",
4
- "module": "esnext",
5
- "moduleResolution": "bundler",
6
- "lib": ["esnext", "dom"],
7
- "strict": true,
8
- "esModuleInterop": true,
9
- "skipLibCheck": true,
10
- "declaration": true
11
- },
12
- "include": ["src"]
13
- }
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2018",
4
+ "module": "esnext",
5
+ "moduleResolution": "bundler",
6
+ "lib": ["esnext", "dom"],
7
+ "strict": true,
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "declaration": true
11
+ },
12
+ "include": ["src"]
13
+ }