@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/ruler.ts CHANGED
@@ -1,239 +1,255 @@
1
- import { Command, Editor, EditorState, Extension, OptionSchema, PooderLayer, Rect, Line, Text } from '@pooder/core';
2
-
3
- export interface RulerToolOptions {
4
- unit: 'px' | 'mm' | 'cm' | 'in';
5
- thickness: number;
6
- backgroundColor: string;
7
- textColor: string;
8
- lineColor: string;
9
- fontSize: number;
10
- }
11
-
12
- export class RulerTool implements Extension<RulerToolOptions> {
13
- public name = 'RulerTool';
14
- public options: RulerToolOptions = {
15
- unit: 'px',
16
- thickness: 20,
17
- backgroundColor: '#f0f0f0',
18
- textColor: '#333333',
19
- lineColor: '#999999',
20
- fontSize: 10
21
- };
22
-
23
- public schema: Record<keyof RulerToolOptions, OptionSchema> = {
24
- unit: {
25
- type: 'select',
26
- options: ['px', 'mm', 'cm', 'in'],
27
- label: 'Unit'
28
- },
29
- thickness: { type: 'number', min: 10, max: 100, label: 'Thickness' },
30
- backgroundColor: { type: 'color', label: 'Background Color' },
31
- textColor: { type: 'color', label: 'Text Color' },
32
- lineColor: { type: 'color', label: 'Line Color' },
33
- fontSize: { type: 'number', min: 8, max: 24, label: 'Font Size' }
34
- };
35
-
36
- onMount(editor: Editor) {
37
- this.createLayer(editor);
38
- this.updateRuler(editor);
39
- }
40
-
41
- onUnmount(editor: Editor) {
42
- this.destroyLayer(editor);
43
- }
44
-
45
- onUpdate(editor: Editor, state: EditorState) {
46
- this.updateRuler(editor);
47
- }
48
-
49
- onDestroy(editor: Editor) {
50
- this.destroyLayer(editor);
51
- }
52
-
53
- private getLayer(editor: Editor) {
54
- return editor.canvas.getObjects().find((obj: any) => obj.data?.id === 'ruler-overlay') as PooderLayer | undefined;
55
- }
56
-
57
- private createLayer(editor: Editor) {
58
- let layer = this.getLayer(editor);
59
-
60
- if (!layer) {
61
- const width = editor.canvas.width || 800;
62
- const height = editor.canvas.height || 600;
63
-
64
- layer = new PooderLayer([], {
65
- width,
66
- height,
67
- selectable: false,
68
- evented: false,
69
- data: { id: 'ruler-overlay' }
70
- } as any);
71
-
72
- editor.canvas.add(layer);
73
- }
74
-
75
- editor.canvas.bringObjectToFront(layer);
76
- }
77
-
78
- private destroyLayer(editor: Editor) {
79
- const layer = this.getLayer(editor);
80
- if (layer) {
81
- editor.canvas.remove(layer);
82
- }
83
- }
84
-
85
- private updateRuler(editor: Editor) {
86
- const layer = this.getLayer(editor);
87
- if (!layer) return;
88
-
89
- layer.remove(...layer.getObjects());
90
-
91
- const { thickness, backgroundColor, lineColor, textColor, fontSize } = this.options;
92
- const width = editor.canvas.width || 800;
93
- const height = editor.canvas.height || 600;
94
-
95
- // Backgrounds
96
- const topBg = new Rect({
97
- left: 0,
98
- top: 0,
99
- width: width,
100
- height: thickness,
101
- fill: backgroundColor,
102
- selectable: false,
103
- evented: false
104
- });
105
-
106
- const leftBg = new Rect({
107
- left: 0,
108
- top: 0,
109
- width: thickness,
110
- height: height,
111
- fill: backgroundColor,
112
- selectable: false,
113
- evented: false
114
- });
115
-
116
- const cornerBg = new Rect({
117
- left: 0,
118
- top: 0,
119
- width: thickness,
120
- height: thickness,
121
- fill: backgroundColor,
122
- stroke: lineColor,
123
- strokeWidth: 1,
124
- selectable: false,
125
- evented: false
126
- });
127
-
128
- layer.add(topBg, leftBg, cornerBg);
129
-
130
- // Drawing Constants (Pixel based for now)
131
- const step = 100; // Major tick
132
- const subStep = 10; // Minor tick
133
- const midStep = 50; // Medium tick
134
-
135
- // Top Ruler
136
- for (let x = 0; x <= width; x += subStep) {
137
- if (x < thickness) continue; // Skip corner
138
-
139
- let len = thickness * 0.25;
140
- if (x % step === 0) len = thickness * 0.8;
141
- else if (x % midStep === 0) len = thickness * 0.5;
142
-
143
- const line = new Line([x, thickness - len, x, thickness], {
144
- stroke: lineColor,
145
- strokeWidth: 1,
146
- selectable: false,
147
- evented: false
148
- });
149
- layer.add(line);
150
-
151
- if (x % step === 0) {
152
- const text = new Text(x.toString(), {
153
- left: x + 2,
154
- top: 2,
155
- fontSize: fontSize,
156
- fill: textColor,
157
- fontFamily: 'Arial',
158
- selectable: false,
159
- evented: false
160
- });
161
- layer.add(text);
162
- }
163
- }
164
-
165
- // Left Ruler
166
- for (let y = 0; y <= height; y += subStep) {
167
- if (y < thickness) continue; // Skip corner
168
-
169
- let len = thickness * 0.25;
170
- if (y % step === 0) len = thickness * 0.8;
171
- else if (y % midStep === 0) len = thickness * 0.5;
172
-
173
- const line = new Line([thickness - len, y, thickness, y], {
174
- stroke: lineColor,
175
- strokeWidth: 1,
176
- selectable: false,
177
- evented: false
178
- });
179
- layer.add(line);
180
-
181
- if (y % step === 0) {
182
- const text = new Text(y.toString(), {
183
- angle: -90,
184
- left: thickness / 2 - fontSize / 3, // approximate centering
185
- top: y + fontSize,
186
- fontSize: fontSize,
187
- fill: textColor,
188
- fontFamily: 'Arial',
189
- originX: 'center',
190
- originY: 'center',
191
- selectable: false,
192
- evented: false
193
- });
194
-
195
- layer.add(text);
196
- }
197
- }
198
-
199
- // Always bring ruler to front
200
- editor.canvas.bringObjectToFront(layer);
201
- editor.canvas.requestRenderAll();
202
- }
203
-
204
- commands: Record<string, Command> = {
205
- setUnit: {
206
- execute: (editor: Editor, unit: 'px' | 'mm' | 'cm' | 'in') => {
207
- if (this.options.unit === unit) return true;
208
- this.options.unit = unit;
209
- this.updateRuler(editor);
210
- return true;
211
- },
212
- schema: {
213
- unit: {
214
- type: 'string',
215
- label: 'Unit',
216
- options: ['px', 'mm', 'cm', 'in'],
217
- required: true
218
- }
219
- }
220
- },
221
- setTheme: {
222
- execute: (editor: Editor, theme: Partial<RulerToolOptions>) => {
223
- const newOptions = { ...this.options, ...theme };
224
- if (JSON.stringify(newOptions) === JSON.stringify(this.options)) return true;
225
-
226
- this.options = newOptions;
227
- this.updateRuler(editor);
228
- return true;
229
- },
230
- schema: {
231
- theme: {
232
- type: 'object',
233
- label: 'Theme',
234
- required: true
235
- }
236
- }
237
- }
238
- };
239
- }
1
+ import {
2
+ Command,
3
+ Editor,
4
+ EditorState,
5
+ Extension,
6
+ OptionSchema,
7
+ PooderLayer,
8
+ Rect,
9
+ Line,
10
+ Text,
11
+ } from "@pooder/core";
12
+
13
+ export interface RulerToolOptions {
14
+ unit: "px" | "mm" | "cm" | "in";
15
+ thickness: number;
16
+ backgroundColor: string;
17
+ textColor: string;
18
+ lineColor: string;
19
+ fontSize: number;
20
+ }
21
+
22
+ export class RulerTool implements Extension<RulerToolOptions> {
23
+ public name = "RulerTool";
24
+ public options: RulerToolOptions = {
25
+ unit: "px",
26
+ thickness: 20,
27
+ backgroundColor: "#f0f0f0",
28
+ textColor: "#333333",
29
+ lineColor: "#999999",
30
+ fontSize: 10,
31
+ };
32
+
33
+ public schema: Record<keyof RulerToolOptions, OptionSchema> = {
34
+ unit: {
35
+ type: "select",
36
+ options: ["px", "mm", "cm", "in"],
37
+ label: "Unit",
38
+ },
39
+ thickness: { type: "number", min: 10, max: 100, label: "Thickness" },
40
+ backgroundColor: { type: "color", label: "Background Color" },
41
+ textColor: { type: "color", label: "Text Color" },
42
+ lineColor: { type: "color", label: "Line Color" },
43
+ fontSize: { type: "number", min: 8, max: 24, label: "Font Size" },
44
+ };
45
+
46
+ onMount(editor: Editor) {
47
+ this.createLayer(editor);
48
+ this.updateRuler(editor);
49
+ }
50
+
51
+ onUnmount(editor: Editor) {
52
+ this.destroyLayer(editor);
53
+ }
54
+
55
+ onUpdate(editor: Editor, state: EditorState) {
56
+ this.updateRuler(editor);
57
+ }
58
+
59
+ onDestroy(editor: Editor) {
60
+ this.destroyLayer(editor);
61
+ }
62
+
63
+ private getLayer(editor: Editor) {
64
+ return editor.canvas
65
+ .getObjects()
66
+ .find((obj: any) => obj.data?.id === "ruler-overlay") as
67
+ | PooderLayer
68
+ | undefined;
69
+ }
70
+
71
+ private createLayer(editor: Editor) {
72
+ let layer = this.getLayer(editor);
73
+
74
+ if (!layer) {
75
+ const width = editor.canvas.width || 800;
76
+ const height = editor.canvas.height || 600;
77
+
78
+ layer = new PooderLayer([], {
79
+ width,
80
+ height,
81
+ selectable: false,
82
+ evented: false,
83
+ data: { id: "ruler-overlay" },
84
+ } as any);
85
+
86
+ editor.canvas.add(layer);
87
+ }
88
+
89
+ editor.canvas.bringObjectToFront(layer);
90
+ }
91
+
92
+ private destroyLayer(editor: Editor) {
93
+ const layer = this.getLayer(editor);
94
+ if (layer) {
95
+ editor.canvas.remove(layer);
96
+ }
97
+ }
98
+
99
+ private updateRuler(editor: Editor) {
100
+ const layer = this.getLayer(editor);
101
+ if (!layer) return;
102
+
103
+ layer.remove(...layer.getObjects());
104
+
105
+ const { thickness, backgroundColor, lineColor, textColor, fontSize } =
106
+ this.options;
107
+ const width = editor.canvas.width || 800;
108
+ const height = editor.canvas.height || 600;
109
+
110
+ // Backgrounds
111
+ const topBg = new Rect({
112
+ left: 0,
113
+ top: 0,
114
+ width: width,
115
+ height: thickness,
116
+ fill: backgroundColor,
117
+ selectable: false,
118
+ evented: false,
119
+ });
120
+
121
+ const leftBg = new Rect({
122
+ left: 0,
123
+ top: 0,
124
+ width: thickness,
125
+ height: height,
126
+ fill: backgroundColor,
127
+ selectable: false,
128
+ evented: false,
129
+ });
130
+
131
+ const cornerBg = new Rect({
132
+ left: 0,
133
+ top: 0,
134
+ width: thickness,
135
+ height: thickness,
136
+ fill: backgroundColor,
137
+ stroke: lineColor,
138
+ strokeWidth: 1,
139
+ selectable: false,
140
+ evented: false,
141
+ });
142
+
143
+ layer.add(topBg, leftBg, cornerBg);
144
+
145
+ // Drawing Constants (Pixel based for now)
146
+ const step = 100; // Major tick
147
+ const subStep = 10; // Minor tick
148
+ const midStep = 50; // Medium tick
149
+
150
+ // Top Ruler
151
+ for (let x = 0; x <= width; x += subStep) {
152
+ if (x < thickness) continue; // Skip corner
153
+
154
+ let len = thickness * 0.25;
155
+ if (x % step === 0) len = thickness * 0.8;
156
+ else if (x % midStep === 0) len = thickness * 0.5;
157
+
158
+ const line = new Line([x, thickness - len, x, thickness], {
159
+ stroke: lineColor,
160
+ strokeWidth: 1,
161
+ selectable: false,
162
+ evented: false,
163
+ });
164
+ layer.add(line);
165
+
166
+ if (x % step === 0) {
167
+ const text = new Text(x.toString(), {
168
+ left: x + 2,
169
+ top: 2,
170
+ fontSize: fontSize,
171
+ fill: textColor,
172
+ fontFamily: "Arial",
173
+ selectable: false,
174
+ evented: false,
175
+ });
176
+ layer.add(text);
177
+ }
178
+ }
179
+
180
+ // Left Ruler
181
+ for (let y = 0; y <= height; y += subStep) {
182
+ if (y < thickness) continue; // Skip corner
183
+
184
+ let len = thickness * 0.25;
185
+ if (y % step === 0) len = thickness * 0.8;
186
+ else if (y % midStep === 0) len = thickness * 0.5;
187
+
188
+ const line = new Line([thickness - len, y, thickness, y], {
189
+ stroke: lineColor,
190
+ strokeWidth: 1,
191
+ selectable: false,
192
+ evented: false,
193
+ });
194
+ layer.add(line);
195
+
196
+ if (y % step === 0) {
197
+ const text = new Text(y.toString(), {
198
+ angle: -90,
199
+ left: thickness / 2 - fontSize / 3, // approximate centering
200
+ top: y + fontSize,
201
+ fontSize: fontSize,
202
+ fill: textColor,
203
+ fontFamily: "Arial",
204
+ originX: "center",
205
+ originY: "center",
206
+ selectable: false,
207
+ evented: false,
208
+ });
209
+
210
+ layer.add(text);
211
+ }
212
+ }
213
+
214
+ // Always bring ruler to front
215
+ editor.canvas.bringObjectToFront(layer);
216
+ editor.canvas.requestRenderAll();
217
+ }
218
+
219
+ commands: Record<string, Command> = {
220
+ setUnit: {
221
+ execute: (editor: Editor, unit: "px" | "mm" | "cm" | "in") => {
222
+ if (this.options.unit === unit) return true;
223
+ this.options.unit = unit;
224
+ this.updateRuler(editor);
225
+ return true;
226
+ },
227
+ schema: {
228
+ unit: {
229
+ type: "string",
230
+ label: "Unit",
231
+ options: ["px", "mm", "cm", "in"],
232
+ required: true,
233
+ },
234
+ },
235
+ },
236
+ setTheme: {
237
+ execute: (editor: Editor, theme: Partial<RulerToolOptions>) => {
238
+ const newOptions = { ...this.options, ...theme };
239
+ if (JSON.stringify(newOptions) === JSON.stringify(this.options))
240
+ return true;
241
+
242
+ this.options = newOptions;
243
+ this.updateRuler(editor);
244
+ return true;
245
+ },
246
+ schema: {
247
+ theme: {
248
+ type: "object",
249
+ label: "Theme",
250
+ required: true,
251
+ },
252
+ },
253
+ },
254
+ };
255
+ }