@meenainwal/rich-text-editor 1.1.1 → 1.2.1

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,1725 @@
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 s = document.createRange();
38
+ s.setStart(n, Math.min(t.startOffset, n.textContent?.length || 0)), s.setEnd(i, Math.min(t.endOffset, i.textContent?.length || 0)), this.restoreSelection(s);
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 s = Array.from(i.parentElement.childNodes).indexOf(i);
49
+ n.unshift(s), 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"), s = n?.src, o = this.editor.getOptions();
136
+ o.onImageDelete && o.onImageDelete(i || void 0, s), i && o.imageEndpoints?.delete && fetch(o.imageEndpoints.delete, {
137
+ method: "DELETE",
138
+ headers: { "Content-Type": "application/json" },
139
+ body: JSON.stringify({ id: i, url: s })
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 s = this.startWidth, o = this.startHeight;
164
+ this.currentHandle?.includes("right") ? s = this.startWidth + n : this.currentHandle?.includes("left") ? s = this.startWidth - n : this.currentHandle?.includes("bottom") ? s = this.startWidth + i * this.aspectRatio : this.currentHandle?.includes("top") && (s = this.startWidth - i * this.aspectRatio), o = s / this.aspectRatio, s > 50 && s < this.editor.el.clientWidth && (t.style.width = `${s}px`, t.style.height = `${o}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
+ }, L = {
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
+ }, k = {
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
+ }, B = {
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
+ }, O = {
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
+ }, q = {
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
+ }, W = {
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
+ { ...L, id: "heading" },
400
+ { ...k, 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
+ { ...B, id: "ordered-list" },
419
+ { ...O, id: "outdent" },
420
+ { ...U, id: "indent" },
421
+ m,
422
+ { ...F, id: "horizontal-rule" },
423
+ { ...W, id: "emoji" },
424
+ { ..._, id: "link" },
425
+ { ...$, id: "image" },
426
+ { ...V, id: "table" },
427
+ { ...q, 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, s, o) {
437
+ this.fields = t, this.onConfirm = n, this.onClose = i, this.theme = s, this.dark = o, 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 s = document.createElement("div");
445
+ s.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), s.appendChild(d);
452
+ }), n.appendChild(s);
453
+ const o = document.createElement("div");
454
+ o.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", o.appendChild(a), o.appendChild(r), n.appendChild(o), 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 s = {};
464
+ this.fields.forEach((o) => {
465
+ const a = this.container.querySelector(`#${o.id}`);
466
+ s[o.id] = a.value;
467
+ }), this.onConfirm(s), this.close();
468
+ });
469
+ const n = (s) => {
470
+ s.key === "Escape" && this.close(), s.key === "Enter" && t.click();
471
+ };
472
+ this.container.addEventListener("keydown", n);
473
+ const i = (s) => {
474
+ this.container.contains(s.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, s = t.left + window.scrollX;
482
+ s + n > window.innerWidth && (s = window.innerWidth - n - 20), this.container.style.top = `${i}px`, this.container.style.left = `${s}px`;
483
+ const o = this.container.querySelector("input");
484
+ o && o.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, s] of Object.entries(n)) {
511
+ const o = t[i];
512
+ o && e.style.setProperty(s, o);
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 s = document.createElement("button");
531
+ s.className = "te-floating-btn", s.title = i.title, s.innerHTML = i.icon || i.title, s.onclick = (o) => {
532
+ o.preventDefault(), o.stopPropagation(), this.handleCommand(i);
533
+ }, e.appendChild(s);
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), this.editor.el.addEventListener("scroll", () => {
562
+ this.isVisible && !this.activeModal && this.hide();
563
+ }, !0), window.addEventListener("mousedown", (t) => {
564
+ !this.container.contains(t.target) && !this.editor.el.contains(t.target) && this.hide();
565
+ }), window.addEventListener("resize", () => {
566
+ this.isVisible && this.updatePosition();
567
+ });
568
+ }
569
+ updatePosition() {
570
+ const e = window.getSelection();
571
+ if (!e || e.rangeCount === 0 || e.isCollapsed) {
572
+ this.activeModal || this.hide();
573
+ return;
574
+ }
575
+ const t = e.getRangeAt(0);
576
+ if (!this.editor.el.contains(t.commonAncestorContainer)) {
577
+ this.hide();
578
+ return;
579
+ }
580
+ const n = t.getBoundingClientRect(), s = (this.container.offsetParent || document.documentElement).getBoundingClientRect();
581
+ this.container.style.display = "flex", this.isVisible = !0;
582
+ const o = this.container.offsetWidth, a = this.container.offsetHeight;
583
+ let r = n.top - s.top - a - 10, l = n.left - s.left + n.width / 2 - o / 2;
584
+ n.top - a - 15 < 0 && (r = n.bottom - s.top + 10);
585
+ const d = 10 - s.left, c = window.innerWidth - 10 - s.left - o;
586
+ l < d && (l = d), l > c && (l = c), this.container.style.top = `${r}px`, this.container.style.left = `${l}px`, this.container.classList.add("te-floating-visible");
587
+ }
588
+ hide() {
589
+ this.container.style.display = "none", this.container.classList.remove("te-floating-visible"), this.isVisible = !1;
590
+ }
591
+ destroy() {
592
+ this.container.remove();
593
+ }
594
+ setDarkMode(e) {
595
+ e ? this.container.classList.add("te-dark") : this.container.classList.remove("te-dark");
596
+ }
597
+ }
598
+ class f {
599
+ /**
600
+ * Compresses an image file using HTML5 Canvas.
601
+ */
602
+ static async compressImage(e, t) {
603
+ return new Promise((n, i) => {
604
+ if (e.size <= t * 1024 * 1024 && e.type === "image/webp")
605
+ return n(e);
606
+ const s = new Image();
607
+ s.src = URL.createObjectURL(e), s.onload = () => {
608
+ const o = document.createElement("canvas");
609
+ let a = s.width, r = s.height;
610
+ const l = 2e3;
611
+ (a > l || r > l) && (a > r ? (r = Math.round(r * l / a), a = l) : (a = Math.round(a * l / r), r = l)), o.width = a, o.height = r;
612
+ const d = o.getContext("2d");
613
+ if (!d)
614
+ return i(new Error("Failed to get canvas context"));
615
+ d.drawImage(s, 0, 0, a, r), o.toBlob(
616
+ (c) => {
617
+ c ? n(c) : i(new Error("Canvas toBlob failed"));
618
+ },
619
+ "image/webp",
620
+ 0.8
621
+ // 80% quality is professional standard
622
+ ), URL.revokeObjectURL(s.src);
623
+ }, s.onerror = () => {
624
+ URL.revokeObjectURL(s.src), i(new Error("Failed to load image for compression"));
625
+ };
626
+ });
627
+ }
628
+ /**
629
+ * Uploads a file based on editor configuration.
630
+ */
631
+ static async uploadFile(e, t) {
632
+ const n = e instanceof File ? e.name : "upload.webp";
633
+ if (t.imageEndpoints?.upload) {
634
+ const i = new FormData();
635
+ i.append("file", e, n);
636
+ try {
637
+ const s = await fetch(t.imageEndpoints.upload, {
638
+ method: "POST",
639
+ body: i
640
+ });
641
+ if (s.ok) {
642
+ const o = await s.json();
643
+ return {
644
+ imageUrl: o.imageUrl,
645
+ imageId: o.imageId
646
+ };
647
+ }
648
+ console.warn("Custom upload endpoint returned an error, falling back.");
649
+ } catch (s) {
650
+ console.error("Custom upload failed:", s);
651
+ }
652
+ }
653
+ if (t.cloudinaryFallback) {
654
+ const { cloudName: i, uploadPreset: s } = t.cloudinaryFallback, o = `https://api.cloudinary.com/v1_1/${i}/image/upload`, a = new FormData();
655
+ a.append("file", e, n), a.append("upload_preset", s);
656
+ try {
657
+ const r = await fetch(o, {
658
+ method: "POST",
659
+ body: a
660
+ });
661
+ if (r.ok)
662
+ return {
663
+ imageUrl: (await r.json()).secure_url
664
+ };
665
+ console.warn("Cloudinary upload failed, falling back.");
666
+ } catch (r) {
667
+ console.error("Cloudinary fallback failed:", r);
668
+ }
669
+ }
670
+ return null;
671
+ }
672
+ }
673
+ class K {
674
+ container;
675
+ editableElement;
676
+ selection;
677
+ imageManager;
678
+ history;
679
+ options;
680
+ saveTimeout = null;
681
+ historyTimeout = null;
682
+ pendingStyles = {};
683
+ observer = null;
684
+ floatingToolbar = null;
685
+ eventListeners = [];
686
+ loaderElement = null;
687
+ isUndoingRedoing = !1;
688
+ constructor(e, t = {}) {
689
+ if (this.options = t, this.container = e, typeof document > "u" || !e) {
690
+ this.editableElement = {}, this.selection = {}, this.imageManager = {}, this.history = {};
691
+ return;
692
+ }
693
+ 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);
694
+ }
695
+ /**
696
+ * Applies custom theme variables to the editor container.
697
+ */
698
+ applyTheme(e) {
699
+ const t = this.container, n = {
700
+ primaryColor: "--te-primary-color",
701
+ primaryHover: "--te-primary-hover",
702
+ bgApp: "--te-bg-app",
703
+ bgEditor: "--te-bg-editor",
704
+ toolbarBg: "--te-toolbar-bg",
705
+ borderColor: "--te-border-color",
706
+ borderFocus: "--te-border-focus",
707
+ textMain: "--te-text-main",
708
+ textMuted: "--te-text-muted",
709
+ placeholder: "--te-placeholder",
710
+ btnHover: "--te-btn-hover",
711
+ btnActive: "--te-btn-active",
712
+ radiusLg: "--te-radius-lg",
713
+ radiusMd: "--te-radius-md",
714
+ radiusSm: "--te-radius-sm",
715
+ shadowSm: "--te-shadow-sm",
716
+ shadowMd: "--te-shadow-md",
717
+ shadowLg: "--te-shadow-lg"
718
+ };
719
+ for (const [i, s] of Object.entries(n)) {
720
+ const o = e[i];
721
+ o && t.style.setProperty(s, o);
722
+ }
723
+ }
724
+ /**
725
+ * Toggles dark mode on the editor.
726
+ */
727
+ setDarkMode(e) {
728
+ 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));
729
+ }
730
+ /**
731
+ * Destroys the editor instance and cleans up.
732
+ */
733
+ destroy() {
734
+ 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 }) => {
735
+ e.removeEventListener(t, n);
736
+ }), 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);
737
+ }
738
+ checkPlaceholder() {
739
+ if (!this.editableElement) return;
740
+ if (this.editableElement.textContent?.trim() === "" && !this.editableElement.querySelector("img") && !this.editableElement.querySelector("table") && !this.editableElement.querySelector("ul") && !this.editableElement.querySelector("ol") && !this.editableElement.querySelector("hr") && !this.editableElement.querySelector("figure") && !this.editableElement.querySelector("blockquote") && !this.editableElement.querySelector("pre")) {
741
+ this.editableElement.classList.add("is-empty");
742
+ const t = this.editableElement.firstElementChild;
743
+ t && (this.editableElement.style.textAlign = t.style.textAlign);
744
+ } else
745
+ this.editableElement.classList.remove("is-empty"), this.editableElement.style.textAlign = "";
746
+ }
747
+ addEventListener(e, t, n, i) {
748
+ e.addEventListener(t, n, i), this.eventListeners.push({ target: e, type: t, handler: n });
749
+ }
750
+ setupImageObserver() {
751
+ this.observer = new MutationObserver((e) => {
752
+ e.forEach((t) => {
753
+ t.addedNodes.forEach((n) => {
754
+ if (n.nodeType === Node.ELEMENT_NODE) {
755
+ const i = n;
756
+ i.tagName === "IMG" && !i.closest(".te-image-container") ? this.wrapImage(i) : i.querySelectorAll("img:not(.te-image)").forEach((o) => {
757
+ o.closest(".te-image-container") || this.wrapImage(o);
758
+ });
759
+ }
760
+ });
761
+ });
762
+ }), this.observer.observe(this.editableElement, {
763
+ childList: !0,
764
+ subtree: !0
765
+ });
766
+ }
767
+ /**
768
+ * Wraps a raw <img> element in the interactive container
769
+ */
770
+ wrapImage(e) {
771
+ const t = e.parentElement;
772
+ if (!t) return;
773
+ const n = document.createElement("figure");
774
+ n.classList.add("te-image-container"), n.setAttribute("contenteditable", "false");
775
+ const i = document.createElement("img");
776
+ 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");
777
+ const s = document.createElement("figcaption");
778
+ 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) => {
779
+ const r = document.createElement("div");
780
+ r.classList.add("te-image-resizer", `te-resizer-${a}`), n.appendChild(r);
781
+ }), n.appendChild(i), n.appendChild(s), t.replaceChild(n, e), !n.nextElementSibling) {
782
+ const a = document.createElement("p");
783
+ a.innerHTML = "<br>", n.after(a);
784
+ }
785
+ }
786
+ setupInputHandlers() {
787
+ this.addEventListener(this.editableElement, "beforeinput", (e) => {
788
+ if (e.inputType === "insertText" && Object.keys(this.pendingStyles).length > 0) {
789
+ const t = e.data;
790
+ if (!t) return;
791
+ e.preventDefault();
792
+ const n = document.createElement("span");
793
+ for (const [s, o] of Object.entries(this.pendingStyles))
794
+ n.style.setProperty(s, o);
795
+ n.textContent = t;
796
+ const i = this.selection.getRange();
797
+ if (i) {
798
+ i.deleteContents(), i.insertNode(n);
799
+ const s = document.createRange();
800
+ s.setStart(n.firstChild, t.length), s.setEnd(n.firstChild, t.length), this.selection.restoreSelection(s), this.pendingStyles = {}, this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
801
+ }
802
+ }
803
+ }), this.addEventListener(this.editableElement, "input", () => {
804
+ this.checkPlaceholder();
805
+ }), this.addEventListener(document, "selectionchange", () => {
806
+ const e = window.getSelection();
807
+ e && e.rangeCount > 0 && (e.getRangeAt(0).collapsed || (this.pendingStyles = {}));
808
+ }), this.addEventListener(this.editableElement, "dragover", (e) => {
809
+ e.preventDefault(), e.dataTransfer.dropEffect = "copy", this.editableElement.classList.add("dragover");
810
+ }), this.addEventListener(this.editableElement, "dragleave", () => {
811
+ this.editableElement.classList.remove("dragover");
812
+ }), this.addEventListener(this.editableElement, "drop", (e) => {
813
+ e.preventDefault(), this.editableElement.classList.remove("dragover");
814
+ const t = e.dataTransfer?.files;
815
+ t && t.length > 0 && this.handleFiles(Array.from(t));
816
+ }), this.addEventListener(this.editableElement, "paste", this.handlePaste.bind(this)), this.addEventListener(this.editableElement, "input", () => {
817
+ this.handleInput();
818
+ }), this.addEventListener(this.editableElement, "keydown", (e) => {
819
+ (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());
820
+ });
821
+ }
822
+ /**
823
+ * Immediately records a history state if one is pending.
824
+ */
825
+ flushHistoryRecord() {
826
+ if (this.historyTimeout) {
827
+ clearTimeout(this.historyTimeout), this.historyTimeout = null;
828
+ const e = this.editableElement.innerHTML, t = this.selection.getSelectionPath(this.editableElement);
829
+ this.history.record(e, t);
830
+ }
831
+ }
832
+ handleInput() {
833
+ this.isUndoingRedoing || (this.scheduleHistoryRecord(), this.options.autoSave && (this.options.onSaving && this.options.onSaving(), this.scheduleAutoSave()), this.options.onChange && this.options.onChange(this.getHTML()));
834
+ }
835
+ scheduleHistoryRecord() {
836
+ this.historyTimeout && clearTimeout(this.historyTimeout), this.historyTimeout = setTimeout(() => {
837
+ const e = this.editableElement.innerHTML, t = this.selection.getSelectionPath(this.editableElement);
838
+ this.history.record(e, t);
839
+ }, 200);
840
+ }
841
+ scheduleAutoSave() {
842
+ this.saveTimeout && clearTimeout(this.saveTimeout);
843
+ const e = this.options.autoSaveInterval || 300;
844
+ this.saveTimeout = setTimeout(() => {
845
+ this.save();
846
+ }, e);
847
+ }
848
+ save() {
849
+ this.options.onSave && this.options.onSave(this.getHTML());
850
+ }
851
+ undo() {
852
+ this.flushHistoryRecord();
853
+ const e = this.history.undo();
854
+ e !== null && (this.isUndoingRedoing = !0, this.editableElement.innerHTML = e.html, e.selection && this.selection.restoreSelectionPath(this.editableElement, e.selection), this.triggerChange(), this.isUndoingRedoing = !1);
855
+ }
856
+ redo() {
857
+ this.flushHistoryRecord();
858
+ const e = this.history.redo();
859
+ e !== null && (this.isUndoingRedoing = !0, this.editableElement.innerHTML = e.html, e.selection && this.selection.restoreSelectionPath(this.editableElement, e.selection), this.triggerChange(), this.isUndoingRedoing = !1);
860
+ }
861
+ triggerChange() {
862
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
863
+ }
864
+ createLoader() {
865
+ this.loaderElement = document.createElement("div"), this.loaderElement.className = "te-loader-overlay";
866
+ const e = document.createElement("div");
867
+ e.className = "te-loader-spinner";
868
+ const t = document.createElement("div");
869
+ t.className = "te-loader-shimmer";
870
+ const n = document.createElement("div");
871
+ 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);
872
+ }
873
+ hideLoader() {
874
+ this.loaderElement && (this.loaderElement.classList.add("hidden"), setTimeout(() => {
875
+ this.loaderElement && this.loaderElement.parentNode && this.loaderElement.parentNode.removeChild(this.loaderElement), this.loaderElement = null;
876
+ }, 400));
877
+ }
878
+ createEditableElement() {
879
+ const e = document.createElement("div");
880
+ 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;
881
+ }
882
+ /**
883
+ * Focuses the editor.
884
+ */
885
+ focus() {
886
+ this.editableElement.focus();
887
+ }
888
+ /**
889
+ * Executes a command on the current selection.
890
+ */
891
+ execute(e, t = null) {
892
+ this.focus(), document.execCommand(e, !1, t ?? void 0), e === "removeFormat" && (document.execCommand("formatBlock", !1, "p"), this.pendingStyles = {}), this.normalize(), this.triggerChange();
893
+ }
894
+ /**
895
+ * Special handler for links to open them in a new tab when clicked.
896
+ */
897
+ setupLinkClickHandlers() {
898
+ this.addEventListener(this.editableElement, "click", (e) => {
899
+ const n = e.target.closest("a");
900
+ if (n && this.editableElement.contains(n)) {
901
+ e.preventDefault();
902
+ const i = n.getAttribute("href");
903
+ i && window.open(i, "_blank", "noopener,noreferrer");
904
+ }
905
+ });
906
+ }
907
+ /**
908
+ * Inserts a table at the current selection.
909
+ */
910
+ insertTable(e = 3, t = 3) {
911
+ this.focus();
912
+ const n = this.selection.getRange();
913
+ if (!n) return;
914
+ const i = document.createElement("table");
915
+ i.classList.add("te-table");
916
+ for (let o = 0; o < e; o++) {
917
+ const a = document.createElement("tr");
918
+ for (let r = 0; r < t; r++) {
919
+ const l = document.createElement("td");
920
+ l.innerHTML = "<br>", a.appendChild(l);
921
+ }
922
+ i.appendChild(a);
923
+ }
924
+ n.deleteContents(), n.insertNode(i);
925
+ const s = i.nextElementSibling;
926
+ if (!s || s.tagName !== "P") {
927
+ const o = document.createElement("p");
928
+ o.innerHTML = "<br>", i.after(o), s && s.tagName === "BR" && s.remove();
929
+ }
930
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
931
+ }
932
+ /**
933
+ * Adds a row to the currently selected table.
934
+ */
935
+ addRow() {
936
+ const e = this.getSelectedTable();
937
+ if (!e) return;
938
+ const t = document.createElement("tr");
939
+ t.style.borderBottom = "1px solid var(--te-border-color)";
940
+ const n = e.rows[0].cells.length;
941
+ for (let s = 0; s < n; s++) {
942
+ const o = document.createElement("td");
943
+ o.innerHTML = "<br>", t.appendChild(o);
944
+ }
945
+ const i = this.getSelectedTd();
946
+ i ? i.parentElement?.after(t) : e.appendChild(t), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
947
+ }
948
+ /**
949
+ * Deletes the currently selected row.
950
+ */
951
+ deleteRow() {
952
+ const e = this.getSelectedTd();
953
+ if (e && e.parentElement) {
954
+ const t = e.parentElement, n = t.parentElement;
955
+ if (n.rows.length > 1) {
956
+ const i = t.rowIndex, s = n.rows[i + 1] || n.rows[i - 1], o = e.cellIndex;
957
+ if (t.remove(), s && s.cells[o]) {
958
+ const a = document.createRange();
959
+ a.selectNodeContents(s.cells[o]), a.collapse(!0), this.selection.restoreSelection(a);
960
+ }
961
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
962
+ }
963
+ }
964
+ }
965
+ /**
966
+ * Adds a column to the currently selected table.
967
+ */
968
+ addColumn() {
969
+ const e = this.getSelectedTable();
970
+ if (!e) return;
971
+ const t = this.getSelectedTd(), n = t ? t.cellIndex : -1;
972
+ for (let i = 0; i < e.rows.length; i++) {
973
+ const s = e.rows[i], o = document.createElement("td");
974
+ o.innerHTML = "<br>", n !== -1 ? s.cells[n].after(o) : s.appendChild(o);
975
+ }
976
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
977
+ }
978
+ /**
979
+ * Deletes the currently selected column.
980
+ */
981
+ deleteColumn() {
982
+ const e = this.getSelectedTd();
983
+ if (!e) return;
984
+ const t = this.getSelectedTable();
985
+ if (!t) return;
986
+ const n = e.cellIndex;
987
+ if (t.rows[0].cells.length > 1) {
988
+ const i = e.nextElementSibling || e.previousElementSibling;
989
+ for (let s = 0; s < t.rows.length; s++)
990
+ t.rows[s].cells[n].remove();
991
+ if (i) {
992
+ const s = document.createRange();
993
+ s.selectNodeContents(i), s.collapse(!0), this.selection.restoreSelection(s);
994
+ }
995
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
996
+ }
997
+ }
998
+ getSelectedTd() {
999
+ const e = window.getSelection();
1000
+ if (!e || e.rangeCount === 0) return null;
1001
+ let t = e.anchorNode;
1002
+ for (; t && t !== this.editableElement; ) {
1003
+ if (t.nodeName === "TD") return t;
1004
+ t = t.parentNode;
1005
+ }
1006
+ return null;
1007
+ }
1008
+ getSelectedTable() {
1009
+ const e = this.getSelectedTd();
1010
+ return e ? e.closest("table") : null;
1011
+ }
1012
+ /**
1013
+ * Recursively removes a style property from all elements in a fragment.
1014
+ */
1015
+ clearStyleRecursive(e, t) {
1016
+ const n = document.createTreeWalker(e, NodeFilter.SHOW_ELEMENT);
1017
+ let i = n.nextNode();
1018
+ for (; i; )
1019
+ i.style.getPropertyValue(t) && i.style.removeProperty(t), i = n.nextNode();
1020
+ }
1021
+ /**
1022
+ * Applies an inline style to the selection.
1023
+ * This is used for properties like font-size (px) and font-family
1024
+ * where execCommand is outdated or limited.
1025
+ */
1026
+ /**
1027
+ * Applies an inline style to the selection.
1028
+ * This is used for properties like font-size (px) and font-family
1029
+ * where execCommand is outdated or limited.
1030
+ */
1031
+ setStyle(e, t, n) {
1032
+ if (!n) {
1033
+ const a = window.getSelection();
1034
+ if (!a || a.rangeCount === 0) return null;
1035
+ n = a.getRangeAt(0);
1036
+ }
1037
+ if (n.collapsed)
1038
+ return this.pendingStyles[e] = t, n;
1039
+ if (["line-height"].includes(e))
1040
+ return this.setBlockStyle(e, t, n);
1041
+ let s = n.commonAncestorContainer;
1042
+ s.nodeType === Node.TEXT_NODE && (s = s.parentElement);
1043
+ let o = null;
1044
+ if (s.tagName === "SPAN" && s.children.length === 0 && s.textContent === n.toString())
1045
+ s.style.setProperty(e, t), o = n.cloneRange();
1046
+ else {
1047
+ const a = document.createElement("span");
1048
+ a.style.setProperty(e, t);
1049
+ try {
1050
+ const r = s.tagName === "SPAN" ? s : null, l = n.extractContents();
1051
+ this.clearStyleRecursive(l, e), a.appendChild(l), n.insertNode(a), r && r.innerHTML === "" && r.remove();
1052
+ const d = document.createRange();
1053
+ d.selectNodeContents(a), o = d;
1054
+ const c = window.getSelection();
1055
+ c && c.rangeCount > 0 && (c.removeAllRanges(), c.addRange(d));
1056
+ } catch (r) {
1057
+ console.warn("Failed to apply style:", r);
1058
+ }
1059
+ }
1060
+ return this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 })), o;
1061
+ }
1062
+ /**
1063
+ * Applies a style to the block-level containers within the range.
1064
+ */
1065
+ setBlockStyle(e, t, n) {
1066
+ const i = ["P", "H1", "H2", "H3", "H4", "H5", "H6", "LI", "TD", "TH", "DIV", "BLOCKQUOTE"], s = /* @__PURE__ */ new Set();
1067
+ if (Array.from(this.editableElement.querySelectorAll(i.join(","))).forEach((a) => {
1068
+ n.intersectsNode(a) && s.add(a);
1069
+ }), s.size === 0) {
1070
+ let a = n.commonAncestorContainer;
1071
+ for (; a && a !== this.editableElement.parentElement; ) {
1072
+ if (a.nodeType === Node.ELEMENT_NODE && i.includes(a.tagName)) {
1073
+ s.add(a);
1074
+ break;
1075
+ }
1076
+ a = a.parentNode;
1077
+ }
1078
+ }
1079
+ return s.forEach((a) => {
1080
+ a.style.setProperty(e, t);
1081
+ }), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 })), n;
1082
+ }
1083
+ /**
1084
+ * Creates a link at the current selection.
1085
+ * Ensures the link opens in a new tab with proper security attributes.
1086
+ */
1087
+ createLink(e) {
1088
+ if (this.focus(), e = e.trim(), /^(javascript|vbscript|data|file):/i.test(e)) {
1089
+ console.warn("Security Warning: Blocked malicious URI scheme.");
1090
+ return;
1091
+ }
1092
+ const n = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(e);
1093
+ !/^https?:\/\//i.test(e) && !/^mailto:/i.test(e) && !e.startsWith("#") && (n ? e = "mailto:" + e : e = "https://" + e);
1094
+ const i = window.getSelection();
1095
+ if (i && i.rangeCount > 0) {
1096
+ const s = i.getRangeAt(0);
1097
+ if (s.collapsed) {
1098
+ const o = document.createTextNode(e);
1099
+ s.insertNode(o);
1100
+ const a = document.createRange();
1101
+ a.selectNodeContents(o), i.removeAllRanges(), i.addRange(a);
1102
+ }
1103
+ }
1104
+ if (document.execCommand("createLink", !1, e), i && i.rangeCount > 0) {
1105
+ let o = i.getRangeAt(0).commonAncestorContainer;
1106
+ o.nodeType === Node.TEXT_NODE && (o = o.parentElement);
1107
+ let a = null;
1108
+ o.tagName === "A" ? a = o : a = o.querySelector("a"), a && (a.setAttribute("target", "_blank"), a.setAttribute("rel", "noopener noreferrer"));
1109
+ }
1110
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
1111
+ }
1112
+ /**
1113
+ * Inserts an image at the current selection.
1114
+ */
1115
+ insertImage(e, t, n = !1) {
1116
+ this.focus();
1117
+ const i = this.selection.getRange();
1118
+ if (!i) return null;
1119
+ const s = document.createElement("figure");
1120
+ s.classList.add("te-image-container"), s.setAttribute("contenteditable", "false"), n && s.classList.add("is-loading");
1121
+ const o = document.createElement("img");
1122
+ o.src = e, o.classList.add("te-image"), t && o.setAttribute("data-image-id", t);
1123
+ const a = document.createElement("figcaption");
1124
+ 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) => {
1125
+ const h = document.createElement("div");
1126
+ h.classList.add("te-image-resizer", `te-resizer-${c}`), s.appendChild(h);
1127
+ }), s.appendChild(o), s.appendChild(a), i.deleteContents(), i.insertNode(s);
1128
+ const l = document.createElement("p");
1129
+ l.innerHTML = "<br>", s.after(l);
1130
+ const d = document.createRange();
1131
+ return d.setStart(l, 0), d.setEnd(l, 0), this.selection.restoreSelection(d), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 })), this.save(), s;
1132
+ }
1133
+ /**
1134
+ * Returns the clean and optimized HTML content of the editor.
1135
+ */
1136
+ getHTML() {
1137
+ return this.normalizeHTML(this.editableElement.innerHTML);
1138
+ }
1139
+ /**
1140
+ * Normalizes the editor's content in-place.
1141
+ */
1142
+ normalize() {
1143
+ const e = this.editableElement.innerHTML, t = this.selection.getRange();
1144
+ let n = null, i = null;
1145
+ if (t && this.editableElement.contains(t.commonAncestorContainer)) {
1146
+ 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";
1147
+ const o = t.cloneRange();
1148
+ o.collapse(!0), o.insertNode(n);
1149
+ const a = t.cloneRange();
1150
+ a.collapse(!1), a.insertNode(i);
1151
+ }
1152
+ const s = this.normalizeHTML(this.editableElement.innerHTML);
1153
+ if (s !== e || n) {
1154
+ this.editableElement.innerHTML = s;
1155
+ const o = this.editableElement.querySelector("#te-selection-start"), a = this.editableElement.querySelector("#te-selection-end");
1156
+ if (o && a) {
1157
+ const r = document.createRange();
1158
+ r.setStartAfter(o), r.setEndBefore(a), this.selection.restoreSelection(r);
1159
+ }
1160
+ this.editableElement.querySelectorAll("#te-selection-start, #te-selection-end").forEach((r) => r.remove());
1161
+ }
1162
+ this.checkPlaceholder();
1163
+ }
1164
+ normalizationContainer = null;
1165
+ /**
1166
+ * Internal helper to strictly sanitize HTML strings.
1167
+ */
1168
+ sanitize(e) {
1169
+ p.addHook("afterSanitizeAttributes", (n) => {
1170
+ n.tagName === "A" && (n.setAttribute("target", "_blank"), n.setAttribute("rel", "noopener noreferrer"));
1171
+ });
1172
+ const t = p.sanitize(e, {
1173
+ ALLOWED_TAGS: [
1174
+ "b",
1175
+ "i",
1176
+ "u",
1177
+ "s",
1178
+ "span",
1179
+ "div",
1180
+ "p",
1181
+ "br",
1182
+ "a",
1183
+ "h1",
1184
+ "h2",
1185
+ "h3",
1186
+ "h4",
1187
+ "h5",
1188
+ "h6",
1189
+ "ul",
1190
+ "ol",
1191
+ "li",
1192
+ "blockquote",
1193
+ "hr",
1194
+ "img",
1195
+ "table",
1196
+ "tbody",
1197
+ "tr",
1198
+ "td",
1199
+ "th",
1200
+ "thead",
1201
+ "tfoot",
1202
+ "figure",
1203
+ "figcaption"
1204
+ ],
1205
+ ALLOWED_ATTR: [
1206
+ "href",
1207
+ "src",
1208
+ "alt",
1209
+ "style",
1210
+ "color",
1211
+ "background-color",
1212
+ "class",
1213
+ "id",
1214
+ "target",
1215
+ "rel",
1216
+ "contenteditable",
1217
+ "data-placeholder",
1218
+ "data-image-id"
1219
+ ],
1220
+ ALLOW_DATA_ATTR: !0,
1221
+ FORBID_TAGS: ["script", "style", "iframe", "object", "embed", "form", "textarea"],
1222
+ FORBID_ATTR: ["onerror", "onload", "onclick", "onmouseover"]
1223
+ });
1224
+ return p.removeHook("afterSanitizeAttributes"), t;
1225
+ }
1226
+ /**
1227
+ * Optimizes HTML by fixing invalid nesting and removing redundant tags.
1228
+ */
1229
+ normalizeHTML(e) {
1230
+ this.normalizationContainer || (this.normalizationContainer = document.createElement("div"));
1231
+ const t = this.normalizationContainer;
1232
+ t.innerHTML = e;
1233
+ const n = Array.from(t.childNodes);
1234
+ let i = null;
1235
+ n.forEach((a) => {
1236
+ 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)) {
1237
+ if (a.nodeType === Node.TEXT_NODE && (a.textContent || "").trim() === "" && !i)
1238
+ return;
1239
+ i || (i = document.createElement("p"), a.before(i)), i.appendChild(a);
1240
+ } else
1241
+ i = null;
1242
+ }), t.querySelectorAll("p").forEach((a) => {
1243
+ const r = a.querySelectorAll("ul, ol");
1244
+ r.length > 0 && (r.forEach((l) => {
1245
+ a.after(l);
1246
+ }), (a.innerHTML.trim() === "" || a.innerHTML.trim() === "<br>") && a.remove());
1247
+ }), t.querySelectorAll("span").forEach((a) => {
1248
+ if (!a.id.startsWith("te-selection-"))
1249
+ if (a.attributes.length === 0) {
1250
+ const r = document.createTextNode(a.textContent || "");
1251
+ a.replaceWith(r);
1252
+ } else a.innerHTML.trim() === "" && a.remove();
1253
+ });
1254
+ const o = Array.from(t.querySelectorAll("p"));
1255
+ o.forEach((a) => {
1256
+ a.innerHTML.trim() === "" && t.childNodes.length > 1 && a !== t.lastElementChild && a.remove();
1257
+ });
1258
+ for (let a = o.length - 1; a >= 0; a--) {
1259
+ const r = o[a], l = r.innerHTML.trim() === "" || r.innerHTML.trim() === "<br>", d = r === t.lastElementChild;
1260
+ if (l && d && t.children.length > 1)
1261
+ r.remove();
1262
+ else
1263
+ break;
1264
+ }
1265
+ return t.innerHTML.trim() === "<p><br></p>" || t.innerHTML.trim() === "<p></p>" ? "" : this.sanitize(t.innerHTML);
1266
+ }
1267
+ // Handle paste events to sanitize inherited malware and styles
1268
+ handlePaste(e) {
1269
+ e.preventDefault();
1270
+ let t = (e.clipboardData || window.clipboardData).getData("text/plain"), n = (e.clipboardData || window.clipboardData).getData("text/html");
1271
+ if (e.clipboardData && e.clipboardData.items) {
1272
+ const s = [];
1273
+ for (let o = 0; o < e.clipboardData.items.length; o++) {
1274
+ const a = e.clipboardData.items[o];
1275
+ if (a.type.startsWith("image/")) {
1276
+ const r = a.getAsFile();
1277
+ r && s.push(r);
1278
+ }
1279
+ }
1280
+ if (s.length > 0) {
1281
+ this.handleFiles(s);
1282
+ return;
1283
+ }
1284
+ }
1285
+ const i = /<([a-z1-6]+)\b[^>]*>[\s\S]*<\/\1>/i.test(t) || /^\s*<[a-z1-6]+\b[^>]*>/i.test(t);
1286
+ if (!n && t && i && (n = t.replace(/(\r\n|\n|\r)/gm, " ").replace(/>\s+</g, "><").trim()), n) {
1287
+ const s = this.sanitize(n);
1288
+ this.execute("insertHTML", s);
1289
+ } else
1290
+ this.execute("insertText", t);
1291
+ }
1292
+ /**
1293
+ * Sets the HTML content of the editor.
1294
+ */
1295
+ setHTML(e) {
1296
+ const t = this.sanitize(e);
1297
+ this.editableElement.innerHTML = t;
1298
+ }
1299
+ /**
1300
+ * Internal access to the editable element.
1301
+ */
1302
+ get el() {
1303
+ return this.editableElement;
1304
+ }
1305
+ /**
1306
+ * Returns the editor options.
1307
+ */
1308
+ getOptions() {
1309
+ return this.options;
1310
+ }
1311
+ /**
1312
+ * Internal helper to handle multiple files.
1313
+ */
1314
+ async handleFiles(e) {
1315
+ const t = this.options.maxImageSizeMB || 5;
1316
+ for (const n of e) {
1317
+ if (!n.type.startsWith("image/")) continue;
1318
+ let i = null;
1319
+ try {
1320
+ if (n.size > t * 1024 * 1024 * 3) {
1321
+ console.warn(`File ${n.name} is too large to even attempt processing.`);
1322
+ continue;
1323
+ }
1324
+ this.options.onSaving && this.options.onSaving();
1325
+ const s = URL.createObjectURL(n);
1326
+ i = this.insertImage(s, void 0, !0);
1327
+ const o = await f.compressImage(n, t), a = URL.createObjectURL(o);
1328
+ if (i) {
1329
+ const l = i.querySelector("img");
1330
+ l && (l.src = a);
1331
+ }
1332
+ if (o.size > t * 1024 * 1024) {
1333
+ alert(`Image "${n.name}" exceeds the ${t}MB limit even after compression.`), i?.remove();
1334
+ continue;
1335
+ }
1336
+ const r = await f.uploadFile(o, this.options);
1337
+ if (r)
1338
+ if (i) {
1339
+ const l = i.querySelector("img");
1340
+ l && (l.src = r.imageUrl, r.imageId && l.setAttribute("data-image-id", r.imageId)), i.classList.remove("is-loading");
1341
+ } else
1342
+ this.insertImage(r.imageUrl, r.imageId);
1343
+ else {
1344
+ const l = new FileReader();
1345
+ l.onload = (d) => {
1346
+ const c = d.target?.result;
1347
+ if (i) {
1348
+ const h = i.querySelector("img");
1349
+ h && (h.src = c), i.classList.remove("is-loading");
1350
+ } else
1351
+ this.insertImage(c);
1352
+ }, l.readAsDataURL(o);
1353
+ }
1354
+ } catch (s) {
1355
+ console.error("Image handling failed", s), i?.remove();
1356
+ } finally {
1357
+ this.options.onSave && this.save();
1358
+ }
1359
+ }
1360
+ }
1361
+ }
1362
+ class X {
1363
+ container;
1364
+ searchInput;
1365
+ emojiGrid;
1366
+ onSelect;
1367
+ onClose;
1368
+ emojiList = [];
1369
+ theme;
1370
+ dark;
1371
+ constructor(e, t, n, i) {
1372
+ 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();
1373
+ }
1374
+ async loadEmojis() {
1375
+ this.emojiGrid.textContent = "Loading...";
1376
+ try {
1377
+ const { EMOJI_LIST: e } = await import("./EmojiList-B-C3-zN2.js");
1378
+ this.emojiList = e, this.renderEmojis(this.emojiList);
1379
+ } catch (e) {
1380
+ console.error("Failed to load emojis:", e), this.emojiGrid.textContent = "Failed to load";
1381
+ }
1382
+ }
1383
+ createPickerElement() {
1384
+ const e = document.createElement("div");
1385
+ return e.classList.add("te-emoji-picker"), this.theme && this.applyTheme(e, this.theme), this.dark && e.classList.add("te-dark"), e.innerHTML = `
1386
+ <div class="te-emoji-header">
1387
+ <input type="text" class="te-emoji-search" placeholder="Search emoji...">
1388
+ </div>
1389
+ <div class="te-emoji-body">
1390
+ <div class="te-emoji-grid"></div>
1391
+ </div>
1392
+ `, e;
1393
+ }
1394
+ setupEvents() {
1395
+ this.searchInput.addEventListener("mousedown", (t) => t.stopPropagation()), this.searchInput.addEventListener("click", (t) => t.stopPropagation()), this.searchInput.addEventListener("input", () => {
1396
+ const t = this.searchInput.value.toLowerCase(), n = this.emojiList.filter(
1397
+ (i) => i.name.toLowerCase().includes(t) || i.category.toLowerCase().includes(t)
1398
+ );
1399
+ this.renderEmojis(n);
1400
+ });
1401
+ const e = (t) => {
1402
+ this.container.contains(t.target) || (this.close(), document.removeEventListener("mousedown", e));
1403
+ };
1404
+ setTimeout(() => document.addEventListener("mousedown", e), 0);
1405
+ }
1406
+ renderEmojis(e) {
1407
+ if (this.emojiGrid.innerHTML = "", e.length === 0) {
1408
+ this.emojiGrid.textContent = "No emoji found";
1409
+ return;
1410
+ }
1411
+ this.searchInput.value.length > 0 ? this.renderGridItems(e) : ["Smileys", "Symbols", "Hands", "Animals", "Food", "Travel", "Objects", "Activities"].forEach((i) => {
1412
+ const s = e.filter((o) => o.category === i);
1413
+ if (s.length > 0) {
1414
+ const o = document.createElement("div");
1415
+ o.classList.add("te-emoji-category-title"), o.textContent = i, this.emojiGrid.appendChild(o), this.renderGridItems(s);
1416
+ }
1417
+ });
1418
+ }
1419
+ renderGridItems(e) {
1420
+ e.forEach((t) => {
1421
+ const n = document.createElement("button");
1422
+ n.type = "button", n.classList.add("te-emoji-item"), n.textContent = t.emoji, n.title = t.name, n.addEventListener("click", () => {
1423
+ this.onSelect(t.emoji), this.close();
1424
+ }), this.emojiGrid.appendChild(n);
1425
+ });
1426
+ }
1427
+ applyTheme(e, t) {
1428
+ const n = {
1429
+ primaryColor: "--te-primary-color",
1430
+ primaryHover: "--te-primary-hover",
1431
+ bgApp: "--te-bg-app",
1432
+ bgEditor: "--te-bg-editor",
1433
+ toolbarBg: "--te-toolbar-bg",
1434
+ borderColor: "--te-border-color",
1435
+ borderFocus: "--te-border-focus",
1436
+ textMain: "--te-text-main",
1437
+ textMuted: "--te-text-muted",
1438
+ placeholder: "--te-placeholder",
1439
+ btnHover: "--te-btn-hover",
1440
+ btnActive: "--te-btn-active",
1441
+ radiusLg: "--te-radius-lg",
1442
+ radiusMd: "--te-radius-md",
1443
+ radiusSm: "--te-radius-sm",
1444
+ shadowSm: "--te-shadow-sm",
1445
+ shadowMd: "--te-shadow-md",
1446
+ shadowLg: "--te-shadow-lg"
1447
+ };
1448
+ for (const [i, s] of Object.entries(n)) {
1449
+ const o = t[i];
1450
+ o && e.style.setProperty(s, o);
1451
+ }
1452
+ }
1453
+ show(e) {
1454
+ document.body.appendChild(this.container);
1455
+ const t = e.getBoundingClientRect(), n = 280;
1456
+ let i = t.bottom + window.scrollY + 5, s = t.left + window.scrollX;
1457
+ s + n > window.innerWidth && (s = window.innerWidth - n - 10), this.container.style.top = `${i}px`, this.container.style.left = `${s}px`, this.searchInput.focus();
1458
+ }
1459
+ close() {
1460
+ this.container.parentElement && (this.container.remove(), this.onClose());
1461
+ }
1462
+ get el() {
1463
+ return this.container;
1464
+ }
1465
+ }
1466
+ class Y {
1467
+ editor;
1468
+ container;
1469
+ savedRange = null;
1470
+ items = v;
1471
+ activePicker = null;
1472
+ activeModal = null;
1473
+ statusEl = null;
1474
+ boundUpdateActiveStates;
1475
+ itemElements = /* @__PURE__ */ new Map();
1476
+ constructor(e) {
1477
+ this.editor = e, this.container = this.createToolbarElement(), this.boundUpdateActiveStates = this.updateActiveStates.bind(this), this.render();
1478
+ }
1479
+ createToolbarElement() {
1480
+ const e = document.createElement("div");
1481
+ 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;
1482
+ }
1483
+ render() {
1484
+ const e = this.editor.getOptions().toolbarItems, t = [];
1485
+ this.items.forEach((s) => {
1486
+ (s.type === "divider" || s.id && (!e || e.includes(s.id))) && t.push(s);
1487
+ });
1488
+ const n = [];
1489
+ t.forEach((s, o) => {
1490
+ if (s.type === "divider") {
1491
+ if (n.length === 0 || n[n.length - 1].type === "divider" || !t.slice(o + 1).some((r) => r.type !== "divider")) return;
1492
+ n.push(s);
1493
+ } else
1494
+ n.push(s);
1495
+ }), n.forEach((s) => {
1496
+ if (s.type === "button")
1497
+ this.renderButton(s);
1498
+ else if (s.type === "select")
1499
+ this.renderSelect(s);
1500
+ else if (s.type === "input")
1501
+ this.renderInput(s);
1502
+ else if (s.type === "color-picker")
1503
+ this.renderColorPicker(s);
1504
+ else if (s.type === "divider") {
1505
+ const o = document.createElement("div");
1506
+ o.classList.add("te-divider"), this.container.appendChild(o);
1507
+ }
1508
+ }), this.editor.getOptions().showStatus !== !1 && this.container.appendChild(this.statusEl), this.editor.el.addEventListener("keyup", this.boundUpdateActiveStates), this.editor.el.addEventListener("mouseup", this.boundUpdateActiveStates);
1509
+ }
1510
+ renderButton(e) {
1511
+ const t = document.createElement("button");
1512
+ t.classList.add("te-button"), t.innerHTML = e.icon || "", t.title = e.title, this.itemElements.set(e, t), t.addEventListener("mousedown", (n) => {
1513
+ if (n.preventDefault(), e.command === "createLink" || e.command === "insertTable") {
1514
+ const i = window.getSelection();
1515
+ if (i && i.rangeCount > 0) {
1516
+ const s = i.getRangeAt(0);
1517
+ this.editor.el.contains(s.commonAncestorContainer) && (this.savedRange = s.cloneRange());
1518
+ }
1519
+ }
1520
+ if (e.command === "insertEmoji") {
1521
+ this.activePicker ? this.activePicker.close() : (this.activePicker = new X(
1522
+ (i) => {
1523
+ this.editor.execute("insertText", i);
1524
+ },
1525
+ () => {
1526
+ this.activePicker = null;
1527
+ },
1528
+ this.editor.getOptions().theme,
1529
+ this.editor.getOptions().dark
1530
+ ), this.activePicker.show(t));
1531
+ return;
1532
+ }
1533
+ if (e.command === "insertImage") {
1534
+ const i = document.createElement("input");
1535
+ i.type = "file", i.accept = "image/*", i.style.display = "none", i.addEventListener("change", (s) => {
1536
+ const a = s.target.files;
1537
+ a && a.length > 0 && this.editor.handleFiles(Array.from(a)), document.body.removeChild(i);
1538
+ }), document.body.appendChild(i), i.click();
1539
+ return;
1540
+ }
1541
+ if (["addRow", "deleteRow", "addColumn", "deleteColumn"].includes(e.command || "")) {
1542
+ const i = e.command;
1543
+ this.editor[i]();
1544
+ return;
1545
+ }
1546
+ if (e.command === "undo") {
1547
+ this.editor.undo();
1548
+ return;
1549
+ }
1550
+ if (e.command === "redo") {
1551
+ this.editor.redo();
1552
+ return;
1553
+ }
1554
+ if (e.command === "createLink") {
1555
+ this.activeModal && this.activeModal.close(), this.activeModal = new g(
1556
+ "Insert Link",
1557
+ [{ id: "url", label: "URL", type: "text", placeholder: "https://example.com" }],
1558
+ (i) => {
1559
+ if (this.savedRange) {
1560
+ const s = window.getSelection();
1561
+ s && (s.removeAllRanges(), s.addRange(this.savedRange));
1562
+ }
1563
+ this.editor.createLink(i.url), this.savedRange = null;
1564
+ },
1565
+ () => {
1566
+ this.activeModal = null, this.savedRange = null;
1567
+ },
1568
+ this.editor.getOptions().theme,
1569
+ this.editor.getOptions().dark
1570
+ ), this.activeModal.show(t);
1571
+ return;
1572
+ }
1573
+ if (e.command === "insertTable") {
1574
+ this.activeModal && this.activeModal.close(), this.activeModal = new g(
1575
+ "Insert Table",
1576
+ [
1577
+ { id: "rows", label: "Rows", type: "number", defaultValue: "3", min: "1" },
1578
+ { id: "cols", label: "Columns", type: "number", defaultValue: "3", min: "1" }
1579
+ ],
1580
+ (i) => {
1581
+ if (this.savedRange) {
1582
+ const a = window.getSelection();
1583
+ a && (a.removeAllRanges(), a.addRange(this.savedRange));
1584
+ }
1585
+ let s = parseInt(i.rows, 10), o = parseInt(i.cols, 10);
1586
+ (isNaN(s) || s < 1) && (s = 1), (isNaN(o) || o < 1) && (o = 1), this.editor.insertTable(s, o), this.savedRange = null;
1587
+ },
1588
+ () => {
1589
+ this.activeModal = null, this.savedRange = null;
1590
+ },
1591
+ this.editor.getOptions().theme,
1592
+ this.editor.getOptions().dark
1593
+ ), this.activeModal.show(t);
1594
+ return;
1595
+ }
1596
+ e.command && this.editor.execute(e.command, e.value || null), this.updateActiveStates();
1597
+ }), this.container.appendChild(t);
1598
+ }
1599
+ renderInput(e) {
1600
+ const t = document.createElement("input");
1601
+ t.type = "number", t.classList.add("te-input"), t.title = e.title, t.value = e.value || "", t.min = "1", t.max = "100";
1602
+ const n = () => {
1603
+ const s = window.getSelection();
1604
+ if (s && s.rangeCount > 0) {
1605
+ const o = s.getRangeAt(0);
1606
+ this.editor.el.contains(o.commonAncestorContainer) && (this.savedRange = o.cloneRange());
1607
+ }
1608
+ };
1609
+ t.addEventListener("mousedown", n), t.addEventListener("focus", n);
1610
+ const i = () => {
1611
+ let s = parseInt(t.value, 10);
1612
+ if (!isNaN(s) && (s = Math.max(1, Math.min(100, s)), t.value = s.toString(), e.command === "fontSize")) {
1613
+ if (this.savedRange) {
1614
+ const o = this.editor.setStyle("font-size", `${s}px`, this.savedRange);
1615
+ o && (this.savedRange = o);
1616
+ } else
1617
+ this.editor.setStyle("font-size", `${s}px`);
1618
+ t.focus();
1619
+ }
1620
+ };
1621
+ t.addEventListener("input", i), t.addEventListener("keydown", (s) => {
1622
+ s.key === "Enter" && (i(), this.editor.focus());
1623
+ }), this.container.appendChild(t);
1624
+ }
1625
+ renderSelect(e) {
1626
+ const t = document.createElement("select");
1627
+ t.classList.add("te-select"), t.title = e.title, e.options && e.options.forEach((n) => {
1628
+ const i = document.createElement("option");
1629
+ i.value = n.value, i.textContent = n.label, t.appendChild(i);
1630
+ }), t.addEventListener("change", () => {
1631
+ const n = t.value;
1632
+ 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();
1633
+ }), t.addEventListener("mousedown", () => {
1634
+ this.savedRange = this.editor.selection.saveSelection();
1635
+ }), this.container.appendChild(t);
1636
+ }
1637
+ renderColorPicker(e) {
1638
+ const t = document.createElement("div");
1639
+ if (t.classList.add("te-color-picker-wrapper"), t.title = e.title, e.icon) {
1640
+ const i = document.createElement("div");
1641
+ i.classList.add("te-button", "te-color-icon"), i.innerHTML = e.icon;
1642
+ const s = document.createElement("div");
1643
+ s.classList.add("te-color-indicator"), s.style.backgroundColor = e.value || "#000000", i.appendChild(s), this.itemElements.set(e, i), t.appendChild(i);
1644
+ }
1645
+ const n = document.createElement("input");
1646
+ 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", () => {
1647
+ this.savedRange = this.editor.selection.saveSelection();
1648
+ }), n.addEventListener("input", () => {
1649
+ if (e.icon) {
1650
+ const i = t.querySelector(".te-color-indicator");
1651
+ i && (i.style.backgroundColor = n.value);
1652
+ }
1653
+ }), n.addEventListener("change", () => {
1654
+ if (this.savedRange && this.editor.selection.restoreSelection(this.savedRange), e.command === "foreColor") {
1655
+ const i = this.savedRange || void 0;
1656
+ this.editor.setStyle("color", n.value, i);
1657
+ } else if (e.command === "backColor") {
1658
+ const i = this.savedRange || void 0;
1659
+ this.editor.setStyle("background-color", n.value, i);
1660
+ } else e.command && this.editor.execute(e.command, n.value);
1661
+ this.editor.focus();
1662
+ }), t.appendChild(n), this.container.appendChild(e.icon ? t : n);
1663
+ }
1664
+ get el() {
1665
+ return this.container;
1666
+ }
1667
+ updateActiveStates() {
1668
+ this.items.forEach((e) => {
1669
+ const t = this.itemElements.get(e);
1670
+ t && e.type === "button" && (e.command && document.queryCommandState(e.command) ? t.classList.add("active") : t.classList.remove("active"));
1671
+ });
1672
+ }
1673
+ updateStatus(e, t = !1) {
1674
+ if (!this.statusEl || this.editor.getOptions().showStatus === !1) return;
1675
+ if (this.statusEl.textContent = "", t) {
1676
+ const i = document.createElement("div");
1677
+ i.classList.add("te-toolbar-loader"), this.statusEl.appendChild(i);
1678
+ }
1679
+ const n = document.createElement("span");
1680
+ n.textContent = e, this.statusEl.appendChild(n);
1681
+ }
1682
+ destroy() {
1683
+ 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);
1684
+ }
1685
+ }
1686
+ class Z extends K {
1687
+ toolbar;
1688
+ constructor(e, t = {}) {
1689
+ const n = {
1690
+ ...t,
1691
+ onSaving: () => {
1692
+ this.toolbar?.updateStatus("Auto saving...", !0), t.onSaving && t.onSaving();
1693
+ },
1694
+ onSave: (i) => {
1695
+ const s = (/* @__PURE__ */ new Date()).toLocaleString([], {
1696
+ year: "numeric",
1697
+ month: "short",
1698
+ day: "numeric",
1699
+ hour: "2-digit",
1700
+ minute: "2-digit",
1701
+ hour12: !0
1702
+ });
1703
+ this.toolbar?.updateStatus(`Saved at ${s}`, !1), t.onSave && t.onSave(i);
1704
+ }
1705
+ };
1706
+ if (super(e, n), typeof document > "u" || !e) {
1707
+ this.toolbar = {};
1708
+ return;
1709
+ }
1710
+ 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();
1711
+ }
1712
+ getToolbar() {
1713
+ return this.toolbar;
1714
+ }
1715
+ destroy() {
1716
+ this.toolbar && this.toolbar.destroy(), super.destroy();
1717
+ }
1718
+ }
1719
+ export {
1720
+ K as CoreEditor,
1721
+ E as HistoryManager,
1722
+ Z as InkFlowEditor,
1723
+ b as SelectionManager,
1724
+ Y as Toolbar
1725
+ };