@labnol/email-editor 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,1772 @@
1
+ import { EditorContent, useEditor, useEditorState } from "@tiptap/react";
2
+ import { forwardRef, useCallback, useEffect, useId, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState } from "react";
3
+ import { FileHandler } from "@tiptap/extension-file-handler";
4
+ import { Image } from "@tiptap/extension-image";
5
+ import { TextAlign } from "@tiptap/extension-text-align";
6
+ import { BackgroundColor, Color, TextStyle } from "@tiptap/extension-text-style";
7
+ import { Placeholder } from "@tiptap/extensions";
8
+ import { Markdown } from "@tiptap/markdown";
9
+ import { StarterKit } from "@tiptap/starter-kit";
10
+ import { Extension } from "@tiptap/core";
11
+ import { Plugin, PluginKey } from "@tiptap/pm/state";
12
+ import { Decoration, DecorationSet } from "@tiptap/pm/view";
13
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
14
+ // src/colors.js
15
+ var DEFAULT_COLORS = [
16
+ "#000000",
17
+ "#e60000",
18
+ "#ff9900",
19
+ "#ffff00",
20
+ "#008a00",
21
+ "#0066cc",
22
+ "#9933ff",
23
+ "#ffffff",
24
+ "#facccc",
25
+ "#ffebcc",
26
+ "#ffffcc",
27
+ "#cce8cc",
28
+ "#cce0f5",
29
+ "#ebd6ff",
30
+ "#bbbbbb",
31
+ "#f06666",
32
+ "#ffc266",
33
+ "#ffff66",
34
+ "#66b966",
35
+ "#66a3e0",
36
+ "#c285ff",
37
+ "#888888",
38
+ "#a10000",
39
+ "#b26b00",
40
+ "#b2b200",
41
+ "#006100",
42
+ "#0047b2",
43
+ "#6b24b2",
44
+ "#444444",
45
+ "#5c0000",
46
+ "#663d14",
47
+ "#666600",
48
+ "#003700",
49
+ "#002966",
50
+ "#3d1466"
51
+ ];
52
+ // src/direction.js
53
+ var RTL_SCRIPT = /[\p{Script=Arabic}\p{Script=Hebrew}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Nko}\p{Script=Adlam}\p{Script=Samaritan}\p{Script=Mandaic}\p{Script=Hanifi_Rohingya}\p{Script=Yezidi}\p{Script=Mende_Kikakui}\p{Script=Old_Hungarian}\p{Script=Imperial_Aramaic}\p{Script=Phoenician}\p{Script=Avestan}\p{Script=Kharoshthi}\p{Script=Old_South_Arabian}\p{Script=Nabataean}\p{Script=Palmyrene}\p{Script=Manichaean}\p{Script=Psalter_Pahlavi}\p{Script=Inscriptional_Pahlavi}\p{Script=Inscriptional_Parthian}\p{Script=Old_North_Arabian}\p{Script=Hatran}\p{Script=Elymaic}\p{Script=Sogdian}\p{Script=Old_Sogdian}\p{Script=Chorasmian}\p{Script=Lydian}\p{Script=Cypriot}\p{Script=Old_Turkic}]/u;
54
+ var LETTER = /\p{L}/u;
55
+ var FIELDS = /\{\{[^{}]*\}\}|<<[^<>]*>>/g;
56
+ /**
57
+ * The direction of a piece of text, from its first letter: the Unicode bidi rule Gmail and Google Docs use for
58
+ * "auto" direction, except that {{merge fields}} are skipped.
59
+ * @param {string | null | undefined} text
60
+ * @returns {'rtl' | 'ltr' | null} null when the text has no letters
61
+ */
62
+ var detectDirection = (text) => {
63
+ if (!text) return null;
64
+ for (const char of String(text).replace(FIELDS, " ")) if (LETTER.test(char)) return RTL_SCRIPT.test(char) ? "rtl" : "ltr";
65
+ return null;
66
+ };
67
+ // src/textDirection.js
68
+ var TEXT_BLOCKS = ["paragraph", "heading"];
69
+ var CONTAINERS = [
70
+ "blockquote",
71
+ "bulletList",
72
+ "orderedList"
73
+ ];
74
+ var DIRECTIONS$1 = ["ltr", "rtl"];
75
+ var validDirection = (value) => DIRECTIONS$1.includes(value) ? value : null;
76
+ /**
77
+ * The dir attribute to write for a direction. In a left-to-right editor, left-to-right blocks get none, so ordinary
78
+ * emails stay free of dir attributes. In a right-to-left editor every block says which way it runs, because the
79
+ * email has no wrapper element to inherit a direction from.
80
+ * @param {'ltr' | 'rtl'} direction
81
+ * @param {'ltr' | 'rtl'} base The editor's direction
82
+ */
83
+ var attributeFor = (direction, base) => direction === "ltr" && base === "ltr" ? null : direction;
84
+ /**
85
+ * Text direction per block, so Arabic, Hebrew, Persian and Urdu paragraphs run right to left in the email.
86
+ *
87
+ * Each paragraph and heading takes its direction from its first letter (merge fields skipped), unless the writer
88
+ * picked one with setDirection. Lists and quotes follow their first paragraph, so bullets and the quote bar sit on
89
+ * the right side. Blocks with no letters take the editor's own direction.
90
+ *
91
+ * The attributes are `dir`, written to the HTML, and `dirAuto`, kept only in the editor: false once the writer has
92
+ * chosen a direction. HTML loaded with a dir that matches its text is treated as automatic, so editing it later
93
+ * still follows the text.
94
+ */
95
+ var TextDirection = Extension.create({
96
+ name: "emailTextDirection",
97
+ addOptions() {
98
+ return { direction: "ltr" };
99
+ },
100
+ addGlobalAttributes() {
101
+ return [{
102
+ types: [...TEXT_BLOCKS, ...CONTAINERS],
103
+ attributes: {
104
+ dir: {
105
+ default: null,
106
+ parseHTML: (element) => validDirection(element.getAttribute("dir")),
107
+ renderHTML: ({ dir }) => dir ? { dir } : {}
108
+ },
109
+ dirAuto: {
110
+ default: true,
111
+ rendered: false,
112
+ parseHTML: (element) => {
113
+ const dir = validDirection(element.getAttribute("dir"));
114
+ if (!dir) return true;
115
+ const detected = detectDirection(element.textContent);
116
+ return detected === null || detected === dir;
117
+ }
118
+ }
119
+ }
120
+ }];
121
+ },
122
+ addCommands() {
123
+ return {
124
+ /**
125
+ * Set the direction of the blocks in the selection: 'auto' follows the text again.
126
+ * @param {'auto' | 'ltr' | 'rtl'} mode
127
+ */
128
+ setDirection: (mode) => ({ tr, state, dispatch }) => {
129
+ const { from, to } = state.selection;
130
+ const base = this.options.direction;
131
+ if (dispatch) state.doc.nodesBetween(from, to, (node, pos) => {
132
+ if (!TEXT_BLOCKS.includes(node.type.name) && !CONTAINERS.includes(node.type.name)) return;
133
+ const attrs = mode === "auto" ? { dirAuto: true } : {
134
+ dirAuto: false,
135
+ dir: attributeFor(mode, base) ?? mode
136
+ };
137
+ tr.setNodeMarkup(pos, void 0, {
138
+ ...node.attrs,
139
+ ...attrs
140
+ });
141
+ });
142
+ return true;
143
+ } };
144
+ },
145
+ addProseMirrorPlugins() {
146
+ const base = this.options.direction;
147
+ const effective = (node) => node.attrs.dir ?? base;
148
+ return [new Plugin({
149
+ key: new PluginKey("emailTextDirection"),
150
+ appendTransaction: (transactions, _oldState, newState) => {
151
+ if (!transactions.some((transaction) => transaction.docChanged)) return null;
152
+ const { tr } = newState;
153
+ newState.doc.descendants((node, pos) => {
154
+ if (!TEXT_BLOCKS.includes(node.type.name) || node.attrs.dirAuto === false) return;
155
+ const dir = attributeFor(detectDirection(node.textContent) ?? base, base);
156
+ if (dir !== node.attrs.dir) tr.setNodeMarkup(pos, void 0, {
157
+ ...node.attrs,
158
+ dir
159
+ });
160
+ });
161
+ const containers = [];
162
+ tr.doc.descendants((node, pos) => {
163
+ if (CONTAINERS.includes(node.type.name) && node.attrs.dirAuto !== false) containers.push({ pos });
164
+ });
165
+ containers.reverse().forEach(({ pos }) => {
166
+ const node = tr.doc.nodeAt(pos);
167
+ let first = null;
168
+ node.descendants((child) => {
169
+ if (first) return false;
170
+ if (child.isTextblock) first = child;
171
+ return !first;
172
+ });
173
+ const dir = attributeFor(first ? effective(first) : base, base);
174
+ if (dir !== node.attrs.dir) tr.setNodeMarkup(pos, void 0, {
175
+ ...node.attrs,
176
+ dir
177
+ });
178
+ });
179
+ return tr.docChanged ? tr : null;
180
+ }
181
+ })];
182
+ }
183
+ });
184
+ // src/uploadPlaceholder.js
185
+ var key = new PluginKey("emailUploadPlaceholder");
186
+ var widget = (src) => {
187
+ const element = document.createElement("span");
188
+ element.className = "lee-upload";
189
+ element.setAttribute("contenteditable", "false");
190
+ element.setAttribute("aria-label", "Uploading image");
191
+ element.setAttribute("role", "img");
192
+ if (src) {
193
+ const preview = document.createElement("img");
194
+ preview.src = src;
195
+ preview.alt = "";
196
+ element.append(preview);
197
+ }
198
+ const spinner = document.createElement("span");
199
+ spinner.className = "lee-upload-spinner";
200
+ element.append(spinner);
201
+ return element;
202
+ };
203
+ /**
204
+ * Shows a faded preview with a spinner where an uploading image will go. It is a decoration, not part of the
205
+ * document, so it never reaches onChange or the undo history, and its position moves with edits around it.
206
+ */
207
+ var UploadPlaceholder = Extension.create({
208
+ name: "emailUploadPlaceholder",
209
+ addProseMirrorPlugins() {
210
+ return [new Plugin({
211
+ key,
212
+ state: {
213
+ init: () => DecorationSet.empty,
214
+ apply(tr, set) {
215
+ let next = set.map(tr.mapping, tr.doc);
216
+ const action = tr.getMeta(key);
217
+ if (action?.add) {
218
+ const { id, pos, src } = action.add;
219
+ const deco = Decoration.widget(pos, () => widget(src), {
220
+ id,
221
+ side: 1,
222
+ key: String(id.key)
223
+ });
224
+ next = next.add(tr.doc, [deco]);
225
+ } else if (action?.remove) next = next.remove(next.find(void 0, void 0, (spec) => spec.id === action.remove.id));
226
+ return next;
227
+ }
228
+ },
229
+ props: { decorations(state) {
230
+ return this.getState(state);
231
+ } }
232
+ })];
233
+ }
234
+ });
235
+ var counter = 0;
236
+ /**
237
+ * Show a placeholder at a position.
238
+ * @param {import('@tiptap/core').Editor} editor
239
+ * @param {number} pos
240
+ * @param {string} [src] A preview, such as an object URL for the file
241
+ * @returns {object} An id for findPlaceholder and removePlaceholder
242
+ */
243
+ var addPlaceholder = (editor, pos, src) => {
244
+ counter += 1;
245
+ const id = { key: counter };
246
+ editor.view.dispatch(editor.state.tr.setMeta(key, { add: {
247
+ id,
248
+ pos,
249
+ src
250
+ } }));
251
+ return id;
252
+ };
253
+ /**
254
+ * Where a placeholder is now, after any edits since it was added, or null when it is gone.
255
+ * @param {import('@tiptap/core').Editor} editor
256
+ * @param {object} id
257
+ */
258
+ var findPlaceholder = (editor, id) => {
259
+ const found = key.getState(editor.state)?.find(void 0, void 0, (spec) => spec.id === id);
260
+ return found?.length ? found[0].from : null;
261
+ };
262
+ /**
263
+ * @param {import('@tiptap/core').Editor} editor
264
+ * @param {object} id
265
+ */
266
+ var removePlaceholder = (editor, id) => {
267
+ editor.view.dispatch(editor.state.tr.setMeta(key, { remove: { id } }));
268
+ };
269
+ // src/extensions.js
270
+ /**
271
+ * The editor's extensions. Every mark writes inline styles or plain tags, because email clients ignore classes.
272
+ * @param {object} options
273
+ * @param {'html' | 'markdown'} options.format
274
+ * @param {'ltr' | 'rtl'} options.direction The editor's direction, for blocks with no letters
275
+ * @param {string} [options.placeholder]
276
+ * @param {string[]} options.imageTypes MIME types accepted on paste and drop
277
+ * @param {boolean} options.resizableImages
278
+ * @param {(files: File[], pos?: number) => void} [options.onFiles] Called with pasted or dropped images; omit to ignore them
279
+ * @param {import('@tiptap/core').AnyExtension[]} [options.extensions] Extra extensions from the caller
280
+ */
281
+ var createExtensions = ({ format, direction, placeholder, imageTypes, resizableImages, onFiles, extensions = [] }) => [
282
+ StarterKit.configure({
283
+ heading: { levels: [
284
+ 1,
285
+ 2,
286
+ 3,
287
+ 4
288
+ ] },
289
+ code: false,
290
+ horizontalRule: false,
291
+ link: {
292
+ openOnClick: false,
293
+ autolink: true,
294
+ defaultProtocol: "https",
295
+ HTMLAttributes: {
296
+ target: "_blank",
297
+ rel: "noopener noreferrer",
298
+ class: null
299
+ }
300
+ }
301
+ }),
302
+ TextStyle,
303
+ Color,
304
+ BackgroundColor,
305
+ TextAlign.configure({ types: ["heading", "paragraph"] }),
306
+ TextDirection.configure({ direction }),
307
+ Image.configure({
308
+ inline: true,
309
+ allowBase64: false,
310
+ HTMLAttributes: { style: "max-width: 100%; height: auto;" },
311
+ resize: resizableImages && format !== "markdown" ? {
312
+ enabled: true,
313
+ alwaysPreserveAspectRatio: true,
314
+ minWidth: 24,
315
+ minHeight: 24
316
+ } : false
317
+ }),
318
+ ...onFiles ? [UploadPlaceholder, FileHandler.configure({
319
+ allowedMimeTypes: imageTypes,
320
+ consumePasteEvent: true,
321
+ onPaste: (_editor, files) => onFiles(files),
322
+ onDrop: (_editor, files, pos) => onFiles(files, pos)
323
+ })] : [],
324
+ ...format === "markdown" ? [Markdown] : [],
325
+ ...placeholder ? [Placeholder.configure({ placeholder })] : [],
326
+ ...extensions
327
+ ];
328
+ // src/icons.jsx
329
+ var Svg = ({ size = 20, className, children }) => /* @__PURE__ */ jsx("svg", {
330
+ viewBox: "0 -960 960 960",
331
+ width: size,
332
+ height: size,
333
+ fill: "currentColor",
334
+ className,
335
+ "aria-hidden": "true",
336
+ focusable: "false",
337
+ children
338
+ });
339
+ var UndoIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M289-200q-13 0-21.5-8.5T259-230q0-13 8.5-21.5T289-260h280q70 0 120.5-46.5T740-422q0-69-50.5-115.5T569-584H274l93 93q9 9 9 21t-9 21q-9 9-21 9t-21-9L181-593q-5-5-7-10t-2-11q0-6 2-11t7-10l144-144q9-9 21-9t21 9q9 9 9 21t-9 21l-93 93h294q95 0 163.5 64T800-422q0 94-68.5 158T568-200H289Z" }) });
340
+ var RedoIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M686-584H391q-70 0-120.5 46.5T220-422q0 69 50.5 115.5T391-260h280q13 0 21.5 8.5T701-230q0 13-8.5 21.5T671-200H392q-95 0-163.5-64T160-422q0-94 68.5-158T392-644h294l-93-93q-9-9-9-21t9-21q9-9 21-9t21 9l144 144q5 5 7 10t2 11q0 6-2 11t-7 10L635-449q-9 9-21 9t-21-9q-9-9-9-21t9-21l93-93Z" }) });
341
+ var Bold = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M335-200q-25 0-42.5-17.5T275-260v-440q0-25 17.5-42.5T335-760h168q66 0 114.5 42T666-612q0 38-21 70t-56 49v6q43 14 69.5 50t26.5 81q0 68-52.5 112T510-200H335Zm26-76h144q38 0 66-25t28-63q0-37-28-62t-66-25H361v175Zm0-247h136q35 0 60.5-23t25.5-58q0-35-25.5-58.5T497-686H361v163Z" }) });
342
+ var Italic = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M264-199q-16.8 0-28.4-11.64-11.6-11.64-11.6-28.5t11.6-28.36Q247.2-279 264-279h94l139-409H378q-16.8 0-28.4-11.64-11.6-11.64-11.6-28.5t11.6-28.36Q361.2-768 378-768h300q16.8 0 28.4 11.64 11.6 11.64 11.6 28.5t-11.6 28.36Q694.8-688 678-688h-94L445-279h119q16.8 0 28.4 11.64 11.6 11.64 11.6 28.5t-11.6 28.36Q580.8-199 564-199H264Z" }) });
343
+ var Underline = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M230-140q-13 0-21.5-8.5T200-170q0-13 8.5-21.5T230-200h500q13 0 21.5 8.5T760-170q0 13-8.5 21.5T730-140H230Zm93.5-198.5Q267-397 267-497v-302q0-17 12.5-29t29.5-12q17 0 29 12t12 29v302q0 63 34 101t96 38q62 0 96-38t34-101v-302q0-17 12.5-29t29.5-12q17 0 29 12t12 29v302q0 100-56.5 158.5T480-280q-100 0-156.5-58.5Z" }) });
344
+ var Strike = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M110-410q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32Q97.25-470 110-470h740q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H110Zm320-120v-170H250q-20.83 0-35.42-14.62Q200-729.24 200-750.12q0-20.88 14.58-35.38Q229.17-800 250-800h460q20.83 0 35.42 14.62Q760-770.76 760-749.88q0 20.88-14.58 35.38Q730.83-700 710-700H530v170H430Zm0 180h100v140q0 20.83-14.62 35.42Q500.76-160 479.88-160q-20.88 0-35.38-14.58Q430-189.17 430-210v-140Z" }) });
345
+ var BulletListIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M407-198q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h403q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H407Zm0-252q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h403q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H407Zm0-253q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h403q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H407ZM189-161q-28.05 0-48.02-19Q121-199 121-227.5t19.5-48q19.5-19.5 48-19.5t47.5 19.98q19 19.97 19 48.02 0 27.23-19.39 46.61Q216.23-161 189-161Zm0-252q-28.05 0-48.02-19.5Q121-452 121-480t19.98-47.5Q160.95-547 189-547q27.23 0 46.61 19.5Q255-508 255-480t-19.39 47.5Q216.23-413 189-413Zm-48.5-272.5Q121-705 121-733t19.5-47.5Q160-800 188-800t47.5 19.5Q255-761 255-733t-19.5 47.5Q216-666 188-666t-47.5-19.5Z" }) });
346
+ var OrderedListIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M150-80q-13 0-21.5-8.5T120-110q0-13 8.5-21.5T150-140h70v-30h-30q-13 0-21.5-8.5T160-200q0-13 8.5-21.5T190-230h30v-30h-70q-13 0-21.5-8.5T120-290q0-13 8.5-21.5T150-320h90q17 0 28.5 11.5T280-280v40q0 17-11.5 28.5T240-200q17 0 28.5 11.5T280-160v40q0 17-11.5 28.5T240-80h-90Zm-7-280q-9 0-16-7t-7-16v-87q0-17 11.5-28.5T160-510h60v-30h-70q-13 0-21.5-8.5T120-570q0-13 8.5-21.5T150-600h90q17 0 28.5 11.5T280-560v70q0 17-11.5 28.5T240-450h-60v30h70q13 0 21.5 8.5T280-390q0 13-8.5 21.5T250-360H143Zm45.5-288.5Q180-657 180-670v-150h-30q-13 0-21.5-8.5T120-850q0-13 8.5-21.5T150-880h68q9 0 15.5 6.5T240-858v188q0 13-8.5 21.5T210-640q-13 0-21.5-8.5ZM399-209q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h411q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H399Zm0-243q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h411q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H399Zm0-243q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h411q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H399Z" }) });
347
+ var AlignLeftIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M150-120q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm0-165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h420q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm0-165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm0-165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h420q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm0-165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Z" }) });
348
+ var AlignCenterIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M150-120q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm160-165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h340q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H310ZM150-450q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm160-165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h340q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H310ZM150-780q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Z" }) });
349
+ var AlignRightIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M150-780q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm240 165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h420q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H390ZM150-450q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm240 165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h420q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H390ZM150-120q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Z" }) });
350
+ var AlignJustifyIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M150-120q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm0-165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm0-165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm0-165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Zm0-165q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h660q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H150Z" }) });
351
+ var RtlIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M450-820v430q0 12.75-8.68 21.37-8.67 8.63-21.5 8.63-12.82 0-21.32-8.63-8.5-8.62-8.5-21.37v-190q-66 0-113-43t-47-106.5q0-63.5 43.5-107T380-880h250q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5h-60v430q0 12.75-8.68 21.37-8.67 8.63-21.5 8.63-12.82 0-21.32-8.63-8.5-8.62-8.5-21.37v-430h-60ZM234-196l53 53q9 9 9 21t-9 21q-9 9-21 9t-21-9L141-205q-9-9-9-21t9-21l104-104q9-9 21-9t21 9q9 9 9 21t-9 21l-53 53h576q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H234Zm156-444v-180q-42 0-71 24t-29 66q0 42 29 66t71 24Zm0-90Z" }) });
352
+ var QuoteIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "m275-287 67-115q-5 1-11 1.5t-11 .5q-66 0-113-47t-47-113q0-66 47-113t113-47q66 0 113 47t47 113q0 21-5.5 41T458-480L329-255q-4 8-11.5 11.5T302-240q-18 0-27-15.5t0-31.5Zm360 0 67-115q-5 1-11 1.5t-11 .5q-66 0-113-47t-47-113q0-66 47-113t113-47q66 0 113 47t47 113q0 21-5.5 41T818-480L689-255q-4 8-11.5 11.5T662-240q-18 0-27-15.5t0-31.5ZM376.5-503.5Q400-527 400-560t-23.5-56.5Q353-640 320-640t-56.5 23.5Q240-593 240-560t23.5 56.5Q287-480 320-480t56.5-23.5Zm360 0Q760-527 760-560t-23.5-56.5Q713-640 680-640t-56.5 23.5Q600-593 600-560t23.5 56.5Q647-480 680-480t56.5-23.5ZM680-560Zm-360 0Z" }) });
353
+ var CodeBlockIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "m330-480 70-70q9-9 9-22t-9-22q-9-9-21.83-9-12.84 0-22.17 9l-93 93q-5 5-7 10.13-2 5.14-2 11 0 5.87 2 10.87 2 5 7 10l94 94q9.33 9 22.17 9 12.83 0 21.83-9 9-9 9-22t-9-22l-71-71Zm300 0-71 71q-9 9-9 22t9 22q9 9 21.83 9 12.84 0 22.17-9l94-94q5-5 7-10.13 2-5.14 2-11 0-5.87-2-10.87-2-5-7-10l-94-94q-4-5-10-7t-12-2q-6 0-11.5 2t-10.17 6.8q-9.33 9.6-9.33 22.4 0 12.8 9 21.8l71 71ZM180-120q-24 0-42-18t-18-42v-600q0-24 18-42t42-18h600q24 0 42 18t18 42v600q0 24-18 42t-42 18H180Zm0-60h600v-600H180v600Zm0-600v600-600Z" }) });
354
+ var LinkIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M280-280q-83 0-141.5-58.5T80-480q0-83 58.5-141.5T280-680h140q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H280q-58.33 0-99.17 40.76-40.83 40.77-40.83 99Q140-422 180.83-381q40.84 41 99.17 41h140q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H280Zm75-170q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h250q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H355Zm185 170q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h140q58.33 0 99.17-40.76 40.83-40.77 40.83-99Q820-538 779.17-579q-40.84-41-99.17-41H540q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h140q83 0 141.5 58.5T880-480q0 83-58.5 141.5T680-280H540Z" }) });
355
+ var ImageIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M180-120q-24 0-42-18t-18-42v-600q0-24 18-42t42-18h600q24 0 42 18t18 42v600q0 24-18 42t-42 18H180Zm0-60h600v-600H180v600Zm0 0v-600 600Zm86-97h429q8.5 0 12.75-8t-.75-16L590-457q-5-6-12-6t-12 6L446-302l-81-111q-5-6-12-6t-12 6l-86 112q-6 8-1.75 16t12.75 8Zm109.5-307.62q14.5-14.62 14.5-35.5 0-20.88-14.62-35.38-14.62-14.5-35.5-14.5-20.88 0-35.38 14.62-14.5 14.62-14.5 35.5 0 20.88 14.62 35.38 14.62 14.5 35.5 14.5 20.88 0 35.38-14.62Z" }) });
356
+ var ClearIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M678-220h173q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H618l60-60Zm-486 60q-5.91 0-11.46-2-5.54-2-10.54-7l-72-75q-18-17-17.5-41.5T97-327l458-498q16-17 40.57-17 24.56 0 41.43 17l205 215q17 17 18 42t-16 42L512-170q-4.55 5-10.24 7.5T490-160H192Zm288-60 324-352-207-217-455 503 64 66h274Zm0-260Z" }) });
357
+ var FieldIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M600-160q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h90q21.25 0 35.63-14.38Q740-248.75 740-270v-100q0-37 22.5-66t57.5-40v-8q-35-10-57.5-39.5T740-590v-100q0-21.25-14.37-35.63Q711.25-740 690-740h-90q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h90q45.83 0 77.92 32.08Q800-735.83 800-690v100q0 21.25 14.38 35.62Q828.75-540 850-540q12.75 0 21.38 8.65 8.62 8.64 8.62 21.42v60.15q0 12.78-8.62 21.28-8.63 8.5-21.38 8.5-21.25 0-35.62 14.37Q800-391.25 800-370v100q0 45.83-32.08 77.92Q735.83-160 690-160h-90Zm-330 0q-45.83 0-77.92-32.08Q160-224.17 160-270v-100q0-21.25-14.37-35.63Q131.25-420 110-420q-12.75 0-21.37-8.65Q80-437.29 80-450.07v-60.15q0-12.78 8.63-21.28Q97.25-540 110-540q21.25 0 35.63-14.38Q160-568.75 160-590v-100q0-45.83 32.08-77.92Q224.17-800 270-800h90q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5h-90q-21.25 0-35.62 14.37Q220-711.25 220-690v100q0 37-22.5 66.5T140-484v8q35 11 57.5 40t22.5 66v100q0 21.25 14.38 35.62Q248.75-220 270-220h90q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5h-90Z" }) });
358
+ var LtrIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M450-820v430q0 12.75-8.68 21.37-8.67 8.63-21.5 8.63-12.82 0-21.32-8.63-8.5-8.62-8.5-21.37v-190q-66 0-113-43t-47-106.5q0-63.5 43.5-107T380-880h250q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5h-60v430q0 12.75-8.68 21.37-8.67 8.63-21.5 8.63-12.82 0-21.32-8.63-8.5-8.62-8.5-21.37v-430h-60Zm-60 180v-180q-42 0-71 24t-29 66q0 42 29 66t71 24Zm0-90Zm336 534H150q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h576l-53-53q-9-9-9-21t9-21q9-9 21-9t21 9l104 104q9 9 9 21t-9 21L715-101q-9 9-21 9t-21-9q-9-9-9-21t9-21l53-53Z" }) });
359
+ var SearchIcon = () => /* @__PURE__ */ jsx(Svg, {
360
+ size: 18,
361
+ children: /* @__PURE__ */ jsx("path", { d: "M378-329q-108.16 0-183.08-75Q120-479 120-585t75-181q75-75 181.5-75t181 75Q632-691 632-584.85 632-542 618-502q-14 40-42 75l242 240q9 8.56 9 21.78T818-143q-9 9-22.22 9-13.22 0-21.78-9L533-384q-30 26-69.96 40.5Q423.08-329 378-329Zm-1-60q81.25 0 138.13-57.5Q572-504 572-585t-56.87-138.5Q458.25-781 377-781q-82.08 0-139.54 57.5Q180-666 180-585t57.46 138.5Q294.92-389 377-389Z" })
362
+ });
363
+ var LineBreakIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M190-470q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h505q60 0 102.5 42.5T840-385q0 60-42.5 103T695-239H554l46 45q9 8 8.63 20.29-.38 12.28-8.63 21.5-9 9.21-21.5 9.21t-21.5-9l-96-97q-5-5-7-10.13-2-5.14-2-11 0-5.87 2-10.87 2-5 7-10l97-98q8.8-9 20.9-9 12.1 0 21.1 8.84 8.25 8.84 8.63 21Q609-356 601-347l-47 48h141q35.21 0 60.11-25.5Q780-350 780-385.15t-24.89-60Q730.21-470 695-470H190Zm0 231q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h140q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H190Zm0-461q-12.75 0-21.37-8.68-8.63-8.67-8.63-21.5 0-12.82 8.63-21.32 8.62-8.5 21.37-8.5h580q12.75 0 21.38 8.68 8.62 8.67 8.62 21.5 0 12.82-8.62 21.32-8.63 8.5-21.38 8.5H190Z" }) });
364
+ var MoreIcon = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M207.86-432Q188-432 174-446.14t-14-34Q160-500 174.14-514t34-14Q228-528 242-513.86t14 34Q256-460 241.86-446t-34 14Zm272 0Q460-432 446-446.14t-14-34Q432-500 446.14-514t34-14Q500-528 514-513.86t14 34Q528-460 513.86-446t-34 14Zm272 0Q732-432 718-446.14t-14-34Q704-500 718.14-514t34-14Q772-528 786-513.86t14 34Q800-460 785.86-446t-34 14Z" }) });
365
+ var ChevronIcon = () => /* @__PURE__ */ jsx(Svg, {
366
+ size: 16,
367
+ children: /* @__PURE__ */ jsx("path", { d: "M469-358q-5-2-10-7L261-563q-9-9-8.5-21.5T262-606q9-9 21.5-9t21.5 9l175 176 176-176q9-9 21-8.5t21 9.5q9 9 9 21.5t-9 21.5L501-365q-5 5-10 7t-11 2q-6 0-11-2Z" })
368
+ });
369
+ var SpinnerIcon = () => /* @__PURE__ */ jsx(Svg, {
370
+ className: "lee-spin",
371
+ children: /* @__PURE__ */ jsx("path", { d: "M323-111q-73-31-127-85t-85-127q-31-73-31-157t31-157q31-73 85-127t127-85q73-31 157-31 12 0 21 9t9 21q0 12-9 21t-21 9q-141 0-240.5 99.5T140-480q0 141 99.5 240.5T480-140q141 0 240.5-99.5T820-480q0-12 9-21t21-9q12 0 21 9t9 21q0 84-31 157t-85 127q-54 54-127 85T480-80q-84 0-157-31Z" })
372
+ });
373
+ var ColorIcon = ({ glyph, bar, color }) => /* @__PURE__ */ jsxs(Svg, { children: [/* @__PURE__ */ jsx("path", { d: glyph }), /* @__PURE__ */ jsx("path", {
374
+ d: bar,
375
+ className: "lee-color-bar",
376
+ style: color ? { fill: color } : void 0
377
+ })] });
378
+ var TextColorIcon = ({ color }) => /* @__PURE__ */ jsx(ColorIcon, {
379
+ glyph: "M271 -280q-19 0-29-15.5t-3-32.5l192-488q5-11 14-17.5t20-6.5h30q11 0 20 6.5t14 17.5l192 489q7 17-3.5 32T689-280q-11 0-20-6t-13-17l-48-127H352l-49 128q-4 10-12.5 16t-19.5 6Zm105-214h208L482-765h-4L376-494Z",
380
+ bar: "M110 0Q97 0 88.5-8.5T80-30v-100q0-13 8.5-21.5T110-160h740q13 0 21.5 8.5T880-130v100q0 13-8.5 21.5T850 0H110Z",
381
+ color
382
+ });
383
+ var BackgroundColorIcon = ({ color }) => /* @__PURE__ */ jsx(ColorIcon, {
384
+ glyph: "M581 -474L466-589 284-407l115 115 182-182Zm-72-158 115 115 186-186-115-115-186 186Zm-64-21 200 200-194 194q-19 19-52.5 19T346-259l-11-11-25 25q-8 8-19.5 13t-23.5 5h-94q-10 0-13.5-9.5T163-253l94-94-4-4q-22-22-21.5-55.5T254-462l191-191Zm0 0 210-210q17-17 43-17t43 17l112 112q17 17 16.5 45.5T852-660L645-453 445-653Z",
385
+ bar: "M140 0q-25 0-42.5-18T80-61q0-25 18-42.5t43-17.5h679q25 0 42.5 17.5T880-61q0 25-18 43T819 0H140Z",
386
+ color
387
+ });
388
+ // src/Popover.jsx
389
+ /**
390
+ * A toolbar button that opens a small panel. Closes on Escape, on a click outside and when the panel calls close().
391
+ * Built in so the package does not depend on a UI library.
392
+ */
393
+ var Popover = ({ label, title, className = "", buttonClassName = "lee-button", buttonContent, isActive = false, disabled = false, align = "start", panelClassName = "", children }) => {
394
+ const [open, setOpen] = useState(false);
395
+ const rootRef = useRef(null);
396
+ const buttonRef = useRef(null);
397
+ const panelId = useId();
398
+ useEffect(() => {
399
+ if (!open) return void 0;
400
+ const onPointerDown = (event) => {
401
+ if (rootRef.current && !rootRef.current.contains(event.target)) setOpen(false);
402
+ };
403
+ const onKeyDown = (event) => {
404
+ if (event.key === "Escape") {
405
+ setOpen(false);
406
+ buttonRef.current?.focus();
407
+ }
408
+ };
409
+ document.addEventListener("pointerdown", onPointerDown);
410
+ document.addEventListener("keydown", onKeyDown);
411
+ return () => {
412
+ document.removeEventListener("pointerdown", onPointerDown);
413
+ document.removeEventListener("keydown", onKeyDown);
414
+ };
415
+ }, [open]);
416
+ const close = () => setOpen(false);
417
+ return /* @__PURE__ */ jsxs("div", {
418
+ className: `lee-popover ${className}`.trim(),
419
+ ref: rootRef,
420
+ children: [/* @__PURE__ */ jsx("button", {
421
+ ref: buttonRef,
422
+ type: "button",
423
+ className: buttonClassName,
424
+ "aria-label": label,
425
+ "data-tip": title ?? label,
426
+ "aria-haspopup": "true",
427
+ "aria-expanded": open,
428
+ "aria-controls": open ? panelId : void 0,
429
+ "data-active": isActive || open || void 0,
430
+ disabled,
431
+ onMouseDown: (event) => event.preventDefault(),
432
+ onClick: () => setOpen((value) => !value),
433
+ children: buttonContent
434
+ }), open && /* @__PURE__ */ jsx("div", {
435
+ id: panelId,
436
+ className: `lee-panel ${panelClassName}`.trim(),
437
+ "data-align": align,
438
+ role: "dialog",
439
+ "aria-label": label,
440
+ children: typeof children === "function" ? children({ close }) : children
441
+ })]
442
+ });
443
+ };
444
+ // src/FieldsMenu.jsx
445
+ var SEARCH_THRESHOLD = 6;
446
+ /**
447
+ * @param {string | { label: string, value?: string }} field
448
+ * @returns {{ label: string, value: string }}
449
+ */
450
+ var toField = (field) => typeof field === "string" ? {
451
+ label: field,
452
+ value: field
453
+ } : {
454
+ label: field.label,
455
+ value: field.value ?? field.label
456
+ };
457
+ var Highlight = ({ text, term }) => {
458
+ const at = term ? text.toLowerCase().indexOf(term) : -1;
459
+ if (at < 0) return text;
460
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
461
+ text.slice(0, at),
462
+ /* @__PURE__ */ jsx("mark", {
463
+ className: "lee-match",
464
+ children: text.slice(at, at + term.length)
465
+ }),
466
+ text.slice(at + term.length)
467
+ ] });
468
+ };
469
+ var FieldList = ({ fields, onPick }) => {
470
+ const [query, setQuery] = useState("");
471
+ const [active, setActive] = useState(0);
472
+ const listRef = useRef(null);
473
+ const showSearch = fields.length > SEARCH_THRESHOLD;
474
+ const term = query.trim().toLowerCase();
475
+ const matches = useMemo(() => term ? fields.filter(({ label }) => label.toLowerCase().includes(term)) : fields, [fields, term]);
476
+ useEffect(() => {
477
+ listRef.current?.children[active]?.scrollIntoView?.({ block: "nearest" });
478
+ }, [active]);
479
+ const onKeyDown = (event) => {
480
+ if (event.key === "ArrowDown") {
481
+ event.preventDefault();
482
+ setActive((index) => Math.min(index + 1, matches.length - 1));
483
+ } else if (event.key === "ArrowUp") {
484
+ event.preventDefault();
485
+ setActive((index) => Math.max(index - 1, 0));
486
+ } else if (event.key === "Enter" && matches[active]) {
487
+ event.preventDefault();
488
+ onPick(matches[active].value);
489
+ }
490
+ };
491
+ return /* @__PURE__ */ jsxs("div", {
492
+ className: "lee-fields",
493
+ onKeyDown,
494
+ children: [
495
+ showSearch && /* @__PURE__ */ jsxs("label", {
496
+ className: "lee-fields-searchbar",
497
+ children: [/* @__PURE__ */ jsx(SearchIcon, {}), /* @__PURE__ */ jsx("input", {
498
+ autoFocus: true,
499
+ type: "text",
500
+ className: "lee-fields-search",
501
+ placeholder: "Search fields",
502
+ "aria-label": "Search fields",
503
+ autoComplete: "off",
504
+ spellCheck: false,
505
+ value: query,
506
+ onChange: (event) => {
507
+ setQuery(event.target.value);
508
+ setActive(0);
509
+ }
510
+ })]
511
+ }),
512
+ /* @__PURE__ */ jsx("div", {
513
+ className: "lee-fields-list",
514
+ role: "listbox",
515
+ "aria-label": "Fields",
516
+ ref: listRef,
517
+ children: matches.map(({ label, value }, index) => /* @__PURE__ */ jsxs("button", {
518
+ title: label,
519
+ type: "button",
520
+ role: "option",
521
+ "aria-selected": index === active,
522
+ className: "lee-menu-item lee-field",
523
+ "data-active": index === active || void 0,
524
+ onMouseEnter: () => setActive(index),
525
+ onMouseDown: (event) => event.preventDefault(),
526
+ onClick: () => onPick(value),
527
+ children: [/* @__PURE__ */ jsx("span", {
528
+ className: "lee-field-name",
529
+ children: /* @__PURE__ */ jsx(Highlight, {
530
+ text: label,
531
+ term
532
+ })
533
+ }), value !== label && /* @__PURE__ */ jsx("code", {
534
+ className: "lee-field-code",
535
+ children: `{{${value}}}`
536
+ })]
537
+ }, `${value}-${index}`))
538
+ }),
539
+ !matches.length && /* @__PURE__ */ jsxs("p", {
540
+ className: "lee-empty",
541
+ children: [
542
+ "No fields match “",
543
+ query.trim(),
544
+ "”"
545
+ ]
546
+ })
547
+ ]
548
+ });
549
+ };
550
+ /**
551
+ * The built-in merge-field picker: one toolbar button, however many fields there are.
552
+ */
553
+ var FieldsMenu = ({ fields, onInsert, disabled, label = "Insert field", shortLabel = "Fields" }) => {
554
+ const list = useMemo(() => fields.map(toField).filter(({ label: name }) => name), [fields]);
555
+ if (!list.length) return null;
556
+ return /* @__PURE__ */ jsx(Popover, {
557
+ label,
558
+ align: "end",
559
+ panelClassName: "lee-panel-flush",
560
+ disabled,
561
+ buttonClassName: "lee-button lee-button-text",
562
+ buttonContent: /* @__PURE__ */ jsxs(Fragment, { children: [
563
+ /* @__PURE__ */ jsx(FieldIcon, {}),
564
+ /* @__PURE__ */ jsx("span", {
565
+ className: "lee-button-label lee-field-label",
566
+ children: shortLabel
567
+ }),
568
+ /* @__PURE__ */ jsx(ChevronIcon, {})
569
+ ] }),
570
+ children: ({ close }) => /* @__PURE__ */ jsx(FieldList, {
571
+ fields: list,
572
+ onPick: (value) => {
573
+ onInsert(value);
574
+ close();
575
+ }
576
+ })
577
+ });
578
+ };
579
+ // src/html.js
580
+ /**
581
+ * HTML going into and coming out of the editor.
582
+ *
583
+ * The editor parses HTML with the browser's DOMParser and serializes its own document,
584
+ * so these fix the few places where that round trip would change what an email shows.
585
+ */
586
+ var ANGLE_MARKER = /<<([^<>]+)>>/g;
587
+ /**
588
+ * Prepare stored HTML for the editor.
589
+ * @param {string | null | undefined} html
590
+ * @returns {string}
591
+ */
592
+ var normalizeInput = (html) => {
593
+ if (!html) return "";
594
+ return String(html).replace(ANGLE_MARKER, "&lt;&lt;$1&gt;&gt;");
595
+ };
596
+ /**
597
+ * True when the HTML holds no text and no image.
598
+ * @param {string | null | undefined} html
599
+ * @returns {boolean}
600
+ */
601
+ var isEmptyHtml = (html) => {
602
+ if (!html) return true;
603
+ const value = String(html);
604
+ if (/<img\b/i.test(value)) return false;
605
+ return value.replace(/<br\s*\/?>/gi, "").replace(/<\/?(p|h[1-6]|div|span)\b[^>]*>/gi, "").replace(/&nbsp;/g, "").trim() === "";
606
+ };
607
+ /**
608
+ * Merge two attribute strings, joining their style attributes.
609
+ * @param {string} outer Attributes of the <li>, with a leading space or empty
610
+ * @param {string} inner Attributes of the <p>, with a leading space or empty
611
+ */
612
+ var mergeAttributes = (outer, inner) => {
613
+ const attributes = /* @__PURE__ */ new Map();
614
+ const styles = [];
615
+ for (const attrs of [outer, inner]) for (const [, name, value] of attrs.matchAll(/\s([\w-]+)="([^"]*)"/g)) if (name === "style") styles.push(value);
616
+ else attributes.set(name, value);
617
+ if (styles.length) attributes.set("style", styles.join(" "));
618
+ return [...attributes].map(([name, value]) => ` ${name}="${value}"`).join("");
619
+ };
620
+ var LIST_ITEM_PARAGRAPH = /<li([^>]*)><p([^>]*)>((?:(?!<\/p>)[\s\S])*)<\/p>(?=<\/li>|<ul|<ol)/g;
621
+ /**
622
+ * Turn the editor's HTML into email-safe HTML.
623
+ * - Empty paragraphs and headings get a <br>, or email clients collapse them and blank lines disappear.
624
+ * - A single &nbsp; becomes a space so lines can wrap. Runs of &nbsp; and one at the end of a block are kept,
625
+ * because a plain space there would collapse.
626
+ * - The <p> inside each list item is removed, or Gmail and other clients give every item paragraph margins.
627
+ * Its alignment and direction move to the <li>.
628
+ * - Empty paragraphs at the end are removed, so the email does not end in blank lines.
629
+ * - An editor with no text returns an empty string instead of "<p></p>".
630
+ * @param {string | null | undefined} html
631
+ * @returns {string}
632
+ */
633
+ var normalizeOutput = (html) => {
634
+ if (!html) return "";
635
+ const output = String(html).replace(/<(p|h[1-6])([^>]*)><\/\1>/g, "<$1$2><br></$1>").replace(LIST_ITEM_PARAGRAPH, (_match, li, para, text) => `<li${mergeAttributes(li, para)}>${text}`).replace(/(?:<p[^>]*><br><\/p>)+$/, "").replace(/(?<!&nbsp;)&nbsp;(?!&nbsp;|$|<\/(?:p|h[1-6]|li|blockquote|pre)>)/g, " ");
636
+ return isEmptyHtml(output) ? "" : output;
637
+ };
638
+ /**
639
+ * The text to insert for a merge field.
640
+ * @param {string} marker A column name such as "First Name", with or without braces
641
+ * @returns {string}
642
+ */
643
+ var markerText = (marker) => {
644
+ const name = String(marker ?? "").replace(/^\s*\{\{\s*|\s*\}\}\s*$/g, "").trim();
645
+ return name ? `{{${name}}}` : "";
646
+ };
647
+ // src/image.js
648
+ var DEFAULT_MAX_IMAGE_SIZE = 4194304;
649
+ var DEFAULT_IMAGE_TYPES = [
650
+ "image/png",
651
+ "image/jpeg",
652
+ "image/gif",
653
+ "image/webp"
654
+ ];
655
+ /**
656
+ * Format a byte count for a size limit message: "4MB", "512KB".
657
+ * @param {number} bytes
658
+ */
659
+ var formatSize = (bytes) => {
660
+ if (bytes >= 1048576) return `${Math.round(bytes / 1048576 * 10) / 10}MB`;
661
+ return `${Math.round(bytes / 1024)}KB`;
662
+ };
663
+ /**
664
+ * Check a file before it is uploaded.
665
+ * @param {{ type?: string, size?: number } | null | undefined} file
666
+ * @param {{ maxSize?: number, types?: string[] }} [options] types: the MIME types allowed
667
+ * @returns {string | null} An error message, or null when the file can be uploaded
668
+ */
669
+ var validateImage = (file, { maxSize = DEFAULT_MAX_IMAGE_SIZE, types = DEFAULT_IMAGE_TYPES } = {}) => {
670
+ if (!file) return "Please select a file to upload";
671
+ const { type = "", size = 0 } = file;
672
+ if (!types.includes(type.toLowerCase())) return `Please select a valid image file (${type})`;
673
+ if (size > maxSize) return `Please upload a file that is ${formatSize(maxSize)} or smaller.`;
674
+ return null;
675
+ };
676
+ /**
677
+ * Read a file as a data: URL, for servers that take images as base64 in a JSON body.
678
+ * @param {Blob} file
679
+ * @returns {Promise<string>}
680
+ */
681
+ var readAsDataURL = (file) => new Promise((resolve, reject) => {
682
+ const reader = new FileReader();
683
+ reader.onload = () => resolve(String(reader.result));
684
+ reader.onerror = () => reject(/* @__PURE__ */ new Error("Failed to read file"));
685
+ reader.readAsDataURL(file);
686
+ });
687
+ /**
688
+ * True for an http(s) URL. Uploads must resolve to one: email clients block data: images.
689
+ * @param {unknown} url
690
+ */
691
+ var isHttpUrl = (url) => typeof url === "string" && /^https?:\/\/\S+$/i.test(url.trim());
692
+ // src/shortcuts.js
693
+ /** True on macOS, iPadOS and iOS, where Mod is ⌘. */
694
+ var isApple = () => {
695
+ if (typeof navigator === "undefined") return false;
696
+ const platform = navigator.userAgentData?.platform || navigator.platform || navigator.userAgent || "";
697
+ return /Mac|iPhone|iPad|iPod/i.test(platform);
698
+ };
699
+ /**
700
+ * The value for aria-keyshortcuts: 'Meta+Shift+Z' on Apple platforms, 'Control+Shift+Z' elsewhere.
701
+ * @param {string} keys
702
+ * @param {boolean} [apple]
703
+ */
704
+ var ariaShortcut = (keys, apple = isApple()) => {
705
+ if (!keys) return void 0;
706
+ const names = {
707
+ Mod: apple ? "Meta" : "Control",
708
+ Ctrl: "Control",
709
+ Alt: "Alt",
710
+ Shift: "Shift"
711
+ };
712
+ return keys.split("+").map((part) => names[part] ?? part).join("+");
713
+ };
714
+ var SHORTCUTS = {
715
+ undo: "Mod+Z",
716
+ redo: "Mod+Shift+Z",
717
+ bold: "Mod+B",
718
+ italic: "Mod+I",
719
+ underline: "Mod+U",
720
+ strike: "Mod+Shift+S",
721
+ bulletList: "Mod+Shift+8",
722
+ orderedList: "Mod+Shift+7",
723
+ blockquote: "Mod+Shift+B",
724
+ codeBlock: "Mod+Alt+C",
725
+ hardBreak: "Shift+Enter",
726
+ paragraph: "Mod+Alt+0",
727
+ heading1: "Mod+Alt+1",
728
+ heading2: "Mod+Alt+2",
729
+ heading3: "Mod+Alt+3",
730
+ heading4: "Mod+Alt+4",
731
+ alignLeft: "Mod+Shift+L",
732
+ alignCenter: "Mod+Shift+E",
733
+ alignRight: "Mod+Shift+R",
734
+ alignJustify: "Mod+Shift+J"
735
+ };
736
+ // src/toolbar-items.js
737
+ var DEFAULT_TOOLBAR = [
738
+ ["undo", "redo"],
739
+ ["heading"],
740
+ [
741
+ "bold",
742
+ "italic",
743
+ "underline"
744
+ ],
745
+ ["color", "backgroundColor"],
746
+ [
747
+ "bulletList",
748
+ "orderedList",
749
+ "align",
750
+ "more"
751
+ ],
752
+ ["spacer"],
753
+ ["link", "image"]
754
+ ];
755
+ var DEFAULT_MARKDOWN_TOOLBAR = [
756
+ ["undo", "redo"],
757
+ ["heading"],
758
+ ["bold", "italic"],
759
+ [
760
+ "bulletList",
761
+ "orderedList",
762
+ "more"
763
+ ],
764
+ ["spacer"],
765
+ ["link", "image"]
766
+ ];
767
+ var DEFAULT_MORE_ITEMS = [
768
+ "hardBreak",
769
+ "strike",
770
+ "blockquote",
771
+ "codeBlock",
772
+ "clear"
773
+ ];
774
+ var MORE_MENU_ITEMS = [
775
+ "hardBreak",
776
+ "blockquote",
777
+ "codeBlock",
778
+ "strike",
779
+ "underline",
780
+ "rtl",
781
+ "clear"
782
+ ];
783
+ var TOOLBAR_ITEMS = [.../* @__PURE__ */ new Set([
784
+ ...DEFAULT_TOOLBAR.flat(),
785
+ "hardBreak",
786
+ "strike",
787
+ "blockquote",
788
+ "rtl",
789
+ "codeBlock",
790
+ "clear"
791
+ ])];
792
+ // src/Tooltip.jsx
793
+ var SHOW_DELAY = 300;
794
+ var WARM_FOR = 500;
795
+ var GAP = 6;
796
+ var EDGE = 4;
797
+ /**
798
+ * Tooltips for every element under `rootRef` with a data-tip attribute. One tooltip element, shown on hover and on keyboard focus, hidden on click, Escape and when the element
799
+ * opens a menu. It is aria-hidden: the buttons already carry the same name in aria-label.
800
+ * @param {import('react').RefObject<HTMLElement>} rootRef A positioned element the tooltip is placed in
801
+ */
802
+ var useTooltip = (rootRef) => {
803
+ const [tip, setTip] = useState(null);
804
+ const [position, setPosition] = useState(null);
805
+ const tipRef = useRef(null);
806
+ const timer = useRef(null);
807
+ const current = useRef(null);
808
+ const hiddenAt = useRef(0);
809
+ const hide = useCallback(() => {
810
+ clearTimeout(timer.current);
811
+ if (current.current) hiddenAt.current = Date.now();
812
+ current.current = null;
813
+ setTip(null);
814
+ }, []);
815
+ const show = useCallback((target) => {
816
+ clearTimeout(timer.current);
817
+ const open = () => {
818
+ current.current = target;
819
+ setTip({
820
+ target,
821
+ text: target.dataset.tip
822
+ });
823
+ };
824
+ if (current.current || Date.now() - hiddenAt.current < WARM_FOR) open();
825
+ else timer.current = setTimeout(open, SHOW_DELAY);
826
+ }, []);
827
+ useEffect(() => {
828
+ const root = rootRef.current;
829
+ if (!root) return void 0;
830
+ const find = (element) => {
831
+ const target = element?.closest?.("[data-tip]");
832
+ return target && root.contains(target) ? target : null;
833
+ };
834
+ const eligible = (target) => target && !target.disabled && target.getAttribute("aria-expanded") !== "true";
835
+ const onPointerOver = (event) => {
836
+ if (event.pointerType === "touch") return;
837
+ const target = find(event.target);
838
+ if (target && target === current.current) return;
839
+ if (eligible(target)) show(target);
840
+ else hide();
841
+ };
842
+ const onFocusIn = (event) => {
843
+ const target = find(event.target);
844
+ if (eligible(target) && target.matches(":focus-visible")) show(target);
845
+ };
846
+ const onKeyDown = (event) => {
847
+ if (event.key === "Escape") hide();
848
+ };
849
+ root.addEventListener("pointerover", onPointerOver);
850
+ root.addEventListener("pointerleave", hide);
851
+ root.addEventListener("focusin", onFocusIn);
852
+ root.addEventListener("focusout", hide);
853
+ root.addEventListener("pointerdown", hide, true);
854
+ document.addEventListener("keydown", onKeyDown, true);
855
+ return () => {
856
+ clearTimeout(timer.current);
857
+ root.removeEventListener("pointerover", onPointerOver);
858
+ root.removeEventListener("pointerleave", hide);
859
+ root.removeEventListener("focusin", onFocusIn);
860
+ root.removeEventListener("focusout", hide);
861
+ root.removeEventListener("pointerdown", hide, true);
862
+ document.removeEventListener("keydown", onKeyDown, true);
863
+ };
864
+ }, [
865
+ rootRef,
866
+ show,
867
+ hide
868
+ ]);
869
+ useLayoutEffect(() => {
870
+ const root = rootRef.current;
871
+ if (!tip || !root || !tipRef.current) {
872
+ setPosition(null);
873
+ return;
874
+ }
875
+ const bounds = root.getBoundingClientRect();
876
+ const target = tip.target.getBoundingClientRect();
877
+ const width = tipRef.current.offsetWidth;
878
+ const centred = target.left - bounds.left + target.width / 2 - width / 2;
879
+ const left = Math.max(EDGE, Math.min(centred, bounds.width - width - EDGE));
880
+ setPosition({
881
+ left,
882
+ top: target.bottom - bounds.top + GAP
883
+ });
884
+ }, [tip, rootRef]);
885
+ if (!tip?.text) return null;
886
+ return /* @__PURE__ */ jsx("div", {
887
+ ref: tipRef,
888
+ className: "lee-tooltip",
889
+ "aria-hidden": "true",
890
+ style: position ?? {
891
+ visibility: "hidden",
892
+ left: 0,
893
+ top: 0
894
+ },
895
+ children: tip.text
896
+ });
897
+ };
898
+ // src/Toolbar.jsx
899
+ var HEADINGS = [
900
+ {
901
+ level: 0,
902
+ label: "Normal",
903
+ shortcut: SHORTCUTS.paragraph
904
+ },
905
+ {
906
+ level: 1,
907
+ label: "Heading 1",
908
+ shortcut: SHORTCUTS.heading1
909
+ },
910
+ {
911
+ level: 2,
912
+ label: "Heading 2",
913
+ shortcut: SHORTCUTS.heading2
914
+ },
915
+ {
916
+ level: 3,
917
+ label: "Heading 3",
918
+ shortcut: SHORTCUTS.heading3
919
+ },
920
+ {
921
+ level: 4,
922
+ label: "Heading 4",
923
+ shortcut: SHORTCUTS.heading4
924
+ }
925
+ ];
926
+ var ALIGNMENTS = [
927
+ {
928
+ value: "left",
929
+ label: "Align left",
930
+ Icon: AlignLeftIcon,
931
+ shortcut: SHORTCUTS.alignLeft
932
+ },
933
+ {
934
+ value: "center",
935
+ label: "Align center",
936
+ Icon: AlignCenterIcon,
937
+ shortcut: SHORTCUTS.alignCenter
938
+ },
939
+ {
940
+ value: "right",
941
+ label: "Align right",
942
+ Icon: AlignRightIcon,
943
+ shortcut: SHORTCUTS.alignRight
944
+ },
945
+ {
946
+ value: "justify",
947
+ label: "Justify",
948
+ Icon: AlignJustifyIcon,
949
+ shortcut: SHORTCUTS.alignJustify
950
+ }
951
+ ];
952
+ var DIRECTIONS = [
953
+ {
954
+ value: "auto",
955
+ label: "Text direction: automatic"
956
+ },
957
+ {
958
+ value: "ltr",
959
+ label: "Left-to-right text",
960
+ Icon: LtrIcon
961
+ },
962
+ {
963
+ value: "rtl",
964
+ label: "Right-to-left text",
965
+ Icon: RtlIcon
966
+ }
967
+ ];
968
+ var selectEditorState = ({ editor }) => {
969
+ const level = [
970
+ 1,
971
+ 2,
972
+ 3,
973
+ 4
974
+ ].find((value) => editor.isActive("heading", { level: value })) ?? 0;
975
+ const block = editor.state.selection.$from.parent;
976
+ const base = editor.view.dom.closest("[dir]")?.getAttribute("dir") === "rtl" ? "rtl" : "ltr";
977
+ const rtl = (block.attrs.dir ?? base) === "rtl";
978
+ const natural = rtl ? "right" : "left";
979
+ const align = ALIGNMENTS.find(({ value }) => editor.isActive({ textAlign: value }))?.value ?? natural;
980
+ let direction = "auto";
981
+ if (block.attrs.dirAuto === false) direction = rtl ? "rtl" : "ltr";
982
+ const textStyle = editor.getAttributes("textStyle");
983
+ return {
984
+ editable: editor.isEditable,
985
+ heading: level,
986
+ bold: editor.isActive("bold"),
987
+ italic: editor.isActive("italic"),
988
+ underline: editor.isActive("underline"),
989
+ strike: editor.isActive("strike"),
990
+ link: editor.isActive("link"),
991
+ linkHref: editor.getAttributes("link").href ?? "",
992
+ blockquote: editor.isActive("blockquote"),
993
+ codeBlock: editor.isActive("codeBlock"),
994
+ orderedList: editor.isActive("orderedList"),
995
+ bulletList: editor.isActive("bulletList"),
996
+ align,
997
+ naturalAlign: natural,
998
+ rtl,
999
+ direction,
1000
+ color: textStyle.color ?? "",
1001
+ backgroundColor: textStyle.backgroundColor ?? "",
1002
+ canUndo: editor.can().undo(),
1003
+ canRedo: editor.can().redo()
1004
+ };
1005
+ };
1006
+ var MENU_ENTRIES = {
1007
+ hardBreak: {
1008
+ label: "Line break",
1009
+ Icon: LineBreakIcon,
1010
+ shortcut: SHORTCUTS.hardBreak,
1011
+ command: (c) => c.setHardBreak()
1012
+ },
1013
+ blockquote: {
1014
+ label: "Quote",
1015
+ Icon: QuoteIcon,
1016
+ shortcut: SHORTCUTS.blockquote,
1017
+ isActive: (s) => s.blockquote,
1018
+ command: (c) => c.toggleBlockquote()
1019
+ },
1020
+ codeBlock: {
1021
+ label: "Code block",
1022
+ Icon: CodeBlockIcon,
1023
+ shortcut: SHORTCUTS.codeBlock,
1024
+ isActive: (s) => s.codeBlock,
1025
+ command: (c) => c.toggleCodeBlock()
1026
+ },
1027
+ strike: {
1028
+ label: "Strikethrough",
1029
+ Icon: Strike,
1030
+ shortcut: SHORTCUTS.strike,
1031
+ isActive: (s) => s.strike,
1032
+ command: (c) => c.toggleStrike()
1033
+ },
1034
+ underline: {
1035
+ label: "Underline",
1036
+ Icon: Underline,
1037
+ shortcut: SHORTCUTS.underline,
1038
+ isActive: (s) => s.underline,
1039
+ command: (c) => c.toggleUnderline()
1040
+ },
1041
+ rtl: {
1042
+ label: "Right-to-left text",
1043
+ Icon: RtlIcon,
1044
+ isActive: (s) => s.direction === "rtl",
1045
+ command: (c, s) => c.setDirection(s.direction === "rtl" ? "auto" : "rtl")
1046
+ },
1047
+ clear: {
1048
+ label: "Clear formatting",
1049
+ Icon: ClearIcon,
1050
+ command: (c) => c.unsetAllMarks().clearNodes().unsetTextAlign().setDirection("auto")
1051
+ }
1052
+ };
1053
+ var Button = ({ label, shortcut, isActive, disabled, onClick, className = "lee-button", children }) => /* @__PURE__ */ jsx("button", {
1054
+ type: "button",
1055
+ className,
1056
+ "aria-label": label,
1057
+ "data-tip": label,
1058
+ "aria-keyshortcuts": ariaShortcut(shortcut),
1059
+ "aria-pressed": isActive === void 0 ? void 0 : isActive,
1060
+ "data-active": isActive || void 0,
1061
+ disabled,
1062
+ onMouseDown: (event) => event.preventDefault(),
1063
+ onClick,
1064
+ children
1065
+ });
1066
+ var ColorPanel = ({ colors, value, onPick, onClear, clearLabel }) => /* @__PURE__ */ jsxs("div", {
1067
+ className: "lee-colors",
1068
+ children: [/* @__PURE__ */ jsxs("button", {
1069
+ type: "button",
1070
+ className: "lee-menu-item lee-color-default",
1071
+ onClick: onClear,
1072
+ children: [/* @__PURE__ */ jsx("span", {
1073
+ className: "lee-swatch lee-swatch-none",
1074
+ "aria-hidden": "true"
1075
+ }), /* @__PURE__ */ jsx("span", { children: clearLabel })]
1076
+ }), /* @__PURE__ */ jsx("div", {
1077
+ className: "lee-swatches",
1078
+ children: colors.map((color) => /* @__PURE__ */ jsx("button", {
1079
+ type: "button",
1080
+ className: "lee-swatch",
1081
+ style: { backgroundColor: color },
1082
+ "aria-label": color,
1083
+ "data-tip": color,
1084
+ "data-active": value?.toLowerCase() === color.toLowerCase() || void 0,
1085
+ onClick: () => onPick(color)
1086
+ }, color))
1087
+ })]
1088
+ });
1089
+ var withProtocol = (url) => {
1090
+ const value = url.trim();
1091
+ if (!value) return "";
1092
+ if (/^(https?:|mailto:|tel:|\{\{)/i.test(value) || value.startsWith("#")) return value;
1093
+ if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) return `mailto:${value}`;
1094
+ return `https://${value}`;
1095
+ };
1096
+ var LinkPanel = ({ editor, href, close }) => {
1097
+ const [value, setValue] = useState(href);
1098
+ const apply = (event) => {
1099
+ event.preventDefault();
1100
+ const url = withProtocol(value);
1101
+ const chain = editor.chain().focus().extendMarkRange("link");
1102
+ if (!url) chain.unsetLink().run();
1103
+ else if (editor.state.selection.empty && !editor.isActive("link")) chain.insertContent({
1104
+ type: "text",
1105
+ text: value.trim(),
1106
+ marks: [{
1107
+ type: "link",
1108
+ attrs: { href: url }
1109
+ }]
1110
+ }).run();
1111
+ else chain.setLink({ href: url }).run();
1112
+ close();
1113
+ };
1114
+ const remove = () => {
1115
+ editor.chain().focus().extendMarkRange("link").unsetLink().run();
1116
+ close();
1117
+ };
1118
+ return /* @__PURE__ */ jsxs("form", {
1119
+ className: "lee-link",
1120
+ onSubmit: apply,
1121
+ children: [
1122
+ /* @__PURE__ */ jsx("input", {
1123
+ autoFocus: true,
1124
+ type: "text",
1125
+ inputMode: "url",
1126
+ className: "lee-input",
1127
+ placeholder: "https://example.com",
1128
+ "aria-label": "Link URL",
1129
+ value,
1130
+ onChange: (event) => setValue(event.target.value)
1131
+ }),
1132
+ /* @__PURE__ */ jsx("button", {
1133
+ type: "submit",
1134
+ className: "lee-text-button lee-primary",
1135
+ children: "Apply"
1136
+ }),
1137
+ href && /* @__PURE__ */ jsx("button", {
1138
+ type: "button",
1139
+ className: "lee-text-button",
1140
+ onClick: remove,
1141
+ children: "Remove"
1142
+ })
1143
+ ]
1144
+ });
1145
+ };
1146
+ /**
1147
+ * How many labels the toolbar can show at its current width: 0 shows all, 1 hides the Link and Image labels,
1148
+ * 2 hides the Fields label too. Measured, so it is exact whatever fonts the system uses.
1149
+ * @param {import('react').RefObject<HTMLElement>} ref
1150
+ */
1151
+ var useCompactLevel = (ref) => {
1152
+ const [level, setLevel] = useState(0);
1153
+ useLayoutEffect(() => {
1154
+ const bar = ref.current;
1155
+ if (!bar) return void 0;
1156
+ const measure = () => {
1157
+ const current = bar.dataset.compact;
1158
+ bar.dataset.compact = "0";
1159
+ const style = getComputedStyle(bar);
1160
+ const content = [...bar.children].filter((child) => !child.classList.contains("lee-spacer") && !child.classList.contains("lee-tooltip")).reduce((sum, child) => sum + child.getBoundingClientRect().width, 0);
1161
+ const spacing = parseFloat(style.paddingLeft) + parseFloat(style.paddingRight) + 4;
1162
+ const width = (selector) => [...bar.querySelectorAll(selector)].reduce((sum, el) => sum + el.getBoundingClientRect().width + 4, 0);
1163
+ const fieldLabel = width(".lee-field-label");
1164
+ const otherLabels = width(".lee-button-label:not(.lee-field-label)");
1165
+ bar.dataset.compact = current;
1166
+ const needed = content + spacing;
1167
+ const available = bar.clientWidth;
1168
+ let next = 0;
1169
+ if (needed > available) {
1170
+ if (needed - otherLabels <= available) next = 1;
1171
+ else if (needed - otherLabels - fieldLabel <= available) next = 2;
1172
+ }
1173
+ setLevel(next);
1174
+ };
1175
+ measure();
1176
+ const observer = new ResizeObserver(measure);
1177
+ observer.observe(bar);
1178
+ document.fonts?.ready.then(measure);
1179
+ return () => observer.disconnect();
1180
+ }, [ref]);
1181
+ return level;
1182
+ };
1183
+ /**
1184
+ * The formatting toolbar. `items` is an array of groups of item ids (see DEFAULT_TOOLBAR).
1185
+ */
1186
+ var Toolbar = ({ editor, items, moreItems, colors, canUploadImages, uploading, onPickImage, end }) => {
1187
+ const state = useEditorState({
1188
+ editor,
1189
+ selector: selectEditorState
1190
+ });
1191
+ const barRef = useRef(null);
1192
+ const compact = useCompactLevel(barRef);
1193
+ const tooltip = useTooltip(barRef);
1194
+ const disabled = !state.editable;
1195
+ const run = (command) => () => command(editor.chain().focus()).run();
1196
+ const render = (id) => {
1197
+ switch (id) {
1198
+ case "heading": {
1199
+ const current = HEADINGS.find(({ level }) => level === state.heading) ?? HEADINGS[0];
1200
+ return /* @__PURE__ */ jsx(Popover, {
1201
+ label: "Text style",
1202
+ disabled,
1203
+ buttonClassName: "lee-button lee-button-text lee-heading-button",
1204
+ buttonContent: /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("span", { children: current.label }), /* @__PURE__ */ jsx(ChevronIcon, {})] }),
1205
+ children: ({ close }) => /* @__PURE__ */ jsx("div", {
1206
+ className: "lee-menu-list",
1207
+ role: "menu",
1208
+ children: HEADINGS.map(({ level, label }) => /* @__PURE__ */ jsx("button", {
1209
+ type: "button",
1210
+ role: "menuitemradio",
1211
+ "aria-checked": level === state.heading,
1212
+ className: "lee-menu-item",
1213
+ "data-active": level === state.heading || void 0,
1214
+ onMouseDown: (event) => event.preventDefault(),
1215
+ onClick: () => {
1216
+ const chain = editor.chain().focus();
1217
+ (level ? chain.setHeading({ level }) : chain.setParagraph()).run();
1218
+ close();
1219
+ },
1220
+ children: /* @__PURE__ */ jsx("span", {
1221
+ className: `lee-heading-preview lee-h${level}`,
1222
+ children: label
1223
+ })
1224
+ }, level))
1225
+ })
1226
+ }, id);
1227
+ }
1228
+ case "bold": return /* @__PURE__ */ jsx(Button, {
1229
+ label: "Bold",
1230
+ shortcut: SHORTCUTS.bold,
1231
+ isActive: state.bold,
1232
+ disabled,
1233
+ onClick: run((c) => c.toggleBold()),
1234
+ children: /* @__PURE__ */ jsx(Bold, {})
1235
+ }, id);
1236
+ case "italic": return /* @__PURE__ */ jsx(Button, {
1237
+ label: "Italic",
1238
+ shortcut: SHORTCUTS.italic,
1239
+ isActive: state.italic,
1240
+ disabled,
1241
+ onClick: run((c) => c.toggleItalic()),
1242
+ children: /* @__PURE__ */ jsx(Italic, {})
1243
+ }, id);
1244
+ case "underline": return /* @__PURE__ */ jsx(Button, {
1245
+ label: "Underline",
1246
+ shortcut: SHORTCUTS.underline,
1247
+ isActive: state.underline,
1248
+ disabled,
1249
+ onClick: run((c) => c.toggleUnderline()),
1250
+ children: /* @__PURE__ */ jsx(Underline, {})
1251
+ }, id);
1252
+ case "strike": return /* @__PURE__ */ jsx(Button, {
1253
+ label: "Strikethrough",
1254
+ shortcut: SHORTCUTS.strike,
1255
+ isActive: state.strike,
1256
+ disabled,
1257
+ onClick: run((c) => c.toggleStrike()),
1258
+ children: /* @__PURE__ */ jsx(Strike, {})
1259
+ }, id);
1260
+ case "color": return /* @__PURE__ */ jsx(Popover, {
1261
+ label: "Text color",
1262
+ disabled,
1263
+ buttonContent: /* @__PURE__ */ jsx(TextColorIcon, { color: state.color }),
1264
+ children: ({ close }) => /* @__PURE__ */ jsx(ColorPanel, {
1265
+ colors,
1266
+ value: state.color,
1267
+ clearLabel: "Default color",
1268
+ onPick: (color) => {
1269
+ editor.chain().focus().setColor(color).run();
1270
+ close();
1271
+ },
1272
+ onClear: () => {
1273
+ editor.chain().focus().unsetColor().run();
1274
+ close();
1275
+ }
1276
+ })
1277
+ }, id);
1278
+ case "backgroundColor": return /* @__PURE__ */ jsx(Popover, {
1279
+ label: "Background color",
1280
+ disabled,
1281
+ buttonContent: /* @__PURE__ */ jsx(BackgroundColorIcon, { color: state.backgroundColor }),
1282
+ children: ({ close }) => /* @__PURE__ */ jsx(ColorPanel, {
1283
+ colors,
1284
+ value: state.backgroundColor,
1285
+ clearLabel: "No background",
1286
+ onPick: (color) => {
1287
+ editor.chain().focus().setBackgroundColor(color).run();
1288
+ close();
1289
+ },
1290
+ onClear: () => {
1291
+ editor.chain().focus().unsetBackgroundColor().run();
1292
+ close();
1293
+ }
1294
+ })
1295
+ }, id);
1296
+ case "link": return /* @__PURE__ */ jsx(Popover, {
1297
+ label: "Insert hyperlink",
1298
+ isActive: state.link,
1299
+ disabled,
1300
+ buttonClassName: "lee-button lee-button-text lee-insert",
1301
+ buttonContent: /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(LinkIcon, {}), /* @__PURE__ */ jsx("span", {
1302
+ className: "lee-button-label",
1303
+ children: "Link"
1304
+ })] }),
1305
+ children: ({ close }) => /* @__PURE__ */ jsx(LinkPanel, {
1306
+ editor,
1307
+ href: state.linkHref,
1308
+ close
1309
+ })
1310
+ }, `${id}-${state.linkHref}`);
1311
+ case "image":
1312
+ if (!canUploadImages) return null;
1313
+ return /* @__PURE__ */ jsxs(Button, {
1314
+ label: uploading ? "Uploading image" : "Insert image",
1315
+ className: "lee-button lee-button-text lee-insert",
1316
+ disabled,
1317
+ onClick: onPickImage,
1318
+ children: [uploading ? /* @__PURE__ */ jsx(SpinnerIcon, {}) : /* @__PURE__ */ jsx(ImageIcon, {}), /* @__PURE__ */ jsx("span", {
1319
+ className: "lee-button-label",
1320
+ children: "Image"
1321
+ })]
1322
+ }, id);
1323
+ case "hardBreak": return /* @__PURE__ */ jsx(Button, {
1324
+ label: "Line break",
1325
+ shortcut: SHORTCUTS.hardBreak,
1326
+ disabled,
1327
+ onClick: run((c) => c.setHardBreak()),
1328
+ children: /* @__PURE__ */ jsx(LineBreakIcon, {})
1329
+ }, id);
1330
+ case "blockquote": return /* @__PURE__ */ jsx(Button, {
1331
+ label: "Quote",
1332
+ shortcut: SHORTCUTS.blockquote,
1333
+ isActive: state.blockquote,
1334
+ disabled,
1335
+ onClick: run((c) => c.toggleBlockquote()),
1336
+ children: /* @__PURE__ */ jsx(QuoteIcon, {})
1337
+ }, id);
1338
+ case "codeBlock": return /* @__PURE__ */ jsx(Button, {
1339
+ label: "Code block",
1340
+ shortcut: SHORTCUTS.codeBlock,
1341
+ isActive: state.codeBlock,
1342
+ disabled,
1343
+ onClick: run((c) => c.toggleCodeBlock()),
1344
+ children: /* @__PURE__ */ jsx(CodeBlockIcon, {})
1345
+ }, id);
1346
+ case "orderedList": return /* @__PURE__ */ jsx(Button, {
1347
+ label: "Numbered list",
1348
+ shortcut: SHORTCUTS.orderedList,
1349
+ isActive: state.orderedList,
1350
+ disabled,
1351
+ onClick: run((c) => c.toggleOrderedList()),
1352
+ children: /* @__PURE__ */ jsx(OrderedListIcon, {})
1353
+ }, id);
1354
+ case "bulletList": return /* @__PURE__ */ jsx(Button, {
1355
+ label: "Bulleted list",
1356
+ shortcut: SHORTCUTS.bulletList,
1357
+ isActive: state.bulletList,
1358
+ disabled,
1359
+ onClick: run((c) => c.toggleBulletList()),
1360
+ children: /* @__PURE__ */ jsx(BulletListIcon, {})
1361
+ }, id);
1362
+ case "align": {
1363
+ const { Icon } = ALIGNMENTS.find(({ value }) => value === state.align) ?? ALIGNMENTS[0];
1364
+ return /* @__PURE__ */ jsx(Popover, {
1365
+ label: "Alignment",
1366
+ disabled,
1367
+ buttonClassName: "lee-button lee-button-text",
1368
+ buttonContent: /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Icon, {}), /* @__PURE__ */ jsx(ChevronIcon, {})] }),
1369
+ children: ({ close }) => /* @__PURE__ */ jsxs("div", {
1370
+ className: "lee-menu",
1371
+ children: [
1372
+ ALIGNMENTS.map(({ value, label, Icon: ItemIcon, shortcut }) => /* @__PURE__ */ jsx(Button, {
1373
+ label,
1374
+ shortcut,
1375
+ isActive: state.align === value,
1376
+ onClick: () => {
1377
+ const chain = editor.chain().focus();
1378
+ (value === state.naturalAlign ? chain.unsetTextAlign() : chain.setTextAlign(value)).run();
1379
+ close();
1380
+ },
1381
+ children: /* @__PURE__ */ jsx(ItemIcon, {})
1382
+ }, value)),
1383
+ /* @__PURE__ */ jsx("span", {
1384
+ className: "lee-divider",
1385
+ "aria-hidden": "true"
1386
+ }),
1387
+ DIRECTIONS.map(({ value, label, Icon: DirIcon }) => /* @__PURE__ */ jsx(Button, {
1388
+ label,
1389
+ className: DirIcon ? "lee-button" : "lee-button lee-button-text",
1390
+ isActive: state.direction === value,
1391
+ onClick: () => {
1392
+ editor.chain().focus().setDirection(value).run();
1393
+ close();
1394
+ },
1395
+ children: DirIcon ? /* @__PURE__ */ jsx(DirIcon, {}) : "Auto"
1396
+ }, value))
1397
+ ]
1398
+ })
1399
+ }, id);
1400
+ }
1401
+ case "rtl": return /* @__PURE__ */ jsx(Button, {
1402
+ label: "Right-to-left text",
1403
+ isActive: state.direction === "rtl",
1404
+ disabled,
1405
+ onClick: run((c) => c.setDirection(state.direction === "rtl" ? "auto" : "rtl")),
1406
+ children: /* @__PURE__ */ jsx(RtlIcon, {})
1407
+ }, id);
1408
+ case "clear": return /* @__PURE__ */ jsx(Button, {
1409
+ label: "Clear formatting",
1410
+ disabled,
1411
+ onClick: run((c) => c.unsetAllMarks().clearNodes().unsetTextAlign().setDirection("auto")),
1412
+ children: /* @__PURE__ */ jsx(ClearIcon, {})
1413
+ }, id);
1414
+ case "undo": return /* @__PURE__ */ jsx(Button, {
1415
+ className: "lee-button lee-flip",
1416
+ label: "Undo",
1417
+ shortcut: SHORTCUTS.undo,
1418
+ disabled: disabled || !state.canUndo,
1419
+ onClick: run((c) => c.undo()),
1420
+ children: /* @__PURE__ */ jsx(UndoIcon, {})
1421
+ }, id);
1422
+ case "redo": return /* @__PURE__ */ jsx(Button, {
1423
+ className: "lee-button lee-flip",
1424
+ label: "Redo",
1425
+ shortcut: SHORTCUTS.redo,
1426
+ disabled: disabled || !state.canRedo,
1427
+ onClick: run((c) => c.redo()),
1428
+ children: /* @__PURE__ */ jsx(RedoIcon, {})
1429
+ }, id);
1430
+ case "more": {
1431
+ const entries = moreItems.filter((item) => MORE_MENU_ITEMS.includes(item) && MENU_ENTRIES[item]);
1432
+ if (!entries.length) return null;
1433
+ return /* @__PURE__ */ jsx(Popover, {
1434
+ label: "More formatting",
1435
+ disabled,
1436
+ isActive: entries.some((item) => MENU_ENTRIES[item].isActive?.(state)),
1437
+ buttonContent: /* @__PURE__ */ jsx(MoreIcon, {}),
1438
+ children: ({ close }) => /* @__PURE__ */ jsx("div", {
1439
+ className: "lee-menu-list",
1440
+ role: "menu",
1441
+ children: entries.map((item) => {
1442
+ const { label, Icon, isActive, command, shortcut } = MENU_ENTRIES[item];
1443
+ const active = isActive?.(state);
1444
+ return /* @__PURE__ */ jsxs("button", {
1445
+ type: "button",
1446
+ role: isActive ? "menuitemcheckbox" : "menuitem",
1447
+ "aria-checked": isActive ? Boolean(active) : void 0,
1448
+ "aria-keyshortcuts": ariaShortcut(shortcut),
1449
+ className: "lee-menu-item lee-menu-item-icon",
1450
+ "data-checked": active || void 0,
1451
+ onMouseDown: (event) => event.preventDefault(),
1452
+ onClick: () => {
1453
+ command(editor.chain().focus(), state).run();
1454
+ close();
1455
+ },
1456
+ children: [/* @__PURE__ */ jsx(Icon, {}), /* @__PURE__ */ jsx("span", { children: label })]
1457
+ }, item);
1458
+ })
1459
+ })
1460
+ }, id);
1461
+ }
1462
+ default: return null;
1463
+ }
1464
+ };
1465
+ return /* @__PURE__ */ jsxs("div", {
1466
+ className: "lee-toolbar",
1467
+ role: "toolbar",
1468
+ "aria-label": "Formatting",
1469
+ ref: barRef,
1470
+ "data-compact": compact,
1471
+ children: [
1472
+ items.map((group) => ({
1473
+ key: group.join("-"),
1474
+ spacer: group.includes("spacer"),
1475
+ buttons: group.map(render).filter(Boolean)
1476
+ })).filter(({ spacer, buttons }) => spacer || buttons.length).map(({ key, spacer, buttons }, index, groups) => spacer ? /* @__PURE__ */ jsx("span", {
1477
+ className: "lee-spacer",
1478
+ "aria-hidden": "true"
1479
+ }, key) : /* @__PURE__ */ jsxs("div", {
1480
+ className: "lee-group",
1481
+ children: [index > 0 && !groups[index - 1].spacer && /* @__PURE__ */ jsx("span", {
1482
+ className: "lee-divider",
1483
+ "aria-hidden": "true"
1484
+ }), buttons]
1485
+ }, key)),
1486
+ end && /* @__PURE__ */ jsx("div", {
1487
+ className: "lee-toolbar-end",
1488
+ children: end
1489
+ }),
1490
+ tooltip
1491
+ ]
1492
+ });
1493
+ };
1494
+ // src/EmailEditor.jsx
1495
+ /**
1496
+ * Text to insert so a merge field is separated from the words around it.
1497
+ * @param {import('@tiptap/core').Editor} editor
1498
+ * @param {string} text
1499
+ */
1500
+ var spaced = (editor, text) => {
1501
+ const { from, to } = editor.state.selection;
1502
+ const { doc } = editor.state;
1503
+ const before = from > 1 ? doc.textBetween(from - 1, from, "\n", "\n") : "";
1504
+ const after = to < doc.content.size ? doc.textBetween(to, to + 1, "\n", "\n") : "";
1505
+ return `${before && !/\s/.test(before) ? " " : ""}${text}${!after || !/[\s.,;:!?)]/.test(after) ? " " : ""}`;
1506
+ };
1507
+ /**
1508
+ * Read the editor's content in the format the caller stores.
1509
+ * @param {import('@tiptap/core').Editor} editor
1510
+ * @param {'html' | 'markdown'} format
1511
+ */
1512
+ var read = (editor, format) => {
1513
+ if (format === "markdown") return editor.isEmpty ? "" : editor.getMarkdown();
1514
+ return normalizeOutput(editor.getHTML());
1515
+ };
1516
+ /**
1517
+ * Content and options for loading a stored value into the editor.
1518
+ * @param {string} value
1519
+ * @param {'html' | 'markdown'} format
1520
+ */
1521
+ var toContent = (value, format) => format === "markdown" ? [value ?? "", { contentType: "markdown" }] : [normalizeInput(value), { contentType: "html" }];
1522
+ /**
1523
+ * A rich-text editor for email bodies, stored as HTML or Markdown.
1524
+ *
1525
+ * @param {object} props
1526
+ * @param {'html' | 'markdown'} [props.format='html'] What `value` and `onChange` carry. Read once, when the editor is created.
1527
+ * Markdown hides the toolbar items it cannot store: colours, underline, alignment and text direction.
1528
+ * @param {'ltr' | 'rtl'} [props.dir='ltr'] The editor's direction. 'rtl' mirrors the toolbar and sets the direction of
1529
+ * paragraphs with no letters; every paragraph still follows its own text. Read once.
1530
+ * @param {string} [props.value] The HTML or Markdown to show. When it changes to something the editor did not send, the editor loads it.
1531
+ * @param {(value: string) => void} [props.onChange] Called with email-safe HTML (or Markdown) after the user stops typing for `debounce` ms,
1532
+ * and right away when the editor loses focus or unmounts
1533
+ * @param {number} [props.debounce=300]
1534
+ * @param {(file: File) => Promise<string | null | undefined>} [props.onUploadImage] Upload an image and resolve to its public
1535
+ * http(s) URL. Resolve to null when you have already told the user why it failed. Without it the image button is hidden
1536
+ * and pasted or dropped images are ignored.
1537
+ * @param {(message: string) => void} [props.onError] Show a message to the user, for example with a toast
1538
+ * @param {Array<string | { label: string, value?: string }>} [props.fields] Merge fields for the built-in "Insert field"
1539
+ * menu, such as column names. One toolbar button however many there are; a search box appears past six.
1540
+ * @param {(api: { insertMarker: (name: string) => void, insertText: (text: string) => void, editor: import('@tiptap/core').Editor }) => import('react').ReactNode} [props.renderMarkers]
1541
+ * Render your own merge-field picker at the end of the toolbar instead of the built-in menu
1542
+ * @param {string} [props.placeholder]
1543
+ * @param {boolean} [props.editable=true]
1544
+ * @param {string[][]} [props.toolbar] Groups of toolbar item ids; see DEFAULT_TOOLBAR and DEFAULT_MARKDOWN_TOOLBAR
1545
+ * @param {string[]} [props.moreItems] What the toolbar's More menu holds: any of hardBreak, blockquote, codeBlock, strike, underline,
1546
+ * rtl and clear. Default: hardBreak, strike, blockquote, codeBlock and clear.
1547
+ * @param {string[]} [props.colors] Swatches for text and background colour
1548
+ * @param {number} [props.maxImageSize] Largest image in bytes; 4MB by default
1549
+ * @param {string[]} [props.imageTypes] MIME types accepted for images
1550
+ * @param {boolean} [props.resizableImages=true] Show drag handles on images; the size is saved as width and height attributes
1551
+ * @param {boolean} [props.autofocus=false]
1552
+ * @param {import('@tiptap/core').AnyExtension[]} [props.extensions] Extra Tiptap extensions. Read once, when the editor is created.
1553
+ * @param {string} [props.className]
1554
+ * @param {import('react').CSSProperties} [props.style]
1555
+ * @param {string} [props.ariaLabel='Email body']
1556
+ */
1557
+ var EmailEditor = forwardRef(({ format: initialFormat = "html", dir: initialDir = "ltr", value = "", onChange, debounce = 300, onUploadImage, onError, fields, renderMarkers, placeholder, editable = true, toolbar, moreItems = DEFAULT_MORE_ITEMS, colors = DEFAULT_COLORS, maxImageSize = DEFAULT_MAX_IMAGE_SIZE, imageTypes = DEFAULT_IMAGE_TYPES, resizableImages = true, autofocus = false, extensions, className = "", style, ariaLabel = "Email body" }, ref) => {
1558
+ const format = useRef(initialFormat === "markdown" ? "markdown" : "html").current;
1559
+ const dir = useRef(initialDir === "rtl" ? "rtl" : "ltr").current;
1560
+ const props = useRef({});
1561
+ props.current = {
1562
+ onChange,
1563
+ onUploadImage,
1564
+ onError,
1565
+ maxImageSize,
1566
+ imageTypes,
1567
+ debounce
1568
+ };
1569
+ const fileInputRef = useRef(null);
1570
+ const timerRef = useRef(null);
1571
+ const pendingRef = useRef(null);
1572
+ const lastSentRef = useRef(value ?? "");
1573
+ const [uploading, setUploading] = useState(0);
1574
+ const report = useCallback((message) => {
1575
+ if (message) props.current.onError?.(message);
1576
+ }, []);
1577
+ const flush = useCallback(() => {
1578
+ clearTimeout(timerRef.current);
1579
+ timerRef.current = null;
1580
+ if (pendingRef.current === null) return;
1581
+ const html = pendingRef.current;
1582
+ pendingRef.current = null;
1583
+ props.current.onChange?.(html);
1584
+ }, []);
1585
+ const editorRef = useRef(null);
1586
+ const uploadFiles = useCallback(async (files, pos) => {
1587
+ const upload = props.current.onUploadImage;
1588
+ const editor = editorRef.current;
1589
+ if (!upload || !editor) return;
1590
+ const accepted = [];
1591
+ files.forEach((file) => {
1592
+ const problem = validateImage(file, {
1593
+ maxSize: props.current.maxImageSize,
1594
+ types: props.current.imageTypes
1595
+ });
1596
+ if (problem) report(problem);
1597
+ else accepted.push(file);
1598
+ });
1599
+ const at = Math.min(typeof pos === "number" ? pos : editor.state.selection.from, editor.state.doc.content.size);
1600
+ const pending = accepted.map((file) => {
1601
+ const preview = typeof URL.createObjectURL === "function" ? URL.createObjectURL(file) : "";
1602
+ return {
1603
+ file,
1604
+ preview,
1605
+ id: addPlaceholder(editor, at, preview)
1606
+ };
1607
+ });
1608
+ for (const { file, preview, id } of pending) {
1609
+ setUploading((count) => count + 1);
1610
+ try {
1611
+ const url = await upload(file);
1612
+ const { current } = editorRef;
1613
+ if (!current || current.isDestroyed) continue;
1614
+ const where = findPlaceholder(current, id);
1615
+ removePlaceholder(current, id);
1616
+ if (!url) continue;
1617
+ if (!isHttpUrl(url)) {
1618
+ report("Sorry the file could not be uploaded!");
1619
+ continue;
1620
+ }
1621
+ const image = {
1622
+ type: "image",
1623
+ attrs: {
1624
+ src: url.trim(),
1625
+ alt: ""
1626
+ }
1627
+ };
1628
+ current.commands.insertContentAt(where ?? current.state.selection.from, image, { updateSelection: false });
1629
+ } catch (error) {
1630
+ const { current } = editorRef;
1631
+ if (current && !current.isDestroyed) removePlaceholder(current, id);
1632
+ report(`Sorry the file could not be uploaded! ${error?.message ?? ""}`.trim());
1633
+ } finally {
1634
+ if (preview) URL.revokeObjectURL(preview);
1635
+ setUploading((count) => count - 1);
1636
+ }
1637
+ }
1638
+ }, [report]);
1639
+ const allExtensions = useMemo(() => createExtensions({
1640
+ format,
1641
+ direction: dir,
1642
+ placeholder,
1643
+ imageTypes,
1644
+ resizableImages,
1645
+ onFiles: onUploadImage ? uploadFiles : void 0,
1646
+ extensions
1647
+ }), []);
1648
+ const [initialContent, { contentType }] = toContent(value, format);
1649
+ const editor = useEditor({
1650
+ extensions: allExtensions,
1651
+ content: initialContent,
1652
+ contentType,
1653
+ editable,
1654
+ autofocus,
1655
+ immediatelyRender: true,
1656
+ shouldRerenderOnTransaction: false,
1657
+ editorProps: { attributes: {
1658
+ class: "lee-content",
1659
+ "aria-label": ariaLabel,
1660
+ "aria-multiline": "true",
1661
+ role: "textbox"
1662
+ } },
1663
+ onUpdate: ({ editor: current }) => {
1664
+ const next = read(current, format);
1665
+ lastSentRef.current = next;
1666
+ pendingRef.current = next;
1667
+ clearTimeout(timerRef.current);
1668
+ timerRef.current = setTimeout(flush, props.current.debounce);
1669
+ },
1670
+ onBlur: () => flush()
1671
+ });
1672
+ editorRef.current = editor;
1673
+ useEffect(() => () => flush(), [flush]);
1674
+ useEffect(() => {
1675
+ if (!editor || editor.isDestroyed) return;
1676
+ const next = value ?? "";
1677
+ if (next === lastSentRef.current) return;
1678
+ lastSentRef.current = next;
1679
+ if (format === "html" && read(editor, format) === normalizeOutput(next)) return;
1680
+ clearTimeout(timerRef.current);
1681
+ pendingRef.current = null;
1682
+ const [content, options] = toContent(next, format);
1683
+ editor.commands.setContent(content, {
1684
+ ...options,
1685
+ emitUpdate: false
1686
+ });
1687
+ }, [
1688
+ editor,
1689
+ value,
1690
+ format
1691
+ ]);
1692
+ useEffect(() => {
1693
+ if (editor && !editor.isDestroyed && editor.isEditable !== editable) editor.setEditable(editable, false);
1694
+ }, [editor, editable]);
1695
+ const insertText = useCallback((text) => {
1696
+ if (!editor || !text) return;
1697
+ editor.chain().focus().insertContent(spaced(editor, text)).run();
1698
+ editor.view.focus();
1699
+ }, [editor]);
1700
+ const insertMarker = useCallback((name) => insertText(markerText(name)), [insertText]);
1701
+ useImperativeHandle(ref, () => ({
1702
+ editor,
1703
+ insertMarker,
1704
+ insertText,
1705
+ focus: () => editor?.commands.focus(),
1706
+ getValue: () => editor ? read(editor, format) : "",
1707
+ getHTML: () => editor ? normalizeOutput(editor.getHTML()) : "",
1708
+ flush
1709
+ }), [
1710
+ editor,
1711
+ insertMarker,
1712
+ insertText,
1713
+ flush,
1714
+ format
1715
+ ]);
1716
+ const pickImage = () => fileInputRef.current?.click();
1717
+ const onFileInput = async (event) => {
1718
+ const files = Array.from(event.target.files ?? []);
1719
+ event.target.value = "";
1720
+ if (!files.length) return;
1721
+ if (editor && !editor.isFocused) editor.commands.focus();
1722
+ await uploadFiles(files);
1723
+ };
1724
+ if (!editor) return null;
1725
+ let toolbarEnd = null;
1726
+ if (renderMarkers) toolbarEnd = renderMarkers({
1727
+ insertMarker,
1728
+ insertText,
1729
+ editor
1730
+ });
1731
+ else if (fields?.length) toolbarEnd = /* @__PURE__ */ jsx(FieldsMenu, {
1732
+ fields,
1733
+ onInsert: insertMarker,
1734
+ disabled: !editable
1735
+ });
1736
+ return /* @__PURE__ */ jsxs("div", {
1737
+ className: `lee-editor ${className}`.trim(),
1738
+ dir,
1739
+ style,
1740
+ "data-disabled": !editable || void 0,
1741
+ children: [
1742
+ /* @__PURE__ */ jsx(Toolbar, {
1743
+ editor,
1744
+ items: toolbar ?? (format === "markdown" ? DEFAULT_MARKDOWN_TOOLBAR : DEFAULT_TOOLBAR),
1745
+ moreItems,
1746
+ colors,
1747
+ canUploadImages: Boolean(onUploadImage),
1748
+ uploading: uploading > 0,
1749
+ onPickImage: pickImage,
1750
+ end: toolbarEnd
1751
+ }),
1752
+ onUploadImage && /* @__PURE__ */ jsx("input", {
1753
+ ref: fileInputRef,
1754
+ type: "file",
1755
+ accept: imageTypes.join(","),
1756
+ multiple: true,
1757
+ hidden: true,
1758
+ tabIndex: -1,
1759
+ "aria-hidden": "true",
1760
+ onChange: onFileInput
1761
+ }),
1762
+ /* @__PURE__ */ jsx(EditorContent, {
1763
+ editor,
1764
+ className: "lee-scroll"
1765
+ })
1766
+ ]
1767
+ });
1768
+ });
1769
+ EmailEditor.displayName = "EmailEditor";
1770
+ export { DEFAULT_COLORS, DEFAULT_IMAGE_TYPES, DEFAULT_MARKDOWN_TOOLBAR, DEFAULT_MAX_IMAGE_SIZE, DEFAULT_MORE_ITEMS, DEFAULT_TOOLBAR, EmailEditor, FieldsMenu, MORE_MENU_ITEMS, TOOLBAR_ITEMS, isEmptyHtml, isHttpUrl, markerText, normalizeInput, normalizeOutput, readAsDataURL, validateImage };
1771
+
1772
+ //# sourceMappingURL=index.js.map