inkflow-editor 1.3.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.
Files changed (49) hide show
  1. package/ReadME.md +230 -0
  2. package/dist/EmojiList-B-C3-zN2.js +25 -0
  3. package/dist/core/Editor.d.ts +227 -0
  4. package/dist/core/HistoryManager.d.ts +36 -0
  5. package/dist/core/ImageManager.d.ts +28 -0
  6. package/dist/core/SelectionManager.d.ts +52 -0
  7. package/dist/core/plugins/ImageUploader.d.ts +15 -0
  8. package/dist/index.d.ts +12 -0
  9. package/dist/inkflow-editor.css +1 -0
  10. package/dist/inkflow-editor.mjs +2010 -0
  11. package/dist/style.d.ts +4 -0
  12. package/dist/ui/Toolbar.d.ts +26 -0
  13. package/dist/ui/toolbar/EmojiList.d.ts +6 -0
  14. package/dist/ui/toolbar/EmojiPicker.d.ts +21 -0
  15. package/dist/ui/toolbar/FloatingToolbar.d.ts +16 -0
  16. package/dist/ui/toolbar/InputModal.d.ts +24 -0
  17. package/dist/ui/toolbar/ToolbarItem.d.ts +16 -0
  18. package/dist/ui/toolbar/items/AlignCenter.d.ts +2 -0
  19. package/dist/ui/toolbar/items/AlignJustify.d.ts +2 -0
  20. package/dist/ui/toolbar/items/AlignLeft.d.ts +2 -0
  21. package/dist/ui/toolbar/items/AlignRight.d.ts +2 -0
  22. package/dist/ui/toolbar/items/Bold.d.ts +2 -0
  23. package/dist/ui/toolbar/items/BulletList.d.ts +2 -0
  24. package/dist/ui/toolbar/items/ClearFormatting.d.ts +2 -0
  25. package/dist/ui/toolbar/items/CodeBlock.d.ts +2 -0
  26. package/dist/ui/toolbar/items/Emoji.d.ts +2 -0
  27. package/dist/ui/toolbar/items/FontFamily.d.ts +2 -0
  28. package/dist/ui/toolbar/items/FontSize.d.ts +2 -0
  29. package/dist/ui/toolbar/items/Heading.d.ts +2 -0
  30. package/dist/ui/toolbar/items/HighlightColor.d.ts +2 -0
  31. package/dist/ui/toolbar/items/HorizontalRule.d.ts +2 -0
  32. package/dist/ui/toolbar/items/Image.d.ts +2 -0
  33. package/dist/ui/toolbar/items/Indent.d.ts +2 -0
  34. package/dist/ui/toolbar/items/Italic.d.ts +2 -0
  35. package/dist/ui/toolbar/items/LineHeight.d.ts +2 -0
  36. package/dist/ui/toolbar/items/Link.d.ts +2 -0
  37. package/dist/ui/toolbar/items/MagicFormat.d.ts +2 -0
  38. package/dist/ui/toolbar/items/OrderedList.d.ts +2 -0
  39. package/dist/ui/toolbar/items/Outdent.d.ts +2 -0
  40. package/dist/ui/toolbar/items/Redo.d.ts +2 -0
  41. package/dist/ui/toolbar/items/ResetMagicFormat.d.ts +2 -0
  42. package/dist/ui/toolbar/items/Strikethrough.d.ts +2 -0
  43. package/dist/ui/toolbar/items/Table.d.ts +2 -0
  44. package/dist/ui/toolbar/items/TableActions.d.ts +5 -0
  45. package/dist/ui/toolbar/items/TextColor.d.ts +2 -0
  46. package/dist/ui/toolbar/items/Underline.d.ts +2 -0
  47. package/dist/ui/toolbar/items/Undo.d.ts +2 -0
  48. package/dist/ui/toolbar/registry.d.ts +2 -0
  49. package/package.json +71 -0
@@ -0,0 +1,2010 @@
1
+ import v from "dompurify";
2
+ class E {
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 x {
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 w {
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 C = {
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
+ }, L = {
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
+ }, S = {
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
+ }, M = {
259
+ type: "input",
260
+ title: "Size (px)",
261
+ command: "fontSize",
262
+ placeholder: "Size",
263
+ value: "16"
264
+ }, T = {
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
+ }, R = {
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
+ }, H = {
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
+ }, A = {
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
+ }, I = {
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
+ }, N = {
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
+ }, P = {
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
+ }, j = {
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
+ }, B = {
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
+ }, O = {
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
+ }, U = {
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
+ }, F = {
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
+ }, q = {
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
+ }, W = {
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
+ }, $ = {
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
+ }, _ = {
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
+ }, V = {
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
+ }, K = {
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
+ }, G = {
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
+ }, X = {
396
+ type: "button",
397
+ id: "code-block",
398
+ title: "Code Block",
399
+ command: "formatBlock",
400
+ value: "PRE",
401
+ icon: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/></svg>'
402
+ }, p = { type: "divider", title: "" }, b = [
403
+ { ...C, id: "undo" },
404
+ { ...L, id: "redo" },
405
+ p,
406
+ { ...k, id: "heading" },
407
+ { ...S, id: "font-family" },
408
+ { ...M, id: "font-size" },
409
+ { ...T, id: "line-height" },
410
+ p,
411
+ { ...R, id: "bold" },
412
+ { ...H, id: "italic" },
413
+ { ...A, id: "underline" },
414
+ { ...I, id: "strikethrough" },
415
+ p,
416
+ { ...N, id: "text-color" },
417
+ { ...P, id: "highlight-color" },
418
+ p,
419
+ { ...j, id: "align-left" },
420
+ { ...B, id: "align-center" },
421
+ { ...O, id: "align-right" },
422
+ { ...D, id: "align-justify" },
423
+ p,
424
+ { ...z, id: "bullet-list" },
425
+ { ...U, id: "ordered-list" },
426
+ { ...F, id: "outdent" },
427
+ { ...q, id: "indent" },
428
+ p,
429
+ { ...W, id: "horizontal-rule" },
430
+ { ..._, id: "emoji" },
431
+ { ...V, id: "link" },
432
+ { ...K, id: "image" },
433
+ { ...G, id: "table" },
434
+ { ...X, id: "code-block" },
435
+ { ...$, id: "clear-formatting" }
436
+ ];
437
+ class g {
438
+ container;
439
+ onConfirm;
440
+ onClose;
441
+ dark;
442
+ theme;
443
+ fields;
444
+ constructor(e, t, n, i, o, s) {
445
+ this.fields = t, this.onConfirm = n, this.onClose = i, this.theme = o, this.dark = s, this.container = this.createModalElement(e, t), this.setupEvents();
446
+ }
447
+ createModalElement(e, t) {
448
+ const n = document.createElement("div");
449
+ n.classList.add("te-modal"), this.theme && this.applyTheme(n, this.theme), this.dark && n.classList.add("te-dark");
450
+ const i = document.createElement("div");
451
+ i.classList.add("te-modal-header"), i.textContent = e, n.appendChild(i);
452
+ const o = document.createElement("div");
453
+ o.classList.add("te-modal-body"), t.forEach((r) => {
454
+ const d = document.createElement("div");
455
+ d.classList.add("te-modal-field");
456
+ const c = document.createElement("label");
457
+ c.setAttribute("for", r.id), c.textContent = r.label;
458
+ const h = document.createElement("input");
459
+ h.type = r.type, h.id = r.id, h.classList.add("te-modal-input"), r.placeholder && (h.placeholder = r.placeholder), r.defaultValue && (h.value = r.defaultValue), r.min && (h.min = r.min), r.max && (h.max = r.max), r.type === "file" && (h.accept = "image/*", h.classList.add("te-modal-file-input")), d.appendChild(c), d.appendChild(h), o.appendChild(d);
460
+ }), n.appendChild(o);
461
+ const s = document.createElement("div");
462
+ s.classList.add("te-modal-footer");
463
+ const a = document.createElement("button");
464
+ a.classList.add("te-modal-btn", "te-modal-btn-cancel"), a.textContent = "Cancel";
465
+ const l = document.createElement("button");
466
+ return l.classList.add("te-modal-btn", "te-modal-btn-confirm"), l.textContent = "Insert", s.appendChild(a), s.appendChild(l), n.appendChild(s), n;
467
+ }
468
+ setupEvents() {
469
+ const e = this.container.querySelector(".te-modal-btn-cancel"), t = this.container.querySelector(".te-modal-btn-confirm");
470
+ e.addEventListener("click", () => this.close()), t.addEventListener("click", () => {
471
+ const o = {};
472
+ this.fields.forEach((s) => {
473
+ const a = this.container.querySelector(`#${s.id}`);
474
+ s.type === "file" ? o[s.id] = a.files && a.files.length > 0 ? a.files[0] : null : o[s.id] = a.value;
475
+ }), this.onConfirm(o), this.close();
476
+ });
477
+ const n = (o) => {
478
+ o.key === "Escape" && this.close(), o.key === "Enter" && t.click();
479
+ };
480
+ this.container.addEventListener("keydown", n);
481
+ const i = (o) => {
482
+ this.container.contains(o.target) || (this.close(), document.removeEventListener("mousedown", i));
483
+ };
484
+ setTimeout(() => document.addEventListener("mousedown", i), 0);
485
+ }
486
+ show(e) {
487
+ document.body.appendChild(this.container);
488
+ const t = e.getBoundingClientRect(), n = 260;
489
+ let i = t.bottom + window.scrollY + 10, o = t.left + window.scrollX;
490
+ o + n > window.innerWidth && (o = window.innerWidth - n - 20), this.container.style.top = `${i}px`, this.container.style.left = `${o}px`;
491
+ const s = this.container.querySelector("input");
492
+ s && s.focus();
493
+ }
494
+ close() {
495
+ this.container.parentElement && (this.container.remove(), this.onClose());
496
+ }
497
+ applyTheme(e, t) {
498
+ const n = {
499
+ primaryColor: "--te-primary-color",
500
+ primaryHover: "--te-primary-hover",
501
+ bgApp: "--te-bg-app",
502
+ bgEditor: "--te-bg-editor",
503
+ toolbarBg: "--te-toolbar-bg",
504
+ borderColor: "--te-border-color",
505
+ borderFocus: "--te-border-focus",
506
+ textMain: "--te-text-main",
507
+ textMuted: "--te-text-muted",
508
+ placeholder: "--te-placeholder",
509
+ btnHover: "--te-btn-hover",
510
+ btnActive: "--te-btn-active",
511
+ radiusLg: "--te-radius-lg",
512
+ radiusMd: "--te-radius-md",
513
+ radiusSm: "--te-radius-sm",
514
+ shadowSm: "--te-shadow-sm",
515
+ shadowMd: "--te-shadow-md",
516
+ shadowLg: "--te-shadow-lg"
517
+ };
518
+ for (const [i, o] of Object.entries(n)) {
519
+ const s = t[i];
520
+ s && e.style.setProperty(o, s);
521
+ }
522
+ }
523
+ }
524
+ class Y {
525
+ container;
526
+ editor;
527
+ activeModal = null;
528
+ savedRange = null;
529
+ isVisible = !1;
530
+ constructor(e) {
531
+ this.editor = e, this.container = this.createContainer(), this.setupListeners(), document.body.appendChild(this.container);
532
+ }
533
+ createContainer() {
534
+ const e = document.createElement("div");
535
+ e.className = "te-floating-toolbar te-glass", e.style.display = "none", e.style.position = "absolute", e.style.zIndex = "2000";
536
+ const n = this.editor.getOptions().toolbarPosition === "floating", i = n ? ["bold", "italic", "underline", "strikethrough", "textColor", "highlight-color", "divider", "heading", "bullet-list", "ordered-list", "divider", "link", "image", "table", "code-block", "emoji", "clear-formatting"] : ["heading", "bold", "italic", "underline", "strikethrough", "highlight-color", "link", "clear-formatting"];
537
+ return (n ? b.filter((s) => s.id && (i.includes(s.id) || s.type === "divider")) : b.filter((s) => s.id && i.includes(s.id))).forEach((s) => {
538
+ if (s.type === "divider") {
539
+ const l = document.createElement("div");
540
+ l.className = "te-floating-divider", e.appendChild(l);
541
+ return;
542
+ }
543
+ const a = document.createElement("button");
544
+ a.className = "te-floating-btn", a.title = s.title, a.innerHTML = s.icon || s.title, a.onclick = (l) => {
545
+ l.preventDefault(), l.stopPropagation(), this.handleCommand(s);
546
+ }, e.appendChild(a);
547
+ }), e;
548
+ }
549
+ handleCommand(e) {
550
+ if (e.command === "createLink") {
551
+ const t = window.getSelection();
552
+ t && t.rangeCount > 0 && (this.savedRange = t.getRangeAt(0).cloneRange()), this.activeModal && this.activeModal.close(), this.activeModal = new g(
553
+ "Insert Link",
554
+ [{ id: "url", label: "URL", type: "text", placeholder: "https://example.com" }],
555
+ (n) => {
556
+ if (this.savedRange) {
557
+ const i = window.getSelection();
558
+ i && (i.removeAllRanges(), i.addRange(this.savedRange));
559
+ }
560
+ this.editor.execute("createLink", n.url), this.savedRange = null, this.hide();
561
+ },
562
+ () => {
563
+ this.activeModal = null, this.savedRange = null;
564
+ },
565
+ this.editor.getOptions().theme,
566
+ this.editor.getOptions().dark
567
+ ), this.activeModal.show(this.container);
568
+ } 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);
569
+ }
570
+ setupListeners() {
571
+ const e = () => {
572
+ setTimeout(() => this.updatePosition(), 50);
573
+ };
574
+ this.editor.el.addEventListener("mouseup", e), this.editor.el.addEventListener("keyup", e), this.editor.el.addEventListener("scroll", () => {
575
+ this.isVisible && !this.activeModal && this.hide();
576
+ }, !0), window.addEventListener("mousedown", (t) => {
577
+ !this.container.contains(t.target) && !this.editor.el.contains(t.target) && this.hide();
578
+ }), window.addEventListener("resize", () => {
579
+ this.isVisible && this.updatePosition();
580
+ });
581
+ }
582
+ updatePosition() {
583
+ const e = window.getSelection(), n = this.editor.getOptions().toolbarPosition === "floating";
584
+ if (!e || e.rangeCount === 0) {
585
+ this.activeModal || this.hide();
586
+ return;
587
+ }
588
+ if (e.isCollapsed && !n) {
589
+ this.activeModal || this.hide();
590
+ return;
591
+ }
592
+ const i = e.getRangeAt(0);
593
+ if (!this.editor.el.contains(i.commonAncestorContainer)) {
594
+ this.hide();
595
+ return;
596
+ }
597
+ const o = i.getBoundingClientRect(), a = (this.container.offsetParent || document.documentElement).getBoundingClientRect();
598
+ this.container.style.display = "flex", this.isVisible = !0;
599
+ const l = this.container.offsetWidth, r = this.container.offsetHeight;
600
+ let d = o.top - a.top - r - 10, c = o.left - a.left + o.width / 2 - l / 2;
601
+ o.top - r - 15 < 0 && (d = o.bottom - a.top + 10);
602
+ const h = 10 - a.left, m = window.innerWidth - 10 - a.left - l;
603
+ c < h && (c = h), c > m && (c = m), this.container.style.top = `${d}px`, this.container.style.left = `${c}px`, this.container.classList.add("te-floating-visible");
604
+ }
605
+ hide() {
606
+ this.container.style.display = "none", this.container.classList.remove("te-floating-visible"), this.isVisible = !1;
607
+ }
608
+ destroy() {
609
+ this.container.remove();
610
+ }
611
+ setDarkMode(e) {
612
+ e ? this.container.classList.add("te-dark") : this.container.classList.remove("te-dark");
613
+ }
614
+ }
615
+ class y {
616
+ /**
617
+ * Compresses an image file using HTML5 Canvas.
618
+ */
619
+ static async compressImage(e, t) {
620
+ return new Promise((n, i) => {
621
+ if (e.size <= t * 1024 * 1024 && e.type === "image/webp")
622
+ return n(e);
623
+ const o = new Image();
624
+ o.src = URL.createObjectURL(e), o.onload = () => {
625
+ const s = document.createElement("canvas");
626
+ let a = o.width, l = o.height;
627
+ const r = 2e3;
628
+ (a > r || l > r) && (a > l ? (l = Math.round(l * r / a), a = r) : (a = Math.round(a * r / l), l = r)), s.width = a, s.height = l;
629
+ const d = s.getContext("2d");
630
+ if (!d)
631
+ return i(new Error("Failed to get canvas context"));
632
+ d.drawImage(o, 0, 0, a, l), s.toBlob(
633
+ (c) => {
634
+ c ? n(c) : i(new Error("Canvas toBlob failed"));
635
+ },
636
+ "image/webp",
637
+ 0.8
638
+ // 80% quality is professional standard
639
+ ), URL.revokeObjectURL(o.src);
640
+ }, o.onerror = () => {
641
+ URL.revokeObjectURL(o.src), i(new Error("Failed to load image for compression"));
642
+ };
643
+ });
644
+ }
645
+ /**
646
+ * Uploads a file based on editor configuration.
647
+ */
648
+ static async uploadFile(e, t) {
649
+ const n = e instanceof File ? e.name : "upload.webp";
650
+ if (t.imageEndpoints?.upload) {
651
+ const i = new FormData();
652
+ i.append("file", e, n);
653
+ try {
654
+ const o = await fetch(t.imageEndpoints.upload, {
655
+ method: "POST",
656
+ body: i
657
+ });
658
+ if (o.ok) {
659
+ const s = await o.json();
660
+ return {
661
+ imageUrl: s.imageUrl,
662
+ imageId: s.imageId
663
+ };
664
+ }
665
+ console.warn("Custom upload endpoint returned an error, falling back.");
666
+ } catch (o) {
667
+ console.error("Custom upload failed:", o);
668
+ }
669
+ }
670
+ if (t.cloudinaryFallback) {
671
+ const { cloudName: i, uploadPreset: o } = t.cloudinaryFallback, s = `https://api.cloudinary.com/v1_1/${i}/image/upload`, a = new FormData();
672
+ a.append("file", e, n), a.append("upload_preset", o);
673
+ try {
674
+ const l = await fetch(s, {
675
+ method: "POST",
676
+ body: a
677
+ });
678
+ if (l.ok)
679
+ return {
680
+ imageUrl: (await l.json()).secure_url
681
+ };
682
+ console.warn("Cloudinary upload failed, falling back.");
683
+ } catch (l) {
684
+ console.error("Cloudinary fallback failed:", l);
685
+ }
686
+ }
687
+ return null;
688
+ }
689
+ }
690
+ class J {
691
+ container;
692
+ editableElement;
693
+ selection;
694
+ imageManager;
695
+ history;
696
+ options;
697
+ saveTimeout = null;
698
+ historyTimeout = null;
699
+ pendingStyles = {};
700
+ observer = null;
701
+ floatingToolbar = null;
702
+ magicStateMap = /* @__PURE__ */ new Map();
703
+ eventListeners = [];
704
+ loaderElement = null;
705
+ isUndoingRedoing = !1;
706
+ constructor(e, t = {}) {
707
+ if (this.options = t, this.container = e, typeof document > "u" || !e) {
708
+ this.editableElement = {}, this.selection = {}, this.imageManager = {}, this.history = {};
709
+ return;
710
+ }
711
+ 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 E(), this.imageManager = new x(this), this.history = new w(this.editableElement.innerHTML), this.setupInputHandlers(), this.setupLimitEnforcement(), 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 Y(this), this.options.dark && this.floatingToolbar.setDarkMode(!0), this.options.showLoader !== !1 && setTimeout(() => this.hideLoader(), 300);
712
+ }
713
+ /**
714
+ * Applies custom theme variables to the editor container.
715
+ */
716
+ applyTheme(e) {
717
+ const t = this.container, n = {
718
+ primaryColor: "--te-primary-color",
719
+ primaryHover: "--te-primary-hover",
720
+ bgApp: "--te-bg-app",
721
+ bgEditor: "--te-bg-editor",
722
+ toolbarBg: "--te-toolbar-bg",
723
+ borderColor: "--te-border-color",
724
+ borderFocus: "--te-border-focus",
725
+ textMain: "--te-text-main",
726
+ textMuted: "--te-text-muted",
727
+ placeholder: "--te-placeholder",
728
+ btnHover: "--te-btn-hover",
729
+ btnActive: "--te-btn-active",
730
+ radiusLg: "--te-radius-lg",
731
+ radiusMd: "--te-radius-md",
732
+ radiusSm: "--te-radius-sm",
733
+ shadowSm: "--te-shadow-sm",
734
+ shadowMd: "--te-shadow-md",
735
+ shadowLg: "--te-shadow-lg"
736
+ };
737
+ for (const [i, o] of Object.entries(n)) {
738
+ const s = e[i];
739
+ s && t.style.setProperty(o, s);
740
+ }
741
+ }
742
+ /**
743
+ * Toggles dark mode on the editor.
744
+ */
745
+ setDarkMode(e) {
746
+ 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));
747
+ }
748
+ /**
749
+ * Destroys the editor instance and cleans up.
750
+ */
751
+ destroy() {
752
+ 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 }) => {
753
+ e.removeEventListener(t, n);
754
+ }), this.eventListeners = [], this.container.innerHTML = "", this.container.classList.remove("te-container", "te-dark", "te-toolbar-bottom", "te-toolbar-floating"), this.container.removeAttribute("style"), this.floatingToolbar && (this.floatingToolbar.destroy(), this.floatingToolbar = null);
755
+ }
756
+ checkPlaceholder() {
757
+ if (!this.editableElement) return;
758
+ 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")) {
759
+ this.editableElement.classList.add("is-empty");
760
+ const t = this.editableElement.firstElementChild;
761
+ t && (this.editableElement.style.textAlign = t.style.textAlign);
762
+ } else
763
+ this.editableElement.classList.remove("is-empty"), this.editableElement.style.textAlign = "";
764
+ }
765
+ addEventListener(e, t, n, i) {
766
+ e.addEventListener(t, n, i), this.eventListeners.push({ target: e, type: t, handler: n });
767
+ }
768
+ setupImageObserver() {
769
+ this.observer = new MutationObserver((e) => {
770
+ e.forEach((t) => {
771
+ t.addedNodes.forEach((n) => {
772
+ if (n.nodeType === Node.ELEMENT_NODE) {
773
+ const i = n;
774
+ i.tagName === "IMG" && !i.closest(".te-image-container") ? this.wrapImage(i) : i.querySelectorAll("img:not(.te-image)").forEach((s) => {
775
+ s.closest(".te-image-container") || this.wrapImage(s);
776
+ });
777
+ }
778
+ });
779
+ });
780
+ }), this.observer.observe(this.editableElement, {
781
+ childList: !0,
782
+ subtree: !0
783
+ });
784
+ }
785
+ /**
786
+ * Wraps a raw <img> element in the interactive container
787
+ */
788
+ wrapImage(e) {
789
+ const t = e.parentElement;
790
+ if (!t) return;
791
+ const n = document.createElement("figure");
792
+ n.classList.add("te-image-container"), n.setAttribute("contenteditable", "false");
793
+ const i = document.createElement("img");
794
+ 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");
795
+ const o = document.createElement("figcaption");
796
+ 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) => {
797
+ const l = document.createElement("div");
798
+ l.classList.add("te-image-resizer", `te-resizer-${a}`), n.appendChild(l);
799
+ }), n.appendChild(i), n.appendChild(o), t.replaceChild(n, e), !n.nextElementSibling) {
800
+ const a = document.createElement("p");
801
+ a.innerHTML = "<br>", n.after(a);
802
+ }
803
+ }
804
+ setupInputHandlers() {
805
+ this.addEventListener(this.editableElement, "beforeinput", (e) => {
806
+ if (e.inputType === "insertText" && Object.keys(this.pendingStyles).length > 0) {
807
+ const t = e.data;
808
+ if (!t) return;
809
+ e.preventDefault();
810
+ const n = document.createElement("span");
811
+ for (const [o, s] of Object.entries(this.pendingStyles))
812
+ n.style.setProperty(o, s);
813
+ n.textContent = t;
814
+ const i = this.selection.getRange();
815
+ if (i) {
816
+ i.deleteContents(), i.insertNode(n);
817
+ const o = document.createRange();
818
+ 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 }));
819
+ }
820
+ }
821
+ }), this.addEventListener(this.editableElement, "input", () => {
822
+ this.checkPlaceholder();
823
+ }), this.addEventListener(document, "selectionchange", () => {
824
+ const e = window.getSelection();
825
+ e && e.rangeCount > 0 && (e.getRangeAt(0).collapsed || (this.pendingStyles = {}));
826
+ }), this.addEventListener(this.editableElement, "dragover", (e) => {
827
+ e.preventDefault(), e.dataTransfer.dropEffect = "copy", this.editableElement.classList.add("dragover");
828
+ }), this.addEventListener(this.editableElement, "dragleave", () => {
829
+ this.editableElement.classList.remove("dragover");
830
+ }), this.addEventListener(this.editableElement, "drop", (e) => {
831
+ e.preventDefault(), this.editableElement.classList.remove("dragover");
832
+ const t = e.dataTransfer?.files;
833
+ t && t.length > 0 && this.handleFiles(Array.from(t));
834
+ }), this.addEventListener(this.editableElement, "paste", this.handlePaste.bind(this)), this.addEventListener(this.editableElement, "input", () => {
835
+ this.handleInput();
836
+ }), this.addEventListener(this.editableElement, "keydown", (e) => {
837
+ if (e.key === "Enter") {
838
+ const t = window.getSelection();
839
+ if (t && t.rangeCount > 0) {
840
+ const n = t.getRangeAt(0);
841
+ (n.startContainer.nodeType === Node.ELEMENT_NODE ? n.startContainer : n.startContainer.parentElement)?.closest("li") && setTimeout(() => {
842
+ this.normalize(), this.triggerChange();
843
+ }, 0);
844
+ }
845
+ }
846
+ if (e.key === "Enter" && !e.shiftKey) {
847
+ const t = this.selection.getRange();
848
+ if (t && t.collapsed) {
849
+ const n = t.startContainer, i = (n.nodeType === Node.ELEMENT_NODE ? n : n.parentElement)?.closest("pre");
850
+ if (i) {
851
+ const o = document.createRange();
852
+ o.setStart(i, 0), o.setEnd(t.startContainer, t.startOffset);
853
+ const s = o.toString(), a = document.createRange();
854
+ a.setStart(t.startContainer, t.startOffset), a.setEnd(i, i.childNodes.length);
855
+ const l = a.toString(), r = s === "" || s.endsWith(`
856
+ `), d = l === "" || l.startsWith(`
857
+ `);
858
+ if (r && d) {
859
+ e.preventDefault();
860
+ const c = i.textContent || "", h = s.length;
861
+ c.charAt(h) === `
862
+ ` ? i.textContent = c.slice(0, h) + c.slice(h + 1) : c.charAt(h - 1) === `
863
+ ` && (i.textContent = c.slice(0, h - 1) + c.slice(h));
864
+ const m = document.createElement("p");
865
+ m.innerHTML = "<br>", i.after(m);
866
+ const f = document.createRange();
867
+ f.setStart(m, 0), f.setEnd(m, 0), this.selection.restoreSelection(f), this.normalize(), this.triggerChange();
868
+ return;
869
+ }
870
+ }
871
+ }
872
+ }
873
+ (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());
874
+ });
875
+ }
876
+ /**
877
+ * Sets up strict character limit enforcement.
878
+ */
879
+ setupLimitEnforcement() {
880
+ this.editableElement.addEventListener("keydown", (e) => {
881
+ if (!this.options.maxCharCount || !this.options.strictCharLimit) return;
882
+ if (this.getCharCount() >= this.options.maxCharCount) {
883
+ const n = [
884
+ "Backspace",
885
+ "Delete",
886
+ "ArrowLeft",
887
+ "ArrowRight",
888
+ "ArrowUp",
889
+ "ArrowDown",
890
+ "Home",
891
+ "End",
892
+ "PageUp",
893
+ "PageDown",
894
+ "Control",
895
+ "Meta",
896
+ "Alt",
897
+ "Shift",
898
+ "a",
899
+ "c",
900
+ "v",
901
+ "x",
902
+ "z",
903
+ "y"
904
+ // Allow common shortcuts
905
+ ];
906
+ if ((e.ctrlKey || e.metaKey) && n.includes(e.key.toLowerCase()))
907
+ return;
908
+ n.includes(e.key) || (e.preventDefault(), e.stopPropagation());
909
+ }
910
+ });
911
+ }
912
+ /**
913
+ * Immediately records a history state if one is pending.
914
+ */
915
+ flushHistoryRecord() {
916
+ if (this.historyTimeout) {
917
+ clearTimeout(this.historyTimeout), this.historyTimeout = null;
918
+ const e = this.editableElement.innerHTML, t = this.selection.getSelectionPath(this.editableElement);
919
+ this.history.record(e, t);
920
+ }
921
+ }
922
+ handleInput() {
923
+ this.isUndoingRedoing || (this.scheduleHistoryRecord(), this.options.autoSave && (this.options.onSaving && this.options.onSaving(), this.scheduleAutoSave()), this.options.onChange && this.options.onChange(this.getHTML()));
924
+ }
925
+ scheduleHistoryRecord() {
926
+ this.historyTimeout && clearTimeout(this.historyTimeout), this.historyTimeout = setTimeout(() => {
927
+ const e = this.editableElement.innerHTML, t = this.selection.getSelectionPath(this.editableElement);
928
+ this.history.record(e, t);
929
+ }, 200);
930
+ }
931
+ scheduleAutoSave() {
932
+ this.saveTimeout && clearTimeout(this.saveTimeout);
933
+ const e = this.options.autoSaveInterval || 300;
934
+ this.saveTimeout = setTimeout(() => {
935
+ this.save();
936
+ }, e);
937
+ }
938
+ save() {
939
+ this.options.onSave && this.options.onSave(this.getHTML());
940
+ }
941
+ undo() {
942
+ this.flushHistoryRecord();
943
+ const e = this.history.undo();
944
+ e !== null && (this.isUndoingRedoing = !0, this.editableElement.innerHTML = e.html, e.selection && this.selection.restoreSelectionPath(this.editableElement, e.selection), this.triggerChange(), this.isUndoingRedoing = !1);
945
+ }
946
+ redo() {
947
+ this.flushHistoryRecord();
948
+ const e = this.history.redo();
949
+ e !== null && (this.isUndoingRedoing = !0, this.editableElement.innerHTML = e.html, e.selection && this.selection.restoreSelectionPath(this.editableElement, e.selection), this.triggerChange(), this.isUndoingRedoing = !1);
950
+ }
951
+ triggerChange() {
952
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
953
+ }
954
+ createLoader() {
955
+ this.loaderElement = document.createElement("div"), this.loaderElement.className = "te-loader-overlay";
956
+ const e = document.createElement("div");
957
+ e.className = "te-loader-spinner";
958
+ const t = document.createElement("div");
959
+ t.className = "te-loader-shimmer";
960
+ const n = document.createElement("div");
961
+ 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);
962
+ }
963
+ hideLoader() {
964
+ this.loaderElement && (this.loaderElement.classList.add("hidden"), setTimeout(() => {
965
+ this.loaderElement && this.loaderElement.parentNode && this.loaderElement.parentNode.removeChild(this.loaderElement), this.loaderElement = null;
966
+ }, 400));
967
+ }
968
+ createEditableElement() {
969
+ const e = document.createElement("div");
970
+ 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;
971
+ }
972
+ /**
973
+ * Focuses the editor.
974
+ */
975
+ focus() {
976
+ this.editableElement.focus();
977
+ }
978
+ /**
979
+ * Executes a command on the current selection.
980
+ */
981
+ execute(e, t = null) {
982
+ if (this.focus(), document.execCommand(e, !1, t ?? void 0), e === "removeFormat" && (document.execCommand("formatBlock", !1, "p"), this.pendingStyles = {}), e === "magicFormat") {
983
+ this.magicFormat();
984
+ return;
985
+ }
986
+ if (e === "resetMagicFormat") {
987
+ this.resetMagicFormat();
988
+ return;
989
+ }
990
+ this.normalize(), this.triggerChange();
991
+ }
992
+ /**
993
+ * Special handler for links to open them in a new tab when clicked.
994
+ */
995
+ setupLinkClickHandlers() {
996
+ this.addEventListener(this.editableElement, "click", (e) => {
997
+ const n = e.target.closest("a");
998
+ if (n && this.editableElement.contains(n)) {
999
+ e.preventDefault();
1000
+ const i = n.getAttribute("href");
1001
+ i && window.open(i, "_blank", "noopener,noreferrer");
1002
+ }
1003
+ });
1004
+ }
1005
+ /**
1006
+ * Magic Format logic: Cycles through aesthetic presets for the entire document.
1007
+ */
1008
+ magicFormat() {
1009
+ const t = ((this.magicStateMap.get(this.editableElement) || 0) + 1) % 3;
1010
+ this.magicStateMap.set(this.editableElement, t);
1011
+ const n = Array.from(this.editableElement.querySelectorAll("p, h1, h2, h3, h4, h5, h6, table, blockquote, figure, li"));
1012
+ n.length !== 0 && (n.forEach((i) => {
1013
+ this.enrichBlockWithEmojis(i), i.tagName === "TABLE" ? this.formatMagicTable(i, t) : i.tagName === "FIGURE" || i.querySelector("img") ? this.formatMagicImage(i, t) : i.tagName.startsWith("H") ? this.formatMagicHeading(i, t) : (i.tagName === "P" || i.tagName === "LI" || i.tagName === "BLOCKQUOTE") && this.formatMagicText(i, t);
1014
+ }), this.normalize(), this.history.record(this.editableElement.innerHTML, this.selection.getSelectionPath(this.editableElement)), this.handleInput());
1015
+ }
1016
+ /**
1017
+ * Resets all magic formatting (inline styles) from the document.
1018
+ */
1019
+ resetMagicFormat() {
1020
+ Array.from(this.editableElement.querySelectorAll("p, h1, h2, h3, h4, h5, h6, table, blockquote, figure, li")).forEach((t) => {
1021
+ t.removeAttribute("style"), t.querySelectorAll("*").forEach((n) => {
1022
+ n.removeAttribute("style");
1023
+ });
1024
+ }), this.magicStateMap.clear(), this.normalize(), this.history.record(this.editableElement.innerHTML, this.selection.getSelectionPath(this.editableElement)), this.handleInput();
1025
+ }
1026
+ formatMagicTable(e, t) {
1027
+ [
1028
+ // State 0: Premium Zebra (Modern Rounded)
1029
+ () => {
1030
+ e.style.borderCollapse = "separate", e.style.borderRadius = "12px", e.style.overflow = "hidden", e.style.border = "1px solid var(--te-border-color)", e.style.boxShadow = "0 4px 6px -1px rgba(0,0,0,0.1)", e.querySelectorAll("td, th").forEach((i) => {
1031
+ i.style.border = "1px solid var(--te-border-color)";
1032
+ });
1033
+ },
1034
+ // State 1: Clean Minimal (No vertical borders, soft header)
1035
+ () => {
1036
+ e.style.borderCollapse = "collapse", e.style.borderRadius = "0", e.style.boxShadow = "none", e.style.border = "none", e.style.borderTop = "2px solid var(--te-primary-color)", e.style.borderBottom = "2px solid var(--te-primary-color)", e.querySelectorAll("td, th").forEach((i) => {
1037
+ i.style.borderLeft = "none", i.style.borderRight = "none", i.style.borderBottom = "1px solid var(--te-border-color)";
1038
+ });
1039
+ },
1040
+ // State 2: Ultra Minimal (No borders whatsoever)
1041
+ () => {
1042
+ e.style.border = "none", e.style.boxShadow = "none", e.style.background = "none", e.style.borderRadius = "0", e.querySelectorAll("td, th").forEach((i) => {
1043
+ i.style.border = "none", i.style.padding = "12px 0";
1044
+ });
1045
+ }
1046
+ ][t]();
1047
+ }
1048
+ formatMagicImage(e, t) {
1049
+ const n = e.querySelector("img");
1050
+ if (!n) return;
1051
+ [
1052
+ // State 0: Shadow & Rounded
1053
+ () => {
1054
+ n.style.borderRadius = "12px", n.style.boxShadow = "0 10px 15px -3px rgba(0,0,0,0.1)", n.style.border = "1px solid var(--te-border-color)";
1055
+ },
1056
+ // State 1: Thick Border Frame
1057
+ () => {
1058
+ n.style.borderRadius = "0", n.style.border = "8px solid white", n.style.boxShadow = "0 1px 3px rgba(0,0,0,0.2)";
1059
+ },
1060
+ // State 2: Soft Minimal
1061
+ () => {
1062
+ n.style.borderRadius = "8px", n.style.boxShadow = "none", n.style.border = "none";
1063
+ }
1064
+ ][t]();
1065
+ }
1066
+ formatMagicHeading(e, t) {
1067
+ [
1068
+ // State 0: Typography Focus (Modern Weight)
1069
+ () => {
1070
+ e.style.fontWeight = "800", e.style.color = "var(--te-primary-color)", e.style.letterSpacing = "-0.02em", e.style.border = "none", e.style.marginBottom = "1.5rem";
1071
+ },
1072
+ // State 1: Elegant Serif Look (Soft Color)
1073
+ () => {
1074
+ e.style.fontFamily = "serif", e.style.color = "#4338ca", e.style.fontStyle = "italic", e.style.border = "none", e.style.letterSpacing = "normal";
1075
+ },
1076
+ // State 2: All Caps & Spaced (Professional Accent)
1077
+ () => {
1078
+ e.style.textTransform = "uppercase", e.style.letterSpacing = "0.2em", e.style.color = "#1e1b4b", e.style.fontWeight = "900", e.style.border = "none";
1079
+ }
1080
+ ][t]();
1081
+ }
1082
+ formatMagicText(e, t) {
1083
+ [
1084
+ // State 0: Premium Reading Mode
1085
+ () => {
1086
+ e.style.lineHeight = "2", e.style.fontSize = "1.15rem", e.style.color = "#334155", e.style.fontWeight = "400", e.style.border = "none";
1087
+ },
1088
+ // State 1: Soft Highlight Look
1089
+ () => {
1090
+ e.style.background = "rgba(99, 102, 241, 0.05)", e.style.borderLeft = "4px solid #818cf8", e.style.padding = "1rem 1.5rem", e.style.borderRadius = "8px", e.style.color = "#1e293b";
1091
+ },
1092
+ // State 2: Modern Clean Minimal
1093
+ () => {
1094
+ e.style.fontWeight = "500", e.style.letterSpacing = "0.01em", e.style.color = "#0f172a", e.style.background = "none", e.style.border = "none", e.style.padding = "0.5rem 0";
1095
+ }
1096
+ ][t]();
1097
+ }
1098
+ /**
1099
+ * Enriches text nodes within a block with emojis without breaking HTML structure.
1100
+ */
1101
+ enrichBlockWithEmojis(e) {
1102
+ const t = {
1103
+ success: "✅",
1104
+ error: "❌",
1105
+ warning: "⚠️",
1106
+ info: "ℹ️",
1107
+ magic: "✨",
1108
+ done: "🎯",
1109
+ plan: "📝",
1110
+ link: "🔗",
1111
+ image: "🖼️",
1112
+ table: "📊",
1113
+ celebrate: "🎉",
1114
+ rocket: "🚀"
1115
+ }, n = document.createTreeWalker(e, NodeFilter.SHOW_TEXT);
1116
+ let i;
1117
+ for (; i = n.nextNode(); ) {
1118
+ let o = i.nodeValue || "", s = !1;
1119
+ Object.entries(t).forEach(([a, l]) => {
1120
+ const r = new RegExp(`\\b${a}\\b`, "gi");
1121
+ r.test(o) && !o.includes(l) && (o = o.replace(r, `${l} ${a}`), s = !0);
1122
+ }), s && (i.nodeValue = o);
1123
+ }
1124
+ }
1125
+ /**
1126
+ * Inserts a table at the current selection.
1127
+ */
1128
+ insertTable(e = 3, t = 3) {
1129
+ this.focus();
1130
+ const n = this.selection.getRange();
1131
+ if (!n) return;
1132
+ const i = document.createElement("table");
1133
+ if (i.classList.add("te-table"), e > 0) {
1134
+ const s = document.createElement("thead"), a = document.createElement("tr");
1135
+ for (let l = 0; l < t; l++) {
1136
+ const r = document.createElement("th");
1137
+ r.innerHTML = "<br>", a.appendChild(r);
1138
+ }
1139
+ s.appendChild(a), i.appendChild(s);
1140
+ }
1141
+ if (e > 1) {
1142
+ const s = document.createElement("tbody");
1143
+ for (let a = 1; a < e; a++) {
1144
+ const l = document.createElement("tr");
1145
+ for (let r = 0; r < t; r++) {
1146
+ const d = document.createElement("td");
1147
+ d.innerHTML = "<br>", l.appendChild(d);
1148
+ }
1149
+ s.appendChild(l);
1150
+ }
1151
+ i.appendChild(s);
1152
+ }
1153
+ n.deleteContents(), n.insertNode(i);
1154
+ const o = i.nextElementSibling;
1155
+ if (!o || o.tagName !== "P") {
1156
+ const s = document.createElement("p");
1157
+ s.innerHTML = "<br>", i.after(s), o && o.tagName === "BR" && o.remove();
1158
+ }
1159
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
1160
+ }
1161
+ /**
1162
+ * Adds a row to the currently selected table.
1163
+ */
1164
+ addRow() {
1165
+ const e = this.getSelectedTable();
1166
+ if (!e) return;
1167
+ const t = document.createElement("tr");
1168
+ t.style.borderBottom = "1px solid var(--te-border-color)";
1169
+ const n = e.rows[0].cells.length;
1170
+ for (let o = 0; o < n; o++) {
1171
+ const s = document.createElement("td");
1172
+ s.innerHTML = "<br>", t.appendChild(s);
1173
+ }
1174
+ const i = this.getSelectedTd();
1175
+ i ? i.parentElement?.after(t) : e.appendChild(t), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
1176
+ }
1177
+ /**
1178
+ * Deletes the currently selected row.
1179
+ */
1180
+ deleteRow() {
1181
+ const e = this.getSelectedTd();
1182
+ if (e && e.parentElement) {
1183
+ const t = e.parentElement, n = t.parentElement;
1184
+ if (n.rows.length > 1) {
1185
+ const i = t.rowIndex, o = n.rows[i + 1] || n.rows[i - 1], s = e.cellIndex;
1186
+ if (t.remove(), o && o.cells[s]) {
1187
+ const a = document.createRange();
1188
+ a.selectNodeContents(o.cells[s]), a.collapse(!0), this.selection.restoreSelection(a);
1189
+ }
1190
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
1191
+ }
1192
+ }
1193
+ }
1194
+ /**
1195
+ * Adds a column to the currently selected table.
1196
+ */
1197
+ addColumn() {
1198
+ const e = this.getSelectedTable();
1199
+ if (!e) return;
1200
+ const t = this.getSelectedTd(), n = t ? t.cellIndex : -1;
1201
+ for (let i = 0; i < e.rows.length; i++) {
1202
+ const o = e.rows[i], s = document.createElement("td");
1203
+ s.innerHTML = "<br>", n !== -1 ? o.cells[n].after(s) : o.appendChild(s);
1204
+ }
1205
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
1206
+ }
1207
+ /**
1208
+ * Deletes the currently selected column.
1209
+ */
1210
+ deleteColumn() {
1211
+ const e = this.getSelectedTd();
1212
+ if (!e) return;
1213
+ const t = this.getSelectedTable();
1214
+ if (!t) return;
1215
+ const n = e.cellIndex;
1216
+ if (t.rows[0].cells.length > 1) {
1217
+ const i = e.nextElementSibling || e.previousElementSibling;
1218
+ for (let o = 0; o < t.rows.length; o++)
1219
+ t.rows[o].cells[n].remove();
1220
+ if (i) {
1221
+ const o = document.createRange();
1222
+ o.selectNodeContents(i), o.collapse(!0), this.selection.restoreSelection(o);
1223
+ }
1224
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
1225
+ }
1226
+ }
1227
+ getSelectedTd() {
1228
+ const e = window.getSelection();
1229
+ if (!e || e.rangeCount === 0) return null;
1230
+ let t = e.anchorNode;
1231
+ for (; t && t !== this.editableElement; ) {
1232
+ if (t.nodeName === "TD") return t;
1233
+ t = t.parentNode;
1234
+ }
1235
+ return null;
1236
+ }
1237
+ getSelectedTable() {
1238
+ const e = this.getSelectedTd();
1239
+ return e ? e.closest("table") : null;
1240
+ }
1241
+ /**
1242
+ * Recursively removes a style property from all elements in a fragment.
1243
+ */
1244
+ clearStyleRecursive(e, t) {
1245
+ const n = document.createTreeWalker(e, NodeFilter.SHOW_ELEMENT);
1246
+ let i = n.nextNode();
1247
+ for (; i; )
1248
+ i.style.getPropertyValue(t) && i.style.removeProperty(t), i = n.nextNode();
1249
+ }
1250
+ /**
1251
+ * Applies an inline style to the selection.
1252
+ * This is used for properties like font-size (px) and font-family
1253
+ * where execCommand is outdated or limited.
1254
+ */
1255
+ /**
1256
+ * Applies an inline style to the selection.
1257
+ * This is used for properties like font-size (px) and font-family
1258
+ * where execCommand is outdated or limited.
1259
+ */
1260
+ setStyle(e, t, n) {
1261
+ if (!n) {
1262
+ const a = window.getSelection();
1263
+ if (!a || a.rangeCount === 0) return null;
1264
+ n = a.getRangeAt(0);
1265
+ }
1266
+ if (n.collapsed)
1267
+ return this.pendingStyles[e] = t, n;
1268
+ if (["line-height"].includes(e))
1269
+ return this.setBlockStyle(e, t, n);
1270
+ let o = n.commonAncestorContainer;
1271
+ o.nodeType === Node.TEXT_NODE && (o = o.parentElement);
1272
+ let s = null;
1273
+ if (o.tagName === "SPAN" && o.children.length === 0 && o.textContent === n.toString())
1274
+ o.style.setProperty(e, t), s = n.cloneRange();
1275
+ else {
1276
+ const a = document.createElement("span");
1277
+ a.style.setProperty(e, t);
1278
+ try {
1279
+ const l = o.tagName === "SPAN" ? o : null, r = n.extractContents();
1280
+ this.clearStyleRecursive(r, e), a.appendChild(r), n.insertNode(a), l && l.innerHTML === "" && l.remove();
1281
+ const d = document.createRange();
1282
+ d.selectNodeContents(a), s = d;
1283
+ const c = window.getSelection();
1284
+ c && c.rangeCount > 0 && (c.removeAllRanges(), c.addRange(d));
1285
+ } catch (l) {
1286
+ console.warn("Failed to apply style:", l);
1287
+ }
1288
+ }
1289
+ return this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 })), s;
1290
+ }
1291
+ /**
1292
+ * Applies a style to the block-level containers within the range.
1293
+ */
1294
+ setBlockStyle(e, t, n) {
1295
+ const i = ["P", "H1", "H2", "H3", "H4", "H5", "H6", "LI", "TD", "TH", "DIV", "BLOCKQUOTE"], o = /* @__PURE__ */ new Set();
1296
+ if (Array.from(this.editableElement.querySelectorAll(i.join(","))).forEach((a) => {
1297
+ n.intersectsNode(a) && o.add(a);
1298
+ }), o.size === 0) {
1299
+ let a = n.commonAncestorContainer;
1300
+ for (; a && a !== this.editableElement.parentElement; ) {
1301
+ if (a.nodeType === Node.ELEMENT_NODE && i.includes(a.tagName)) {
1302
+ o.add(a);
1303
+ break;
1304
+ }
1305
+ a = a.parentNode;
1306
+ }
1307
+ }
1308
+ return o.forEach((a) => {
1309
+ a.style.setProperty(e, t);
1310
+ }), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 })), n;
1311
+ }
1312
+ /**
1313
+ * Creates a link at the current selection.
1314
+ * Ensures the link opens in a new tab with proper security attributes.
1315
+ */
1316
+ createLink(e) {
1317
+ if (this.focus(), e = e.trim(), /^(javascript|vbscript|data|file):/i.test(e)) {
1318
+ console.warn("Security Warning: Blocked malicious URI scheme.");
1319
+ return;
1320
+ }
1321
+ const n = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(e);
1322
+ !/^https?:\/\//i.test(e) && !/^mailto:/i.test(e) && !e.startsWith("#") && (n ? e = "mailto:" + e : e = "https://" + e);
1323
+ const i = window.getSelection();
1324
+ if (i && i.rangeCount > 0) {
1325
+ const o = i.getRangeAt(0);
1326
+ if (o.collapsed) {
1327
+ const s = document.createTextNode(e);
1328
+ o.insertNode(s);
1329
+ const a = document.createRange();
1330
+ a.selectNodeContents(s), i.removeAllRanges(), i.addRange(a);
1331
+ }
1332
+ }
1333
+ if (document.execCommand("createLink", !1, e), i && i.rangeCount > 0) {
1334
+ let s = i.getRangeAt(0).commonAncestorContainer;
1335
+ s.nodeType === Node.TEXT_NODE && (s = s.parentElement);
1336
+ let a = null;
1337
+ s.tagName === "A" ? a = s : a = s.querySelector("a"), a && (a.setAttribute("target", "_blank"), a.setAttribute("rel", "noopener noreferrer"));
1338
+ }
1339
+ this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 }));
1340
+ }
1341
+ /**
1342
+ * Inserts an image at the current selection.
1343
+ */
1344
+ insertImage(e, t, n = !1) {
1345
+ this.focus();
1346
+ const i = this.selection.getRange();
1347
+ if (!i) return null;
1348
+ const o = document.createElement("figure");
1349
+ o.classList.add("te-image-container"), o.setAttribute("contenteditable", "false"), n && o.classList.add("is-loading");
1350
+ const s = document.createElement("img");
1351
+ s.src = e, s.classList.add("te-image"), t && s.setAttribute("data-image-id", t);
1352
+ const a = document.createElement("figcaption");
1353
+ 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) => {
1354
+ const h = document.createElement("div");
1355
+ h.classList.add("te-image-resizer", `te-resizer-${c}`), o.appendChild(h);
1356
+ }), o.appendChild(s), o.appendChild(a), i.deleteContents(), i.insertNode(o);
1357
+ const r = document.createElement("p");
1358
+ r.innerHTML = "<br>", o.after(r);
1359
+ const d = document.createRange();
1360
+ return d.setStart(r, 0), d.setEnd(r, 0), this.selection.restoreSelection(d), this.editableElement.dispatchEvent(new Event("input", { bubbles: !0 })), this.save(), o;
1361
+ }
1362
+ /**
1363
+ * Returns the clean and optimized HTML content of the editor.
1364
+ */
1365
+ getHTML() {
1366
+ return this.normalizeHTML(this.editableElement.innerHTML);
1367
+ }
1368
+ /**
1369
+ * Returns the plain text content of the editor.
1370
+ */
1371
+ getText() {
1372
+ return this.editableElement.innerText || this.editableElement.textContent || "";
1373
+ }
1374
+ /**
1375
+ * Returns the current character count based on plain text.
1376
+ */
1377
+ getCharCount() {
1378
+ return this.getText().replace(/\n$/, "").length;
1379
+ }
1380
+ /**
1381
+ * Normalizes the editor's content in-place.
1382
+ */
1383
+ normalize() {
1384
+ const e = this.editableElement.innerHTML, t = this.selection.getRange();
1385
+ let n = null, i = null;
1386
+ if (t && this.editableElement.contains(t.commonAncestorContainer)) {
1387
+ 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";
1388
+ const s = t.cloneRange();
1389
+ s.collapse(!0), s.insertNode(n);
1390
+ const a = t.cloneRange();
1391
+ a.collapse(!1), a.insertNode(i);
1392
+ }
1393
+ const o = this.normalizeHTML(this.editableElement.innerHTML);
1394
+ if (o !== e || n) {
1395
+ this.editableElement.innerHTML = o;
1396
+ const s = this.editableElement.querySelector("#te-selection-start"), a = this.editableElement.querySelector("#te-selection-end");
1397
+ if (s && a) {
1398
+ const l = document.createRange();
1399
+ l.setStartAfter(s), l.setEndBefore(a), this.selection.restoreSelection(l);
1400
+ }
1401
+ this.editableElement.querySelectorAll("#te-selection-start, #te-selection-end").forEach((l) => l.remove());
1402
+ }
1403
+ this.checkPlaceholder();
1404
+ }
1405
+ normalizationContainer = null;
1406
+ /**
1407
+ * Internal helper to strictly sanitize HTML strings.
1408
+ */
1409
+ sanitize(e) {
1410
+ v.addHook("afterSanitizeAttributes", (n) => {
1411
+ n.tagName === "A" && (n.setAttribute("target", "_blank"), n.setAttribute("rel", "noopener noreferrer"));
1412
+ });
1413
+ const t = v.sanitize(e, {
1414
+ ALLOWED_TAGS: [
1415
+ "b",
1416
+ "i",
1417
+ "u",
1418
+ "s",
1419
+ "span",
1420
+ "div",
1421
+ "p",
1422
+ "br",
1423
+ "a",
1424
+ "h1",
1425
+ "h2",
1426
+ "h3",
1427
+ "h4",
1428
+ "h5",
1429
+ "h6",
1430
+ "ul",
1431
+ "ol",
1432
+ "li",
1433
+ "blockquote",
1434
+ "hr",
1435
+ "pre",
1436
+ "code",
1437
+ "img",
1438
+ "table",
1439
+ "tbody",
1440
+ "tr",
1441
+ "td",
1442
+ "th",
1443
+ "thead",
1444
+ "tfoot",
1445
+ "figure",
1446
+ "figcaption"
1447
+ ],
1448
+ ALLOWED_ATTR: [
1449
+ "href",
1450
+ "src",
1451
+ "alt",
1452
+ "style",
1453
+ "color",
1454
+ "background-color",
1455
+ "class",
1456
+ "id",
1457
+ "target",
1458
+ "rel",
1459
+ "contenteditable",
1460
+ "data-placeholder",
1461
+ "data-image-id"
1462
+ ],
1463
+ ALLOW_DATA_ATTR: !0,
1464
+ FORBID_TAGS: ["script", "style", "iframe", "object", "embed", "form", "textarea"],
1465
+ FORBID_ATTR: ["onerror", "onload", "onclick", "onmouseover"]
1466
+ });
1467
+ return v.removeHook("afterSanitizeAttributes"), t;
1468
+ }
1469
+ /**
1470
+ * Optimizes HTML by fixing invalid nesting and removing redundant tags.
1471
+ */
1472
+ normalizeHTML(e) {
1473
+ this.normalizationContainer || (this.normalizationContainer = document.createElement("div"));
1474
+ const t = this.normalizationContainer;
1475
+ t.innerHTML = e;
1476
+ const n = Array.from(t.childNodes);
1477
+ let i = null;
1478
+ n.forEach((r) => {
1479
+ if (r.nodeType === Node.TEXT_NODE || r.nodeType === Node.ELEMENT_NODE && !["P", "DIV", "H1", "H2", "H3", "H4", "H5", "H6", "UL", "OL", "TABLE", "BLOCKQUOTE", "PRE", "HR", "FIGURE"].includes(r.tagName)) {
1480
+ if (r.nodeType === Node.TEXT_NODE && (r.textContent || "").trim() === "" && !i)
1481
+ return;
1482
+ i || (i = document.createElement("p"), r.before(i)), i.appendChild(r);
1483
+ } else
1484
+ i = null;
1485
+ }), t.querySelectorAll("p").forEach((r) => {
1486
+ const d = r.querySelectorAll("ul, ol, table, h1, h2, h3, h4, h5, h6, pre, blockquote");
1487
+ d.length > 0 && (d.forEach((c) => {
1488
+ r.after(c);
1489
+ }), (r.innerHTML.trim() === "" || r.innerHTML.trim() === "<br>") && r.remove());
1490
+ }), t.querySelectorAll("span").forEach((r) => {
1491
+ if (!r.id.startsWith("te-selection-"))
1492
+ if (r.attributes.length === 0) {
1493
+ const d = document.createTextNode(r.textContent || "");
1494
+ r.replaceWith(d);
1495
+ } else r.innerHTML.trim() === "" && r.remove();
1496
+ });
1497
+ const s = Array.from(t.querySelectorAll("p"));
1498
+ s.forEach((r) => {
1499
+ r.innerHTML.trim() === "" && t.childNodes.length > 1 && r !== t.lastElementChild && r.remove();
1500
+ });
1501
+ for (let r = s.length - 1; r >= 0; r--) {
1502
+ const d = s[r], c = d.innerHTML.trim() === "" || d.innerHTML.trim() === "<br>", h = d === t.lastElementChild;
1503
+ if (c && h && t.children.length > 1)
1504
+ d.remove();
1505
+ else
1506
+ break;
1507
+ }
1508
+ const a = t.lastElementChild;
1509
+ if (a && ["PRE", "TABLE", "FIGURE", "BLOCKQUOTE", "UL", "OL", "HR"].includes(a.tagName)) {
1510
+ const r = document.createElement("p");
1511
+ r.innerHTML = "<br>", t.appendChild(r);
1512
+ }
1513
+ return t.innerHTML.trim() === "<p><br></p>" || t.innerHTML.trim() === "<p></p>" ? "" : this.sanitize(t.innerHTML);
1514
+ }
1515
+ // Handle paste events to sanitize inherited malware and styles
1516
+ handlePaste(e) {
1517
+ e.preventDefault();
1518
+ let t = (e.clipboardData || window.clipboardData).getData("text/plain"), n = (e.clipboardData || window.clipboardData).getData("text/html");
1519
+ if (e.clipboardData && e.clipboardData.items) {
1520
+ const o = [];
1521
+ for (let s = 0; s < e.clipboardData.items.length; s++) {
1522
+ const a = e.clipboardData.items[s];
1523
+ if (a.type.startsWith("image/")) {
1524
+ const l = a.getAsFile();
1525
+ l && o.push(l);
1526
+ }
1527
+ }
1528
+ if (o.length > 0) {
1529
+ this.handleFiles(o);
1530
+ return;
1531
+ }
1532
+ }
1533
+ if (this.options.maxCharCount && this.options.strictCharLimit) {
1534
+ const o = this.getCharCount(), s = window.getSelection();
1535
+ let a = 0;
1536
+ s && s.rangeCount > 0 && (a = s.toString().length);
1537
+ const l = this.options.maxCharCount - (o - a);
1538
+ if (l <= 0)
1539
+ return;
1540
+ t.length > l && (t = t.substring(0, l), n = "");
1541
+ }
1542
+ const i = /<([a-z1-6]+)\b[^>]*>[\s\S]*<\/\1>/i.test(t) || /^\s*<[a-z1-6]+\b[^>]*>/i.test(t);
1543
+ if (!n && t && i && (n = t.replace(/(\r\n|\n|\r)/gm, " ").replace(/>\s+</g, "><").trim()), n) {
1544
+ const o = this.sanitize(n);
1545
+ this.execute("insertHTML", o);
1546
+ } else t && this.execute("insertText", t);
1547
+ }
1548
+ /**
1549
+ * Sets the HTML content of the editor.
1550
+ */
1551
+ setHTML(e) {
1552
+ const t = this.sanitize(e);
1553
+ this.editableElement.innerHTML = t;
1554
+ }
1555
+ /**
1556
+ * Internal access to the editable element.
1557
+ */
1558
+ get el() {
1559
+ return this.editableElement;
1560
+ }
1561
+ /**
1562
+ * Returns the editor options.
1563
+ */
1564
+ getOptions() {
1565
+ return this.options;
1566
+ }
1567
+ /**
1568
+ * Internal helper to handle multiple files.
1569
+ */
1570
+ async handleFiles(e) {
1571
+ const t = this.options.maxImageSizeMB || 5;
1572
+ for (const n of e) {
1573
+ if (!n.type.startsWith("image/")) continue;
1574
+ let i = null;
1575
+ try {
1576
+ if (n.size > t * 1024 * 1024 * 3) {
1577
+ console.warn(`File ${n.name} is too large to even attempt processing.`);
1578
+ continue;
1579
+ }
1580
+ this.options.onSaving && this.options.onSaving();
1581
+ const o = URL.createObjectURL(n);
1582
+ i = this.insertImage(o, void 0, !0);
1583
+ const s = await y.compressImage(n, t), a = URL.createObjectURL(s);
1584
+ if (i) {
1585
+ const r = i.querySelector("img");
1586
+ r && (r.src = a);
1587
+ }
1588
+ if (s.size > t * 1024 * 1024) {
1589
+ alert(`Image "${n.name}" exceeds the ${t}MB limit even after compression.`), i?.remove();
1590
+ continue;
1591
+ }
1592
+ const l = await y.uploadFile(s, this.options);
1593
+ if (l)
1594
+ if (i) {
1595
+ const r = i.querySelector("img");
1596
+ r && (r.src = l.imageUrl, l.imageId && r.setAttribute("data-image-id", l.imageId)), i.classList.remove("is-loading");
1597
+ } else
1598
+ this.insertImage(l.imageUrl, l.imageId);
1599
+ else {
1600
+ const r = new FileReader();
1601
+ r.onload = (d) => {
1602
+ const c = d.target?.result;
1603
+ if (i) {
1604
+ const h = i.querySelector("img");
1605
+ h && (h.src = c), i.classList.remove("is-loading");
1606
+ } else
1607
+ this.insertImage(c);
1608
+ }, r.readAsDataURL(s);
1609
+ }
1610
+ } catch (o) {
1611
+ console.error("Image handling failed", o), i?.remove();
1612
+ } finally {
1613
+ this.options.onSave && this.save();
1614
+ }
1615
+ }
1616
+ }
1617
+ }
1618
+ class Q {
1619
+ container;
1620
+ searchInput;
1621
+ emojiGrid;
1622
+ onSelect;
1623
+ onClose;
1624
+ emojiList = [];
1625
+ theme;
1626
+ dark;
1627
+ constructor(e, t, n, i) {
1628
+ 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();
1629
+ }
1630
+ async loadEmojis() {
1631
+ this.emojiGrid.textContent = "Loading...";
1632
+ try {
1633
+ const { EMOJI_LIST: e } = await import("./EmojiList-B-C3-zN2.js");
1634
+ this.emojiList = e, this.renderEmojis(this.emojiList);
1635
+ } catch (e) {
1636
+ console.error("Failed to load emojis:", e), this.emojiGrid.textContent = "Failed to load";
1637
+ }
1638
+ }
1639
+ createPickerElement() {
1640
+ const e = document.createElement("div");
1641
+ return e.classList.add("te-emoji-picker"), this.theme && this.applyTheme(e, this.theme), this.dark && e.classList.add("te-dark"), e.innerHTML = `
1642
+ <div class="te-emoji-header">
1643
+ <input type="text" class="te-emoji-search" placeholder="Search emoji...">
1644
+ </div>
1645
+ <div class="te-emoji-body">
1646
+ <div class="te-emoji-grid"></div>
1647
+ </div>
1648
+ `, e;
1649
+ }
1650
+ setupEvents() {
1651
+ this.searchInput.addEventListener("mousedown", (t) => t.stopPropagation()), this.searchInput.addEventListener("click", (t) => t.stopPropagation()), this.searchInput.addEventListener("input", () => {
1652
+ const t = this.searchInput.value.toLowerCase(), n = this.emojiList.filter(
1653
+ (i) => i.name.toLowerCase().includes(t) || i.category.toLowerCase().includes(t)
1654
+ );
1655
+ this.renderEmojis(n);
1656
+ });
1657
+ const e = (t) => {
1658
+ this.container.contains(t.target) || (this.close(), document.removeEventListener("mousedown", e));
1659
+ };
1660
+ setTimeout(() => document.addEventListener("mousedown", e), 0);
1661
+ }
1662
+ renderEmojis(e) {
1663
+ if (this.emojiGrid.innerHTML = "", e.length === 0) {
1664
+ this.emojiGrid.textContent = "No emoji found";
1665
+ return;
1666
+ }
1667
+ this.searchInput.value.length > 0 ? this.renderGridItems(e) : ["Smileys", "Symbols", "Hands", "Animals", "Food", "Travel", "Objects", "Activities"].forEach((i) => {
1668
+ const o = e.filter((s) => s.category === i);
1669
+ if (o.length > 0) {
1670
+ const s = document.createElement("div");
1671
+ s.classList.add("te-emoji-category-title"), s.textContent = i, this.emojiGrid.appendChild(s), this.renderGridItems(o);
1672
+ }
1673
+ });
1674
+ }
1675
+ renderGridItems(e) {
1676
+ e.forEach((t) => {
1677
+ const n = document.createElement("button");
1678
+ n.type = "button", n.classList.add("te-emoji-item"), n.textContent = t.emoji, n.title = t.name, n.addEventListener("click", () => {
1679
+ this.onSelect(t.emoji), this.close();
1680
+ }), this.emojiGrid.appendChild(n);
1681
+ });
1682
+ }
1683
+ applyTheme(e, t) {
1684
+ const n = {
1685
+ primaryColor: "--te-primary-color",
1686
+ primaryHover: "--te-primary-hover",
1687
+ bgApp: "--te-bg-app",
1688
+ bgEditor: "--te-bg-editor",
1689
+ toolbarBg: "--te-toolbar-bg",
1690
+ borderColor: "--te-border-color",
1691
+ borderFocus: "--te-border-focus",
1692
+ textMain: "--te-text-main",
1693
+ textMuted: "--te-text-muted",
1694
+ placeholder: "--te-placeholder",
1695
+ btnHover: "--te-btn-hover",
1696
+ btnActive: "--te-btn-active",
1697
+ radiusLg: "--te-radius-lg",
1698
+ radiusMd: "--te-radius-md",
1699
+ radiusSm: "--te-radius-sm",
1700
+ shadowSm: "--te-shadow-sm",
1701
+ shadowMd: "--te-shadow-md",
1702
+ shadowLg: "--te-shadow-lg"
1703
+ };
1704
+ for (const [i, o] of Object.entries(n)) {
1705
+ const s = t[i];
1706
+ s && e.style.setProperty(o, s);
1707
+ }
1708
+ }
1709
+ show(e) {
1710
+ document.body.appendChild(this.container);
1711
+ const t = e.getBoundingClientRect(), n = 280;
1712
+ let i = t.bottom + window.scrollY + 5, o = t.left + window.scrollX;
1713
+ o + n > window.innerWidth && (o = window.innerWidth - n - 10), this.container.style.top = `${i}px`, this.container.style.left = `${o}px`, this.searchInput.focus();
1714
+ }
1715
+ close() {
1716
+ this.container.parentElement && (this.container.remove(), this.onClose());
1717
+ }
1718
+ get el() {
1719
+ return this.container;
1720
+ }
1721
+ }
1722
+ class Z {
1723
+ editor;
1724
+ container;
1725
+ savedRange = null;
1726
+ items = b;
1727
+ activePicker = null;
1728
+ activeModal = null;
1729
+ statusEl = null;
1730
+ charCountEl = null;
1731
+ saveStatusEl = null;
1732
+ boundUpdateActiveStates;
1733
+ itemElements = /* @__PURE__ */ new Map();
1734
+ constructor(e) {
1735
+ this.editor = e, this.container = this.createToolbarElement(), this.boundUpdateActiveStates = this.updateActiveStates.bind(this), this.render();
1736
+ }
1737
+ createToolbarElement() {
1738
+ const e = document.createElement("div");
1739
+ 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", this.saveStatusEl = document.createElement("span"), this.charCountEl = document.createElement("span"), this.charCountEl.style.fontWeight = "500", this.statusEl.appendChild(this.charCountEl), this.statusEl.appendChild(this.saveStatusEl), e;
1740
+ }
1741
+ render() {
1742
+ const e = this.editor.getOptions().toolbarItems, t = [];
1743
+ this.items.forEach((o) => {
1744
+ (o.type === "divider" || o.id && (!e || e.includes(o.id))) && t.push(o);
1745
+ });
1746
+ const n = [];
1747
+ t.forEach((o, s) => {
1748
+ if (o.type === "divider") {
1749
+ if (n.length === 0 || n[n.length - 1].type === "divider" || !t.slice(s + 1).some((l) => l.type !== "divider")) return;
1750
+ n.push(o);
1751
+ } else
1752
+ n.push(o);
1753
+ }), n.forEach((o) => {
1754
+ if (o.type === "button")
1755
+ this.renderButton(o);
1756
+ else if (o.type === "select")
1757
+ this.renderSelect(o);
1758
+ else if (o.type === "input")
1759
+ this.renderInput(o);
1760
+ else if (o.type === "color-picker")
1761
+ this.renderColorPicker(o);
1762
+ else if (o.type === "divider") {
1763
+ const s = document.createElement("div");
1764
+ s.classList.add("te-divider"), this.container.appendChild(s);
1765
+ }
1766
+ }), this.editor.getOptions().showStatus !== !1 && this.container.appendChild(this.statusEl), this.editor.el.addEventListener("keyup", this.boundUpdateActiveStates), this.editor.el.addEventListener("mouseup", this.boundUpdateActiveStates);
1767
+ }
1768
+ renderButton(e) {
1769
+ const t = document.createElement("button");
1770
+ t.classList.add("te-button"), t.innerHTML = e.icon || "", t.title = e.title, this.itemElements.set(e, t), t.addEventListener("mousedown", (n) => {
1771
+ if (n.preventDefault(), e.command === "createLink" || e.command === "insertTable" || e.command === "insertImage") {
1772
+ const i = window.getSelection();
1773
+ if (i && i.rangeCount > 0) {
1774
+ const o = i.getRangeAt(0);
1775
+ this.editor.el.contains(o.commonAncestorContainer) && (this.savedRange = o.cloneRange());
1776
+ }
1777
+ }
1778
+ if (e.command === "insertEmoji") {
1779
+ this.activePicker ? this.activePicker.close() : (this.activePicker = new Q(
1780
+ (i) => {
1781
+ this.editor.execute("insertText", i);
1782
+ },
1783
+ () => {
1784
+ this.activePicker = null;
1785
+ },
1786
+ this.editor.getOptions().theme,
1787
+ this.editor.getOptions().dark
1788
+ ), this.activePicker.show(t));
1789
+ return;
1790
+ }
1791
+ if (e.command === "insertImage") {
1792
+ this.activeModal && this.activeModal.close(), this.activeModal = new g(
1793
+ "Insert Image",
1794
+ [
1795
+ { id: "url", label: "Image URL", type: "text", placeholder: "https://example.com/image.jpg" },
1796
+ { id: "file", label: "Or Upload File", type: "file" }
1797
+ ],
1798
+ (i) => {
1799
+ if (this.savedRange) {
1800
+ const o = window.getSelection();
1801
+ o && (o.removeAllRanges(), o.addRange(this.savedRange));
1802
+ }
1803
+ i.file ? this.editor.handleFiles([i.file]) : i.url && i.url.trim() !== "" && this.editor.insertImage(i.url), this.savedRange = null;
1804
+ },
1805
+ () => {
1806
+ this.activeModal = null, this.savedRange = null;
1807
+ },
1808
+ this.editor.getOptions().theme,
1809
+ this.editor.getOptions().dark
1810
+ ), this.activeModal.show(t);
1811
+ return;
1812
+ }
1813
+ if (["addRow", "deleteRow", "addColumn", "deleteColumn"].includes(e.command || "")) {
1814
+ const i = e.command;
1815
+ this.editor[i]();
1816
+ return;
1817
+ }
1818
+ if (e.command === "undo") {
1819
+ this.editor.undo();
1820
+ return;
1821
+ }
1822
+ if (e.command === "redo") {
1823
+ this.editor.redo();
1824
+ return;
1825
+ }
1826
+ if (e.command === "createLink") {
1827
+ this.activeModal && this.activeModal.close(), this.activeModal = new g(
1828
+ "Insert Link",
1829
+ [{ id: "url", label: "URL", type: "text", placeholder: "https://example.com" }],
1830
+ (i) => {
1831
+ if (this.savedRange) {
1832
+ const o = window.getSelection();
1833
+ o && (o.removeAllRanges(), o.addRange(this.savedRange));
1834
+ }
1835
+ this.editor.createLink(i.url), this.savedRange = null;
1836
+ },
1837
+ () => {
1838
+ this.activeModal = null, this.savedRange = null;
1839
+ },
1840
+ this.editor.getOptions().theme,
1841
+ this.editor.getOptions().dark
1842
+ ), this.activeModal.show(t);
1843
+ return;
1844
+ }
1845
+ if (e.command === "insertTable") {
1846
+ this.activeModal && this.activeModal.close(), this.activeModal = new g(
1847
+ "Insert Table",
1848
+ [
1849
+ { id: "rows", label: "Rows", type: "number", defaultValue: "3", min: "1" },
1850
+ { id: "cols", label: "Columns", type: "number", defaultValue: "3", min: "1" }
1851
+ ],
1852
+ (i) => {
1853
+ if (this.savedRange) {
1854
+ const a = window.getSelection();
1855
+ a && (a.removeAllRanges(), a.addRange(this.savedRange));
1856
+ }
1857
+ let o = parseInt(i.rows, 10), s = parseInt(i.cols, 10);
1858
+ (isNaN(o) || o < 1) && (o = 1), (isNaN(s) || s < 1) && (s = 1), this.editor.insertTable(o, s), this.savedRange = null;
1859
+ },
1860
+ () => {
1861
+ this.activeModal = null, this.savedRange = null;
1862
+ },
1863
+ this.editor.getOptions().theme,
1864
+ this.editor.getOptions().dark
1865
+ ), this.activeModal.show(t);
1866
+ return;
1867
+ }
1868
+ e.command && this.editor.execute(e.command, e.value || null), this.updateActiveStates();
1869
+ }), this.container.appendChild(t);
1870
+ }
1871
+ renderInput(e) {
1872
+ const t = document.createElement("input");
1873
+ t.type = "number", t.classList.add("te-input"), t.title = e.title, t.value = e.value || "", t.min = "1", t.max = "100";
1874
+ const n = () => {
1875
+ const o = window.getSelection();
1876
+ if (o && o.rangeCount > 0) {
1877
+ const s = o.getRangeAt(0);
1878
+ this.editor.el.contains(s.commonAncestorContainer) && (this.savedRange = s.cloneRange());
1879
+ }
1880
+ };
1881
+ t.addEventListener("mousedown", n), t.addEventListener("focus", n);
1882
+ const i = () => {
1883
+ let o = parseInt(t.value, 10);
1884
+ if (!isNaN(o) && (o = Math.max(1, Math.min(100, o)), t.value = o.toString(), e.command === "fontSize")) {
1885
+ if (this.savedRange) {
1886
+ const s = this.editor.setStyle("font-size", `${o}px`, this.savedRange);
1887
+ s && (this.savedRange = s);
1888
+ } else
1889
+ this.editor.setStyle("font-size", `${o}px`);
1890
+ t.focus();
1891
+ }
1892
+ };
1893
+ t.addEventListener("input", i), t.addEventListener("keydown", (o) => {
1894
+ o.key === "Enter" && (i(), this.editor.focus());
1895
+ }), this.container.appendChild(t);
1896
+ }
1897
+ renderSelect(e) {
1898
+ const t = document.createElement("select");
1899
+ t.classList.add("te-select"), t.title = e.title, e.options && e.options.forEach((n) => {
1900
+ const i = document.createElement("option");
1901
+ i.value = n.value, i.textContent = n.label, t.appendChild(i);
1902
+ }), t.addEventListener("change", () => {
1903
+ const n = t.value;
1904
+ 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();
1905
+ }), t.addEventListener("mousedown", () => {
1906
+ this.savedRange = this.editor.selection.saveSelection();
1907
+ }), this.container.appendChild(t);
1908
+ }
1909
+ renderColorPicker(e) {
1910
+ const t = document.createElement("div");
1911
+ if (t.classList.add("te-color-picker-wrapper"), t.title = e.title, e.icon) {
1912
+ const i = document.createElement("div");
1913
+ i.classList.add("te-button", "te-color-icon"), i.innerHTML = e.icon;
1914
+ const o = document.createElement("div");
1915
+ o.classList.add("te-color-indicator"), o.style.backgroundColor = e.value || "#000000", i.appendChild(o), this.itemElements.set(e, i), t.appendChild(i);
1916
+ }
1917
+ const n = document.createElement("input");
1918
+ 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", () => {
1919
+ this.savedRange = this.editor.selection.saveSelection();
1920
+ }), n.addEventListener("input", () => {
1921
+ if (e.icon) {
1922
+ const i = t.querySelector(".te-color-indicator");
1923
+ i && (i.style.backgroundColor = n.value);
1924
+ }
1925
+ }), n.addEventListener("change", () => {
1926
+ if (this.savedRange && this.editor.selection.restoreSelection(this.savedRange), e.command === "foreColor") {
1927
+ const i = this.savedRange || void 0;
1928
+ this.editor.setStyle("color", n.value, i);
1929
+ } else if (e.command === "backColor") {
1930
+ const i = this.savedRange || void 0;
1931
+ this.editor.setStyle("background-color", n.value, i);
1932
+ } else e.command && this.editor.execute(e.command, n.value);
1933
+ this.editor.focus();
1934
+ }), t.appendChild(n), this.container.appendChild(e.icon ? t : n);
1935
+ }
1936
+ get el() {
1937
+ return this.container;
1938
+ }
1939
+ updateActiveStates() {
1940
+ this.items.forEach((e) => {
1941
+ const t = this.itemElements.get(e);
1942
+ t && e.type === "button" && (e.command && document.queryCommandState(e.command) ? t.classList.add("active") : t.classList.remove("active"));
1943
+ });
1944
+ }
1945
+ updateStatus(e, t = !1) {
1946
+ if (!this.saveStatusEl || this.editor.getOptions().showStatus === !1) return;
1947
+ if (this.saveStatusEl.textContent = "", t) {
1948
+ const i = document.createElement("div");
1949
+ i.classList.add("te-toolbar-loader"), this.saveStatusEl.appendChild(i);
1950
+ }
1951
+ const n = document.createElement("span");
1952
+ n.textContent = e, this.saveStatusEl.appendChild(n);
1953
+ }
1954
+ updateMetrics() {
1955
+ const e = this.editor.getOptions();
1956
+ if (!this.charCountEl || e.showCharCount === !1) {
1957
+ this.charCountEl && (this.charCountEl.textContent = "");
1958
+ return;
1959
+ }
1960
+ const t = this.editor.getCharCount(), n = e.maxCharCount;
1961
+ n ? (this.charCountEl.textContent = `Chars: ${t}/${n}`, t > n ? this.charCountEl.style.color = "#ef4444" : this.charCountEl.style.color = "inherit") : (this.charCountEl.textContent = `Chars: ${t}`, this.charCountEl.style.color = "inherit"), this.charCountEl.textContent && this.saveStatusEl?.textContent ? (this.charCountEl.style.marginRight = "8px", this.charCountEl.style.borderRight = "1px solid var(--te-border-color)", this.charCountEl.style.paddingRight = "8px") : (this.charCountEl.style.marginRight = "0", this.charCountEl.style.borderRight = "none", this.charCountEl.style.paddingRight = "0");
1962
+ }
1963
+ destroy() {
1964
+ 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);
1965
+ }
1966
+ }
1967
+ class te extends J {
1968
+ toolbar;
1969
+ constructor(e, t = {}) {
1970
+ const n = {
1971
+ ...t,
1972
+ onSaving: () => {
1973
+ this.toolbar?.updateStatus("Auto saving...", !0), t.onSaving && t.onSaving();
1974
+ },
1975
+ onSave: (o) => {
1976
+ const s = (/* @__PURE__ */ new Date()).toLocaleString([], {
1977
+ year: "numeric",
1978
+ month: "short",
1979
+ day: "numeric",
1980
+ hour: "2-digit",
1981
+ minute: "2-digit",
1982
+ hour12: !0
1983
+ });
1984
+ this.toolbar?.updateStatus(`Saved at ${s}`, !1), t.onSave && t.onSave(o);
1985
+ }
1986
+ };
1987
+ if (super(e, n), typeof document > "u" || !e) {
1988
+ this.toolbar = {};
1989
+ return;
1990
+ }
1991
+ this.toolbar = new Z(this);
1992
+ const i = n.toolbarPosition || "top";
1993
+ i === "top" ? this.container.insertBefore(this.toolbar.el, this.editableElement) : i === "bottom" ? (this.container.appendChild(this.toolbar.el), this.container.classList.add("te-toolbar-bottom")) : i === "left" ? (this.container.insertBefore(this.toolbar.el, this.editableElement), this.container.classList.add("te-toolbar-left")) : i === "right" ? (this.container.appendChild(this.toolbar.el), this.container.classList.add("te-toolbar-right")) : i === "floating" && this.container.classList.add("te-toolbar-floating"), n.showStatus !== !1 && this.toolbar && i !== "floating" && this.toolbar.updateStatus("All changes saved", !1), n.showCharCount && this.toolbar.updateMetrics(), this.el.addEventListener("input", () => {
1994
+ this.toolbar?.updateMetrics();
1995
+ }), this.triggerChange();
1996
+ }
1997
+ getToolbar() {
1998
+ return this.toolbar;
1999
+ }
2000
+ destroy() {
2001
+ this.toolbar && this.toolbar.destroy(), super.destroy();
2002
+ }
2003
+ }
2004
+ export {
2005
+ J as CoreEditor,
2006
+ w as HistoryManager,
2007
+ te as InkflowEditor,
2008
+ E as SelectionManager,
2009
+ Z as Toolbar
2010
+ };