@meenainwal/rich-text-editor 1.1.1 → 1.2.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.
@@ -1,1102 +0,0 @@
1
- class u {
2
- /**
3
- * Returns the current selection object.
4
- */
5
- getSelection() {
6
- return window.getSelection();
7
- }
8
- /**
9
- * Returns the first range of the current selection.
10
- */
11
- getRange() {
12
- const e = this.getSelection();
13
- return !e || e.rangeCount === 0 ? null : e.getRangeAt(0);
14
- }
15
- /**
16
- * Saves the current selection range.
17
- */
18
- saveSelection() {
19
- const e = this.getRange();
20
- return e ? e.cloneRange() : null;
21
- }
22
- /**
23
- * Restores a previously saved range.
24
- */
25
- restoreSelection(e) {
26
- if (!e) return;
27
- const t = this.getSelection();
28
- t && (t.removeAllRanges(), t.addRange(e));
29
- }
30
- /**
31
- * Checks if the selection is within a specific element.
32
- */
33
- isSelectionInElement(e) {
34
- const t = this.getRange();
35
- return t ? e.contains(t.commonAncestorContainer) : !1;
36
- }
37
- /**
38
- * Clears the current selection.
39
- */
40
- clearSelection() {
41
- const e = this.getSelection();
42
- e && e.removeAllRanges();
43
- }
44
- }
45
- class m {
46
- editor;
47
- activeContainer = null;
48
- isResizing = !1;
49
- startX = 0;
50
- startY = 0;
51
- startWidth = 0;
52
- startHeight = 0;
53
- currentHandle = null;
54
- aspectRatio = 1;
55
- boundMouseDown;
56
- boundMouseMove;
57
- boundMouseUp;
58
- boundKeyDown;
59
- constructor(e) {
60
- this.editor = e, this.boundMouseDown = this.handleMouseDown.bind(this), this.boundMouseMove = this.handleMouseMove.bind(this), this.boundMouseUp = this.handleMouseUp.bind(this), this.boundKeyDown = this.handleKeyDown.bind(this), this.setupListeners();
61
- }
62
- setupListeners() {
63
- const e = this.editor.el;
64
- e.addEventListener("mousedown", this.boundMouseDown), window.addEventListener("mousemove", this.boundMouseMove), window.addEventListener("mouseup", this.boundMouseUp), e.addEventListener("keydown", this.boundKeyDown);
65
- }
66
- handleMouseDown(e) {
67
- const t = e.target;
68
- if (t.classList.contains("te-image-resizer")) {
69
- e.preventDefault(), e.stopPropagation();
70
- const n = t.closest(".te-image-container");
71
- n && (this.selectImage(n), this.startResize(e, t));
72
- return;
73
- }
74
- const i = t.closest(".te-image-container");
75
- i ? this.selectImage(i) : this.deselectImage();
76
- }
77
- handleMouseMove(e) {
78
- this.isResizing && this.handleResize(e);
79
- }
80
- handleMouseUp() {
81
- this.isResizing && this.stopResize();
82
- }
83
- handleKeyDown(e) {
84
- (e.key === "Backspace" || e.key === "Delete") && this.activeContainer && (e.preventDefault(), this.activeContainer.remove(), this.activeContainer = null, this.editor.el.dispatchEvent(new Event("input", { bubbles: !0 })));
85
- }
86
- destroy() {
87
- const e = this.editor.el;
88
- e && (e.removeEventListener("mousedown", this.boundMouseDown), e.removeEventListener("keydown", this.boundKeyDown)), window.removeEventListener("mousemove", this.boundMouseMove), window.removeEventListener("mouseup", this.boundMouseUp);
89
- }
90
- selectImage(e) {
91
- this.activeContainer && this.activeContainer.classList.remove("active"), this.activeContainer = e, this.activeContainer.classList.add("active");
92
- }
93
- deselectImage() {
94
- this.activeContainer && (this.activeContainer.classList.remove("active"), this.activeContainer = null);
95
- }
96
- startResize(e, t) {
97
- if (!this.activeContainer) return;
98
- this.isResizing = !0, this.currentHandle = Array.from(t.classList).find((n) => n.startsWith("te-resizer-"))?.replace("te-resizer-", "") || null;
99
- const i = this.activeContainer.querySelector("img");
100
- this.startX = e.clientX, this.startY = e.clientY, this.startWidth = i.clientWidth, this.startHeight = i.clientHeight, this.aspectRatio = this.startWidth / this.startHeight, document.body.style.cursor = window.getComputedStyle(t).cursor;
101
- }
102
- handleResize(e) {
103
- if (!this.activeContainer || !this.isResizing) return;
104
- const t = this.activeContainer.querySelector("img"), i = e.clientX - this.startX, n = e.clientY - this.startY;
105
- let s = this.startWidth, o = this.startHeight;
106
- this.currentHandle?.includes("right") ? s = this.startWidth + i : this.currentHandle?.includes("left") ? s = this.startWidth - i : this.currentHandle?.includes("bottom") ? s = this.startWidth + n * this.aspectRatio : this.currentHandle?.includes("top") && (s = this.startWidth - n * this.aspectRatio), o = s / this.aspectRatio, s > 50 && s < this.editor.el.clientWidth && (t.style.width = `${s}px`, t.style.height = `${o}px`);
107
- }
108
- stopResize() {
109
- this.isResizing = !1, this.currentHandle = null, document.body.style.cursor = "", this.editor.el.dispatchEvent(new Event("input", { bubbles: !0 }));
110
- }
111
- }
112
- class p {
113
- stack = [];
114
- index = -1;
115
- maxDepth = 50;
116
- constructor(e) {
117
- e !== void 0 && this.record(e);
118
- }
119
- /**
120
- * Records a new state in the history stack.
121
- * Clears any "redo" states if we record a new action.
122
- */
123
- record(e) {
124
- this.index >= 0 && this.stack[this.index].html === e || (this.index < this.stack.length - 1 && (this.stack = this.stack.slice(0, this.index + 1)), this.stack.push({ html: e }), this.index++, this.stack.length > this.maxDepth && (this.stack.shift(), this.index--));
125
- }
126
- /**
127
- * Returns the previous state if available.
128
- */
129
- undo() {
130
- return this.index > 0 ? (this.index--, this.stack[this.index].html) : null;
131
- }
132
- /**
133
- * Returns the next state if available.
134
- */
135
- redo() {
136
- return this.index < this.stack.length - 1 ? (this.index++, this.stack[this.index].html) : null;
137
- }
138
- /**
139
- * Checks if undo is possible.
140
- */
141
- canUndo() {
142
- return this.index > 0;
143
- }
144
- /**
145
- * Checks if redo is possible.
146
- */
147
- canRedo() {
148
- return this.index < this.stack.length - 1;
149
- }
150
- }
151
- class v {
152
- container;
153
- editableElement;
154
- selection;
155
- imageManager;
156
- history;
157
- options;
158
- saveTimeout = null;
159
- historyTimeout = null;
160
- pendingStyles = {};
161
- observer = null;
162
- eventListeners = [];
163
- constructor(e, t = {}) {
164
- if (this.options = t, this.container = e, typeof document > "u" || !e) {
165
- this.editableElement = {}, this.selection = {}, this.imageManager = {}, this.history = {};
166
- return;
167
- }
168
- this.container.innerHTML = "", this.container.classList.add("te-container"), this.options.dark && this.container.classList.add("te-dark"), this.editableElement = this.createEditableElement(), this.selection = new u(), this.imageManager = new m(this), this.history = new p(this.editableElement.innerHTML), this.setupInputHandlers(), this.setupImageObserver(), this.checkPlaceholder(), this.container.appendChild(this.editableElement), this.options.autofocus && this.focus(), this.options.theme && this.applyTheme(this.options.theme), document.execCommand("defaultParagraphSeparator", !1, "p");
169
- }
170
- /**
171
- * Applies custom theme variables to the editor container.
172
- */
173
- applyTheme(e) {
174
- const t = this.container, i = {
175
- primaryColor: "--te-primary-color",
176
- primaryHover: "--te-primary-hover",
177
- bgApp: "--te-bg-app",
178
- bgEditor: "--te-bg-editor",
179
- toolbarBg: "--te-toolbar-bg",
180
- borderColor: "--te-border-color",
181
- borderFocus: "--te-border-focus",
182
- textMain: "--te-text-main",
183
- textMuted: "--te-text-muted",
184
- placeholder: "--te-placeholder",
185
- btnHover: "--te-btn-hover",
186
- btnActive: "--te-btn-active",
187
- radiusLg: "--te-radius-lg",
188
- radiusMd: "--te-radius-md",
189
- radiusSm: "--te-radius-sm",
190
- shadowSm: "--te-shadow-sm",
191
- shadowMd: "--te-shadow-md",
192
- shadowLg: "--te-shadow-lg"
193
- };
194
- for (const [n, s] of Object.entries(i)) {
195
- const o = e[n];
196
- o && t.style.setProperty(s, o);
197
- }
198
- }
199
- /**
200
- * Toggles dark mode on the editor.
201
- */
202
- setDarkMode(e) {
203
- this.options.dark = e, e ? this.container.classList.add("te-dark") : this.container.classList.remove("te-dark");
204
- }
205
- /**
206
- * Destroys the editor instance and cleans up.
207
- */
208
- destroy() {
209
- this.observer && (this.observer.disconnect(), this.observer = null), this.saveTimeout && clearTimeout(this.saveTimeout), this.historyTimeout && clearTimeout(this.historyTimeout), this.imageManager && typeof this.imageManager.destroy == "function" && this.imageManager.destroy(), this.eventListeners.forEach(({ target: e, type: t, handler: i }) => {
210
- e.removeEventListener(t, i);
211
- }), this.eventListeners = [], this.container.innerHTML = "", this.container.classList.remove("te-container", "te-dark"), this.container.removeAttribute("style");
212
- }
213
- checkPlaceholder() {
214
- if (!this.editableElement) return;
215
- this.editableElement.textContent?.trim() === "" && !this.editableElement.querySelector("img") && !this.editableElement.querySelector("table") ? this.editableElement.classList.add("is-empty") : this.editableElement.classList.remove("is-empty");
216
- }
217
- addEventListener(e, t, i, n) {
218
- e.addEventListener(t, i, n), this.eventListeners.push({ target: e, type: t, handler: i });
219
- }
220
- setupImageObserver() {
221
- this.observer = new MutationObserver((e) => {
222
- e.forEach((t) => {
223
- t.addedNodes.forEach((i) => {
224
- if (i.nodeType === Node.ELEMENT_NODE) {
225
- const n = i;
226
- n.tagName === "IMG" && !n.closest(".te-image-container") ? this.wrapImage(n) : n.querySelectorAll("img:not(.te-image)").forEach((o) => {
227
- o.closest(".te-image-container") || this.wrapImage(o);
228
- });
229
- }
230
- });
231
- });
232
- }), this.observer.observe(this.editableElement, {
233
- childList: !0,
234
- subtree: !0
235
- });
236
- }
237
- /**
238
- * Wraps a raw <img> element in the interactive container
239
- */
240
- wrapImage(e) {
241
- const t = e.parentElement;
242
- if (!t) return;
243
- const i = document.createElement("figure");
244
- i.classList.add("te-image-container"), i.setAttribute("contenteditable", "false");
245
- const n = document.createElement("img");
246
- n.src = e.src, n.alt = e.alt || "", e.width && (n.style.width = `${e.width}px`), e.height && (n.style.height = `${e.height}px`), n.classList.add("te-image");
247
- const s = document.createElement("figcaption");
248
- if (s.classList.add("te-image-caption"), s.setAttribute("contenteditable", "true"), s.setAttribute("data-placeholder", "Type caption..."), ["top-left", "top-right", "bottom-left", "bottom-right"].forEach((a) => {
249
- const l = document.createElement("div");
250
- l.classList.add("te-image-resizer", `te-resizer-${a}`), i.appendChild(l);
251
- }), i.appendChild(n), i.appendChild(s), t.replaceChild(i, e), !i.nextElementSibling) {
252
- const a = document.createElement("p");
253
- a.innerHTML = "<br>", i.after(a);
254
- }
255
- }
256
- setupInputHandlers() {
257
- this.addEventListener(this.editableElement, "beforeinput", (e) => {
258
- if (e.inputType === "insertText" && Object.keys(this.pendingStyles).length > 0) {
259
- const t = e.data;
260
- if (!t) return;
261
- e.preventDefault();
262
- const i = document.createElement("span");
263
- for (const [s, o] of Object.entries(this.pendingStyles))
264
- i.style.setProperty(s, o);
265
- i.textContent = t;
266
- const n = this.selection.getRange();
267
- if (n) {
268
- n.deleteContents(), n.insertNode(i);
269
- const s = document.createRange();
270
- s.setStart(i.firstChild, t.length), s.setEnd(i.firstChild, t.length), this.selection.restoreSelection(s), this.pendingStyles = {}, this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
271
- }
272
- }
273
- }), this.addEventListener(this.editableElement, "input", () => {
274
- this.checkPlaceholder();
275
- }), this.addEventListener(document, "selectionchange", () => {
276
- const e = window.getSelection();
277
- e && e.rangeCount > 0 && (e.getRangeAt(0).collapsed || (this.pendingStyles = {}));
278
- }), this.addEventListener(this.editableElement, "dragover", (e) => {
279
- e.preventDefault(), e.dataTransfer.dropEffect = "copy", this.editableElement.classList.add("dragover");
280
- }), this.addEventListener(this.editableElement, "dragleave", () => {
281
- this.editableElement.classList.remove("dragover");
282
- }), this.addEventListener(this.editableElement, "drop", (e) => {
283
- e.preventDefault(), this.editableElement.classList.remove("dragover");
284
- const t = e.dataTransfer?.files;
285
- if (t && t.length > 0)
286
- for (let i = 0; i < t.length; i++) {
287
- const n = t[i];
288
- if (n.type.startsWith("image/")) {
289
- const s = new FileReader();
290
- s.onload = (o) => {
291
- const a = o.target?.result;
292
- this.insertImage(a);
293
- }, s.readAsDataURL(n);
294
- }
295
- }
296
- }), this.addEventListener(this.editableElement, "paste", (e) => {
297
- const t = e.clipboardData;
298
- if (!t) return;
299
- if (t.items)
300
- for (let s = 0; s < t.items.length; s++) {
301
- const o = t.items[s];
302
- if (o.type.startsWith("image/")) {
303
- const a = o.getAsFile();
304
- if (a) {
305
- e.preventDefault();
306
- const l = new FileReader();
307
- l.onload = (r) => {
308
- const d = r.target?.result;
309
- this.insertImage(d);
310
- }, l.readAsDataURL(a);
311
- return;
312
- }
313
- }
314
- }
315
- const i = t.getData("text/plain"), n = /<([a-z1-6]+)\b[^>]*>[\s\S]*<\/\1>/i.test(i) || /^\s*<[a-z1-6]+\b[^>]*>/i.test(i);
316
- if (i && n) {
317
- const s = i.replace(/(\r\n|\n|\r)/gm, " ").replace(/>\s+</g, "><").trim();
318
- e.preventDefault(), this.execute("insertHTML", s);
319
- return;
320
- }
321
- }), this.addEventListener(this.editableElement, "input", () => {
322
- this.handleInput();
323
- }), this.addEventListener(this.editableElement, "keydown", (e) => {
324
- (e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "z" ? (e.preventDefault(), e.shiftKey ? this.redo() : this.undo()) : (e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "y" && (e.preventDefault(), this.redo());
325
- });
326
- }
327
- handleInput() {
328
- this.scheduleHistoryRecord(), this.options.autoSave && (this.options.onSaving && this.options.onSaving(), this.scheduleAutoSave()), this.options.onChange && this.options.onChange(this.editableElement.innerHTML);
329
- }
330
- scheduleHistoryRecord() {
331
- this.historyTimeout && clearTimeout(this.historyTimeout), this.historyTimeout = setTimeout(() => {
332
- this.history.record(this.editableElement.innerHTML);
333
- }, 500);
334
- }
335
- scheduleAutoSave() {
336
- this.saveTimeout && clearTimeout(this.saveTimeout);
337
- const e = this.options.autoSaveInterval || 1e3;
338
- this.saveTimeout = setTimeout(() => {
339
- this.save();
340
- }, e);
341
- }
342
- save() {
343
- this.options.onSave && this.options.onSave(this.editableElement.innerHTML);
344
- }
345
- undo() {
346
- const e = this.history.undo();
347
- e !== null && (this.editableElement.innerHTML = e, this.triggerChange());
348
- }
349
- redo() {
350
- const e = this.history.redo();
351
- e !== null && (this.editableElement.innerHTML = e, this.triggerChange());
352
- }
353
- triggerChange() {
354
- this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
355
- }
356
- createEditableElement() {
357
- const e = document.createElement("div");
358
- return e.setAttribute("contenteditable", "true"), e.setAttribute("role", "textbox"), e.setAttribute("spellcheck", "true"), e.classList.add("te-content"), this.options.placeholder && e.setAttribute("data-placeholder", this.options.placeholder), e.style.minHeight = "150px", e.style.outline = "none", e.style.padding = "1rem", e.innerHTML === "" && (e.innerHTML = "<p><br></p>"), e;
359
- }
360
- /**
361
- * Focuses the editor.
362
- */
363
- focus() {
364
- this.editableElement.focus();
365
- }
366
- /**
367
- * Executes a command on the current selection.
368
- */
369
- execute(e, t = null) {
370
- this.focus(), document.execCommand(e, !1, t ?? void 0), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
371
- }
372
- /**
373
- * Inserts a table at the current selection.
374
- */
375
- insertTable(e = 3, t = 3) {
376
- this.focus();
377
- const i = this.selection.getRange();
378
- if (!i) return;
379
- const n = document.createElement("table");
380
- n.classList.add("te-table");
381
- for (let o = 0; o < e; o++) {
382
- const a = document.createElement("tr");
383
- for (let l = 0; l < t; l++) {
384
- const r = document.createElement("td");
385
- r.innerHTML = "<br>", a.appendChild(r);
386
- }
387
- n.appendChild(a);
388
- }
389
- i.deleteContents(), i.insertNode(n);
390
- const s = n.nextElementSibling;
391
- if (!s || s.tagName !== "P") {
392
- const o = document.createElement("p");
393
- o.innerHTML = "<br>", n.after(o), s && s.tagName === "BR" && s.remove();
394
- }
395
- this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
396
- }
397
- /**
398
- * Adds a row to the currently selected table.
399
- */
400
- addRow() {
401
- const e = this.getSelectedTable();
402
- if (!e) return;
403
- const t = document.createElement("tr");
404
- t.style.borderBottom = "1px solid var(--te-border-color)";
405
- const i = e.rows[0].cells.length;
406
- for (let s = 0; s < i; s++) {
407
- const o = document.createElement("td");
408
- o.innerHTML = "<br>", t.appendChild(o);
409
- }
410
- const n = this.getSelectedTd();
411
- n ? n.parentElement?.after(t) : e.appendChild(t), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
412
- }
413
- /**
414
- * Deletes the currently selected row.
415
- */
416
- deleteRow() {
417
- const e = this.getSelectedTd();
418
- if (e && e.parentElement) {
419
- const t = e.parentElement;
420
- t.parentElement.rows.length > 1 && (t.remove(), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 })));
421
- }
422
- }
423
- /**
424
- * Adds a column to the currently selected table.
425
- */
426
- addColumn() {
427
- const e = this.getSelectedTable();
428
- if (!e) return;
429
- const t = this.getSelectedTd(), i = t ? t.cellIndex : -1;
430
- for (let n = 0; n < e.rows.length; n++) {
431
- const s = e.rows[n], o = document.createElement("td");
432
- o.innerHTML = "<br>", i !== -1 ? s.cells[i].after(o) : s.appendChild(o);
433
- }
434
- this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
435
- }
436
- /**
437
- * Deletes the currently selected column.
438
- */
439
- deleteColumn() {
440
- const e = this.getSelectedTd();
441
- if (!e) return;
442
- const t = this.getSelectedTable();
443
- if (!t) return;
444
- const i = e.cellIndex;
445
- if (t.rows[0].cells.length > 1) {
446
- for (let n = 0; n < t.rows.length; n++)
447
- t.rows[n].cells[i].remove();
448
- this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
449
- }
450
- }
451
- getSelectedTd() {
452
- const e = window.getSelection();
453
- if (!e || e.rangeCount === 0) return null;
454
- let t = e.anchorNode;
455
- for (; t && t !== this.editableElement; ) {
456
- if (t.nodeName === "TD") return t;
457
- t = t.parentNode;
458
- }
459
- return null;
460
- }
461
- getSelectedTable() {
462
- const e = this.getSelectedTd();
463
- return e ? e.closest("table") : null;
464
- }
465
- /**
466
- * Recursively removes a style property from all elements in a fragment.
467
- */
468
- clearStyleRecursive(e, t) {
469
- const i = document.createTreeWalker(e, NodeFilter.SHOW_ELEMENT);
470
- let n = i.nextNode();
471
- for (; n; )
472
- n.style.getPropertyValue(t) && n.style.removeProperty(t), n = i.nextNode();
473
- }
474
- /**
475
- * Applies an inline style to the selection.
476
- * This is used for properties like font-size (px) and font-family
477
- * where execCommand is outdated or limited.
478
- */
479
- setStyle(e, t, i) {
480
- if (e === "font-size" && t.endsWith("px"), !i) {
481
- const o = window.getSelection();
482
- if (!o || o.rangeCount === 0) return null;
483
- i = o.getRangeAt(0);
484
- }
485
- if (i.collapsed)
486
- return this.pendingStyles[e] = t, i;
487
- let n = i.commonAncestorContainer;
488
- n.nodeType === Node.TEXT_NODE && (n = n.parentElement);
489
- let s = null;
490
- if (n.tagName === "SPAN" && n.children.length === 0 && n.textContent === i.toString())
491
- n.style.setProperty(e, t), s = i.cloneRange();
492
- else {
493
- const o = document.createElement("span");
494
- o.style.setProperty(e, t);
495
- try {
496
- const a = n.tagName === "SPAN" ? n : null, l = i.extractContents();
497
- this.clearStyleRecursive(l, e), o.appendChild(l), i.insertNode(o), a && a.innerHTML === "" && a.remove();
498
- const r = document.createRange();
499
- r.selectNodeContents(o), s = r;
500
- const d = window.getSelection();
501
- d && d.rangeCount > 0 && (d.removeAllRanges(), d.addRange(r));
502
- } catch (a) {
503
- console.warn("Failed to apply style:", a);
504
- }
505
- }
506
- return this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 })), s;
507
- }
508
- /**
509
- * Creates a link at the current selection.
510
- * Ensures the link opens in a new tab with proper security attributes.
511
- */
512
- createLink(e) {
513
- this.focus(), !/^https?:\/\//i.test(e) && !/^mailto:/i.test(e) && !e.startsWith("#") && (e = "https://" + e), document.execCommand("createLink", !1, e);
514
- const t = window.getSelection();
515
- if (t && t.rangeCount > 0) {
516
- let n = t.getRangeAt(0).commonAncestorContainer;
517
- n.nodeType === Node.TEXT_NODE && (n = n.parentElement);
518
- let s = null;
519
- n.tagName === "A" ? s = n : s = n.querySelector("a"), s && (s.setAttribute("target", "_blank"), s.setAttribute("rel", "noopener noreferrer"));
520
- }
521
- this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
522
- }
523
- /**
524
- * Inserts an image at the current selection.
525
- */
526
- insertImage(e) {
527
- this.focus();
528
- const t = this.selection.getRange();
529
- if (!t) return;
530
- const i = document.createElement("figure");
531
- i.classList.add("te-image-container"), i.setAttribute("contenteditable", "false");
532
- const n = document.createElement("img");
533
- n.src = e, n.classList.add("te-image");
534
- const s = document.createElement("figcaption");
535
- s.classList.add("te-image-caption"), s.setAttribute("contenteditable", "true"), s.setAttribute("data-placeholder", "Type caption..."), ["top-left", "top-right", "bottom-left", "bottom-right"].forEach((r) => {
536
- const d = document.createElement("div");
537
- d.classList.add("te-image-resizer", `te-resizer-${r}`), i.appendChild(d);
538
- }), i.appendChild(n), i.appendChild(s), t.deleteContents(), t.insertNode(i);
539
- const a = document.createElement("p");
540
- a.innerHTML = "<br>", i.after(a);
541
- const l = document.createRange();
542
- l.setStart(a, 0), l.setEnd(a, 0), this.selection.restoreSelection(l), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
543
- }
544
- /**
545
- * Returns the clean HTML content of the editor.
546
- */
547
- getHTML() {
548
- return this.editableElement.innerHTML;
549
- }
550
- /**
551
- * Sets the HTML content of the editor.
552
- */
553
- setHTML(e) {
554
- this.editableElement.innerHTML = e;
555
- }
556
- /**
557
- * Internal access to the editable element.
558
- */
559
- get el() {
560
- return this.editableElement;
561
- }
562
- /**
563
- * Returns the editor options.
564
- */
565
- getOptions() {
566
- return this.options;
567
- }
568
- }
569
- const g = {
570
- type: "button",
571
- title: "Undo",
572
- command: "undo",
573
- icon: '<svg viewBox="0 0 24 24"><path d="M9 14L4 9l5-5"></path><path d="M20 20v-7a4 4 0 0 0-4-4H4"></path></svg>'
574
- }, f = {
575
- type: "button",
576
- title: "Redo",
577
- command: "redo",
578
- icon: '<svg viewBox="0 0 24 24"><path d="M15 14l5-5-5-5"></path><path d="M4 20v-7a4 4 0 0 1 4-4h12"></path></svg>'
579
- }, b = {
580
- type: "select",
581
- title: "Heading",
582
- command: "formatBlock",
583
- options: [
584
- { label: "Paragraph", value: "P" },
585
- { label: "Heading 1", value: "H1" },
586
- { label: "Heading 2", value: "H2" },
587
- { label: "Heading 3", value: "H3" },
588
- { label: "Heading 4", value: "H4" },
589
- { label: "Heading 5", value: "H5" },
590
- { label: "Heading 6", value: "H6" }
591
- ]
592
- }, y = {
593
- type: "select",
594
- title: "Font",
595
- command: "fontFamily",
596
- options: [
597
- { label: "Inter", value: "'Inter', sans-serif" },
598
- { label: "Arial", value: "Arial, sans-serif" },
599
- { label: "Georgia", value: "Georgia, serif" },
600
- { label: "Courier", value: "'Courier New', monospace" },
601
- { label: "Times New Roman", value: "'Times New Roman', serif" },
602
- { label: "Verdana", value: "Verdana, sans-serif" },
603
- { label: "Tahoma", value: "Tahoma, sans-serif" },
604
- { label: "Roboto", value: "'Roboto', sans-serif" },
605
- { label: "Open Sans", value: "'Open Sans', sans-serif" },
606
- { label: "Montserrat", value: "'Montserrat', sans-serif" },
607
- { label: "Lato", value: "'Lato', sans-serif" },
608
- { label: "Poppins", value: "'Poppins', sans-serif" },
609
- { label: "Oswald", value: "'Oswald', sans-serif" },
610
- { label: "Playfair Display", value: "'Playfair Display', serif" },
611
- { label: "Merriweather", value: "'Merriweather', serif" }
612
- ]
613
- }, E = {
614
- type: "input",
615
- title: "Size (px)",
616
- command: "fontSize",
617
- placeholder: "Size",
618
- value: "16"
619
- }, w = {
620
- type: "select",
621
- title: "Line Height",
622
- command: "lineHeight",
623
- options: [
624
- { label: "Normal", value: "normal" },
625
- { label: "0.5", value: "0.5" },
626
- { label: "1.0", value: "1.0" },
627
- { label: "1.15", value: "1.15" },
628
- { label: "1.5", value: "1.5" },
629
- { label: "2.0", value: "2.0" },
630
- { label: "2.5", value: "2.5" },
631
- { label: "3.0", value: "3.0" },
632
- { label: "3.5", value: "3.5" },
633
- { label: "4.0", value: "4.0" },
634
- { label: "4.5", value: "4.5" },
635
- { label: "5.0", value: "5.0" },
636
- { label: "5.5", value: "5.5" },
637
- { label: "6.0", value: "6.0" },
638
- { label: "6.5", value: "6.5" },
639
- { label: "7.0", value: "7.0" },
640
- { label: "7.5", value: "7.5" },
641
- { label: "8.0", value: "8.0" },
642
- { label: "8.5", value: "8.5" },
643
- { label: "9.0", value: "9.0" },
644
- { label: "9.5", value: "9.5" },
645
- { label: "10.0", value: "10.0" }
646
- ],
647
- value: "normal"
648
- }, x = {
649
- type: "button",
650
- title: "Bold",
651
- command: "bold",
652
- icon: '<svg viewBox="0 0 24 24"><path d="M6 4h8a4 4 0 0 1 4 4 4 4 0 0 1-4 4H6z"></path><path d="M6 12h9a4 4 0 0 1 4 4 4 4 0 0 1-4 4H6z"></path></svg>'
653
- }, L = {
654
- type: "button",
655
- title: "Italic",
656
- command: "italic",
657
- icon: '<svg viewBox="0 0 24 24"><line x1="19" y1="4" x2="10" y2="4"></line><line x1="14" y1="20" x2="5" y2="20"></line><line x1="15" y1="4" x2="9" y2="20"></line></svg>'
658
- }, C = {
659
- type: "button",
660
- title: "Underline",
661
- command: "underline",
662
- icon: '<svg viewBox="0 0 24 24"><path d="M6 3v7a6 6 0 0 0 6 6 6 6 0 0 0 6-6V3"></path><line x1="4" y1="21" x2="20" y2="21"></line></svg>'
663
- }, S = {
664
- type: "button",
665
- title: "Strikethrough",
666
- command: "strikeThrough",
667
- icon: '<svg viewBox="0 0 24 24"><line x1="5" y1="12" x2="19" y2="12"></line><path d="M16 4.9C15.1 4.3 13.9 4 12.6 4c-3.1 0-5.6 1.8-5.6 4s1.8 3.3 4.4 3.8"></path><path d="M7 19.1c.9.6 2.1.9 3.4.9 3.1 0 5.6-1.8 5.6-4s-1.8-3.3-4.4-3.8"></path></svg>'
668
- }, k = {
669
- type: "color-picker",
670
- title: "Text Color",
671
- command: "foreColor",
672
- icon: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 20h16"/><path d="m6 16 6-12 6 12"/><path d="M8 12h8"/></svg>',
673
- value: "#1e293b"
674
- }, M = {
675
- type: "color-picker",
676
- title: "Highlight Color",
677
- command: "backColor",
678
- icon: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m9 11-6 6v3h9l3-3"/><path d="m22 12-4.6 4.6a2 2 0 0 1-2.8 0l-5.2-5.2a2 2 0 0 1 0-2.8L14 4"/></svg>',
679
- value: "#ffffff"
680
- }, T = {
681
- type: "button",
682
- title: "Align Left",
683
- command: "justifyLeft",
684
- icon: '<svg viewBox="0 0 24 24"><line x1="17" y1="10" x2="3" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="17" y1="18" x2="3" y2="18"></line></svg>'
685
- }, R = {
686
- type: "button",
687
- title: "Align Center",
688
- command: "justifyCenter",
689
- icon: '<svg viewBox="0 0 24 24"><line x1="18" y1="10" x2="6" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="18" y1="18" x2="6" y2="18"></line></svg>'
690
- }, H = {
691
- type: "button",
692
- title: "Align Right",
693
- command: "justifyRight",
694
- icon: '<svg viewBox="0 0 24 24"><line x1="21" y1="10" x2="7" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="21" y1="18" x2="7" y2="18"></line></svg>'
695
- }, I = {
696
- type: "button",
697
- title: "Justify",
698
- command: "justifyFull",
699
- icon: '<svg viewBox="0 0 24 24"><line x1="21" y1="10" x2="3" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="21" y1="18" x2="3" y2="18"></line></svg>'
700
- }, A = {
701
- type: "button",
702
- title: "Bulleted List",
703
- command: "insertUnorderedList",
704
- icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="8" y1="6" x2="21" y2="6"></line><line x1="8" y1="12" x2="21" y2="12"></line><line x1="8" y1="18" x2="21" y2="18"></line><circle cx="3" cy="6" r="1" fill="currentColor"></circle><circle cx="3" cy="12" r="1" fill="currentColor"></circle><circle cx="3" cy="18" r="1" fill="currentColor"></circle></svg>'
705
- }, j = {
706
- type: "button",
707
- title: "Numbered List",
708
- command: "insertOrderedList",
709
- icon: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="10" y1="6" x2="21" y2="6"></line><line x1="10" y1="12" x2="21" y2="12"></line><line x1="10" y1="18" x2="21" y2="18"></line><path d="M4 6h1v4"></path><path d="M4 10h2"></path><path d="M6 18H4c0-1 2-2 2-3s-1-1.5-2-1"></path></svg>'
710
- }, z = {
711
- type: "button",
712
- title: "Outdent",
713
- command: "outdent",
714
- icon: '<svg viewBox="0 0 24 24"><polyline points="15 18 9 12 15 6"></polyline><line x1="21" y1="12" x2="9" y2="12"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="18" x2="3" y2="18"></line></svg>'
715
- }, D = {
716
- type: "button",
717
- title: "Indent",
718
- command: "indent",
719
- icon: '<svg viewBox="0 0 24 24"><polyline points="9 18 15 12 9 6"></polyline><line x1="3" y1="12" x2="15" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg>'
720
- }, N = {
721
- type: "button",
722
- title: "Horizontal Rule",
723
- command: "insertHorizontalRule",
724
- icon: '<svg viewBox="0 0 24 24"><line x1="5" y1="12" x2="19" y2="12"></line></svg>'
725
- }, P = {
726
- type: "button",
727
- title: "Clear Formatting",
728
- command: "removeFormat",
729
- icon: '<svg viewBox="0 0 24 24"><path d="M17.41 15.41L12 10l-5.41 5.41L5.17 14l5.41-5.41L5.17 3.17 6.59 1.76 12 7.17l5.41-5.41 1.41 1.41-5.41 5.41 5.41 5.41-1.41 1.42z"></path></svg>'
730
- }, B = {
731
- type: "button",
732
- title: "Insert Emoji",
733
- command: "insertEmoji",
734
- icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><path d="M8 14s1.5 2 4 2 4-2 4-2"></path><line x1="9" y1="9" x2="9.01" y2="9"></line><line x1="15" y1="9" x2="15.01" y2="9"></line></svg>'
735
- }, O = {
736
- type: "button",
737
- title: "Insert Link",
738
- command: "createLink",
739
- icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>'
740
- }, W = {
741
- type: "button",
742
- title: "Insert Image",
743
- command: "insertImage",
744
- icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><circle cx="8.5" cy="8.5" r="1.5"></circle><polyline points="21 15 16 10 5 21"></polyline></svg>'
745
- }, U = {
746
- type: "button",
747
- command: "insertTable",
748
- title: "Insert Table",
749
- icon: '<svg viewBox="0 0 24 24" width="18" height="18"><path fill="currentColor" d="M3 3h18v18H3V3m2 2v4h4V5H5m6 0v4h4V5h-4m6 0v4h2V5h-2M5 11v4h4v-4H5m6 0v4h4v-4h-4m6 0v4h2v-4h-2M5 17v2h4v-2H5m6 0v2h4v-2h-4m6 0v2h2v-2h-2Z"/></svg>'
750
- }, h = { type: "divider", title: "" }, F = [
751
- { ...g, id: "undo" },
752
- { ...f, id: "redo" },
753
- h,
754
- { ...b, id: "heading" },
755
- { ...y, id: "font-family" },
756
- { ...E, id: "font-size" },
757
- { ...w, id: "line-height" },
758
- h,
759
- { ...x, id: "bold" },
760
- { ...L, id: "italic" },
761
- { ...C, id: "underline" },
762
- { ...S, id: "strikethrough" },
763
- h,
764
- { ...k, id: "text-color" },
765
- { ...M, id: "highlight-color" },
766
- h,
767
- { ...T, id: "align-left" },
768
- { ...R, id: "align-center" },
769
- { ...H, id: "align-right" },
770
- { ...I, id: "align-justify" },
771
- h,
772
- { ...A, id: "bullet-list" },
773
- { ...j, id: "ordered-list" },
774
- { ...z, id: "outdent" },
775
- { ...D, id: "indent" },
776
- h,
777
- { ...N, id: "horizontal-rule" },
778
- { ...B, id: "emoji" },
779
- { ...O, id: "link" },
780
- { ...W, id: "image" },
781
- { ...U, id: "table" },
782
- { ...P, id: "clear-formatting" }
783
- ];
784
- class G {
785
- container;
786
- searchInput;
787
- emojiGrid;
788
- onSelect;
789
- onClose;
790
- emojiList = [];
791
- theme;
792
- dark;
793
- constructor(e, t, i, n) {
794
- this.onSelect = e, this.onClose = t, this.theme = i, this.dark = n, this.container = this.createPickerElement(), this.searchInput = this.container.querySelector(".te-emoji-search"), this.emojiGrid = this.container.querySelector(".te-emoji-grid"), this.setupEvents(), this.loadEmojis();
795
- }
796
- async loadEmojis() {
797
- this.emojiGrid.innerHTML = '<div class="te-emoji-loading">Loading...</div>';
798
- try {
799
- const { EMOJI_LIST: e } = await import("./EmojiList-CwY20rtO.js");
800
- this.emojiList = e, this.renderEmojis(this.emojiList);
801
- } catch (e) {
802
- console.error("Failed to load emojis:", e), this.emojiGrid.innerHTML = '<div class="te-emoji-error">Failed to load</div>';
803
- }
804
- }
805
- createPickerElement() {
806
- const e = document.createElement("div");
807
- return e.classList.add("te-emoji-picker"), this.theme && this.applyTheme(e, this.theme), this.dark && e.classList.add("te-dark"), e.innerHTML = `
808
- <div class="te-emoji-header">
809
- <input type="text" class="te-emoji-search" placeholder="Search emoji...">
810
- </div>
811
- <div class="te-emoji-body">
812
- <div class="te-emoji-grid"></div>
813
- </div>
814
- `, e;
815
- }
816
- setupEvents() {
817
- this.searchInput.addEventListener("mousedown", (t) => t.stopPropagation()), this.searchInput.addEventListener("click", (t) => t.stopPropagation()), this.searchInput.addEventListener("input", () => {
818
- const t = this.searchInput.value.toLowerCase(), i = this.emojiList.filter(
819
- (n) => n.name.toLowerCase().includes(t) || n.category.toLowerCase().includes(t)
820
- );
821
- this.renderEmojis(i);
822
- });
823
- const e = (t) => {
824
- this.container.contains(t.target) || (this.close(), document.removeEventListener("mousedown", e));
825
- };
826
- setTimeout(() => document.addEventListener("mousedown", e), 0);
827
- }
828
- renderEmojis(e) {
829
- if (this.emojiGrid.innerHTML = "", e.length === 0) {
830
- this.emojiGrid.innerHTML = '<div class="te-emoji-empty">No emoji found</div>';
831
- return;
832
- }
833
- this.searchInput.value.length > 0 ? this.renderGridItems(e) : ["Smileys", "Symbols", "Hands", "Animals", "Food", "Travel", "Objects", "Activities"].forEach((n) => {
834
- const s = e.filter((o) => o.category === n);
835
- if (s.length > 0) {
836
- const o = document.createElement("div");
837
- o.classList.add("te-emoji-category-title"), o.textContent = n, this.emojiGrid.appendChild(o), this.renderGridItems(s);
838
- }
839
- });
840
- }
841
- renderGridItems(e) {
842
- e.forEach((t) => {
843
- const i = document.createElement("button");
844
- i.type = "button", i.classList.add("te-emoji-item"), i.textContent = t.emoji, i.title = t.name, i.addEventListener("click", () => {
845
- this.onSelect(t.emoji), this.close();
846
- }), this.emojiGrid.appendChild(i);
847
- });
848
- }
849
- applyTheme(e, t) {
850
- const i = {
851
- primaryColor: "--te-primary-color",
852
- primaryHover: "--te-primary-hover",
853
- bgApp: "--te-bg-app",
854
- bgEditor: "--te-bg-editor",
855
- toolbarBg: "--te-toolbar-bg",
856
- borderColor: "--te-border-color",
857
- borderFocus: "--te-border-focus",
858
- textMain: "--te-text-main",
859
- textMuted: "--te-text-muted",
860
- placeholder: "--te-placeholder",
861
- btnHover: "--te-btn-hover",
862
- btnActive: "--te-btn-active",
863
- radiusLg: "--te-radius-lg",
864
- radiusMd: "--te-radius-md",
865
- radiusSm: "--te-radius-sm",
866
- shadowSm: "--te-shadow-sm",
867
- shadowMd: "--te-shadow-md",
868
- shadowLg: "--te-shadow-lg"
869
- };
870
- for (const [n, s] of Object.entries(i)) {
871
- const o = t[n];
872
- o && e.style.setProperty(s, o);
873
- }
874
- }
875
- show(e) {
876
- document.body.appendChild(this.container);
877
- const t = e.getBoundingClientRect(), i = 280;
878
- let n = t.bottom + window.scrollY + 5, s = t.left + window.scrollX;
879
- s + i > window.innerWidth && (s = window.innerWidth - i - 10), this.container.style.top = `${n}px`, this.container.style.left = `${s}px`, this.searchInput.focus();
880
- }
881
- close() {
882
- this.container.parentElement && (this.container.remove(), this.onClose());
883
- }
884
- get el() {
885
- return this.container;
886
- }
887
- }
888
- class q {
889
- editor;
890
- container;
891
- savedRange = null;
892
- items = F;
893
- activePicker = null;
894
- statusEl = null;
895
- boundUpdateActiveStates;
896
- constructor(e) {
897
- this.editor = e, this.container = this.createToolbarElement(), this.boundUpdateActiveStates = this.updateActiveStates.bind(this), this.render();
898
- }
899
- createToolbarElement() {
900
- const e = document.createElement("div");
901
- return e.classList.add("te-toolbar"), this.statusEl = document.createElement("div"), this.statusEl.classList.add("te-toolbar-status"), this.statusEl.style.marginLeft = "auto", this.statusEl.style.display = "flex", this.statusEl.style.alignItems = "center", this.statusEl.style.gap = "6px", this.statusEl.style.fontSize = "12px", this.statusEl.style.color = "var(--te-text-muted)", this.statusEl.style.paddingRight = "12px", e;
902
- }
903
- render() {
904
- const e = this.editor.getOptions().toolbarItems, t = [];
905
- this.items.forEach((s) => {
906
- (s.type === "divider" || s.id && (!e || e.includes(s.id))) && t.push(s);
907
- });
908
- const i = [];
909
- t.forEach((s, o) => {
910
- if (s.type === "divider") {
911
- if (i.length === 0 || i[i.length - 1].type === "divider" || !t.slice(o + 1).some((l) => l.type !== "divider")) return;
912
- i.push(s);
913
- } else
914
- i.push(s);
915
- }), i.forEach((s) => {
916
- if (s.type === "button")
917
- this.renderButton(s);
918
- else if (s.type === "select")
919
- this.renderSelect(s);
920
- else if (s.type === "input")
921
- this.renderInput(s);
922
- else if (s.type === "color-picker")
923
- this.renderColorPicker(s);
924
- else if (s.type === "divider") {
925
- const o = document.createElement("div");
926
- o.classList.add("te-divider"), this.container.appendChild(o);
927
- }
928
- }), this.editor.getOptions().showStatus !== !1 && this.container.appendChild(this.statusEl), this.editor.el.addEventListener("keyup", this.boundUpdateActiveStates), this.editor.el.addEventListener("mouseup", this.boundUpdateActiveStates);
929
- }
930
- renderButton(e) {
931
- const t = document.createElement("button");
932
- t.classList.add("te-button"), t.innerHTML = e.icon || "", t.title = e.title, t.addEventListener("mousedown", (i) => {
933
- if (i.preventDefault(), e.command === "insertEmoji") {
934
- this.activePicker ? this.activePicker.close() : (this.activePicker = new G(
935
- (n) => {
936
- this.editor.execute("insertText", n);
937
- },
938
- () => {
939
- this.activePicker = null;
940
- },
941
- this.editor.getOptions().theme,
942
- this.editor.getOptions().dark
943
- ), this.activePicker.show(t));
944
- return;
945
- }
946
- if (e.command === "createLink") {
947
- const n = window.prompt("Enter the URL");
948
- n && this.editor.createLink(n);
949
- return;
950
- }
951
- if (e.command === "insertImage") {
952
- const n = document.createElement("input");
953
- n.type = "file", n.accept = "image/*", n.style.display = "none", n.addEventListener("change", (s) => {
954
- const a = s.target.files?.[0];
955
- if (a) {
956
- const l = new FileReader();
957
- l.onload = (r) => {
958
- const d = r.target?.result;
959
- this.editor.insertImage(d);
960
- }, l.readAsDataURL(a);
961
- }
962
- document.body.removeChild(n);
963
- }), document.body.appendChild(n), n.click();
964
- return;
965
- }
966
- if (e.command === "insertTable") {
967
- const n = window.prompt("Enter number of rows", "3"), s = window.prompt("Enter number of columns", "3");
968
- n && s && this.editor.insertTable(parseInt(n, 10), parseInt(s, 10));
969
- return;
970
- }
971
- if (["addRow", "deleteRow", "addColumn", "deleteColumn"].includes(e.command || "")) {
972
- const n = e.command;
973
- this.editor[n]();
974
- return;
975
- }
976
- e.command && this.editor.execute(e.command, e.value || null), this.updateActiveStates();
977
- }), this.container.appendChild(t);
978
- }
979
- renderInput(e) {
980
- const t = document.createElement("input");
981
- t.type = "number", t.classList.add("te-input"), t.title = e.title, t.value = e.value || "", t.min = "1", t.max = "100";
982
- const i = () => {
983
- const s = window.getSelection();
984
- if (s && s.rangeCount > 0) {
985
- const o = s.getRangeAt(0);
986
- this.editor.el.contains(o.commonAncestorContainer) && (this.savedRange = o.cloneRange());
987
- }
988
- };
989
- t.addEventListener("mousedown", i), t.addEventListener("focus", i);
990
- const n = () => {
991
- let s = parseInt(t.value, 10);
992
- if (!isNaN(s) && (s = Math.max(1, Math.min(100, s)), t.value = s.toString(), e.command === "fontSize")) {
993
- if (this.savedRange) {
994
- const o = this.editor.setStyle("font-size", `${s}px`, this.savedRange);
995
- o && (this.savedRange = o);
996
- } else
997
- this.editor.setStyle("font-size", `${s}px`);
998
- t.focus();
999
- }
1000
- };
1001
- t.addEventListener("input", n), t.addEventListener("keydown", (s) => {
1002
- s.key === "Enter" && (n(), this.editor.focus());
1003
- }), this.container.appendChild(t);
1004
- }
1005
- renderSelect(e) {
1006
- const t = document.createElement("select");
1007
- t.classList.add("te-select"), t.title = e.title, e.options && e.options.forEach((i) => {
1008
- const n = document.createElement("option");
1009
- n.value = i.value, n.textContent = i.label, t.appendChild(n);
1010
- }), t.addEventListener("change", () => {
1011
- const i = t.value;
1012
- this.savedRange && this.editor.selection.restoreSelection(this.savedRange), e.command === "formatBlock" ? this.editor.execute(e.command, i) : e.command === "fontFamily" ? this.editor.setStyle("font-family", i) : e.command === "lineHeight" && this.editor.setStyle("line-height", i), this.editor.focus();
1013
- }), t.addEventListener("mousedown", () => {
1014
- this.savedRange = this.editor.selection.saveSelection();
1015
- }), this.container.appendChild(t);
1016
- }
1017
- renderColorPicker(e) {
1018
- const t = document.createElement("div");
1019
- if (t.classList.add("te-color-picker-wrapper"), t.title = e.title, e.icon) {
1020
- const n = document.createElement("div");
1021
- n.classList.add("te-button", "te-color-icon"), n.innerHTML = e.icon;
1022
- const s = document.createElement("div");
1023
- s.classList.add("te-color-indicator"), s.style.backgroundColor = e.value || "#000000", n.appendChild(s), t.appendChild(n);
1024
- }
1025
- const i = document.createElement("input");
1026
- i.type = "color", i.classList.add("te-color-picker-input"), e.icon || (i.classList.add("te-color-picker"), i.title = e.title), i.value = e.value || "#000000", i.addEventListener("mousedown", () => {
1027
- this.savedRange = this.editor.selection.saveSelection();
1028
- }), i.addEventListener("input", () => {
1029
- if (e.icon) {
1030
- const n = t.querySelector(".te-color-indicator");
1031
- n && (n.style.backgroundColor = i.value);
1032
- }
1033
- }), i.addEventListener("change", () => {
1034
- this.savedRange && this.editor.selection.restoreSelection(this.savedRange), e.command && this.editor.execute(e.command, i.value), this.editor.focus();
1035
- }), t.appendChild(i), this.container.appendChild(e.icon ? t : i);
1036
- }
1037
- get el() {
1038
- return this.container;
1039
- }
1040
- updateActiveStates() {
1041
- const e = this.container.querySelectorAll(".te-button");
1042
- let t = 0;
1043
- this.items.forEach((i) => {
1044
- if (i.type === "button") {
1045
- const n = e[t++];
1046
- i.command && document.queryCommandState(i.command) ? n.classList.add("active") : n.classList.remove("active");
1047
- }
1048
- });
1049
- }
1050
- updateStatus(e, t = !1) {
1051
- if (!this.statusEl || this.editor.getOptions().showStatus === !1) return;
1052
- if (this.statusEl.innerHTML = "", t) {
1053
- const n = document.createElement("div");
1054
- n.classList.add("te-toolbar-loader"), this.statusEl.appendChild(n);
1055
- }
1056
- const i = document.createElement("span");
1057
- i.textContent = e, this.statusEl.appendChild(i);
1058
- }
1059
- destroy() {
1060
- this.editor.el.removeEventListener("keyup", this.boundUpdateActiveStates), this.editor.el.removeEventListener("mouseup", this.boundUpdateActiveStates), this.activePicker && (this.activePicker.close(), this.activePicker = null), this.container.parentNode && this.container.parentNode.removeChild(this.container);
1061
- }
1062
- }
1063
- class K extends v {
1064
- toolbar;
1065
- constructor(e, t = {}) {
1066
- const i = {
1067
- ...t,
1068
- onSaving: () => {
1069
- this.toolbar?.updateStatus("Auto saving...", !0), t.onSaving && t.onSaving();
1070
- },
1071
- onSave: (n) => {
1072
- const s = (/* @__PURE__ */ new Date()).toLocaleString([], {
1073
- year: "numeric",
1074
- month: "short",
1075
- day: "numeric",
1076
- hour: "2-digit",
1077
- minute: "2-digit",
1078
- hour12: !0
1079
- });
1080
- this.toolbar?.updateStatus(`Saved at ${s}`, !1), t.onSave && t.onSave(n);
1081
- }
1082
- };
1083
- if (super(e, i), typeof document > "u" || !e) {
1084
- this.toolbar = {};
1085
- return;
1086
- }
1087
- this.toolbar = new q(this), this.container.insertBefore(this.toolbar.el, this.editableElement), i.showStatus !== !1 && this.toolbar && this.toolbar.updateStatus("All changes saved", !1);
1088
- }
1089
- getToolbar() {
1090
- return this.toolbar;
1091
- }
1092
- destroy() {
1093
- this.toolbar && this.toolbar.destroy(), super.destroy();
1094
- }
1095
- }
1096
- export {
1097
- v as CoreEditor,
1098
- p as HistoryManager,
1099
- u as SelectionManager,
1100
- K as TestEditor,
1101
- q as Toolbar
1102
- };