@pooder/kit 0.0.2 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pooder/kit",
3
- "version": "0.0.2",
3
+ "version": "2.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,7 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "paper": "^0.12.18",
21
- "@pooder/core": "0.0.2"
21
+ "@pooder/core": "0.1.0"
22
22
  },
23
23
  "scripts": {
24
24
  "build": "tsup src/index.ts --format cjs,esm --dts",
package/src/background.ts CHANGED
@@ -1,173 +1,183 @@
1
- import {Command, Editor, EditorState, Extension, Image, OptionSchema, PooderLayer, Rect} from '@pooder/core';
2
-
3
- interface BackgroundToolOptions {
4
- color: string;
5
- url: string;
6
- }
7
- export class BackgroundTool implements Extension<BackgroundToolOptions> {
8
- public name = 'BackgroundTool';
9
- public options: BackgroundToolOptions = {
10
- color: '',
11
- url: ''
12
- };
13
- public schema: Record<keyof BackgroundToolOptions, OptionSchema> = {
14
- color: {
15
- type: 'color',
16
- label: 'Background Color'
17
- },
18
- url: {
19
- type: 'string',
20
- label: 'Image URL'
21
- }
22
- };
23
-
24
- private initLayer(editor: Editor) {
25
- let backgroundLayer=editor.getLayer('background')
26
- if(!backgroundLayer){
27
- backgroundLayer=new PooderLayer([], {
28
- width: editor.canvas.width,
29
- height: editor.canvas.height,
30
- selectable: false,
31
- evented: false,
32
- data: {
33
- id: 'background'
34
- },
35
- })
36
-
37
- editor.canvas.add(backgroundLayer)
38
- editor.canvas.sendObjectToBack(backgroundLayer)
39
- }
40
-
41
- this.updateBackground(editor, this.options);
42
- }
43
- onMount(editor: Editor) {
44
- this.initLayer(editor);
45
- }
46
-
47
- onUnmount(editor: Editor) {
48
- const layer = editor.getLayer('background');
49
- if (layer) {
50
- editor.canvas.remove(layer);
51
- }
52
- }
53
-
54
- onUpdate(editor: Editor, state: EditorState) {
55
- this.updateBackground(editor, this.options);
56
- }
57
-
58
- private async updateBackground(editor: Editor,options: BackgroundToolOptions) {
59
- const layer = editor.getLayer('background');
60
- if (!layer) {
61
- console.warn('[BackgroundTool] Background layer not found');
62
- return;
63
- }
64
-
65
- const { color, url } = options;
66
- const width = editor.state.width;
67
- const height = editor.state.height;
68
-
69
- let rect=editor.getObject('background-color-rect',"background")
70
- if (rect) {
71
- rect.set({
72
- fill: color
73
- })
74
- } else {
75
- rect = new Rect({
76
- width,
77
- height,
78
- fill: color,
79
- selectable: false,
80
- evented: false,
81
- data: {
82
- id: 'background-color-rect'
83
- }
84
- });
85
- layer.add(rect);
86
- layer.sendObjectToBack(rect);
87
- }
88
-
89
- let img=editor.getObject('background-image',"background") as Image;
90
- try {
91
- if(img){
92
- if(img.getSrc() !== url){
93
- if(url){
94
- await img.setSrc(url);
95
- }else {
96
- layer.remove(img)
97
- }
98
- }
99
- }else {
100
- if (url) {
101
- img = await Image.fromURL(url, { crossOrigin: 'anonymous' });
102
- img.set({
103
- originX: 'left',
104
- originY: 'top',
105
- left: 0,
106
- top: 0,
107
- selectable: false,
108
- evented: false,
109
- data:{
110
- id: 'background-image'
111
- }
112
- })
113
- img.scaleToWidth(width)
114
- if (img.getScaledHeight() < height)
115
- img.scaleToHeight(height);
116
- layer.add(img);
117
- }
118
- }
119
- editor.canvas.requestRenderAll();
120
- } catch (e) {
121
- console.error('[BackgroundTool] Failed to load image', e);
122
- }
123
- }
124
-
125
- commands: Record<string, Command> = {
126
- reset: {
127
- execute: (editor: Editor) => {
128
- this.updateBackground(editor, this.options);
129
- return true;
130
- }
131
- },
132
- clear: {
133
- execute: (editor: Editor) => {
134
- this.options = {
135
- color: 'transparent',
136
- url: ''
137
- };
138
- this.updateBackground(editor, this.options);
139
- return true;
140
- }
141
- },
142
- setBackgroundColor: {
143
- execute: (editor: Editor, color: string) => {
144
- if (this.options.color === color) return true;
145
- this.options.color = color;
146
- this.updateBackground(editor, this.options);
147
- return true;
148
- },
149
- schema: {
150
- color: {
151
- type: 'string', // Should be 'color' if supported by CommandArgSchema, but using 'string' for now as per previous plan
152
- label: 'Background Color',
153
- required: true
154
- }
155
- }
156
- },
157
- setBackgroundImage: {
158
- execute: (editor: Editor, url: string) => {
159
- if (this.options.url === url) return true;
160
- this.options.url = url;
161
- this.updateBackground(editor, this.options);
162
- return true;
163
- },
164
- schema: {
165
- url: {
166
- type: 'string',
167
- label: 'Image URL',
168
- required: true
169
- }
170
- }
171
- }
172
- };
173
- }
1
+ import {
2
+ Command,
3
+ Editor,
4
+ EditorState,
5
+ Extension,
6
+ Image,
7
+ OptionSchema,
8
+ PooderLayer,
9
+ Rect,
10
+ } from "@pooder/core";
11
+
12
+ interface BackgroundToolOptions {
13
+ color: string;
14
+ url: string;
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
+ },
31
+ };
32
+
33
+ private initLayer(editor: Editor) {
34
+ let backgroundLayer = editor.getLayer("background");
35
+ if (!backgroundLayer) {
36
+ backgroundLayer = new PooderLayer([], {
37
+ width: editor.canvas.width,
38
+ height: editor.canvas.height,
39
+ selectable: false,
40
+ evented: false,
41
+ data: {
42
+ id: "background",
43
+ },
44
+ });
45
+
46
+ editor.canvas.add(backgroundLayer);
47
+ editor.canvas.sendObjectToBack(backgroundLayer);
48
+ }
49
+ }
50
+ onMount(editor: Editor) {
51
+ this.initLayer(editor);
52
+ this.updateBackground(editor, this.options);
53
+ }
54
+
55
+ onUnmount(editor: Editor) {
56
+ const layer = editor.getLayer("background");
57
+ if (layer) {
58
+ editor.canvas.remove(layer);
59
+ }
60
+ }
61
+
62
+ onUpdate(editor: Editor, state: EditorState) {
63
+ this.updateBackground(editor, this.options);
64
+ }
65
+
66
+ private async updateBackground(
67
+ editor: Editor,
68
+ options: BackgroundToolOptions,
69
+ ) {
70
+ const layer = editor.getLayer("background");
71
+ if (!layer) {
72
+ console.warn("[BackgroundTool] Background layer not found");
73
+ return;
74
+ }
75
+
76
+ const { color, url } = options;
77
+ const width = editor.state.width;
78
+ const height = editor.state.height;
79
+
80
+ let rect = editor.getObject("background-color-rect", "background");
81
+ if (rect) {
82
+ rect.set({
83
+ fill: color,
84
+ });
85
+ } else {
86
+ rect = new Rect({
87
+ width,
88
+ height,
89
+ fill: color,
90
+ selectable: false,
91
+ evented: false,
92
+ data: {
93
+ id: "background-color-rect",
94
+ },
95
+ });
96
+ layer.add(rect);
97
+ layer.sendObjectToBack(rect);
98
+ }
99
+
100
+ let img = editor.getObject("background-image", "background") as Image;
101
+ try {
102
+ if (img) {
103
+ if (img.getSrc() !== url) {
104
+ if (url) {
105
+ await img.setSrc(url);
106
+ } else {
107
+ layer.remove(img);
108
+ }
109
+ }
110
+ } else {
111
+ if (url) {
112
+ img = await Image.fromURL(url, { crossOrigin: "anonymous" });
113
+ img.set({
114
+ originX: "left",
115
+ originY: "top",
116
+ left: 0,
117
+ top: 0,
118
+ selectable: false,
119
+ evented: false,
120
+ data: {
121
+ id: "background-image",
122
+ },
123
+ });
124
+ img.scaleToWidth(width);
125
+ if (img.getScaledHeight() < height) img.scaleToHeight(height);
126
+ layer.add(img);
127
+ }
128
+ }
129
+ editor.canvas.requestRenderAll();
130
+ } catch (e) {
131
+ console.error("[BackgroundTool] Failed to load image", e);
132
+ }
133
+ }
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
+ }