@pooder/core 0.0.2
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 +7 -0
- package/dist/index.d.mts +155 -0
- package/dist/index.d.ts +155 -0
- package/dist/index.js +437 -0
- package/dist/index.mjs +396 -0
- package/package.json +25 -0
- package/src/canvas.ts +3 -0
- package/src/command.ts +61 -0
- package/src/editor.ts +171 -0
- package/src/event.ts +48 -0
- package/src/extension.ts +189 -0
- package/src/index.ts +9 -0
- package/src/layer.ts +13 -0
- package/src/obj.ts +7 -0
- package/src/types.ts +102 -0
- package/tsconfig.json +13 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
Circle: () => import_fabric4.Circle,
|
|
24
|
+
Ellipse: () => import_fabric4.Ellipse,
|
|
25
|
+
Group: () => import_fabric4.Group,
|
|
26
|
+
Image: () => import_fabric4.FabricImage,
|
|
27
|
+
Line: () => import_fabric4.Line,
|
|
28
|
+
Path: () => import_fabric4.Path,
|
|
29
|
+
Pattern: () => import_fabric4.Pattern,
|
|
30
|
+
Point: () => import_fabric4.Point,
|
|
31
|
+
PooderCanvas: () => import_fabric.Canvas,
|
|
32
|
+
PooderEditor: () => PooderEditor,
|
|
33
|
+
PooderLayer: () => import_fabric2.Group,
|
|
34
|
+
PooderObject: () => import_fabric3.FabricObject,
|
|
35
|
+
Rect: () => import_fabric4.Rect,
|
|
36
|
+
Text: () => import_fabric4.Text,
|
|
37
|
+
filters: () => import_fabric4.filters
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
|
|
41
|
+
// src/event.ts
|
|
42
|
+
var EventBus = class {
|
|
43
|
+
constructor() {
|
|
44
|
+
this.events = /* @__PURE__ */ new Map();
|
|
45
|
+
}
|
|
46
|
+
on(event, handler, priority = 0) {
|
|
47
|
+
if (this.events.has(event)) {
|
|
48
|
+
const listeners = this.events.get(event);
|
|
49
|
+
listeners.push({ handler, priority });
|
|
50
|
+
listeners.sort((a, b) => b.priority - a.priority);
|
|
51
|
+
} else {
|
|
52
|
+
this.events.set(event, [{ handler, priority }]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
off(event, handler) {
|
|
56
|
+
const listeners = this.events.get(event);
|
|
57
|
+
if (!listeners) return;
|
|
58
|
+
const index = listeners.findIndex((l) => l.handler === handler);
|
|
59
|
+
listeners.splice(index, 1);
|
|
60
|
+
}
|
|
61
|
+
emit(event, ...args) {
|
|
62
|
+
const listeners = this.events.get(event);
|
|
63
|
+
if (!listeners) return;
|
|
64
|
+
for (const { handler } of listeners) {
|
|
65
|
+
const result = handler(...args);
|
|
66
|
+
if (result === false) break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
clear() {
|
|
70
|
+
this.events.clear();
|
|
71
|
+
}
|
|
72
|
+
count(event) {
|
|
73
|
+
var _a, _b;
|
|
74
|
+
return (_b = (_a = this.events.get(event)) == null ? void 0 : _a.length) != null ? _b : 0;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// src/command.ts
|
|
79
|
+
var DefaultCommandManager = class {
|
|
80
|
+
constructor(editor) {
|
|
81
|
+
this.editor = editor;
|
|
82
|
+
}
|
|
83
|
+
register(name, command) {
|
|
84
|
+
if (this.editor.commands.has(name)) {
|
|
85
|
+
console.warn(`Command "${name}" already exists. It will be overwritten.`);
|
|
86
|
+
}
|
|
87
|
+
this.editor.commands.set(name, command);
|
|
88
|
+
}
|
|
89
|
+
unregister(name) {
|
|
90
|
+
this.editor.commands.delete(name);
|
|
91
|
+
}
|
|
92
|
+
execute(name, ...args) {
|
|
93
|
+
const command = this.editor.commands.get(name);
|
|
94
|
+
if (!command) {
|
|
95
|
+
console.warn(`Command "${name}" not found`);
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
return command.execute(...args);
|
|
100
|
+
} catch (e) {
|
|
101
|
+
console.error(`Error executing command "${name}":`, e);
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
get(name) {
|
|
106
|
+
return this.editor.commands.get(name);
|
|
107
|
+
}
|
|
108
|
+
has(name) {
|
|
109
|
+
return this.editor.commands.has(name);
|
|
110
|
+
}
|
|
111
|
+
count() {
|
|
112
|
+
return this.editor.commands.size;
|
|
113
|
+
}
|
|
114
|
+
list() {
|
|
115
|
+
return Array.from(this.editor.commands.keys());
|
|
116
|
+
}
|
|
117
|
+
clear() {
|
|
118
|
+
this.editor.commands.clear();
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// src/extension.ts
|
|
123
|
+
var DefaultExtensionManager = class {
|
|
124
|
+
constructor(editor) {
|
|
125
|
+
this.mounted = false;
|
|
126
|
+
this.editor = editor;
|
|
127
|
+
}
|
|
128
|
+
_registerCommands(extension) {
|
|
129
|
+
if (extension.commands) {
|
|
130
|
+
Object.entries(extension.commands).forEach(([name, command]) => {
|
|
131
|
+
const commandName = `${extension.name}.${name}`;
|
|
132
|
+
this.editor.registerCommand(commandName, command);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
_unregisterCommands(extension) {
|
|
137
|
+
if (extension.commands) {
|
|
138
|
+
Object.keys(extension.commands).forEach((name) => {
|
|
139
|
+
const commandName = `${extension.name}.${name}`;
|
|
140
|
+
this.editor.unregisterCommand(commandName);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
register(extension) {
|
|
145
|
+
var _a, _b;
|
|
146
|
+
if (this.editor.extensions.has(extension.name)) {
|
|
147
|
+
console.warn(`Plugin "${extension.name}" already registered. It will be overwritten.`);
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
if (extension.enabled === void 0) {
|
|
151
|
+
extension.enabled = true;
|
|
152
|
+
}
|
|
153
|
+
this.editor.extensions.set(extension.name, extension);
|
|
154
|
+
(_a = extension.onCreate) == null ? void 0 : _a.call(extension, this.editor);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
console.error(`Error in onCreate hook for plugin "${extension.name}":`, error);
|
|
157
|
+
}
|
|
158
|
+
if (extension.enabled) {
|
|
159
|
+
this._registerCommands(extension);
|
|
160
|
+
if (this.mounted) {
|
|
161
|
+
try {
|
|
162
|
+
(_b = extension.onMount) == null ? void 0 : _b.call(extension, this.editor);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
console.error(`Error in onMount hook for plugin "${extension.name}":`, error);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
console.log(`Plugin "${extension.name}" registered successfully`);
|
|
169
|
+
}
|
|
170
|
+
unregister(name) {
|
|
171
|
+
var _a, _b;
|
|
172
|
+
const extension = this.editor.extensions.get(name);
|
|
173
|
+
if (!extension) {
|
|
174
|
+
console.warn(`Plugin "${name}" not found.`);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (this.mounted && extension.enabled) {
|
|
178
|
+
try {
|
|
179
|
+
(_a = extension.onUnmount) == null ? void 0 : _a.call(extension, this.editor);
|
|
180
|
+
} catch (error) {
|
|
181
|
+
console.error(`Error in onUnmount hook for plugin "${name}":`, error);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
(_b = extension.onDestroy) == null ? void 0 : _b.call(extension, this.editor);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
console.error(`Error in onDestroy hook for plugin "${name}":`, error);
|
|
188
|
+
}
|
|
189
|
+
this._unregisterCommands(extension);
|
|
190
|
+
this.editor.extensions.delete(name);
|
|
191
|
+
console.log(`Plugin "${name}" unregistered`);
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
enable(name) {
|
|
195
|
+
var _a;
|
|
196
|
+
const extension = this.get(name);
|
|
197
|
+
if (!extension) {
|
|
198
|
+
console.warn(`Plugin "${name}" not found.`);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (extension.enabled) return;
|
|
202
|
+
extension.enabled = true;
|
|
203
|
+
this._registerCommands(extension);
|
|
204
|
+
if (this.mounted) {
|
|
205
|
+
try {
|
|
206
|
+
(_a = extension.onMount) == null ? void 0 : _a.call(extension, this.editor);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
console.error(`Error in onMount hook for plugin "${name}":`, error);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
disable(name) {
|
|
213
|
+
var _a;
|
|
214
|
+
const extension = this.get(name);
|
|
215
|
+
if (!extension) {
|
|
216
|
+
console.warn(`Plugin "${name}" not found.`);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
if (!extension.enabled) return;
|
|
220
|
+
extension.enabled = false;
|
|
221
|
+
this._unregisterCommands(extension);
|
|
222
|
+
if (this.mounted) {
|
|
223
|
+
try {
|
|
224
|
+
(_a = extension.onUnmount) == null ? void 0 : _a.call(extension, this.editor);
|
|
225
|
+
} catch (error) {
|
|
226
|
+
console.error(`Error in onUnmount hook for plugin "${name}":`, error);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
get(name) {
|
|
231
|
+
return this.editor.extensions.get(name);
|
|
232
|
+
}
|
|
233
|
+
has(name) {
|
|
234
|
+
return this.editor.extensions.has(name);
|
|
235
|
+
}
|
|
236
|
+
count() {
|
|
237
|
+
return this.editor.extensions.size;
|
|
238
|
+
}
|
|
239
|
+
list() {
|
|
240
|
+
return Array.from(this.editor.extensions.values());
|
|
241
|
+
}
|
|
242
|
+
mount() {
|
|
243
|
+
if (this.mounted) return;
|
|
244
|
+
this.editor.extensions.forEach((extension) => {
|
|
245
|
+
var _a;
|
|
246
|
+
if (extension.enabled) {
|
|
247
|
+
try {
|
|
248
|
+
(_a = extension.onMount) == null ? void 0 : _a.call(extension, this.editor);
|
|
249
|
+
} catch (e) {
|
|
250
|
+
console.error(`Error in onMount hook for plugin "${extension.name}":`, e);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
this.mounted = true;
|
|
255
|
+
}
|
|
256
|
+
update() {
|
|
257
|
+
const state = this.editor.getState();
|
|
258
|
+
this.editor.extensions.forEach((extension) => {
|
|
259
|
+
var _a;
|
|
260
|
+
if (extension.enabled) {
|
|
261
|
+
try {
|
|
262
|
+
(_a = extension.onUpdate) == null ? void 0 : _a.call(extension, this.editor, state);
|
|
263
|
+
} catch (e) {
|
|
264
|
+
console.error(`Error in onUpdate hook for plugin "${extension.name}":`, e);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
destroy() {
|
|
270
|
+
const extensionNames = Array.from(this.editor.extensions.keys());
|
|
271
|
+
extensionNames.forEach((name) => this.unregister(name));
|
|
272
|
+
this.mounted = false;
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// src/canvas.ts
|
|
277
|
+
var import_fabric = require("fabric");
|
|
278
|
+
|
|
279
|
+
// src/editor.ts
|
|
280
|
+
var PooderEditor = class {
|
|
281
|
+
constructor(el, options = {}) {
|
|
282
|
+
this.extensions = /* @__PURE__ */ new Map();
|
|
283
|
+
this.commands = /* @__PURE__ */ new Map();
|
|
284
|
+
this.destroyed = false;
|
|
285
|
+
this.state = {
|
|
286
|
+
width: options.width || 800,
|
|
287
|
+
height: options.height || 600
|
|
288
|
+
};
|
|
289
|
+
this.canvas = new import_fabric.Canvas(el, { width: this.state.width, height: this.state.height });
|
|
290
|
+
this.eventBus = new EventBus();
|
|
291
|
+
this.commandManager = new DefaultCommandManager(this);
|
|
292
|
+
this.extensionManager = new DefaultExtensionManager(this);
|
|
293
|
+
if (options.extensions && options.extensions.length > 0) {
|
|
294
|
+
options.extensions.forEach(this.extensionManager.register);
|
|
295
|
+
}
|
|
296
|
+
this.extensionManager.mount();
|
|
297
|
+
console.log("Editor initialized with", this.extensionManager.count(), "plugins");
|
|
298
|
+
}
|
|
299
|
+
use(extension) {
|
|
300
|
+
if (this.destroyed) {
|
|
301
|
+
throw new Error("Cannot register plugin: Editor is destroyed");
|
|
302
|
+
}
|
|
303
|
+
this.extensionManager.register(extension);
|
|
304
|
+
this.emit("update", this.state);
|
|
305
|
+
}
|
|
306
|
+
unuse(name) {
|
|
307
|
+
return this.extensionManager.unregister(name);
|
|
308
|
+
}
|
|
309
|
+
getExtension(name) {
|
|
310
|
+
return this.extensionManager.get(name);
|
|
311
|
+
}
|
|
312
|
+
getExtensions() {
|
|
313
|
+
return this.extensionManager.list();
|
|
314
|
+
}
|
|
315
|
+
enableExtension(name) {
|
|
316
|
+
this.extensionManager.enable(name);
|
|
317
|
+
this.emit("update", this.state);
|
|
318
|
+
}
|
|
319
|
+
disableExtension(name) {
|
|
320
|
+
this.extensionManager.disable(name);
|
|
321
|
+
this.emit("update", this.state);
|
|
322
|
+
}
|
|
323
|
+
registerCommand(name, command) {
|
|
324
|
+
this.commandManager.register(name, command);
|
|
325
|
+
}
|
|
326
|
+
unregisterCommand(name) {
|
|
327
|
+
this.commandManager.unregister(name);
|
|
328
|
+
}
|
|
329
|
+
executeCommand(name, ...args) {
|
|
330
|
+
if (this.destroyed) {
|
|
331
|
+
console.warn("Cannot execute command: Editor is destroyed");
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
this.emit("beforeCommand", name, ...args);
|
|
335
|
+
const result = this.commandManager.execute(name, ...args);
|
|
336
|
+
this.emit("afterCommand", name, args, result);
|
|
337
|
+
return result;
|
|
338
|
+
}
|
|
339
|
+
on(event, handler, priority) {
|
|
340
|
+
this.eventBus.on(event, handler, priority);
|
|
341
|
+
}
|
|
342
|
+
off(event, handler) {
|
|
343
|
+
this.eventBus.off(event, handler);
|
|
344
|
+
}
|
|
345
|
+
emit(event, ...args) {
|
|
346
|
+
this.eventBus.emit(event, ...args);
|
|
347
|
+
}
|
|
348
|
+
getObjects() {
|
|
349
|
+
if (this.destroyed) {
|
|
350
|
+
throw new Error("Cannot get objects: Editor is destroyed");
|
|
351
|
+
}
|
|
352
|
+
return this.canvas.getObjects();
|
|
353
|
+
}
|
|
354
|
+
getObject(id, layerId) {
|
|
355
|
+
var _a;
|
|
356
|
+
let objs;
|
|
357
|
+
if (layerId) {
|
|
358
|
+
objs = (_a = this.getLayer(layerId)) == null ? void 0 : _a.getObjects();
|
|
359
|
+
} else {
|
|
360
|
+
objs = this.getObjects();
|
|
361
|
+
}
|
|
362
|
+
return objs == null ? void 0 : objs.find((obj) => {
|
|
363
|
+
var _a2;
|
|
364
|
+
return ((_a2 = obj == null ? void 0 : obj.data) == null ? void 0 : _a2.id) === id;
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
getLayers() {
|
|
368
|
+
return this.getObjects().filter((obj) => obj.type === "group");
|
|
369
|
+
}
|
|
370
|
+
getLayer(id) {
|
|
371
|
+
return this.getLayers().find((obj) => {
|
|
372
|
+
var _a;
|
|
373
|
+
return ((_a = obj == null ? void 0 : obj.data) == null ? void 0 : _a.id) === id;
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
updateState(updater) {
|
|
377
|
+
if (this.destroyed) {
|
|
378
|
+
console.warn("Cannot update state: Editor is destroyed");
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
this.state = updater(this.state);
|
|
382
|
+
this.extensionManager.update();
|
|
383
|
+
this.emit("update", this.state);
|
|
384
|
+
}
|
|
385
|
+
toJSON() {
|
|
386
|
+
return {
|
|
387
|
+
width: this.state.width,
|
|
388
|
+
height: this.state.height,
|
|
389
|
+
metadata: this.state.metadata
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
async loadFromJSON(json) {
|
|
393
|
+
}
|
|
394
|
+
getState() {
|
|
395
|
+
return { ...this.state };
|
|
396
|
+
}
|
|
397
|
+
destroy() {
|
|
398
|
+
if (this.destroyed) return;
|
|
399
|
+
this.emit("beforeDestroy");
|
|
400
|
+
this.canvas.dispose();
|
|
401
|
+
this.extensionManager.destroy();
|
|
402
|
+
this.eventBus.clear();
|
|
403
|
+
this.commandManager.clear();
|
|
404
|
+
this.destroyed = true;
|
|
405
|
+
console.log("Editor destroyed");
|
|
406
|
+
}
|
|
407
|
+
isDestroyed() {
|
|
408
|
+
return this.destroyed;
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
// src/layer.ts
|
|
413
|
+
var import_fabric2 = require("fabric");
|
|
414
|
+
|
|
415
|
+
// src/obj.ts
|
|
416
|
+
var import_fabric3 = require("fabric");
|
|
417
|
+
|
|
418
|
+
// src/index.ts
|
|
419
|
+
var import_fabric4 = require("fabric");
|
|
420
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
421
|
+
0 && (module.exports = {
|
|
422
|
+
Circle,
|
|
423
|
+
Ellipse,
|
|
424
|
+
Group,
|
|
425
|
+
Image,
|
|
426
|
+
Line,
|
|
427
|
+
Path,
|
|
428
|
+
Pattern,
|
|
429
|
+
Point,
|
|
430
|
+
PooderCanvas,
|
|
431
|
+
PooderEditor,
|
|
432
|
+
PooderLayer,
|
|
433
|
+
PooderObject,
|
|
434
|
+
Rect,
|
|
435
|
+
Text,
|
|
436
|
+
filters
|
|
437
|
+
});
|