visbug-editor 0.1.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/LICENSE +169 -0
- package/README.md +490 -0
- package/dist/visbug-editor.cjs.js +1 -0
- package/dist/visbug-editor.esm.js +1 -0
- package/dist/visbug-editor.umd.js +1 -0
- package/package.json +70 -0
- package/types/index.d.ts +457 -0
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for visbug-editor
|
|
3
|
+
*
|
|
4
|
+
* A framework-agnostic library for inline visual editing.
|
|
5
|
+
* Provides tools for position, text, font, and image editing
|
|
6
|
+
* with undo/redo support.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const VERSION: string;
|
|
10
|
+
|
|
11
|
+
// ============================================================================
|
|
12
|
+
// Main Editor Class
|
|
13
|
+
// ============================================================================
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Configuration options for VisBugEditor
|
|
17
|
+
*/
|
|
18
|
+
export interface VisBugEditorOptions {
|
|
19
|
+
/**
|
|
20
|
+
* The container element whose children will be editable
|
|
21
|
+
*/
|
|
22
|
+
container: HTMLElement;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Container mode: 'shadowDOM' or 'div'
|
|
26
|
+
* @default 'shadowDOM'
|
|
27
|
+
*/
|
|
28
|
+
mode?: "shadowDOM" | "div";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Initial tool to activate
|
|
32
|
+
* @default 'position'
|
|
33
|
+
*/
|
|
34
|
+
initialTool?: "position" | "text" | "font";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Callback when tool changes
|
|
38
|
+
*/
|
|
39
|
+
onToolChange?: (tool: string) => void;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Callback when selection changes
|
|
43
|
+
*/
|
|
44
|
+
onSelectionChange?: (elements: HTMLElement[]) => void;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Callback when any edit is made
|
|
48
|
+
*/
|
|
49
|
+
onChange?: (state: { canUndo: boolean; canRedo: boolean }) => void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Async callback for handling image uploads
|
|
53
|
+
* Should return the URL of the uploaded image
|
|
54
|
+
*/
|
|
55
|
+
onImageUpload?: (file: File) => Promise<string>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Custom styles for editor UI
|
|
59
|
+
*/
|
|
60
|
+
styles?: Record<string, string>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether to clear history when setContent is called
|
|
64
|
+
* @default true
|
|
65
|
+
*/
|
|
66
|
+
clearHistoryOnSetContent?: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Main editor class for the visbug-editor library
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const editor = new VisBugEditor({
|
|
75
|
+
* container: document.getElementById('editable-area'),
|
|
76
|
+
* mode: 'shadowDOM',
|
|
77
|
+
* initialTool: 'position',
|
|
78
|
+
* onToolChange: (tool) => console.log('Tool changed:', tool),
|
|
79
|
+
* });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export class VisBugEditor {
|
|
83
|
+
/**
|
|
84
|
+
* The container element
|
|
85
|
+
*/
|
|
86
|
+
readonly container: HTMLElement;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Container mode
|
|
90
|
+
*/
|
|
91
|
+
readonly mode: "shadowDOM" | "div";
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Initial tool name
|
|
95
|
+
*/
|
|
96
|
+
readonly initialTool: string;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Configuration options
|
|
100
|
+
*/
|
|
101
|
+
readonly options: VisBugEditorOptions;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Currently active tool
|
|
105
|
+
*/
|
|
106
|
+
readonly currentTool: string | null;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* History manager instance
|
|
110
|
+
*/
|
|
111
|
+
readonly historyManager: HistoryManager;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Whether the editor is initialized
|
|
115
|
+
*/
|
|
116
|
+
readonly isInitialized: boolean;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Create a new VisBugEditor instance
|
|
120
|
+
*/
|
|
121
|
+
constructor(options: VisBugEditorOptions);
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Activate a specific editing tool
|
|
125
|
+
*/
|
|
126
|
+
activateTool(toolName: "position" | "text" | "font"): void;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Get the currently active tool
|
|
130
|
+
*/
|
|
131
|
+
getCurrentTool(): string | null;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Select a specific element
|
|
135
|
+
*/
|
|
136
|
+
selectElement(element: HTMLElement): void;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Select multiple elements
|
|
140
|
+
*/
|
|
141
|
+
selectElements(elements: HTMLElement[]): void;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Get currently selected elements
|
|
145
|
+
*/
|
|
146
|
+
getSelectedElements(): HTMLElement[];
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Clear current selection
|
|
150
|
+
*/
|
|
151
|
+
clearSelection(): void;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Undo the last change
|
|
155
|
+
* @returns True if undo was successful
|
|
156
|
+
*/
|
|
157
|
+
undo(): boolean;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Redo the last undone change
|
|
161
|
+
* @returns True if redo was successful
|
|
162
|
+
*/
|
|
163
|
+
redo(): boolean;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Check if undo is available
|
|
167
|
+
*/
|
|
168
|
+
canUndo(): boolean;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Check if redo is available
|
|
172
|
+
*/
|
|
173
|
+
canRedo(): boolean;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Get the history array
|
|
177
|
+
*/
|
|
178
|
+
getHistory(): Change[];
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Clear the history
|
|
182
|
+
*/
|
|
183
|
+
clearHistory(): void;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Get clean HTML content (without editor UI elements)
|
|
187
|
+
*/
|
|
188
|
+
getContent(): string;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Set content (replaces all user content)
|
|
192
|
+
*/
|
|
193
|
+
setContent(htmlString: string): void;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Add event listener
|
|
197
|
+
*/
|
|
198
|
+
on(eventName: string, callback: Function): void;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Remove event listener
|
|
202
|
+
*/
|
|
203
|
+
off(eventName: string, callback: Function): void;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Destroy the editor and clean up
|
|
207
|
+
*/
|
|
208
|
+
destroy(): void;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// ============================================================================
|
|
212
|
+
// History System
|
|
213
|
+
// ============================================================================
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Options for HistoryManager
|
|
217
|
+
*/
|
|
218
|
+
export interface HistoryManagerOptions {
|
|
219
|
+
/**
|
|
220
|
+
* Maximum number of changes to keep in history
|
|
221
|
+
* @default 50
|
|
222
|
+
*/
|
|
223
|
+
maxSize?: number;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Time in milliseconds to merge similar changes
|
|
227
|
+
* @default 1000
|
|
228
|
+
*/
|
|
229
|
+
mergeTimeout?: number;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Manages undo/redo history for editor changes
|
|
234
|
+
*/
|
|
235
|
+
export class HistoryManager {
|
|
236
|
+
/**
|
|
237
|
+
* Maximum size of the undo stack
|
|
238
|
+
*/
|
|
239
|
+
readonly maxSize: number;
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Timeout for merging similar changes
|
|
243
|
+
*/
|
|
244
|
+
readonly mergeTimeout: number;
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Create a new HistoryManager instance
|
|
248
|
+
*/
|
|
249
|
+
constructor(options?: HistoryManagerOptions);
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Add event listener
|
|
253
|
+
*/
|
|
254
|
+
on(event: "change", callback: (state: any) => void): void;
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Remove event listener
|
|
258
|
+
*/
|
|
259
|
+
off(event: "change", callback: (state: any) => void): void;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Record a change
|
|
263
|
+
*/
|
|
264
|
+
push(change: Change | Change[]): void;
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Begin batch mode for multiple related changes
|
|
268
|
+
*/
|
|
269
|
+
beginBatch(): void;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* End batch mode and record all changes as one
|
|
273
|
+
*/
|
|
274
|
+
endBatch(): void;
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Undo the last change
|
|
278
|
+
* @returns True if undo was successful
|
|
279
|
+
*/
|
|
280
|
+
undo(): boolean;
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Redo the last undone change
|
|
284
|
+
* @returns True if redo was successful
|
|
285
|
+
*/
|
|
286
|
+
redo(): boolean;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Check if undo is available
|
|
290
|
+
*/
|
|
291
|
+
canUndo(): boolean;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Check if redo is available
|
|
295
|
+
*/
|
|
296
|
+
canRedo(): boolean;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Get the current history state
|
|
300
|
+
*/
|
|
301
|
+
getHistory(): Change[];
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Clear all history
|
|
305
|
+
*/
|
|
306
|
+
clear(): void;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Base class for all changes
|
|
311
|
+
*/
|
|
312
|
+
export abstract class Change {
|
|
313
|
+
/**
|
|
314
|
+
* The type of change
|
|
315
|
+
*/
|
|
316
|
+
readonly type: string;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Undo this change
|
|
320
|
+
*/
|
|
321
|
+
abstract undo(): void;
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Redo this change
|
|
325
|
+
*/
|
|
326
|
+
abstract redo(): void;
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Check if this change can be merged with another
|
|
330
|
+
*/
|
|
331
|
+
canMerge(other: Change): boolean;
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Merge this change with another
|
|
335
|
+
*/
|
|
336
|
+
merge(other: Change): Change;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* A style change on an element
|
|
341
|
+
*/
|
|
342
|
+
export class StyleChange extends Change {
|
|
343
|
+
readonly element: HTMLElement;
|
|
344
|
+
readonly property: string;
|
|
345
|
+
readonly oldValue: string;
|
|
346
|
+
readonly newValue: string;
|
|
347
|
+
|
|
348
|
+
constructor(element: HTMLElement, property: string, oldValue: string, newValue: string);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* A text content change
|
|
353
|
+
*/
|
|
354
|
+
export class TextChange extends Change {
|
|
355
|
+
readonly element: HTMLElement;
|
|
356
|
+
readonly oldValue: string;
|
|
357
|
+
readonly newValue: string;
|
|
358
|
+
|
|
359
|
+
constructor(element: HTMLElement, oldValue: string, newValue: string);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* An attribute change
|
|
364
|
+
*/
|
|
365
|
+
export class AttributeChange extends Change {
|
|
366
|
+
readonly element: HTMLElement;
|
|
367
|
+
readonly attribute: string;
|
|
368
|
+
readonly oldValue: string | null;
|
|
369
|
+
readonly newValue: string | null;
|
|
370
|
+
|
|
371
|
+
constructor(
|
|
372
|
+
element: HTMLElement,
|
|
373
|
+
attribute: string,
|
|
374
|
+
oldValue: string | null,
|
|
375
|
+
newValue: string | null
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* A batch of multiple changes
|
|
381
|
+
*/
|
|
382
|
+
export class BatchChange extends Change {
|
|
383
|
+
readonly changes: Change[];
|
|
384
|
+
|
|
385
|
+
constructor(changes: Change[]);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// ============================================================================
|
|
389
|
+
// Utilities
|
|
390
|
+
// ============================================================================
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Utility functions exported by visbug-editor
|
|
394
|
+
*/
|
|
395
|
+
export namespace utilities {
|
|
396
|
+
/**
|
|
397
|
+
* Color utilities
|
|
398
|
+
*/
|
|
399
|
+
export namespace colors {
|
|
400
|
+
export function hex2hsl(hex: string): [number, number, number];
|
|
401
|
+
export function hex2rgb(hex: string): [number, number, number];
|
|
402
|
+
export function rgb2hex(r: number, g: number, b: number): string;
|
|
403
|
+
export function hsl2hex(h: number, s: number, l: number): string;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Common utilities
|
|
408
|
+
*/
|
|
409
|
+
export namespace common {
|
|
410
|
+
export function getStyle(el: HTMLElement, prop: string): string;
|
|
411
|
+
export function setStyle(el: HTMLElement, prop: string, value: string): void;
|
|
412
|
+
export function getComputedStyles(el: HTMLElement): CSSStyleDeclaration;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Number utilities
|
|
417
|
+
*/
|
|
418
|
+
export namespace numbers {
|
|
419
|
+
export function clamp(value: number, min: number, max: number): number;
|
|
420
|
+
export function roundToNearest(value: number, nearest: number): number;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* String utilities
|
|
425
|
+
*/
|
|
426
|
+
export namespace strings {
|
|
427
|
+
export function camelToKebab(str: string): string;
|
|
428
|
+
export function kebabToCamel(str: string): string;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Design property utilities
|
|
433
|
+
*/
|
|
434
|
+
export namespace designProperties {
|
|
435
|
+
export function getSelectorEngine(el: HTMLElement): string;
|
|
436
|
+
export function combineNodeNameAndClass(el: HTMLElement): string;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// ============================================================================
|
|
441
|
+
// Custom Elements (automatically registered)
|
|
442
|
+
// ============================================================================
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Custom element for selection handles
|
|
446
|
+
*/
|
|
447
|
+
declare global {
|
|
448
|
+
interface HTMLElementTagNameMap {
|
|
449
|
+
"visbug-handles": HTMLElement;
|
|
450
|
+
"visbug-handle": HTMLElement;
|
|
451
|
+
"visbug-label": HTMLElement;
|
|
452
|
+
"visbug-hover": HTMLElement;
|
|
453
|
+
"visbug-overlay": HTMLElement;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export {};
|