@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.
@@ -0,0 +1,1719 @@
1
+ import p from "dompurify";
2
+ class b {
3
+ /**
4
+ * Returns the current selection object.
5
+ */
6
+ getSelection() {
7
+ return window.getSelection();
8
+ }
9
+ /**
10
+ * Returns the first range of the current selection.
11
+ */
12
+ getRange() {
13
+ const e = this.getSelection();
14
+ return !e || e.rangeCount === 0 ? null : e.getRangeAt(0);
15
+ }
16
+ /**
17
+ * Serializes the current selection into a path-based format relative to a root element.
18
+ * This allows restoring selection even if the DOM nodes are replaced but the structure is similar.
19
+ */
20
+ getSelectionPath(e) {
21
+ const t = this.getRange();
22
+ return !t || !e.contains(t.commonAncestorContainer) ? null : {
23
+ startPath: this.getNodePath(t.startContainer, e),
24
+ startOffset: t.startOffset,
25
+ endPath: this.getNodePath(t.endContainer, e),
26
+ endOffset: t.endOffset
27
+ };
28
+ }
29
+ /**
30
+ * Restores selection from a path-based serialization.
31
+ */
32
+ restoreSelectionPath(e, t) {
33
+ if (t)
34
+ try {
35
+ const n = this.getNodeByPath(t.startPath, e), i = this.getNodeByPath(t.endPath, e);
36
+ if (n && i) {
37
+ const o = document.createRange();
38
+ o.setStart(n, Math.min(t.startOffset, n.textContent?.length || 0)), o.setEnd(i, Math.min(t.endOffset, i.textContent?.length || 0)), this.restoreSelection(o);
39
+ }
40
+ } catch (n) {
41
+ console.warn("Failed to restore selection path:", n);
42
+ }
43
+ }
44
+ getNodePath(e, t) {
45
+ const n = [];
46
+ let i = e;
47
+ for (; i !== t && i.parentElement; ) {
48
+ const o = Array.from(i.parentElement.childNodes).indexOf(i);
49
+ n.unshift(o), i = i.parentElement;
50
+ }
51
+ return n;
52
+ }
53
+ getNodeByPath(e, t) {
54
+ let n = t;
55
+ for (const i of e)
56
+ if (n.childNodes[i])
57
+ n = n.childNodes[i];
58
+ else
59
+ return null;
60
+ return n;
61
+ }
62
+ /**
63
+ * Saves the current selection range.
64
+ */
65
+ saveSelection() {
66
+ const e = this.getRange();
67
+ return e ? e.cloneRange() : null;
68
+ }
69
+ /**
70
+ * Restores a previously saved range.
71
+ */
72
+ restoreSelection(e) {
73
+ if (!e) return;
74
+ const t = this.getSelection();
75
+ t && (t.removeAllRanges(), t.addRange(e));
76
+ }
77
+ /**
78
+ * Checks if the selection is within a specific element.
79
+ */
80
+ isSelectionInElement(e) {
81
+ const t = this.getRange();
82
+ return t ? e.contains(t.commonAncestorContainer) : !1;
83
+ }
84
+ /**
85
+ * Clears the current selection.
86
+ */
87
+ clearSelection() {
88
+ const e = this.getSelection();
89
+ e && e.removeAllRanges();
90
+ }
91
+ }
92
+ class y {
93
+ editor;
94
+ activeContainer = null;
95
+ isResizing = !1;
96
+ startX = 0;
97
+ startY = 0;
98
+ startWidth = 0;
99
+ startHeight = 0;
100
+ currentHandle = null;
101
+ aspectRatio = 1;
102
+ boundMouseDown;
103
+ boundMouseMove;
104
+ boundMouseUp;
105
+ boundKeyDown;
106
+ constructor(e) {
107
+ 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();
108
+ }
109
+ setupListeners() {
110
+ const e = this.editor.el;
111
+ e.addEventListener("mousedown", this.boundMouseDown), window.addEventListener("mousemove", this.boundMouseMove), window.addEventListener("mouseup", this.boundMouseUp), e.addEventListener("keydown", this.boundKeyDown), e.addEventListener("blur", this.deselectImage.bind(this));
112
+ }
113
+ handleMouseDown(e) {
114
+ const t = e.target;
115
+ if (t.classList.contains("te-image-resizer")) {
116
+ e.preventDefault(), e.stopPropagation();
117
+ const i = t.closest(".te-image-container");
118
+ i && (this.selectImage(i), this.startResize(e, t));
119
+ return;
120
+ }
121
+ const n = t.closest(".te-image-container");
122
+ n ? this.selectImage(n) : this.deselectImage();
123
+ }
124
+ handleMouseMove(e) {
125
+ this.isResizing && this.handleResize(e);
126
+ }
127
+ handleMouseUp() {
128
+ this.isResizing && this.stopResize();
129
+ }
130
+ handleKeyDown(e) {
131
+ if ((e.key === "Backspace" || e.key === "Delete") && this.activeContainer) {
132
+ const t = this.editor.selection.getRange();
133
+ if (t && this.activeContainer.contains(t.commonAncestorContainer)) {
134
+ e.preventDefault();
135
+ const n = this.activeContainer.querySelector("img"), i = n?.getAttribute("data-image-id"), o = n?.src, s = this.editor.getOptions();
136
+ s.onImageDelete && s.onImageDelete(i || void 0, o), i && s.imageEndpoints?.delete && fetch(s.imageEndpoints.delete, {
137
+ method: "DELETE",
138
+ headers: { "Content-Type": "application/json" },
139
+ body: JSON.stringify({ id: i, url: o })
140
+ }).catch((a) => console.error("Failed to notify server of image deletion", a)), this.activeContainer.remove(), this.activeContainer = null, this.editor.el.dispatchEvent(new Event("input", { bubbles: !0 }));
141
+ }
142
+ }
143
+ }
144
+ destroy() {
145
+ const e = this.editor.el;
146
+ e && (e.removeEventListener("mousedown", this.boundMouseDown), e.removeEventListener("keydown", this.boundKeyDown), e.removeEventListener("blur", this.deselectImage.bind(this))), window.removeEventListener("mousemove", this.boundMouseMove), window.removeEventListener("mouseup", this.boundMouseUp);
147
+ }
148
+ selectImage(e) {
149
+ this.activeContainer && this.activeContainer.classList.remove("active"), this.activeContainer = e, this.activeContainer.classList.add("active");
150
+ }
151
+ deselectImage() {
152
+ this.activeContainer && (this.activeContainer.classList.remove("active"), this.activeContainer = null);
153
+ }
154
+ startResize(e, t) {
155
+ if (!this.activeContainer) return;
156
+ this.isResizing = !0, this.currentHandle = Array.from(t.classList).find((i) => i.startsWith("te-resizer-"))?.replace("te-resizer-", "") || null;
157
+ const n = this.activeContainer.querySelector("img");
158
+ this.startX = e.clientX, this.startY = e.clientY, this.startWidth = n.clientWidth, this.startHeight = n.clientHeight, this.aspectRatio = this.startWidth / this.startHeight, document.body.style.cursor = window.getComputedStyle(t).cursor;
159
+ }
160
+ handleResize(e) {
161
+ if (!this.activeContainer || !this.isResizing) return;
162
+ const t = this.activeContainer.querySelector("img"), n = e.clientX - this.startX, i = e.clientY - this.startY;
163
+ let o = this.startWidth, s = this.startHeight;
164
+ this.currentHandle?.includes("right") ? o = this.startWidth + n : this.currentHandle?.includes("left") ? o = this.startWidth - n : this.currentHandle?.includes("bottom") ? o = this.startWidth + i * this.aspectRatio : this.currentHandle?.includes("top") && (o = this.startWidth - i * this.aspectRatio), s = o / this.aspectRatio, o > 50 && o < this.editor.el.clientWidth && (t.style.width = `${o}px`, t.style.height = `${s}px`);
165
+ }
166
+ stopResize() {
167
+ this.isResizing = !1, this.currentHandle = null, document.body.style.cursor = "", this.editor.el.dispatchEvent(new Event("input", { bubbles: !0 }));
168
+ }
169
+ }
170
+ class E {
171
+ stack = [];
172
+ index = -1;
173
+ maxDepth = 50;
174
+ constructor(e) {
175
+ e !== void 0 && this.record(e, null);
176
+ }
177
+ /**
178
+ * Records a new state in the history stack.
179
+ * Clears any "redo" states if we record a new action.
180
+ */
181
+ record(e, t) {
182
+ if (this.index >= 0 && this.stack[this.index].html === e) {
183
+ this.stack[this.index].selection = t;
184
+ return;
185
+ }
186
+ this.index < this.stack.length - 1 && (this.stack = this.stack.slice(0, this.index + 1)), this.stack.push({ html: e, selection: t }), this.index++, this.stack.length > this.maxDepth && (this.stack.shift(), this.index--);
187
+ }
188
+ /**
189
+ * Returns the previous state if available.
190
+ */
191
+ undo() {
192
+ return this.index > 0 ? (this.index--, this.stack[this.index]) : null;
193
+ }
194
+ /**
195
+ * Returns the next state if available.
196
+ */
197
+ redo() {
198
+ return this.index < this.stack.length - 1 ? (this.index++, this.stack[this.index]) : null;
199
+ }
200
+ /**
201
+ * Checks if undo is possible.
202
+ */
203
+ canUndo() {
204
+ return this.index > 0;
205
+ }
206
+ /**
207
+ * Checks if redo is possible.
208
+ */
209
+ canRedo() {
210
+ return this.index < this.stack.length - 1;
211
+ }
212
+ }
213
+ const w = {
214
+ type: "button",
215
+ title: "Undo",
216
+ command: "undo",
217
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 7v6h6"></path><path d="M21 17a9 9 0 0 0-9-9 9 9 0 0 0-6 2.3L3 13"></path></svg>'
218
+ }, x = {
219
+ type: "button",
220
+ title: "Redo",
221
+ command: "redo",
222
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 7v6h-6"></path><path d="M3 17a9 9 0 0 1 9-9 9 9 0 0 1 6 2.3l3 2.7"></path></svg>'
223
+ }, k = {
224
+ type: "select",
225
+ title: "Heading",
226
+ command: "formatBlock",
227
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 12h8"></path><path d="M4 18V6"></path><path d="M12 18V6"></path><path d="M17 12h3"></path><path d="M17 18V6"></path></svg>',
228
+ options: [
229
+ { label: "Paragraph", value: "P" },
230
+ { label: "Heading 1", value: "H1" },
231
+ { label: "Heading 2", value: "H2" },
232
+ { label: "Heading 3", value: "H3" },
233
+ { label: "Heading 4", value: "H4" },
234
+ { label: "Heading 5", value: "H5" },
235
+ { label: "Heading 6", value: "H6" }
236
+ ]
237
+ }, L = {
238
+ type: "select",
239
+ title: "Font",
240
+ command: "fontFamily",
241
+ options: [
242
+ { label: "Inter", value: "'Inter', sans-serif" },
243
+ { label: "Arial", value: "Arial, sans-serif" },
244
+ { label: "Georgia", value: "Georgia, serif" },
245
+ { label: "Courier", value: "'Courier New', monospace" },
246
+ { label: "Times New Roman", value: "'Times New Roman', serif" },
247
+ { label: "Verdana", value: "Verdana, sans-serif" },
248
+ { label: "Tahoma", value: "Tahoma, sans-serif" },
249
+ { label: "Roboto", value: "'Roboto', sans-serif" },
250
+ { label: "Open Sans", value: "'Open Sans', sans-serif" },
251
+ { label: "Montserrat", value: "'Montserrat', sans-serif" },
252
+ { label: "Lato", value: "'Lato', sans-serif" },
253
+ { label: "Poppins", value: "'Poppins', sans-serif" },
254
+ { label: "Oswald", value: "'Oswald', sans-serif" },
255
+ { label: "Playfair Display", value: "'Playfair Display', serif" },
256
+ { label: "Merriweather", value: "'Merriweather', serif" }
257
+ ]
258
+ }, C = {
259
+ type: "input",
260
+ title: "Size (px)",
261
+ command: "fontSize",
262
+ placeholder: "Size",
263
+ value: "16"
264
+ }, S = {
265
+ type: "select",
266
+ title: "Line Height",
267
+ command: "lineHeight",
268
+ options: [
269
+ { label: "Normal", value: "normal" },
270
+ { label: "0.5", value: "0.5" },
271
+ { label: "1.0", value: "1.0" },
272
+ { label: "1.15", value: "1.15" },
273
+ { label: "1.5", value: "1.5" },
274
+ { label: "2.0", value: "2.0" },
275
+ { label: "2.5", value: "2.5" },
276
+ { label: "3.0", value: "3.0" },
277
+ { label: "3.5", value: "3.5" },
278
+ { label: "4.0", value: "4.0" },
279
+ { label: "4.5", value: "4.5" },
280
+ { label: "5.0", value: "5.0" },
281
+ { label: "5.5", value: "5.5" },
282
+ { label: "6.0", value: "6.0" },
283
+ { label: "6.5", value: "6.5" },
284
+ { label: "7.0", value: "7.0" },
285
+ { label: "7.5", value: "7.5" },
286
+ { label: "8.0", value: "8.0" },
287
+ { label: "8.5", value: "8.5" },
288
+ { label: "9.0", value: "9.0" },
289
+ { label: "9.5", value: "9.5" },
290
+ { label: "10.0", value: "10.0" }
291
+ ],
292
+ value: "normal"
293
+ }, M = {
294
+ type: "button",
295
+ title: "Bold",
296
+ command: "bold",
297
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 12a4 4 0 0 0 0-8H6v8"/><path d="M15 20a4 4 0 0 0 0-8H6v8Z"/></svg>'
298
+ }, T = {
299
+ type: "button",
300
+ title: "Italic",
301
+ command: "italic",
302
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><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>'
303
+ }, R = {
304
+ type: "button",
305
+ title: "Underline",
306
+ command: "underline",
307
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><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>'
308
+ }, H = {
309
+ type: "button",
310
+ title: "Strikethrough",
311
+ command: "strikeThrough",
312
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M16 4H9a3 3 0 0 0-2.83 4"></path><path d="M14 12a4 4 0 0 1 0 8H6"></path><line x1="4" y1="12" x2="20" y2="12"></line></svg>'
313
+ }, A = {
314
+ type: "color-picker",
315
+ title: "Text Color",
316
+ command: "foreColor",
317
+ icon: '<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>',
318
+ value: "#1e293b"
319
+ }, I = {
320
+ type: "color-picker",
321
+ title: "Highlight Color",
322
+ command: "backColor",
323
+ icon: '<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>',
324
+ value: "#ffffff"
325
+ }, N = {
326
+ type: "button",
327
+ title: "Align Left",
328
+ command: "justifyLeft",
329
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="21" y1="6" x2="3" y2="6"></line><line x1="15" y1="10" x2="3" y2="10"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="15" y1="18" x2="3" y2="18"></line></svg>'
330
+ }, j = {
331
+ type: "button",
332
+ title: "Align Center",
333
+ command: "justifyCenter",
334
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="21" y1="6" x2="3" y2="6"></line><line x1="18" y1="10" x2="6" y2="10"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="18" y1="18" x2="6" y2="18"></line></svg>'
335
+ }, P = {
336
+ type: "button",
337
+ title: "Align Right",
338
+ command: "justifyRight",
339
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="10" x2="9" y2="10"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="21" y1="18" x2="9" y2="18"></line></svg>'
340
+ }, D = {
341
+ type: "button",
342
+ title: "Justify",
343
+ command: "justifyFull",
344
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="10" x2="3" y2="10"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="21" y1="18" x2="3" y2="18"></line></svg>'
345
+ }, z = {
346
+ type: "button",
347
+ title: "Bulleted List",
348
+ command: "insertUnorderedList",
349
+ 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><line x1="3" y1="6" x2="3.01" y2="6"></line><line x1="3" y1="12" x2="3.01" y2="12"></line><line x1="3" y1="18" x2="3.01" y2="18"></line></svg>'
350
+ }, O = {
351
+ type: "button",
352
+ title: "Numbered List",
353
+ command: "insertOrderedList",
354
+ icon: '<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>'
355
+ }, B = {
356
+ type: "button",
357
+ title: "Outdent",
358
+ command: "outdent",
359
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><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>'
360
+ }, U = {
361
+ type: "button",
362
+ title: "Indent",
363
+ command: "indent",
364
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><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>'
365
+ }, F = {
366
+ type: "button",
367
+ title: "Horizontal Rule",
368
+ command: "insertHorizontalRule",
369
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="5" y1="12" x2="19" y2="12"></line></svg>'
370
+ }, W = {
371
+ type: "button",
372
+ title: "Clear Formatting",
373
+ command: "removeFormat",
374
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 7V4h16v3"></path><path d="M5 20h6"></path><path d="M13 4 8 20"></path><path d="m15 15 5 5"></path><path d="m20 15-5 5"></path></svg>'
375
+ }, q = {
376
+ type: "button",
377
+ title: "Insert Emoji",
378
+ command: "insertEmoji",
379
+ 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>'
380
+ }, _ = {
381
+ type: "button",
382
+ title: "Insert Link",
383
+ command: "createLink",
384
+ 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>'
385
+ }, $ = {
386
+ type: "button",
387
+ title: "Insert Image",
388
+ command: "insertImage",
389
+ 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>'
390
+ }, V = {
391
+ type: "button",
392
+ command: "insertTable",
393
+ title: "Insert Table",
394
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 3h18v18H3zM21 9H3M21 15H3M12 3v18"/></svg>'
395
+ }, m = { type: "divider", title: "" }, v = [
396
+ { ...w, id: "undo" },
397
+ { ...x, id: "redo" },
398
+ m,
399
+ { ...k, id: "heading" },
400
+ { ...L, id: "font-family" },
401
+ { ...C, id: "font-size" },
402
+ { ...S, id: "line-height" },
403
+ m,
404
+ { ...M, id: "bold" },
405
+ { ...T, id: "italic" },
406
+ { ...R, id: "underline" },
407
+ { ...H, id: "strikethrough" },
408
+ m,
409
+ { ...A, id: "text-color" },
410
+ { ...I, id: "highlight-color" },
411
+ m,
412
+ { ...N, id: "align-left" },
413
+ { ...j, id: "align-center" },
414
+ { ...P, id: "align-right" },
415
+ { ...D, id: "align-justify" },
416
+ m,
417
+ { ...z, id: "bullet-list" },
418
+ { ...O, id: "ordered-list" },
419
+ { ...B, id: "outdent" },
420
+ { ...U, id: "indent" },
421
+ m,
422
+ { ...F, id: "horizontal-rule" },
423
+ { ...q, id: "emoji" },
424
+ { ..._, id: "link" },
425
+ { ...$, id: "image" },
426
+ { ...V, id: "table" },
427
+ { ...W, id: "clear-formatting" }
428
+ ];
429
+ class g {
430
+ container;
431
+ onConfirm;
432
+ onClose;
433
+ dark;
434
+ theme;
435
+ fields;
436
+ constructor(e, t, n, i, o, s) {
437
+ this.fields = t, this.onConfirm = n, this.onClose = i, this.theme = o, this.dark = s, this.container = this.createModalElement(e, t), this.setupEvents();
438
+ }
439
+ createModalElement(e, t) {
440
+ const n = document.createElement("div");
441
+ n.classList.add("te-modal"), this.theme && this.applyTheme(n, this.theme), this.dark && n.classList.add("te-dark");
442
+ const i = document.createElement("div");
443
+ i.classList.add("te-modal-header"), i.textContent = e, n.appendChild(i);
444
+ const o = document.createElement("div");
445
+ o.classList.add("te-modal-body"), t.forEach((l) => {
446
+ const d = document.createElement("div");
447
+ d.classList.add("te-modal-field");
448
+ const c = document.createElement("label");
449
+ c.setAttribute("for", l.id), c.textContent = l.label;
450
+ const h = document.createElement("input");
451
+ h.type = l.type, h.id = l.id, h.classList.add("te-modal-input"), l.placeholder && (h.placeholder = l.placeholder), l.defaultValue && (h.value = l.defaultValue), l.min && (h.min = l.min), l.max && (h.max = l.max), d.appendChild(c), d.appendChild(h), o.appendChild(d);
452
+ }), n.appendChild(o);
453
+ const s = document.createElement("div");
454
+ s.classList.add("te-modal-footer");
455
+ const a = document.createElement("button");
456
+ a.classList.add("te-modal-btn", "te-modal-btn-cancel"), a.textContent = "Cancel";
457
+ const r = document.createElement("button");
458
+ return r.classList.add("te-modal-btn", "te-modal-btn-confirm"), r.textContent = "Insert", s.appendChild(a), s.appendChild(r), n.appendChild(s), n;
459
+ }
460
+ setupEvents() {
461
+ const e = this.container.querySelector(".te-modal-btn-cancel"), t = this.container.querySelector(".te-modal-btn-confirm");
462
+ e.addEventListener("click", () => this.close()), t.addEventListener("click", () => {
463
+ const o = {};
464
+ this.fields.forEach((s) => {
465
+ const a = this.container.querySelector(`#${s.id}`);
466
+ o[s.id] = a.value;
467
+ }), this.onConfirm(o), this.close();
468
+ });
469
+ const n = (o) => {
470
+ o.key === "Escape" && this.close(), o.key === "Enter" && t.click();
471
+ };
472
+ this.container.addEventListener("keydown", n);
473
+ const i = (o) => {
474
+ this.container.contains(o.target) || (this.close(), document.removeEventListener("mousedown", i));
475
+ };
476
+ setTimeout(() => document.addEventListener("mousedown", i), 0);
477
+ }
478
+ show(e) {
479
+ document.body.appendChild(this.container);
480
+ const t = e.getBoundingClientRect(), n = 260;
481
+ let i = t.bottom + window.scrollY + 10, o = t.left + window.scrollX;
482
+ o + n > window.innerWidth && (o = window.innerWidth - n - 20), this.container.style.top = `${i}px`, this.container.style.left = `${o}px`;
483
+ const s = this.container.querySelector("input");
484
+ s && s.focus();
485
+ }
486
+ close() {
487
+ this.container.parentElement && (this.container.remove(), this.onClose());
488
+ }
489
+ applyTheme(e, t) {
490
+ const n = {
491
+ primaryColor: "--te-primary-color",
492
+ primaryHover: "--te-primary-hover",
493
+ bgApp: "--te-bg-app",
494
+ bgEditor: "--te-bg-editor",
495
+ toolbarBg: "--te-toolbar-bg",
496
+ borderColor: "--te-border-color",
497
+ borderFocus: "--te-border-focus",
498
+ textMain: "--te-text-main",
499
+ textMuted: "--te-text-muted",
500
+ placeholder: "--te-placeholder",
501
+ btnHover: "--te-btn-hover",
502
+ btnActive: "--te-btn-active",
503
+ radiusLg: "--te-radius-lg",
504
+ radiusMd: "--te-radius-md",
505
+ radiusSm: "--te-radius-sm",
506
+ shadowSm: "--te-shadow-sm",
507
+ shadowMd: "--te-shadow-md",
508
+ shadowLg: "--te-shadow-lg"
509
+ };
510
+ for (const [i, o] of Object.entries(n)) {
511
+ const s = t[i];
512
+ s && e.style.setProperty(o, s);
513
+ }
514
+ }
515
+ }
516
+ class G {
517
+ container;
518
+ editor;
519
+ activeModal = null;
520
+ savedRange = null;
521
+ isVisible = !1;
522
+ constructor(e) {
523
+ this.editor = e, this.container = this.createContainer(), this.setupListeners(), document.body.appendChild(this.container);
524
+ }
525
+ createContainer() {
526
+ const e = document.createElement("div");
527
+ e.className = "te-floating-toolbar te-glass", e.style.display = "none", e.style.position = "absolute", e.style.zIndex = "2000";
528
+ const t = ["heading", "bold", "italic", "underline", "strikethrough", "highlight-color", "link", "clear-formatting"];
529
+ return v.filter((i) => i.id && t.includes(i.id)).forEach((i) => {
530
+ const o = document.createElement("button");
531
+ o.className = "te-floating-btn", o.title = i.title, o.innerHTML = i.icon || i.title, o.onclick = (s) => {
532
+ s.preventDefault(), s.stopPropagation(), this.handleCommand(i);
533
+ }, e.appendChild(o);
534
+ }), e;
535
+ }
536
+ handleCommand(e) {
537
+ if (e.command === "createLink") {
538
+ const t = window.getSelection();
539
+ t && t.rangeCount > 0 && (this.savedRange = t.getRangeAt(0).cloneRange()), this.activeModal && this.activeModal.close(), this.activeModal = new g(
540
+ "Insert Link",
541
+ [{ id: "url", label: "URL", type: "text", placeholder: "https://example.com" }],
542
+ (n) => {
543
+ if (this.savedRange) {
544
+ const i = window.getSelection();
545
+ i && (i.removeAllRanges(), i.addRange(this.savedRange));
546
+ }
547
+ this.editor.execute("createLink", n.url), this.savedRange = null, this.hide();
548
+ },
549
+ () => {
550
+ this.activeModal = null, this.savedRange = null;
551
+ },
552
+ this.editor.getOptions().theme,
553
+ this.editor.getOptions().dark
554
+ ), this.activeModal.show(this.container);
555
+ } else e.id === "heading" ? this.editor.execute("formatBlock", "H2") : e.id === "highlight-color" ? this.editor.execute("backColor", "#fef08a") : this.editor.execute(e.command || "", e.value);
556
+ }
557
+ setupListeners() {
558
+ const e = () => {
559
+ setTimeout(() => this.updatePosition(), 50);
560
+ };
561
+ this.editor.el.addEventListener("mouseup", e), this.editor.el.addEventListener("keyup", e), window.addEventListener("mousedown", (t) => {
562
+ !this.container.contains(t.target) && !this.editor.el.contains(t.target) && this.hide();
563
+ }), window.addEventListener("resize", () => {
564
+ this.isVisible && this.updatePosition();
565
+ });
566
+ }
567
+ updatePosition() {
568
+ const e = window.getSelection();
569
+ if (!e || e.rangeCount === 0 || e.isCollapsed) {
570
+ this.activeModal || this.hide();
571
+ return;
572
+ }
573
+ const t = e.getRangeAt(0);
574
+ if (!this.editor.el.contains(t.commonAncestorContainer)) {
575
+ this.hide();
576
+ return;
577
+ }
578
+ const n = t.getBoundingClientRect();
579
+ this.container.style.display = "flex", this.isVisible = !0;
580
+ const i = this.container.offsetWidth, o = this.container.offsetHeight;
581
+ let s = n.top + window.scrollY - o - 10, a = n.left + window.scrollX + n.width / 2 - i / 2;
582
+ s < window.scrollY && (s = n.bottom + window.scrollY + 10), a < 10 && (a = 10), a + i > window.innerWidth - 10 && (a = window.innerWidth - i - 10), this.container.style.top = `${s}px`, this.container.style.left = `${a}px`, this.container.classList.add("te-floating-visible");
583
+ }
584
+ hide() {
585
+ this.container.style.display = "none", this.container.classList.remove("te-floating-visible"), this.isVisible = !1;
586
+ }
587
+ destroy() {
588
+ this.container.remove();
589
+ }
590
+ setDarkMode(e) {
591
+ e ? this.container.classList.add("te-dark") : this.container.classList.remove("te-dark");
592
+ }
593
+ }
594
+ class f {
595
+ /**
596
+ * Compresses an image file using HTML5 Canvas.
597
+ */
598
+ static async compressImage(e, t) {
599
+ return new Promise((n, i) => {
600
+ if (e.size <= t * 1024 * 1024 && e.type === "image/webp")
601
+ return n(e);
602
+ const o = new Image();
603
+ o.src = URL.createObjectURL(e), o.onload = () => {
604
+ const s = document.createElement("canvas");
605
+ let a = o.width, r = o.height;
606
+ const l = 2e3;
607
+ (a > l || r > l) && (a > r ? (r = Math.round(r * l / a), a = l) : (a = Math.round(a * l / r), r = l)), s.width = a, s.height = r;
608
+ const d = s.getContext("2d");
609
+ if (!d)
610
+ return i(new Error("Failed to get canvas context"));
611
+ d.drawImage(o, 0, 0, a, r), s.toBlob(
612
+ (c) => {
613
+ c ? n(c) : i(new Error("Canvas toBlob failed"));
614
+ },
615
+ "image/webp",
616
+ 0.8
617
+ // 80% quality is professional standard
618
+ ), URL.revokeObjectURL(o.src);
619
+ }, o.onerror = () => {
620
+ URL.revokeObjectURL(o.src), i(new Error("Failed to load image for compression"));
621
+ };
622
+ });
623
+ }
624
+ /**
625
+ * Uploads a file based on editor configuration.
626
+ */
627
+ static async uploadFile(e, t) {
628
+ const n = e instanceof File ? e.name : "upload.webp";
629
+ if (t.imageEndpoints?.upload) {
630
+ const i = new FormData();
631
+ i.append("file", e, n);
632
+ try {
633
+ const o = await fetch(t.imageEndpoints.upload, {
634
+ method: "POST",
635
+ body: i
636
+ });
637
+ if (o.ok) {
638
+ const s = await o.json();
639
+ return {
640
+ imageUrl: s.imageUrl,
641
+ imageId: s.imageId
642
+ };
643
+ }
644
+ console.warn("Custom upload endpoint returned an error, falling back.");
645
+ } catch (o) {
646
+ console.error("Custom upload failed:", o);
647
+ }
648
+ }
649
+ if (t.cloudinaryFallback) {
650
+ const { cloudName: i, uploadPreset: o } = t.cloudinaryFallback, s = `https://api.cloudinary.com/v1_1/${i}/image/upload`, a = new FormData();
651
+ a.append("file", e, n), a.append("upload_preset", o);
652
+ try {
653
+ const r = await fetch(s, {
654
+ method: "POST",
655
+ body: a
656
+ });
657
+ if (r.ok)
658
+ return {
659
+ imageUrl: (await r.json()).secure_url
660
+ };
661
+ console.warn("Cloudinary upload failed, falling back.");
662
+ } catch (r) {
663
+ console.error("Cloudinary fallback failed:", r);
664
+ }
665
+ }
666
+ return null;
667
+ }
668
+ }
669
+ class K {
670
+ container;
671
+ editableElement;
672
+ selection;
673
+ imageManager;
674
+ history;
675
+ options;
676
+ saveTimeout = null;
677
+ historyTimeout = null;
678
+ pendingStyles = {};
679
+ observer = null;
680
+ floatingToolbar = null;
681
+ eventListeners = [];
682
+ loaderElement = null;
683
+ isUndoingRedoing = !1;
684
+ constructor(e, t = {}) {
685
+ if (this.options = t, this.container = e, typeof document > "u" || !e) {
686
+ this.editableElement = {}, this.selection = {}, this.imageManager = {}, this.history = {};
687
+ return;
688
+ }
689
+ this.container.innerHTML = "", this.container.classList.add("te-container"), this.options.dark && this.container.classList.add("te-dark"), this.options.showLoader !== !1 && this.createLoader(), this.editableElement = this.createEditableElement(), this.selection = new b(), this.imageManager = new y(this), this.history = new E(this.editableElement.innerHTML), this.setupInputHandlers(), this.setupLinkClickHandlers(), this.setupImageObserver(), this.checkPlaceholder(), this.container.appendChild(this.editableElement), this.options.autofocus && this.focus(), this.options.theme && this.applyTheme(this.options.theme), this.options.maxImageSizeMB === void 0 && (this.options.maxImageSizeMB = 5), document.execCommand("defaultParagraphSeparator", !1, "p"), this.floatingToolbar = new G(this), this.options.dark && this.floatingToolbar.setDarkMode(!0), this.options.showLoader !== !1 && setTimeout(() => this.hideLoader(), 300);
690
+ }
691
+ /**
692
+ * Applies custom theme variables to the editor container.
693
+ */
694
+ applyTheme(e) {
695
+ const t = this.container, n = {
696
+ primaryColor: "--te-primary-color",
697
+ primaryHover: "--te-primary-hover",
698
+ bgApp: "--te-bg-app",
699
+ bgEditor: "--te-bg-editor",
700
+ toolbarBg: "--te-toolbar-bg",
701
+ borderColor: "--te-border-color",
702
+ borderFocus: "--te-border-focus",
703
+ textMain: "--te-text-main",
704
+ textMuted: "--te-text-muted",
705
+ placeholder: "--te-placeholder",
706
+ btnHover: "--te-btn-hover",
707
+ btnActive: "--te-btn-active",
708
+ radiusLg: "--te-radius-lg",
709
+ radiusMd: "--te-radius-md",
710
+ radiusSm: "--te-radius-sm",
711
+ shadowSm: "--te-shadow-sm",
712
+ shadowMd: "--te-shadow-md",
713
+ shadowLg: "--te-shadow-lg"
714
+ };
715
+ for (const [i, o] of Object.entries(n)) {
716
+ const s = e[i];
717
+ s && t.style.setProperty(o, s);
718
+ }
719
+ }
720
+ /**
721
+ * Toggles dark mode on the editor.
722
+ */
723
+ setDarkMode(e) {
724
+ this.options.dark = e, e ? (this.container.classList.add("te-dark"), this.floatingToolbar?.setDarkMode(!0)) : (this.container.classList.remove("te-dark"), this.floatingToolbar?.setDarkMode(!1));
725
+ }
726
+ /**
727
+ * Destroys the editor instance and cleans up.
728
+ */
729
+ destroy() {
730
+ 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: n }) => {
731
+ e.removeEventListener(t, n);
732
+ }), this.eventListeners = [], this.container.innerHTML = "", this.container.classList.remove("te-container", "te-dark"), this.container.removeAttribute("style"), this.floatingToolbar && (this.floatingToolbar.destroy(), this.floatingToolbar = null);
733
+ }
734
+ checkPlaceholder() {
735
+ if (!this.editableElement) return;
736
+ this.editableElement.textContent?.trim() === "" && !this.editableElement.querySelector("img") && !this.editableElement.querySelector("table") && !this.editableElement.querySelector("ul") && !this.editableElement.querySelector("ol") ? this.editableElement.classList.add("is-empty") : this.editableElement.classList.remove("is-empty");
737
+ }
738
+ addEventListener(e, t, n, i) {
739
+ e.addEventListener(t, n, i), this.eventListeners.push({ target: e, type: t, handler: n });
740
+ }
741
+ setupImageObserver() {
742
+ this.observer = new MutationObserver((e) => {
743
+ e.forEach((t) => {
744
+ t.addedNodes.forEach((n) => {
745
+ if (n.nodeType === Node.ELEMENT_NODE) {
746
+ const i = n;
747
+ i.tagName === "IMG" && !i.closest(".te-image-container") ? this.wrapImage(i) : i.querySelectorAll("img:not(.te-image)").forEach((s) => {
748
+ s.closest(".te-image-container") || this.wrapImage(s);
749
+ });
750
+ }
751
+ });
752
+ });
753
+ }), this.observer.observe(this.editableElement, {
754
+ childList: !0,
755
+ subtree: !0
756
+ });
757
+ }
758
+ /**
759
+ * Wraps a raw <img> element in the interactive container
760
+ */
761
+ wrapImage(e) {
762
+ const t = e.parentElement;
763
+ if (!t) return;
764
+ const n = document.createElement("figure");
765
+ n.classList.add("te-image-container"), n.setAttribute("contenteditable", "false");
766
+ const i = document.createElement("img");
767
+ i.src = e.src, i.alt = e.alt || "", e.width && (i.style.width = `${e.width}px`), e.height && (i.style.height = `${e.height}px`), i.classList.add("te-image");
768
+ const o = document.createElement("figcaption");
769
+ if (o.classList.add("te-image-caption"), o.setAttribute("contenteditable", "true"), o.setAttribute("data-placeholder", "Type caption..."), ["top-left", "top-right", "bottom-left", "bottom-right"].forEach((a) => {
770
+ const r = document.createElement("div");
771
+ r.classList.add("te-image-resizer", `te-resizer-${a}`), n.appendChild(r);
772
+ }), n.appendChild(i), n.appendChild(o), t.replaceChild(n, e), !n.nextElementSibling) {
773
+ const a = document.createElement("p");
774
+ a.innerHTML = "<br>", n.after(a);
775
+ }
776
+ }
777
+ setupInputHandlers() {
778
+ this.addEventListener(this.editableElement, "beforeinput", (e) => {
779
+ if (e.inputType === "insertText" && Object.keys(this.pendingStyles).length > 0) {
780
+ const t = e.data;
781
+ if (!t) return;
782
+ e.preventDefault();
783
+ const n = document.createElement("span");
784
+ for (const [o, s] of Object.entries(this.pendingStyles))
785
+ n.style.setProperty(o, s);
786
+ n.textContent = t;
787
+ const i = this.selection.getRange();
788
+ if (i) {
789
+ i.deleteContents(), i.insertNode(n);
790
+ const o = document.createRange();
791
+ o.setStart(n.firstChild, t.length), o.setEnd(n.firstChild, t.length), this.selection.restoreSelection(o), this.pendingStyles = {}, this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
792
+ }
793
+ }
794
+ }), this.addEventListener(this.editableElement, "input", () => {
795
+ this.checkPlaceholder();
796
+ }), this.addEventListener(document, "selectionchange", () => {
797
+ const e = window.getSelection();
798
+ e && e.rangeCount > 0 && (e.getRangeAt(0).collapsed || (this.pendingStyles = {}));
799
+ }), this.addEventListener(this.editableElement, "dragover", (e) => {
800
+ e.preventDefault(), e.dataTransfer.dropEffect = "copy", this.editableElement.classList.add("dragover");
801
+ }), this.addEventListener(this.editableElement, "dragleave", () => {
802
+ this.editableElement.classList.remove("dragover");
803
+ }), this.addEventListener(this.editableElement, "drop", (e) => {
804
+ e.preventDefault(), this.editableElement.classList.remove("dragover");
805
+ const t = e.dataTransfer?.files;
806
+ t && t.length > 0 && this.handleFiles(Array.from(t));
807
+ }), this.addEventListener(this.editableElement, "paste", this.handlePaste.bind(this)), this.addEventListener(this.editableElement, "input", () => {
808
+ this.handleInput();
809
+ }), this.addEventListener(this.editableElement, "keydown", (e) => {
810
+ (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());
811
+ });
812
+ }
813
+ /**
814
+ * Immediately records a history state if one is pending.
815
+ */
816
+ flushHistoryRecord() {
817
+ if (this.historyTimeout) {
818
+ clearTimeout(this.historyTimeout), this.historyTimeout = null;
819
+ const e = this.editableElement.innerHTML, t = this.selection.getSelectionPath(this.editableElement);
820
+ this.history.record(e, t);
821
+ }
822
+ }
823
+ handleInput() {
824
+ this.isUndoingRedoing || (this.scheduleHistoryRecord(), this.options.autoSave && (this.options.onSaving && this.options.onSaving(), this.scheduleAutoSave()), this.options.onChange && this.options.onChange(this.getHTML()));
825
+ }
826
+ scheduleHistoryRecord() {
827
+ this.historyTimeout && clearTimeout(this.historyTimeout), this.historyTimeout = setTimeout(() => {
828
+ const e = this.editableElement.innerHTML, t = this.selection.getSelectionPath(this.editableElement);
829
+ this.history.record(e, t);
830
+ }, 200);
831
+ }
832
+ scheduleAutoSave() {
833
+ this.saveTimeout && clearTimeout(this.saveTimeout);
834
+ const e = this.options.autoSaveInterval || 300;
835
+ this.saveTimeout = setTimeout(() => {
836
+ this.save();
837
+ }, e);
838
+ }
839
+ save() {
840
+ this.options.onSave && this.options.onSave(this.getHTML());
841
+ }
842
+ undo() {
843
+ this.flushHistoryRecord();
844
+ const e = this.history.undo();
845
+ e !== null && (this.isUndoingRedoing = !0, this.editableElement.innerHTML = e.html, e.selection && this.selection.restoreSelectionPath(this.editableElement, e.selection), this.triggerChange(), this.isUndoingRedoing = !1);
846
+ }
847
+ redo() {
848
+ this.flushHistoryRecord();
849
+ const e = this.history.redo();
850
+ e !== null && (this.isUndoingRedoing = !0, this.editableElement.innerHTML = e.html, e.selection && this.selection.restoreSelectionPath(this.editableElement, e.selection), this.triggerChange(), this.isUndoingRedoing = !1);
851
+ }
852
+ triggerChange() {
853
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
854
+ }
855
+ createLoader() {
856
+ this.loaderElement = document.createElement("div"), this.loaderElement.className = "te-loader-overlay";
857
+ const e = document.createElement("div");
858
+ e.className = "te-loader-spinner";
859
+ const t = document.createElement("div");
860
+ t.className = "te-loader-shimmer";
861
+ const n = document.createElement("div");
862
+ n.className = "te-loader-text", n.textContent = "Initializing Editor...", this.loaderElement.appendChild(e), this.loaderElement.appendChild(t), this.loaderElement.appendChild(n), this.container.appendChild(this.loaderElement);
863
+ }
864
+ hideLoader() {
865
+ this.loaderElement && (this.loaderElement.classList.add("hidden"), setTimeout(() => {
866
+ this.loaderElement && this.loaderElement.parentNode && this.loaderElement.parentNode.removeChild(this.loaderElement), this.loaderElement = null;
867
+ }, 400));
868
+ }
869
+ createEditableElement() {
870
+ const e = document.createElement("div");
871
+ 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;
872
+ }
873
+ /**
874
+ * Focuses the editor.
875
+ */
876
+ focus() {
877
+ this.editableElement.focus();
878
+ }
879
+ /**
880
+ * Executes a command on the current selection.
881
+ */
882
+ execute(e, t = null) {
883
+ this.focus(), document.execCommand(e, !1, t ?? void 0), e === "removeFormat" && (document.execCommand("formatBlock", !1, "p"), this.pendingStyles = {}), this.normalize(), this.triggerChange();
884
+ }
885
+ /**
886
+ * Special handler for links to open them in a new tab when clicked.
887
+ */
888
+ setupLinkClickHandlers() {
889
+ this.addEventListener(this.editableElement, "click", (e) => {
890
+ const n = e.target.closest("a");
891
+ if (n && this.editableElement.contains(n)) {
892
+ e.preventDefault();
893
+ const i = n.getAttribute("href");
894
+ i && window.open(i, "_blank", "noopener,noreferrer");
895
+ }
896
+ });
897
+ }
898
+ /**
899
+ * Inserts a table at the current selection.
900
+ */
901
+ insertTable(e = 3, t = 3) {
902
+ this.focus();
903
+ const n = this.selection.getRange();
904
+ if (!n) return;
905
+ const i = document.createElement("table");
906
+ i.classList.add("te-table");
907
+ for (let s = 0; s < e; s++) {
908
+ const a = document.createElement("tr");
909
+ for (let r = 0; r < t; r++) {
910
+ const l = document.createElement("td");
911
+ l.innerHTML = "<br>", a.appendChild(l);
912
+ }
913
+ i.appendChild(a);
914
+ }
915
+ n.deleteContents(), n.insertNode(i);
916
+ const o = i.nextElementSibling;
917
+ if (!o || o.tagName !== "P") {
918
+ const s = document.createElement("p");
919
+ s.innerHTML = "<br>", i.after(s), o && o.tagName === "BR" && o.remove();
920
+ }
921
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
922
+ }
923
+ /**
924
+ * Adds a row to the currently selected table.
925
+ */
926
+ addRow() {
927
+ const e = this.getSelectedTable();
928
+ if (!e) return;
929
+ const t = document.createElement("tr");
930
+ t.style.borderBottom = "1px solid var(--te-border-color)";
931
+ const n = e.rows[0].cells.length;
932
+ for (let o = 0; o < n; o++) {
933
+ const s = document.createElement("td");
934
+ s.innerHTML = "<br>", t.appendChild(s);
935
+ }
936
+ const i = this.getSelectedTd();
937
+ i ? i.parentElement?.after(t) : e.appendChild(t), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
938
+ }
939
+ /**
940
+ * Deletes the currently selected row.
941
+ */
942
+ deleteRow() {
943
+ const e = this.getSelectedTd();
944
+ if (e && e.parentElement) {
945
+ const t = e.parentElement, n = t.parentElement;
946
+ if (n.rows.length > 1) {
947
+ const i = t.rowIndex, o = n.rows[i + 1] || n.rows[i - 1], s = e.cellIndex;
948
+ if (t.remove(), o && o.cells[s]) {
949
+ const a = document.createRange();
950
+ a.selectNodeContents(o.cells[s]), a.collapse(!0), this.selection.restoreSelection(a);
951
+ }
952
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
953
+ }
954
+ }
955
+ }
956
+ /**
957
+ * Adds a column to the currently selected table.
958
+ */
959
+ addColumn() {
960
+ const e = this.getSelectedTable();
961
+ if (!e) return;
962
+ const t = this.getSelectedTd(), n = t ? t.cellIndex : -1;
963
+ for (let i = 0; i < e.rows.length; i++) {
964
+ const o = e.rows[i], s = document.createElement("td");
965
+ s.innerHTML = "<br>", n !== -1 ? o.cells[n].after(s) : o.appendChild(s);
966
+ }
967
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
968
+ }
969
+ /**
970
+ * Deletes the currently selected column.
971
+ */
972
+ deleteColumn() {
973
+ const e = this.getSelectedTd();
974
+ if (!e) return;
975
+ const t = this.getSelectedTable();
976
+ if (!t) return;
977
+ const n = e.cellIndex;
978
+ if (t.rows[0].cells.length > 1) {
979
+ const i = e.nextElementSibling || e.previousElementSibling;
980
+ for (let o = 0; o < t.rows.length; o++)
981
+ t.rows[o].cells[n].remove();
982
+ if (i) {
983
+ const o = document.createRange();
984
+ o.selectNodeContents(i), o.collapse(!0), this.selection.restoreSelection(o);
985
+ }
986
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
987
+ }
988
+ }
989
+ getSelectedTd() {
990
+ const e = window.getSelection();
991
+ if (!e || e.rangeCount === 0) return null;
992
+ let t = e.anchorNode;
993
+ for (; t && t !== this.editableElement; ) {
994
+ if (t.nodeName === "TD") return t;
995
+ t = t.parentNode;
996
+ }
997
+ return null;
998
+ }
999
+ getSelectedTable() {
1000
+ const e = this.getSelectedTd();
1001
+ return e ? e.closest("table") : null;
1002
+ }
1003
+ /**
1004
+ * Recursively removes a style property from all elements in a fragment.
1005
+ */
1006
+ clearStyleRecursive(e, t) {
1007
+ const n = document.createTreeWalker(e, NodeFilter.SHOW_ELEMENT);
1008
+ let i = n.nextNode();
1009
+ for (; i; )
1010
+ i.style.getPropertyValue(t) && i.style.removeProperty(t), i = n.nextNode();
1011
+ }
1012
+ /**
1013
+ * Applies an inline style to the selection.
1014
+ * This is used for properties like font-size (px) and font-family
1015
+ * where execCommand is outdated or limited.
1016
+ */
1017
+ /**
1018
+ * Applies an inline style to the selection.
1019
+ * This is used for properties like font-size (px) and font-family
1020
+ * where execCommand is outdated or limited.
1021
+ */
1022
+ setStyle(e, t, n) {
1023
+ if (!n) {
1024
+ const a = window.getSelection();
1025
+ if (!a || a.rangeCount === 0) return null;
1026
+ n = a.getRangeAt(0);
1027
+ }
1028
+ if (n.collapsed)
1029
+ return this.pendingStyles[e] = t, n;
1030
+ if (["line-height"].includes(e))
1031
+ return this.setBlockStyle(e, t, n);
1032
+ let o = n.commonAncestorContainer;
1033
+ o.nodeType === Node.TEXT_NODE && (o = o.parentElement);
1034
+ let s = null;
1035
+ if (o.tagName === "SPAN" && o.children.length === 0 && o.textContent === n.toString())
1036
+ o.style.setProperty(e, t), s = n.cloneRange();
1037
+ else {
1038
+ const a = document.createElement("span");
1039
+ a.style.setProperty(e, t);
1040
+ try {
1041
+ const r = o.tagName === "SPAN" ? o : null, l = n.extractContents();
1042
+ this.clearStyleRecursive(l, e), a.appendChild(l), n.insertNode(a), r && r.innerHTML === "" && r.remove();
1043
+ const d = document.createRange();
1044
+ d.selectNodeContents(a), s = d;
1045
+ const c = window.getSelection();
1046
+ c && c.rangeCount > 0 && (c.removeAllRanges(), c.addRange(d));
1047
+ } catch (r) {
1048
+ console.warn("Failed to apply style:", r);
1049
+ }
1050
+ }
1051
+ return this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 })), s;
1052
+ }
1053
+ /**
1054
+ * Applies a style to the block-level containers within the range.
1055
+ */
1056
+ setBlockStyle(e, t, n) {
1057
+ const i = ["P", "H1", "H2", "H3", "H4", "H5", "H6", "LI", "TD", "TH", "DIV", "BLOCKQUOTE"], o = /* @__PURE__ */ new Set();
1058
+ if (Array.from(this.editableElement.querySelectorAll(i.join(","))).forEach((a) => {
1059
+ n.intersectsNode(a) && o.add(a);
1060
+ }), o.size === 0) {
1061
+ let a = n.commonAncestorContainer;
1062
+ for (; a && a !== this.editableElement.parentElement; ) {
1063
+ if (a.nodeType === Node.ELEMENT_NODE && i.includes(a.tagName)) {
1064
+ o.add(a);
1065
+ break;
1066
+ }
1067
+ a = a.parentNode;
1068
+ }
1069
+ }
1070
+ return o.forEach((a) => {
1071
+ a.style.setProperty(e, t);
1072
+ }), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 })), n;
1073
+ }
1074
+ /**
1075
+ * Creates a link at the current selection.
1076
+ * Ensures the link opens in a new tab with proper security attributes.
1077
+ */
1078
+ createLink(e) {
1079
+ if (this.focus(), e = e.trim(), /^(javascript|vbscript|data|file):/i.test(e)) {
1080
+ console.warn("Security Warning: Blocked malicious URI scheme.");
1081
+ return;
1082
+ }
1083
+ const n = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(e);
1084
+ !/^https?:\/\//i.test(e) && !/^mailto:/i.test(e) && !e.startsWith("#") && (n ? e = "mailto:" + e : e = "https://" + e);
1085
+ const i = window.getSelection();
1086
+ if (i && i.rangeCount > 0) {
1087
+ const o = i.getRangeAt(0);
1088
+ if (o.collapsed) {
1089
+ const s = document.createTextNode(e);
1090
+ o.insertNode(s);
1091
+ const a = document.createRange();
1092
+ a.selectNodeContents(s), i.removeAllRanges(), i.addRange(a);
1093
+ }
1094
+ }
1095
+ if (document.execCommand("createLink", !1, e), i && i.rangeCount > 0) {
1096
+ let s = i.getRangeAt(0).commonAncestorContainer;
1097
+ s.nodeType === Node.TEXT_NODE && (s = s.parentElement);
1098
+ let a = null;
1099
+ s.tagName === "A" ? a = s : a = s.querySelector("a"), a && (a.setAttribute("target", "_blank"), a.setAttribute("rel", "noopener noreferrer"));
1100
+ }
1101
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
1102
+ }
1103
+ /**
1104
+ * Inserts an image at the current selection.
1105
+ */
1106
+ insertImage(e, t, n = !1) {
1107
+ this.focus();
1108
+ const i = this.selection.getRange();
1109
+ if (!i) return null;
1110
+ const o = document.createElement("figure");
1111
+ o.classList.add("te-image-container"), o.setAttribute("contenteditable", "false"), n && o.classList.add("is-loading");
1112
+ const s = document.createElement("img");
1113
+ s.src = e, s.classList.add("te-image"), t && s.setAttribute("data-image-id", t);
1114
+ const a = document.createElement("figcaption");
1115
+ a.classList.add("te-image-caption"), a.setAttribute("contenteditable", "true"), a.setAttribute("data-placeholder", "Type caption..."), ["top-left", "top-right", "bottom-left", "bottom-right"].forEach((c) => {
1116
+ const h = document.createElement("div");
1117
+ h.classList.add("te-image-resizer", `te-resizer-${c}`), o.appendChild(h);
1118
+ }), o.appendChild(s), o.appendChild(a), i.deleteContents(), i.insertNode(o);
1119
+ const l = document.createElement("p");
1120
+ l.innerHTML = "<br>", o.after(l);
1121
+ const d = document.createRange();
1122
+ return d.setStart(l, 0), d.setEnd(l, 0), this.selection.restoreSelection(d), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 })), this.save(), o;
1123
+ }
1124
+ /**
1125
+ * Returns the clean and optimized HTML content of the editor.
1126
+ */
1127
+ getHTML() {
1128
+ return this.normalizeHTML(this.editableElement.innerHTML);
1129
+ }
1130
+ /**
1131
+ * Normalizes the editor's content in-place.
1132
+ */
1133
+ normalize() {
1134
+ const e = this.editableElement.innerHTML, t = this.selection.getRange();
1135
+ let n = null, i = null;
1136
+ if (t && this.editableElement.contains(t.commonAncestorContainer)) {
1137
+ n = document.createElement("span"), n.id = "te-selection-start", n.style.display = "none", i = document.createElement("span"), i.id = "te-selection-end", i.style.display = "none";
1138
+ const s = t.cloneRange();
1139
+ s.collapse(!0), s.insertNode(n);
1140
+ const a = t.cloneRange();
1141
+ a.collapse(!1), a.insertNode(i);
1142
+ }
1143
+ const o = this.normalizeHTML(this.editableElement.innerHTML);
1144
+ if (o !== e || n) {
1145
+ this.editableElement.innerHTML = o;
1146
+ const s = this.editableElement.querySelector("#te-selection-start"), a = this.editableElement.querySelector("#te-selection-end");
1147
+ if (s && a) {
1148
+ const r = document.createRange();
1149
+ r.setStartAfter(s), r.setEndBefore(a), this.selection.restoreSelection(r);
1150
+ }
1151
+ this.editableElement.querySelectorAll("#te-selection-start, #te-selection-end").forEach((r) => r.remove());
1152
+ }
1153
+ }
1154
+ normalizationContainer = null;
1155
+ /**
1156
+ * Internal helper to strictly sanitize HTML strings.
1157
+ */
1158
+ sanitize(e) {
1159
+ p.addHook("afterSanitizeAttributes", (n) => {
1160
+ n.tagName === "A" && (n.setAttribute("target", "_blank"), n.setAttribute("rel", "noopener noreferrer"));
1161
+ });
1162
+ const t = p.sanitize(e, {
1163
+ ALLOWED_TAGS: [
1164
+ "b",
1165
+ "i",
1166
+ "u",
1167
+ "s",
1168
+ "span",
1169
+ "div",
1170
+ "p",
1171
+ "br",
1172
+ "a",
1173
+ "h1",
1174
+ "h2",
1175
+ "h3",
1176
+ "h4",
1177
+ "h5",
1178
+ "h6",
1179
+ "ul",
1180
+ "ol",
1181
+ "li",
1182
+ "blockquote",
1183
+ "hr",
1184
+ "img",
1185
+ "table",
1186
+ "tbody",
1187
+ "tr",
1188
+ "td",
1189
+ "th",
1190
+ "thead",
1191
+ "tfoot",
1192
+ "figure",
1193
+ "figcaption"
1194
+ ],
1195
+ ALLOWED_ATTR: [
1196
+ "href",
1197
+ "src",
1198
+ "alt",
1199
+ "style",
1200
+ "color",
1201
+ "background-color",
1202
+ "class",
1203
+ "id",
1204
+ "target",
1205
+ "rel",
1206
+ "contenteditable",
1207
+ "data-placeholder",
1208
+ "data-image-id"
1209
+ ],
1210
+ ALLOW_DATA_ATTR: !0,
1211
+ FORBID_TAGS: ["script", "style", "iframe", "object", "embed", "form", "textarea"],
1212
+ FORBID_ATTR: ["onerror", "onload", "onclick", "onmouseover"]
1213
+ });
1214
+ return p.removeHook("afterSanitizeAttributes"), t;
1215
+ }
1216
+ /**
1217
+ * Optimizes HTML by fixing invalid nesting and removing redundant tags.
1218
+ */
1219
+ normalizeHTML(e) {
1220
+ this.normalizationContainer || (this.normalizationContainer = document.createElement("div"));
1221
+ const t = this.normalizationContainer;
1222
+ t.innerHTML = e;
1223
+ const n = Array.from(t.childNodes);
1224
+ let i = null;
1225
+ n.forEach((a) => {
1226
+ if (a.nodeType === Node.TEXT_NODE || a.nodeType === Node.ELEMENT_NODE && !["P", "DIV", "H1", "H2", "H3", "H4", "H5", "H6", "UL", "OL", "TABLE", "BLOCKQUOTE", "PRE", "HR", "FIGURE"].includes(a.tagName)) {
1227
+ if (a.nodeType === Node.TEXT_NODE && (a.textContent || "").trim() === "" && !i)
1228
+ return;
1229
+ i || (i = document.createElement("p"), a.before(i)), i.appendChild(a);
1230
+ } else
1231
+ i = null;
1232
+ }), t.querySelectorAll("p").forEach((a) => {
1233
+ const r = a.querySelectorAll("ul, ol");
1234
+ r.length > 0 && (r.forEach((l) => {
1235
+ a.after(l);
1236
+ }), (a.innerHTML.trim() === "" || a.innerHTML.trim() === "<br>") && a.remove());
1237
+ }), t.querySelectorAll("span").forEach((a) => {
1238
+ if (!a.id.startsWith("te-selection-"))
1239
+ if (a.attributes.length === 0) {
1240
+ const r = document.createTextNode(a.textContent || "");
1241
+ a.replaceWith(r);
1242
+ } else a.innerHTML.trim() === "" && a.remove();
1243
+ });
1244
+ const s = Array.from(t.querySelectorAll("p"));
1245
+ s.forEach((a) => {
1246
+ a.innerHTML.trim() === "" && t.childNodes.length > 1 && a !== t.lastElementChild && a.remove();
1247
+ });
1248
+ for (let a = s.length - 1; a >= 0; a--) {
1249
+ const r = s[a], l = r.innerHTML.trim() === "" || r.innerHTML.trim() === "<br>", d = r === t.lastElementChild;
1250
+ if (l && d && t.children.length > 1)
1251
+ r.remove();
1252
+ else
1253
+ break;
1254
+ }
1255
+ return t.innerHTML.trim() === "<p><br></p>" || t.innerHTML.trim() === "<p></p>" ? "" : this.sanitize(t.innerHTML);
1256
+ }
1257
+ // Handle paste events to sanitize inherited malware and styles
1258
+ handlePaste(e) {
1259
+ e.preventDefault();
1260
+ let t = (e.clipboardData || window.clipboardData).getData("text/plain"), n = (e.clipboardData || window.clipboardData).getData("text/html");
1261
+ if (e.clipboardData && e.clipboardData.items) {
1262
+ const o = [];
1263
+ for (let s = 0; s < e.clipboardData.items.length; s++) {
1264
+ const a = e.clipboardData.items[s];
1265
+ if (a.type.startsWith("image/")) {
1266
+ const r = a.getAsFile();
1267
+ r && o.push(r);
1268
+ }
1269
+ }
1270
+ if (o.length > 0) {
1271
+ this.handleFiles(o);
1272
+ return;
1273
+ }
1274
+ }
1275
+ const i = /<([a-z1-6]+)\b[^>]*>[\s\S]*<\/\1>/i.test(t) || /^\s*<[a-z1-6]+\b[^>]*>/i.test(t);
1276
+ if (!n && t && i && (n = t.replace(/(\r\n|\n|\r)/gm, " ").replace(/>\s+</g, "><").trim()), n) {
1277
+ const o = this.sanitize(n);
1278
+ this.execute("insertHTML", o);
1279
+ } else
1280
+ this.execute("insertText", t);
1281
+ }
1282
+ /**
1283
+ * Sets the HTML content of the editor.
1284
+ */
1285
+ setHTML(e) {
1286
+ const t = this.sanitize(e);
1287
+ this.editableElement.innerHTML = t;
1288
+ }
1289
+ /**
1290
+ * Internal access to the editable element.
1291
+ */
1292
+ get el() {
1293
+ return this.editableElement;
1294
+ }
1295
+ /**
1296
+ * Returns the editor options.
1297
+ */
1298
+ getOptions() {
1299
+ return this.options;
1300
+ }
1301
+ /**
1302
+ * Internal helper to handle multiple files.
1303
+ */
1304
+ async handleFiles(e) {
1305
+ const t = this.options.maxImageSizeMB || 5;
1306
+ for (const n of e) {
1307
+ if (!n.type.startsWith("image/")) continue;
1308
+ let i = null;
1309
+ try {
1310
+ if (n.size > t * 1024 * 1024 * 3) {
1311
+ console.warn(`File ${n.name} is too large to even attempt processing.`);
1312
+ continue;
1313
+ }
1314
+ this.options.onSaving && this.options.onSaving();
1315
+ const o = URL.createObjectURL(n);
1316
+ i = this.insertImage(o, void 0, !0);
1317
+ const s = await f.compressImage(n, t), a = URL.createObjectURL(s);
1318
+ if (i) {
1319
+ const l = i.querySelector("img");
1320
+ l && (l.src = a);
1321
+ }
1322
+ if (s.size > t * 1024 * 1024) {
1323
+ alert(`Image "${n.name}" exceeds the ${t}MB limit even after compression.`), i?.remove();
1324
+ continue;
1325
+ }
1326
+ const r = await f.uploadFile(s, this.options);
1327
+ if (r)
1328
+ if (i) {
1329
+ const l = i.querySelector("img");
1330
+ l && (l.src = r.imageUrl, r.imageId && l.setAttribute("data-image-id", r.imageId)), i.classList.remove("is-loading");
1331
+ } else
1332
+ this.insertImage(r.imageUrl, r.imageId);
1333
+ else {
1334
+ const l = new FileReader();
1335
+ l.onload = (d) => {
1336
+ const c = d.target?.result;
1337
+ if (i) {
1338
+ const h = i.querySelector("img");
1339
+ h && (h.src = c), i.classList.remove("is-loading");
1340
+ } else
1341
+ this.insertImage(c);
1342
+ }, l.readAsDataURL(s);
1343
+ }
1344
+ } catch (o) {
1345
+ console.error("Image handling failed", o), i?.remove();
1346
+ } finally {
1347
+ this.options.onSave && this.save();
1348
+ }
1349
+ }
1350
+ }
1351
+ }
1352
+ class X {
1353
+ container;
1354
+ searchInput;
1355
+ emojiGrid;
1356
+ onSelect;
1357
+ onClose;
1358
+ emojiList = [];
1359
+ theme;
1360
+ dark;
1361
+ constructor(e, t, n, i) {
1362
+ this.onSelect = e, this.onClose = t, this.theme = n, this.dark = i, this.container = this.createPickerElement(), this.searchInput = this.container.querySelector(".te-emoji-search"), this.emojiGrid = this.container.querySelector(".te-emoji-grid"), this.setupEvents(), this.loadEmojis();
1363
+ }
1364
+ async loadEmojis() {
1365
+ this.emojiGrid.textContent = "Loading...";
1366
+ try {
1367
+ const { EMOJI_LIST: e } = await import("./EmojiList-B-C3-zN2.js");
1368
+ this.emojiList = e, this.renderEmojis(this.emojiList);
1369
+ } catch (e) {
1370
+ console.error("Failed to load emojis:", e), this.emojiGrid.textContent = "Failed to load";
1371
+ }
1372
+ }
1373
+ createPickerElement() {
1374
+ const e = document.createElement("div");
1375
+ return e.classList.add("te-emoji-picker"), this.theme && this.applyTheme(e, this.theme), this.dark && e.classList.add("te-dark"), e.innerHTML = `
1376
+ <div class="te-emoji-header">
1377
+ <input type="text" class="te-emoji-search" placeholder="Search emoji...">
1378
+ </div>
1379
+ <div class="te-emoji-body">
1380
+ <div class="te-emoji-grid"></div>
1381
+ </div>
1382
+ `, e;
1383
+ }
1384
+ setupEvents() {
1385
+ this.searchInput.addEventListener("mousedown", (t) => t.stopPropagation()), this.searchInput.addEventListener("click", (t) => t.stopPropagation()), this.searchInput.addEventListener("input", () => {
1386
+ const t = this.searchInput.value.toLowerCase(), n = this.emojiList.filter(
1387
+ (i) => i.name.toLowerCase().includes(t) || i.category.toLowerCase().includes(t)
1388
+ );
1389
+ this.renderEmojis(n);
1390
+ });
1391
+ const e = (t) => {
1392
+ this.container.contains(t.target) || (this.close(), document.removeEventListener("mousedown", e));
1393
+ };
1394
+ setTimeout(() => document.addEventListener("mousedown", e), 0);
1395
+ }
1396
+ renderEmojis(e) {
1397
+ if (this.emojiGrid.innerHTML = "", e.length === 0) {
1398
+ this.emojiGrid.textContent = "No emoji found";
1399
+ return;
1400
+ }
1401
+ this.searchInput.value.length > 0 ? this.renderGridItems(e) : ["Smileys", "Symbols", "Hands", "Animals", "Food", "Travel", "Objects", "Activities"].forEach((i) => {
1402
+ const o = e.filter((s) => s.category === i);
1403
+ if (o.length > 0) {
1404
+ const s = document.createElement("div");
1405
+ s.classList.add("te-emoji-category-title"), s.textContent = i, this.emojiGrid.appendChild(s), this.renderGridItems(o);
1406
+ }
1407
+ });
1408
+ }
1409
+ renderGridItems(e) {
1410
+ e.forEach((t) => {
1411
+ const n = document.createElement("button");
1412
+ n.type = "button", n.classList.add("te-emoji-item"), n.textContent = t.emoji, n.title = t.name, n.addEventListener("click", () => {
1413
+ this.onSelect(t.emoji), this.close();
1414
+ }), this.emojiGrid.appendChild(n);
1415
+ });
1416
+ }
1417
+ applyTheme(e, t) {
1418
+ const n = {
1419
+ primaryColor: "--te-primary-color",
1420
+ primaryHover: "--te-primary-hover",
1421
+ bgApp: "--te-bg-app",
1422
+ bgEditor: "--te-bg-editor",
1423
+ toolbarBg: "--te-toolbar-bg",
1424
+ borderColor: "--te-border-color",
1425
+ borderFocus: "--te-border-focus",
1426
+ textMain: "--te-text-main",
1427
+ textMuted: "--te-text-muted",
1428
+ placeholder: "--te-placeholder",
1429
+ btnHover: "--te-btn-hover",
1430
+ btnActive: "--te-btn-active",
1431
+ radiusLg: "--te-radius-lg",
1432
+ radiusMd: "--te-radius-md",
1433
+ radiusSm: "--te-radius-sm",
1434
+ shadowSm: "--te-shadow-sm",
1435
+ shadowMd: "--te-shadow-md",
1436
+ shadowLg: "--te-shadow-lg"
1437
+ };
1438
+ for (const [i, o] of Object.entries(n)) {
1439
+ const s = t[i];
1440
+ s && e.style.setProperty(o, s);
1441
+ }
1442
+ }
1443
+ show(e) {
1444
+ document.body.appendChild(this.container);
1445
+ const t = e.getBoundingClientRect(), n = 280;
1446
+ let i = t.bottom + window.scrollY + 5, o = t.left + window.scrollX;
1447
+ o + n > window.innerWidth && (o = window.innerWidth - n - 10), this.container.style.top = `${i}px`, this.container.style.left = `${o}px`, this.searchInput.focus();
1448
+ }
1449
+ close() {
1450
+ this.container.parentElement && (this.container.remove(), this.onClose());
1451
+ }
1452
+ get el() {
1453
+ return this.container;
1454
+ }
1455
+ }
1456
+ class Y {
1457
+ editor;
1458
+ container;
1459
+ savedRange = null;
1460
+ items = v;
1461
+ activePicker = null;
1462
+ activeModal = null;
1463
+ statusEl = null;
1464
+ boundUpdateActiveStates;
1465
+ constructor(e) {
1466
+ this.editor = e, this.container = this.createToolbarElement(), this.boundUpdateActiveStates = this.updateActiveStates.bind(this), this.render();
1467
+ }
1468
+ createToolbarElement() {
1469
+ const e = document.createElement("div");
1470
+ 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;
1471
+ }
1472
+ render() {
1473
+ const e = this.editor.getOptions().toolbarItems, t = [];
1474
+ this.items.forEach((o) => {
1475
+ (o.type === "divider" || o.id && (!e || e.includes(o.id))) && t.push(o);
1476
+ });
1477
+ const n = [];
1478
+ t.forEach((o, s) => {
1479
+ if (o.type === "divider") {
1480
+ if (n.length === 0 || n[n.length - 1].type === "divider" || !t.slice(s + 1).some((r) => r.type !== "divider")) return;
1481
+ n.push(o);
1482
+ } else
1483
+ n.push(o);
1484
+ }), n.forEach((o) => {
1485
+ if (o.type === "button")
1486
+ this.renderButton(o);
1487
+ else if (o.type === "select")
1488
+ this.renderSelect(o);
1489
+ else if (o.type === "input")
1490
+ this.renderInput(o);
1491
+ else if (o.type === "color-picker")
1492
+ this.renderColorPicker(o);
1493
+ else if (o.type === "divider") {
1494
+ const s = document.createElement("div");
1495
+ s.classList.add("te-divider"), this.container.appendChild(s);
1496
+ }
1497
+ }), this.editor.getOptions().showStatus !== !1 && this.container.appendChild(this.statusEl), this.editor.el.addEventListener("keyup", this.boundUpdateActiveStates), this.editor.el.addEventListener("mouseup", this.boundUpdateActiveStates);
1498
+ }
1499
+ renderButton(e) {
1500
+ const t = document.createElement("button");
1501
+ t.classList.add("te-button"), t.innerHTML = e.icon || "", t.title = e.title, t.addEventListener("mousedown", (n) => {
1502
+ if (n.preventDefault(), e.command === "createLink" || e.command === "insertTable") {
1503
+ const i = window.getSelection();
1504
+ if (i && i.rangeCount > 0) {
1505
+ const o = i.getRangeAt(0);
1506
+ this.editor.el.contains(o.commonAncestorContainer) && (this.savedRange = o.cloneRange());
1507
+ }
1508
+ }
1509
+ if (e.command === "insertEmoji") {
1510
+ this.activePicker ? this.activePicker.close() : (this.activePicker = new X(
1511
+ (i) => {
1512
+ this.editor.execute("insertText", i);
1513
+ },
1514
+ () => {
1515
+ this.activePicker = null;
1516
+ },
1517
+ this.editor.getOptions().theme,
1518
+ this.editor.getOptions().dark
1519
+ ), this.activePicker.show(t));
1520
+ return;
1521
+ }
1522
+ if (e.command === "insertImage") {
1523
+ const i = document.createElement("input");
1524
+ i.type = "file", i.accept = "image/*", i.style.display = "none", i.addEventListener("change", (o) => {
1525
+ const a = o.target.files;
1526
+ a && a.length > 0 && this.editor.handleFiles(Array.from(a)), document.body.removeChild(i);
1527
+ }), document.body.appendChild(i), i.click();
1528
+ return;
1529
+ }
1530
+ if (["addRow", "deleteRow", "addColumn", "deleteColumn"].includes(e.command || "")) {
1531
+ const i = e.command;
1532
+ this.editor[i]();
1533
+ return;
1534
+ }
1535
+ if (e.command === "undo") {
1536
+ this.editor.undo();
1537
+ return;
1538
+ }
1539
+ if (e.command === "redo") {
1540
+ this.editor.redo();
1541
+ return;
1542
+ }
1543
+ if (e.command === "createLink") {
1544
+ this.activeModal && this.activeModal.close(), this.activeModal = new g(
1545
+ "Insert Link",
1546
+ [{ id: "url", label: "URL", type: "text", placeholder: "https://example.com" }],
1547
+ (i) => {
1548
+ if (this.savedRange) {
1549
+ const o = window.getSelection();
1550
+ o && (o.removeAllRanges(), o.addRange(this.savedRange));
1551
+ }
1552
+ this.editor.createLink(i.url), this.savedRange = null;
1553
+ },
1554
+ () => {
1555
+ this.activeModal = null, this.savedRange = null;
1556
+ },
1557
+ this.editor.getOptions().theme,
1558
+ this.editor.getOptions().dark
1559
+ ), this.activeModal.show(t);
1560
+ return;
1561
+ }
1562
+ if (e.command === "insertTable") {
1563
+ this.activeModal && this.activeModal.close(), this.activeModal = new g(
1564
+ "Insert Table",
1565
+ [
1566
+ { id: "rows", label: "Rows", type: "number", defaultValue: "3", min: "1" },
1567
+ { id: "cols", label: "Columns", type: "number", defaultValue: "3", min: "1" }
1568
+ ],
1569
+ (i) => {
1570
+ if (this.savedRange) {
1571
+ const a = window.getSelection();
1572
+ a && (a.removeAllRanges(), a.addRange(this.savedRange));
1573
+ }
1574
+ let o = parseInt(i.rows, 10), s = parseInt(i.cols, 10);
1575
+ (isNaN(o) || o < 1) && (o = 1), (isNaN(s) || s < 1) && (s = 1), this.editor.insertTable(o, s), this.savedRange = null;
1576
+ },
1577
+ () => {
1578
+ this.activeModal = null, this.savedRange = null;
1579
+ },
1580
+ this.editor.getOptions().theme,
1581
+ this.editor.getOptions().dark
1582
+ ), this.activeModal.show(t);
1583
+ return;
1584
+ }
1585
+ e.command && this.editor.execute(e.command, e.value || null), this.updateActiveStates();
1586
+ }), this.container.appendChild(t);
1587
+ }
1588
+ renderInput(e) {
1589
+ const t = document.createElement("input");
1590
+ t.type = "number", t.classList.add("te-input"), t.title = e.title, t.value = e.value || "", t.min = "1", t.max = "100";
1591
+ const n = () => {
1592
+ const o = window.getSelection();
1593
+ if (o && o.rangeCount > 0) {
1594
+ const s = o.getRangeAt(0);
1595
+ this.editor.el.contains(s.commonAncestorContainer) && (this.savedRange = s.cloneRange());
1596
+ }
1597
+ };
1598
+ t.addEventListener("mousedown", n), t.addEventListener("focus", n);
1599
+ const i = () => {
1600
+ let o = parseInt(t.value, 10);
1601
+ if (!isNaN(o) && (o = Math.max(1, Math.min(100, o)), t.value = o.toString(), e.command === "fontSize")) {
1602
+ if (this.savedRange) {
1603
+ const s = this.editor.setStyle("font-size", `${o}px`, this.savedRange);
1604
+ s && (this.savedRange = s);
1605
+ } else
1606
+ this.editor.setStyle("font-size", `${o}px`);
1607
+ t.focus();
1608
+ }
1609
+ };
1610
+ t.addEventListener("input", i), t.addEventListener("keydown", (o) => {
1611
+ o.key === "Enter" && (i(), this.editor.focus());
1612
+ }), this.container.appendChild(t);
1613
+ }
1614
+ renderSelect(e) {
1615
+ const t = document.createElement("select");
1616
+ t.classList.add("te-select"), t.title = e.title, e.options && e.options.forEach((n) => {
1617
+ const i = document.createElement("option");
1618
+ i.value = n.value, i.textContent = n.label, t.appendChild(i);
1619
+ }), t.addEventListener("change", () => {
1620
+ const n = t.value;
1621
+ this.savedRange && this.editor.selection.restoreSelection(this.savedRange), e.command === "formatBlock" ? this.editor.execute(e.command, n) : e.command === "fontFamily" ? this.editor.setStyle("font-family", n) : e.command === "lineHeight" && this.editor.setStyle("line-height", n), this.editor.focus();
1622
+ }), t.addEventListener("mousedown", () => {
1623
+ this.savedRange = this.editor.selection.saveSelection();
1624
+ }), this.container.appendChild(t);
1625
+ }
1626
+ renderColorPicker(e) {
1627
+ const t = document.createElement("div");
1628
+ if (t.classList.add("te-color-picker-wrapper"), t.title = e.title, e.icon) {
1629
+ const i = document.createElement("div");
1630
+ i.classList.add("te-button", "te-color-icon"), i.innerHTML = e.icon;
1631
+ const o = document.createElement("div");
1632
+ o.classList.add("te-color-indicator"), o.style.backgroundColor = e.value || "#000000", i.appendChild(o), t.appendChild(i);
1633
+ }
1634
+ const n = document.createElement("input");
1635
+ n.type = "color", n.classList.add("te-color-picker-input"), e.icon || (n.classList.add("te-color-picker"), n.title = e.title), n.value = e.value || "#000000", n.addEventListener("mousedown", () => {
1636
+ this.savedRange = this.editor.selection.saveSelection();
1637
+ }), n.addEventListener("input", () => {
1638
+ if (e.icon) {
1639
+ const i = t.querySelector(".te-color-indicator");
1640
+ i && (i.style.backgroundColor = n.value);
1641
+ }
1642
+ }), n.addEventListener("change", () => {
1643
+ if (this.savedRange && this.editor.selection.restoreSelection(this.savedRange), e.command === "foreColor") {
1644
+ const i = this.savedRange || void 0;
1645
+ this.editor.setStyle("color", n.value, i);
1646
+ } else if (e.command === "backColor") {
1647
+ const i = this.savedRange || void 0;
1648
+ this.editor.setStyle("background-color", n.value, i);
1649
+ } else e.command && this.editor.execute(e.command, n.value);
1650
+ this.editor.focus();
1651
+ }), t.appendChild(n), this.container.appendChild(e.icon ? t : n);
1652
+ }
1653
+ get el() {
1654
+ return this.container;
1655
+ }
1656
+ updateActiveStates() {
1657
+ const e = this.container.querySelectorAll(".te-button");
1658
+ let t = 0;
1659
+ this.items.forEach((n) => {
1660
+ if (n.type === "button") {
1661
+ const i = e[t++];
1662
+ if (!i) return;
1663
+ n.command && document.queryCommandState(n.command) ? i.classList.add("active") : i.classList.remove("active");
1664
+ }
1665
+ });
1666
+ }
1667
+ updateStatus(e, t = !1) {
1668
+ if (!this.statusEl || this.editor.getOptions().showStatus === !1) return;
1669
+ if (this.statusEl.textContent = "", t) {
1670
+ const i = document.createElement("div");
1671
+ i.classList.add("te-toolbar-loader"), this.statusEl.appendChild(i);
1672
+ }
1673
+ const n = document.createElement("span");
1674
+ n.textContent = e, this.statusEl.appendChild(n);
1675
+ }
1676
+ destroy() {
1677
+ 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);
1678
+ }
1679
+ }
1680
+ class Z extends K {
1681
+ toolbar;
1682
+ constructor(e, t = {}) {
1683
+ const n = {
1684
+ ...t,
1685
+ onSaving: () => {
1686
+ this.toolbar?.updateStatus("Auto saving...", !0), t.onSaving && t.onSaving();
1687
+ },
1688
+ onSave: (i) => {
1689
+ const o = (/* @__PURE__ */ new Date()).toLocaleString([], {
1690
+ year: "numeric",
1691
+ month: "short",
1692
+ day: "numeric",
1693
+ hour: "2-digit",
1694
+ minute: "2-digit",
1695
+ hour12: !0
1696
+ });
1697
+ this.toolbar?.updateStatus(`Saved at ${o}`, !1), t.onSave && t.onSave(i);
1698
+ }
1699
+ };
1700
+ if (super(e, n), typeof document > "u" || !e) {
1701
+ this.toolbar = {};
1702
+ return;
1703
+ }
1704
+ this.toolbar = new Y(this), this.container.insertBefore(this.toolbar.el, this.editableElement), n.showStatus !== !1 && this.toolbar && this.toolbar.updateStatus("All changes saved", !1), this.triggerChange();
1705
+ }
1706
+ getToolbar() {
1707
+ return this.toolbar;
1708
+ }
1709
+ destroy() {
1710
+ this.toolbar && this.toolbar.destroy(), super.destroy();
1711
+ }
1712
+ }
1713
+ export {
1714
+ K as CoreEditor,
1715
+ E as HistoryManager,
1716
+ Z as InkFlowEditor,
1717
+ b as SelectionManager,
1718
+ Y as Toolbar
1719
+ };