@bizapps/design-system-testing 1.9.0 → 1.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/editor.js ADDED
@@ -0,0 +1,1467 @@
1
+ "use client";
2
+ import { createContext, useId, useState, useCallback, useMemo, useRef, useEffect, useContext } from 'react';
3
+ import { EditorContent, ReactNodeViewRenderer, useEditor, NodeViewWrapper, useEditorState } from '@tiptap/react';
4
+ import { clsx } from 'clsx';
5
+ import { twMerge } from 'tailwind-merge';
6
+ import { Slot, Label as Label$1, DropdownMenu as DropdownMenu$1, Dialog as Dialog$1 } from 'radix-ui';
7
+ import { jsx, jsxs } from 'react/jsx-runtime';
8
+ import { cva } from 'class-variance-authority';
9
+ import { Trash2, ChevronDown, Check, Bold, Italic, Heading2, Highlighter, Underline, Strikethrough, Quote, List, ListOrdered, Link, ImagePlus, Table, Ban, LoaderCircle, XIcon, UploadIcon, ExternalLink, Pencil, Unlink } from 'lucide-react';
10
+ import { motion, AnimatePresence } from 'motion/react';
11
+ import { useDropzone } from 'react-dropzone';
12
+ import Highlight from '@tiptap/extension-highlight';
13
+ import { TableKit } from '@tiptap/extension-table';
14
+ import StarterKit from '@tiptap/starter-kit';
15
+ import { Image } from '@tiptap/extension-image';
16
+ import { BubbleMenu } from '@tiptap/react/menus';
17
+
18
+ // src/components/compounds/RichTextEditor/rich-text-editor.tsx
19
+ function cn(...inputs) {
20
+ return twMerge(clsx(inputs));
21
+ }
22
+ function Label({ className, ...props }) {
23
+ return /* @__PURE__ */ jsx(
24
+ Label$1.Root,
25
+ {
26
+ "data-slot": "label",
27
+ className: cn(
28
+ "text-foreground text-sm leading-6 font-medium transition-colors select-none",
29
+ "group-has-disabled/field:cursor-not-allowed group-has-disabled/field:opacity-50",
30
+ "peer-disabled:cursor-not-allowed peer-disabled:opacity-50",
31
+ "aria-invalid:text-destructive dark:aria-invalid:text-destructive/90",
32
+ className
33
+ ),
34
+ ...props
35
+ }
36
+ );
37
+ }
38
+ var buttonVariants = cva(
39
+ "group/button inline-flex shrink-0 items-center justify-center gap-2 rounded-lg border border-transparent bg-clip-padding text-sm font-semibold whitespace-nowrap transition-all duration-150 outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/30 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 data-[loading]:cursor-not-allowed data-[loading]:opacity-80 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0",
40
+ {
41
+ variants: {
42
+ variant: {
43
+ primary: "cursor-pointer bg-primary text-primary-foreground hover:bg-primary/80 focus-visible:ring-primary/30",
44
+ secondary: "cursor-pointer bg-secondary text-secondary-foreground hover:bg-[var(--color-orange-700)] focus-visible:ring-secondary/30",
45
+ cancel: "cursor-pointer bg-destructive text-secondary-foreground hover:bg-[var(--color-red-600)] focus-visible:ring-destructive/30",
46
+ outline: "border border-foreground/50 bg-transparent text-muted-foreground hover:bg-foreground/50 hover:text-white",
47
+ ghost: "cursor-pointer bg-transparent text-muted-foreground hover:bg-foreground/10 hover:text-primary focus-visible:ring-ring/30 border-none",
48
+ cancelGhost: "cursor-pointer bg-destructive/10 text-red-600 hover:bg-destructive/15 hover:text-red-600 focus-visible:ring-ring/30"
49
+ },
50
+ size: {
51
+ sm: "h-8 px-3 text-xs",
52
+ md: "h-9 px-4 text-sm",
53
+ lg: "h-10 px-6 text-sm",
54
+ "icon-sm": "size-8 rounded-full p-0"
55
+ }
56
+ },
57
+ defaultVariants: {
58
+ variant: "primary",
59
+ size: "md"
60
+ }
61
+ }
62
+ );
63
+ function Button({
64
+ className,
65
+ variant = "primary",
66
+ size = "md",
67
+ asChild = false,
68
+ isLoading = false,
69
+ children,
70
+ onClick,
71
+ ...props
72
+ }) {
73
+ const Comp = asChild ? Slot.Root : "button";
74
+ function handleClick(e) {
75
+ if (isLoading) {
76
+ e.preventDefault();
77
+ return;
78
+ }
79
+ onClick?.(e);
80
+ }
81
+ return /* @__PURE__ */ jsxs(
82
+ Comp,
83
+ {
84
+ "data-slot": "button",
85
+ "data-variant": variant,
86
+ "data-size": size,
87
+ "data-loading": isLoading || void 0,
88
+ "aria-busy": isLoading || void 0,
89
+ className: cn(buttonVariants({ variant, size, className })),
90
+ onClick: handleClick,
91
+ ...props,
92
+ children: [
93
+ isLoading && /* @__PURE__ */ jsx(LoaderCircle, { size: 16, className: "animate-spin", "aria-hidden": "true" }),
94
+ children
95
+ ]
96
+ }
97
+ );
98
+ }
99
+ function RichTextToolbar({ editor, items }) {
100
+ return /* @__PURE__ */ jsx(
101
+ "div",
102
+ {
103
+ role: "toolbar",
104
+ "data-slot": "rich-text-toolbar",
105
+ className: "flex items-center gap-1 rounded-t-md border border-b-0 p-1",
106
+ children: items.map((item, index) => {
107
+ if (item.kind === "separator") {
108
+ return /* @__PURE__ */ jsx(
109
+ "div",
110
+ {
111
+ "data-slot": "rich-text-toolbar-separator",
112
+ "aria-hidden": "true",
113
+ className: "bg-border mx-1 h-5 w-px"
114
+ },
115
+ `separator-${index}`
116
+ );
117
+ }
118
+ if (item.kind === "custom") {
119
+ return /* @__PURE__ */ jsx("span", { children: item.render(editor) }, item.id);
120
+ }
121
+ const active = item.active ? editor?.isActive(item.active.name, item.active.attrs) ?? false : false;
122
+ return /* @__PURE__ */ jsx(
123
+ Button,
124
+ {
125
+ type: "button",
126
+ size: "sm",
127
+ variant: active ? "secondary" : "ghost",
128
+ disabled: !editor,
129
+ title: item.label,
130
+ "aria-label": item.label,
131
+ "aria-pressed": item.active ? active : void 0,
132
+ "data-slot": "rich-text-toolbar-button",
133
+ onClick: () => editor && item.run(editor),
134
+ children: item.icon
135
+ },
136
+ item.id
137
+ );
138
+ })
139
+ }
140
+ );
141
+ }
142
+ var EDITOR_CONTENT_STYLE = cn(
143
+ "min-h-[220px] px-3 py-2 text-sm outline-none",
144
+ "[&_h2]:text-lg [&_h2]:font-semibold",
145
+ "[&_ol]:my-4 [&_ol]:list-decimal [&_ol]:pl-5 [&_ul]:my-4 [&_ul]:list-disc [&_ul]:pl-5",
146
+ "[&_blockquote]:border-secondary [&_blockquote]:my-4 [&_blockquote]:ml-4 [&_blockquote]:border-l-4 [&_blockquote]:pl-2",
147
+ "[&_a]:text-primary [&_a]:underline",
148
+ // last-of-type, not last-child: prosemirror-tables appends its
149
+ // .column-resize-handle widget inside the hovered cell, which makes the
150
+ // cell's <p> stop matching :last-child and springs mb-4 back on — the row
151
+ // grows 16px on hover. The handle is absolutely positioned but still counts
152
+ // as a child for structural selectors.
153
+ "[&_p]:mb-4 [&_p]:min-h-[1.25em] [&_p:last-of-type]:mb-0",
154
+ // Paragraphs inside a quote sit closer together than body copy. Every Enter
155
+ // inside a blockquote opens a new <p>, and at the body's mb-4 the lines of a
156
+ // single quote read as separate paragraphs. Declared after the rule above so
157
+ // it wins on equal specificity.
158
+ "[&_blockquote_p]:mb-1 [&_blockquote_p:last-of-type]:mb-0",
159
+ "[&_img]:my-2 [&_img]:max-w-full [&_img]:rounded-md",
160
+ "[&_table]:my-3 [&_table]:w-full [&_table]:table-fixed [&_table]:border-collapse",
161
+ "[&_td]:relative [&_td]:border [&_td]:px-2 [&_td]:py-1 [&_td]:align-top",
162
+ "[&_th]:bg-muted [&_th]:relative [&_th]:border [&_th]:px-2 [&_th]:py-1 [&_th]:text-left [&_th]:font-semibold",
163
+ "[&_.selectedCell]:bg-primary/10",
164
+ // prosemirror-tables renders one handle PER ROW, so the column reads as a
165
+ // single bar down the table. At full primary it looked like the table had
166
+ // broken rather than like something to grab — hence the muted tint and the
167
+ // wider, border-straddling hit area (matches upstream's 4px).
168
+ "[&_.column-resize-handle]:bg-primary/40 [&_.column-resize-handle]:pointer-events-none [&_.column-resize-handle]:absolute [&_.column-resize-handle]:top-0 [&_.column-resize-handle]:-right-0.5 [&_.column-resize-handle]:bottom-0 [&_.column-resize-handle]:z-20 [&_.column-resize-handle]:w-1",
169
+ "[&.resize-cursor]:cursor-col-resize",
170
+ // Selected-node outline, scoped away from images.
171
+ //
172
+ // ProseMirror marks whichever node is selected, and this ring is the only
173
+ // cue for most of them — but an image already announces its selection with a
174
+ // visible resize handle, so the extra box only framed the picture.
175
+ //
176
+ // Excluded via `.node-image`, the class Tiptap puts on the wrapper it
177
+ // generates for a node view (`div.react-renderer.node-<name>`). That wrapper,
178
+ // not the NodeViewWrapper inside it, is what receives
179
+ // ProseMirror-selectednode — so a data-slot on our own element sits one level
180
+ // too deep to match.
181
+ "[&_.ProseMirror-selectednode:not(.node-image)]:outline-primary [&_.ProseMirror-selectednode:not(.node-image)]:outline-2 [&_.ProseMirror-selectednode:not(.node-image)]:outline-offset-2 [&_.ProseMirror-selectednode:not(.node-image)]:outline"
182
+ );
183
+ function RichTextEditor({ editor, items, label, error, footer, className }) {
184
+ const errorId = useId();
185
+ return /* @__PURE__ */ jsxs("div", { "data-slot": "rich-text-editor", className: cn("flex flex-col gap-1", className), children: [
186
+ label && /* @__PURE__ */ jsx(Label, { children: label }),
187
+ /* @__PURE__ */ jsx(RichTextToolbar, { editor, items }),
188
+ /* @__PURE__ */ jsx(
189
+ EditorContent,
190
+ {
191
+ editor,
192
+ "aria-invalid": !!error,
193
+ "aria-errormessage": error ? errorId : void 0,
194
+ onClick: () => editor?.commands.focus(),
195
+ className: cn(
196
+ "rounded-t-none rounded-b-md border shadow-xs",
197
+ "focus-within:border-ring focus-within:ring-ring/30 focus-within:ring-3",
198
+ error && "border-destructive"
199
+ )
200
+ }
201
+ ),
202
+ error && /* @__PURE__ */ jsx("span", { id: errorId, role: "alert", className: "text-destructive text-xs", children: error }),
203
+ footer
204
+ ] });
205
+ }
206
+ function DropdownMenu({ ...props }) {
207
+ return /* @__PURE__ */ jsx(DropdownMenu$1.Root, { ...props });
208
+ }
209
+ function DropdownMenuTrigger({
210
+ ...props
211
+ }) {
212
+ return /* @__PURE__ */ jsx(DropdownMenu$1.Trigger, { "data-slot": "dropdown-menu-trigger", ...props });
213
+ }
214
+ function DropdownMenuContent({
215
+ className,
216
+ align = "start",
217
+ sideOffset = 4,
218
+ ...props
219
+ }) {
220
+ return /* @__PURE__ */ jsx(DropdownMenu$1.Portal, { children: /* @__PURE__ */ jsx(
221
+ DropdownMenu$1.Content,
222
+ {
223
+ "data-slot": "dropdown-menu-content",
224
+ sideOffset,
225
+ align,
226
+ className: cn(
227
+ "bg-popover text-popover-foreground ring-foreground/5 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 dark:ring-foreground/10 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 z-50 max-h-(--radix-dropdown-menu-content-available-height) w-(--radix-dropdown-menu-trigger-width) min-w-48 origin-(--radix-dropdown-menu-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-3xl p-1.5 shadow-lg ring-1 duration-100 data-[state=closed]:overflow-hidden",
228
+ className
229
+ ),
230
+ ...props
231
+ }
232
+ ) });
233
+ }
234
+ function DropdownMenuItem({
235
+ className,
236
+ inset,
237
+ variant = "default",
238
+ ...props
239
+ }) {
240
+ return /* @__PURE__ */ jsx(
241
+ DropdownMenu$1.Item,
242
+ {
243
+ "data-slot": "dropdown-menu-item",
244
+ "data-inset": inset,
245
+ "data-variant": variant,
246
+ className: cn(
247
+ "group/dropdown-menu-item focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-[variant=destructive]:text-destructive data-[variant=destructive]:focus:bg-destructive/10 data-[variant=destructive]:focus:text-destructive dark:data-[variant=destructive]:focus:bg-destructive/20 data-[variant=destructive]:*:[svg]:text-destructive relative flex cursor-default items-center gap-2.5 rounded-2xl px-3 py-2 text-sm font-medium outline-hidden select-none data-disabled:pointer-events-none data-disabled:opacity-50 data-inset:pl-9.5 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
248
+ className
249
+ ),
250
+ ...props
251
+ }
252
+ );
253
+ }
254
+ function DropdownMenuSeparator({
255
+ className,
256
+ ...props
257
+ }) {
258
+ return /* @__PURE__ */ jsx(
259
+ DropdownMenu$1.Separator,
260
+ {
261
+ "data-slot": "dropdown-menu-separator",
262
+ className: cn("bg-border/50 -mx-1.5 my-1.5 h-px", className),
263
+ ...props
264
+ }
265
+ );
266
+ }
267
+ var DEFAULT_ROWS = 8;
268
+ var DEFAULT_COLUMNS = 10;
269
+ function TableMenu({
270
+ editor,
271
+ labels,
272
+ icon,
273
+ maxRows = DEFAULT_ROWS,
274
+ maxColumns = DEFAULT_COLUMNS
275
+ }) {
276
+ const [hovered, setHovered] = useState({ rows: 0, columns: 0 });
277
+ const insideTable = editor?.can().deleteTable() ?? false;
278
+ function insert(rows, columns) {
279
+ editor?.chain().focus().insertTable({ rows, cols: columns, withHeaderRow: true }).run();
280
+ }
281
+ return /* @__PURE__ */ jsxs(DropdownMenu, { children: [
282
+ /* @__PURE__ */ jsx(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsx(
283
+ Button,
284
+ {
285
+ type: "button",
286
+ size: "sm",
287
+ variant: editor?.isActive("table") ? "secondary" : "ghost",
288
+ disabled: !editor,
289
+ title: labels.trigger,
290
+ "aria-label": labels.trigger,
291
+ "data-slot": "table-menu-trigger",
292
+ children: icon
293
+ }
294
+ ) }),
295
+ /* @__PURE__ */ jsxs(DropdownMenuContent, { align: "start", className: "w-56", children: [
296
+ /* @__PURE__ */ jsxs("div", { role: "none", className: "px-2 py-1.5", children: [
297
+ /* @__PURE__ */ jsx("p", { className: "text-muted-foreground mb-1.5 text-xs", children: labels.insert }),
298
+ /* @__PURE__ */ jsx(
299
+ "div",
300
+ {
301
+ className: "grid gap-0.5",
302
+ style: { gridTemplateColumns: `repeat(${maxColumns}, 1fr)` },
303
+ onMouseLeave: () => setHovered({ rows: 0, columns: 0 }),
304
+ children: Array.from({ length: maxRows * maxColumns }, (_, index) => {
305
+ const rows = Math.floor(index / maxColumns) + 1;
306
+ const columns = index % maxColumns + 1;
307
+ const active = rows <= hovered.rows && columns <= hovered.columns;
308
+ return /* @__PURE__ */ jsx(
309
+ "button",
310
+ {
311
+ type: "button",
312
+ "aria-label": labels.dimensions(rows, columns),
313
+ className: cn(
314
+ "h-3.5 w-3.5 cursor-pointer rounded-[2px] border",
315
+ active ? "bg-primary border-primary" : "bg-background"
316
+ ),
317
+ onMouseEnter: () => setHovered({ rows, columns }),
318
+ onFocus: () => setHovered({ rows, columns }),
319
+ onClick: () => insert(rows, columns)
320
+ },
321
+ index
322
+ );
323
+ })
324
+ }
325
+ ),
326
+ /* @__PURE__ */ jsx(
327
+ "p",
328
+ {
329
+ "aria-live": "polite",
330
+ "data-slot": "table-menu-dimensions",
331
+ className: "text-muted-foreground mt-1.5 text-xs tabular-nums",
332
+ children: labels.dimensions(hovered.rows, hovered.columns)
333
+ }
334
+ )
335
+ ] }),
336
+ /* @__PURE__ */ jsx(DropdownMenuSeparator, {}),
337
+ /* @__PURE__ */ jsx(
338
+ DropdownMenuItem,
339
+ {
340
+ className: "cursor-pointer",
341
+ disabled: !insideTable,
342
+ onSelect: () => editor?.chain().focus().addRowBefore().run(),
343
+ children: labels.rowBefore
344
+ }
345
+ ),
346
+ /* @__PURE__ */ jsx(
347
+ DropdownMenuItem,
348
+ {
349
+ className: "cursor-pointer",
350
+ disabled: !insideTable,
351
+ onSelect: () => editor?.chain().focus().addRowAfter().run(),
352
+ children: labels.rowAfter
353
+ }
354
+ ),
355
+ /* @__PURE__ */ jsx(
356
+ DropdownMenuItem,
357
+ {
358
+ className: "cursor-pointer",
359
+ variant: "destructive",
360
+ disabled: !insideTable,
361
+ onSelect: () => editor?.chain().focus().deleteRow().run(),
362
+ children: labels.deleteRow
363
+ }
364
+ ),
365
+ /* @__PURE__ */ jsx(DropdownMenuSeparator, {}),
366
+ /* @__PURE__ */ jsx(
367
+ DropdownMenuItem,
368
+ {
369
+ className: "cursor-pointer",
370
+ disabled: !insideTable,
371
+ onSelect: () => editor?.chain().focus().addColumnBefore().run(),
372
+ children: labels.columnBefore
373
+ }
374
+ ),
375
+ /* @__PURE__ */ jsx(
376
+ DropdownMenuItem,
377
+ {
378
+ className: "cursor-pointer",
379
+ disabled: !insideTable,
380
+ onSelect: () => editor?.chain().focus().addColumnAfter().run(),
381
+ children: labels.columnAfter
382
+ }
383
+ ),
384
+ /* @__PURE__ */ jsx(
385
+ DropdownMenuItem,
386
+ {
387
+ className: "cursor-pointer",
388
+ variant: "destructive",
389
+ disabled: !insideTable,
390
+ onSelect: () => editor?.chain().focus().deleteColumn().run(),
391
+ children: labels.deleteColumn
392
+ }
393
+ ),
394
+ /* @__PURE__ */ jsx(DropdownMenuSeparator, {}),
395
+ /* @__PURE__ */ jsx(
396
+ DropdownMenuItem,
397
+ {
398
+ className: "cursor-pointer",
399
+ disabled: !insideTable,
400
+ onSelect: () => editor?.chain().focus().toggleHeaderRow().run(),
401
+ children: labels.toggleHeader
402
+ }
403
+ ),
404
+ /* @__PURE__ */ jsx(
405
+ DropdownMenuItem,
406
+ {
407
+ className: "cursor-pointer",
408
+ variant: "destructive",
409
+ disabled: !insideTable,
410
+ onSelect: () => editor?.chain().focus().deleteTable().run(),
411
+ children: labels.deleteTable
412
+ }
413
+ )
414
+ ] })
415
+ ] });
416
+ }
417
+
418
+ // src/tokens/animation.ts
419
+ var duration = {
420
+ normal: 0.25};
421
+ var ease = {
422
+ smooth: [0.4, 0, 0.2, 1]};
423
+ var spring = {
424
+ gentle: { type: "spring", stiffness: 260, damping: 28 }};
425
+ var transition = {
426
+ // Modal / dialog — más lento para dar peso al overlay
427
+ modal: {
428
+ ...spring.gentle
429
+ }};
430
+ var DialogContext = createContext(false);
431
+ function Dialog({ children, ...props }) {
432
+ const [internalOpen, setInternalOpen] = useState(props.defaultOpen ?? false);
433
+ const isOpen = props.open !== void 0 ? props.open : internalOpen;
434
+ return /* @__PURE__ */ jsx(DialogContext.Provider, { value: isOpen, children: /* @__PURE__ */ jsx(
435
+ Dialog$1.Root,
436
+ {
437
+ "data-slot": "dialog",
438
+ open: isOpen,
439
+ onOpenChange: (v) => {
440
+ setInternalOpen(v);
441
+ props.onOpenChange?.(v);
442
+ },
443
+ ...props,
444
+ children
445
+ }
446
+ ) });
447
+ }
448
+ function DialogPortal({ children, ...props }) {
449
+ const isOpen = useContext(DialogContext);
450
+ return /* @__PURE__ */ jsx(Dialog$1.Portal, { "data-slot": "dialog-portal", forceMount: true, ...props, children: /* @__PURE__ */ jsx(AnimatePresence, { mode: "sync", children: isOpen && children }) });
451
+ }
452
+ function DialogOverlay({ className, ...props }) {
453
+ return /* @__PURE__ */ jsx(Dialog$1.Overlay, { "data-slot": "dialog-overlay", asChild: true, forceMount: true, ...props, children: /* @__PURE__ */ jsx(
454
+ motion.div,
455
+ {
456
+ className: cn(
457
+ "fixed inset-0 isolate z-50 bg-black/30 supports-backdrop-filter:backdrop-blur-sm",
458
+ className
459
+ ),
460
+ initial: { opacity: 0 },
461
+ animate: { opacity: 1 },
462
+ exit: { opacity: 0 },
463
+ transition: { duration: duration.normal, ease: ease.smooth }
464
+ }
465
+ ) });
466
+ }
467
+ function DialogContent({
468
+ className,
469
+ children,
470
+ showCloseButton = false,
471
+ ...props
472
+ }) {
473
+ return /* @__PURE__ */ jsxs(DialogPortal, { children: [
474
+ /* @__PURE__ */ jsx(DialogOverlay, {}, "dialog-overlay"),
475
+ /* @__PURE__ */ jsx(
476
+ Dialog$1.Content,
477
+ {
478
+ "data-slot": "dialog-content",
479
+ asChild: true,
480
+ forceMount: true,
481
+ className: cn(
482
+ "bg-popover text-popover-foreground ring-foreground/5 dark:ring-foreground/10 fixed top-1/2 left-1/2 z-50 flex max-h-[85vh] w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 flex-col rounded-lg text-sm shadow-xl ring-1 outline-none sm:max-w-sm",
483
+ className
484
+ ),
485
+ ...props,
486
+ children: /* @__PURE__ */ jsxs(
487
+ motion.div,
488
+ {
489
+ className: "flex max-h-[85vh] flex-col",
490
+ initial: { opacity: 0, scale: 0.96, y: 8 },
491
+ animate: { opacity: 1, scale: 1, y: 0 },
492
+ exit: { opacity: 0, scale: 0.96, y: 4 },
493
+ transition: transition.modal,
494
+ children: [
495
+ children,
496
+ showCloseButton && /* @__PURE__ */ jsx(Dialog$1.Close, { "data-slot": "dialog-close", asChild: true, children: /* @__PURE__ */ jsxs(
497
+ Button,
498
+ {
499
+ variant: "ghost",
500
+ className: "absolute top-4 right-4",
501
+ size: "icon-sm",
502
+ children: [
503
+ /* @__PURE__ */ jsx(XIcon, {}),
504
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Close" })
505
+ ]
506
+ }
507
+ ) })
508
+ ]
509
+ }
510
+ )
511
+ },
512
+ "dialog-content"
513
+ )
514
+ ] });
515
+ }
516
+ function DialogHeader({ className, ...props }) {
517
+ return /* @__PURE__ */ jsx(
518
+ "div",
519
+ {
520
+ "data-slot": "dialog-header",
521
+ className: cn("flex shrink-0 flex-col gap-3 px-6 pt-6", className),
522
+ ...props
523
+ }
524
+ );
525
+ }
526
+ function DialogFooter({
527
+ className,
528
+ showCloseButton = false,
529
+ children,
530
+ ...props
531
+ }) {
532
+ return /* @__PURE__ */ jsxs(
533
+ "div",
534
+ {
535
+ "data-slot": "dialog-footer",
536
+ className: cn(
537
+ "border-border bg-muted/40 flex shrink-0 flex-wrap items-center justify-end gap-2 rounded-b-lg border-t px-6 pt-4 pb-4",
538
+ className
539
+ ),
540
+ ...props,
541
+ children: [
542
+ children,
543
+ showCloseButton && /* @__PURE__ */ jsx(Dialog$1.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { variant: "secondary", children: "Close" }) })
544
+ ]
545
+ }
546
+ );
547
+ }
548
+ function DialogTitle({ className, ...props }) {
549
+ return /* @__PURE__ */ jsx(
550
+ Dialog$1.Title,
551
+ {
552
+ "data-slot": "dialog-title",
553
+ className: cn("font-heading text-lg leading-none font-bold", className),
554
+ ...props
555
+ }
556
+ );
557
+ }
558
+ var renderBytes = (bytes) => {
559
+ const units = ["B", "KB", "MB", "GB", "TB", "PB"];
560
+ let size = bytes;
561
+ let unitIndex = 0;
562
+ while (size >= 1024 && unitIndex < units.length - 1) {
563
+ size /= 1024;
564
+ unitIndex++;
565
+ }
566
+ return `${size.toFixed(2)}${units[unitIndex]}`;
567
+ };
568
+ var DropzoneContext = createContext(void 0);
569
+ var dropzoneVariants = cva("", {
570
+ variants: {
571
+ border: {
572
+ default: "border border-solid border-foreground/15",
573
+ dashed: "border-dashed border-foreground/50"
574
+ }
575
+ },
576
+ defaultVariants: {
577
+ border: "default"
578
+ }
579
+ });
580
+ var Dropzone = ({
581
+ accept,
582
+ maxFiles = 1,
583
+ maxSize,
584
+ minSize,
585
+ onDrop,
586
+ onError,
587
+ disabled,
588
+ src,
589
+ className,
590
+ children,
591
+ border,
592
+ ...props
593
+ }) => {
594
+ const { getRootProps, getInputProps, isDragActive } = useDropzone({
595
+ accept,
596
+ maxFiles,
597
+ maxSize,
598
+ minSize,
599
+ onError,
600
+ disabled,
601
+ onDrop: (acceptedFiles, fileRejections, event) => {
602
+ if (fileRejections.length > 0) {
603
+ const message = fileRejections.at(0)?.errors.at(0)?.message;
604
+ onError?.(new Error(message));
605
+ }
606
+ if (acceptedFiles.length > 0) {
607
+ onDrop?.(acceptedFiles, fileRejections, event);
608
+ }
609
+ },
610
+ ...props
611
+ });
612
+ return /* @__PURE__ */ jsx(
613
+ DropzoneContext.Provider,
614
+ {
615
+ value: { src, accept, maxSize, minSize, maxFiles },
616
+ children: /* @__PURE__ */ jsxs(
617
+ Button,
618
+ {
619
+ "data-slot": "dropzone",
620
+ "data-border": border ?? "default",
621
+ className: cn(
622
+ "cursor-auto relative h-auto w-full flex-col overflow-hidden p-8",
623
+ dropzoneVariants({ border }),
624
+ isDragActive && "bg-foreground/10 outline-none",
625
+ className
626
+ ),
627
+ disabled,
628
+ type: "button",
629
+ variant: "ghost",
630
+ ...getRootProps(),
631
+ children: [
632
+ /* @__PURE__ */ jsx("input", { ...getInputProps(), disabled }),
633
+ children
634
+ ]
635
+ }
636
+ )
637
+ },
638
+ JSON.stringify(src)
639
+ );
640
+ };
641
+ var useDropzoneContext = () => {
642
+ const context = useContext(DropzoneContext);
643
+ if (!context) {
644
+ throw new Error("useDropzoneContext must be used within a Dropzone");
645
+ }
646
+ return context;
647
+ };
648
+ var maxLabelItems = 3;
649
+ var DropzoneContent = ({ children, className }) => {
650
+ const { src } = useDropzoneContext();
651
+ if (!src) {
652
+ return null;
653
+ }
654
+ if (children) {
655
+ return children;
656
+ }
657
+ return /* @__PURE__ */ jsxs("div", { className: cn("flex flex-col items-center justify-center", className), children: [
658
+ /* @__PURE__ */ jsx("div", { className: "bg-muted text-muted-foreground flex size-8 items-center justify-center rounded-md", children: /* @__PURE__ */ jsx(UploadIcon, { size: 16 }) }),
659
+ /* @__PURE__ */ jsx("p", { className: "text-foreground my-2 w-full truncate text-sm font-medium", children: src.length > maxLabelItems ? `${new Intl.ListFormat("en").format(
660
+ src.slice(0, maxLabelItems).map((file) => file.name)
661
+ )} and ${src.length - maxLabelItems} more` : new Intl.ListFormat("en").format(src.map((file) => file.name)) }),
662
+ /* @__PURE__ */ jsx("p", { className: "text-muted-foreground w-full text-xs font-normal text-wrap", children: "Drag and drop or click to replace" })
663
+ ] });
664
+ };
665
+ var DropzoneEmptyState = ({ children, className }) => {
666
+ const { src, accept, maxSize, minSize, maxFiles } = useDropzoneContext();
667
+ if (src) {
668
+ return null;
669
+ }
670
+ if (children) {
671
+ return children;
672
+ }
673
+ let caption = "";
674
+ if (accept) {
675
+ caption += "Accepts ";
676
+ caption += new Intl.ListFormat("en").format(Object.keys(accept));
677
+ }
678
+ if (minSize && maxSize) {
679
+ caption += ` between ${renderBytes(minSize)} and ${renderBytes(maxSize)}`;
680
+ } else if (minSize) {
681
+ caption += ` at least ${renderBytes(minSize)}`;
682
+ } else if (maxSize) {
683
+ caption += ` less than ${renderBytes(maxSize)}`;
684
+ }
685
+ return /* @__PURE__ */ jsxs("div", { className: cn("flex flex-col items-center justify-center", className), children: [
686
+ /* @__PURE__ */ jsx("div", { className: "bg-muted text-muted-foreground flex size-8 items-center justify-center rounded-md", children: /* @__PURE__ */ jsx(UploadIcon, { size: 16 }) }),
687
+ /* @__PURE__ */ jsxs("p", { className: "text-foreground my-2 w-full text-center text-sm font-medium text-wrap", children: [
688
+ "Upload ",
689
+ maxFiles === 1 ? "a file" : "files"
690
+ ] }),
691
+ /* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-center text-xs font-normal text-wrap", children: "Drag and drop or click to upload" }),
692
+ caption && /* @__PURE__ */ jsxs("p", { className: "text-muted-foreground text-xs font-normal text-wrap", children: [
693
+ caption,
694
+ "."
695
+ ] })
696
+ ] });
697
+ };
698
+ function Input({ className, type, ...props }) {
699
+ return /* @__PURE__ */ jsx(
700
+ "input",
701
+ {
702
+ type,
703
+ "data-slot": "input",
704
+ className: cn(
705
+ "border-input h-9 w-full min-w-0 rounded-md border bg-transparent px-3 py-1 text-base shadow-xs transition-[border-color,box-shadow] outline-hidden md:text-sm",
706
+ "placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground",
707
+ "file:text-foreground file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium",
708
+ "focus-visible:border-ring focus-visible:ring-ring/30 focus-visible:ring-3",
709
+ "disabled:cursor-not-allowed disabled:opacity-50",
710
+ "aria-invalid:border-destructive aria-invalid:ring-destructive/20 aria-invalid:ring-3",
711
+ "dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
712
+ className
713
+ ),
714
+ ...props
715
+ }
716
+ );
717
+ }
718
+ var DEFAULT_MAX_SIZE = 5 * 1024 * 1024;
719
+ var DEFAULT_ACCEPT = { "image/*": [".png", ".jpg", ".jpeg", ".webp"] };
720
+ function InsertImageDialog({
721
+ open,
722
+ onOpenChange,
723
+ labels,
724
+ url,
725
+ onUrlChange,
726
+ alt,
727
+ onAltChange,
728
+ file,
729
+ onFileChange,
730
+ uploading,
731
+ onApply,
732
+ onCancel,
733
+ removable = false,
734
+ onRemove,
735
+ maxSizeBytes = DEFAULT_MAX_SIZE,
736
+ accept = DEFAULT_ACCEPT
737
+ }) {
738
+ const urlId = useId();
739
+ const altId = useId();
740
+ const canApply = Boolean(file || url.trim()) && Boolean(alt.trim()) && !uploading;
741
+ return /* @__PURE__ */ jsx(Dialog, { open, onOpenChange, children: /* @__PURE__ */ jsxs(DialogContent, { className: "px-4 py-2", "data-slot": "insert-image-dialog", children: [
742
+ /* @__PURE__ */ jsx(DialogHeader, { children: /* @__PURE__ */ jsx(DialogTitle, { children: labels.title }) }),
743
+ /* @__PURE__ */ jsxs("div", { className: "mt-4 flex flex-col gap-3", children: [
744
+ /* @__PURE__ */ jsxs(
745
+ Dropzone,
746
+ {
747
+ accept,
748
+ maxFiles: 1,
749
+ maxSize: maxSizeBytes,
750
+ disabled: uploading,
751
+ src: file ? [file] : [],
752
+ onDrop: (files) => onFileChange(files[0] ?? null),
753
+ children: [
754
+ /* @__PURE__ */ jsx(DropzoneEmptyState, {}),
755
+ /* @__PURE__ */ jsx(DropzoneContent, {})
756
+ ]
757
+ }
758
+ ),
759
+ file && /* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-xs", children: file.name }),
760
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
761
+ /* @__PURE__ */ jsx(Label, { htmlFor: urlId, children: labels.urlLabel }),
762
+ /* @__PURE__ */ jsx(
763
+ Input,
764
+ {
765
+ id: urlId,
766
+ placeholder: labels.urlPlaceholder,
767
+ value: url,
768
+ onChange: (event) => onUrlChange(event.target.value),
769
+ onKeyDown: (event) => {
770
+ if (event.key === "Enter" && canApply) {
771
+ event.preventDefault();
772
+ onApply();
773
+ }
774
+ }
775
+ }
776
+ )
777
+ ] }),
778
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
779
+ /* @__PURE__ */ jsx(Label, { htmlFor: altId, children: labels.altLabel }),
780
+ /* @__PURE__ */ jsx(
781
+ Input,
782
+ {
783
+ id: altId,
784
+ value: alt,
785
+ onChange: (event) => onAltChange(event.target.value)
786
+ }
787
+ )
788
+ ] }),
789
+ /* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-xs", children: labels.hint })
790
+ ] }),
791
+ /* @__PURE__ */ jsxs(DialogFooter, { children: [
792
+ removable && onRemove && /* @__PURE__ */ jsxs(
793
+ Button,
794
+ {
795
+ type: "button",
796
+ variant: "ghost",
797
+ className: "text-destructive",
798
+ onClick: onRemove,
799
+ children: [
800
+ /* @__PURE__ */ jsx(Trash2, { className: "h-4 w-4" }),
801
+ labels.remove
802
+ ]
803
+ }
804
+ ),
805
+ /* @__PURE__ */ jsx(
806
+ Button,
807
+ {
808
+ type: "button",
809
+ variant: "outline",
810
+ onClick: () => {
811
+ if (onCancel) onCancel();
812
+ else onOpenChange(false);
813
+ },
814
+ children: labels.cancel
815
+ }
816
+ ),
817
+ /* @__PURE__ */ jsx(Button, { type: "button", onClick: onApply, disabled: !canApply, children: labels.apply })
818
+ ] })
819
+ ] }) });
820
+ }
821
+ var DEFAULT_MIN_WIDTH = 120;
822
+ var DEFAULT_START_WIDTH = 400;
823
+ var DEFAULT_RATIO = 4 / 3;
824
+ var KEYBOARD_STEP = 20;
825
+ function ResizableImageView({ node, updateAttributes, selected, extension }) {
826
+ const { src, alt, width, height } = node.attrs;
827
+ const sizeRef = useRef({ width, height });
828
+ sizeRef.current = { width, height };
829
+ const { resizeLabel, minWidth = DEFAULT_MIN_WIDTH } = extension.options;
830
+ const dragRef = useRef(null);
831
+ const [dragging, setDragging] = useState(false);
832
+ const resizeTo = useCallback(
833
+ (nextWidth, ratio, maxWidth) => {
834
+ const clamped = Math.max(minWidth, Math.min(Math.round(nextWidth), maxWidth));
835
+ updateAttributes({ width: clamped, height: Math.round(clamped / ratio) });
836
+ },
837
+ [minWidth, updateAttributes]
838
+ );
839
+ const measure = useCallback((handle) => {
840
+ const image = handle.parentElement?.querySelector("img") ?? null;
841
+ const current = sizeRef.current;
842
+ const editable = handle.closest(".ProseMirror");
843
+ const available = editable?.clientWidth ?? 0;
844
+ const naturalWidth = image?.naturalWidth ?? 0;
845
+ const naturalHeight = image?.naturalHeight ?? 0;
846
+ const ratio = current.width > 0 && current.height > 0 ? current.width / current.height : naturalWidth > 0 && naturalHeight > 0 ? naturalWidth / naturalHeight : DEFAULT_RATIO;
847
+ const unbounded = current.width > 0 ? current.width : naturalWidth || DEFAULT_START_WIDTH;
848
+ const startWidth = available > 0 ? Math.min(unbounded, available) : unbounded;
849
+ return {
850
+ width: startWidth,
851
+ ratio,
852
+ // jsdom reports clientWidth as 0, so a zero measure means "no
853
+ // constraint" — the real clamp only applies in a browser.
854
+ maxWidth: available > 0 ? available : Infinity
855
+ };
856
+ }, []);
857
+ const imageRef = useRef(null);
858
+ const fittedSrcRef = useRef(null);
859
+ useEffect(() => {
860
+ const img = imageRef.current;
861
+ if (!img) return;
862
+ if (sizeRef.current.width > 0 || fittedSrcRef.current === src) return;
863
+ const fit = () => {
864
+ const available = img.closest(".ProseMirror")?.clientWidth ?? 0;
865
+ if (!img.naturalWidth || !img.naturalHeight || available <= 0) return false;
866
+ fittedSrcRef.current = src;
867
+ const nextWidth = Math.min(img.naturalWidth, available);
868
+ updateAttributes({
869
+ width: nextWidth,
870
+ height: Math.round(nextWidth / (img.naturalWidth / img.naturalHeight))
871
+ });
872
+ return true;
873
+ };
874
+ if (img.complete && fit()) return;
875
+ const onLoad = () => {
876
+ if (fit()) img.removeEventListener("load", onLoad);
877
+ };
878
+ img.addEventListener("load", onLoad);
879
+ return () => img.removeEventListener("load", onLoad);
880
+ }, [src, updateAttributes]);
881
+ const onPointerDown = useCallback(
882
+ (event) => {
883
+ event.preventDefault();
884
+ event.stopPropagation();
885
+ const start = measure(event.currentTarget);
886
+ dragRef.current = { startX: event.clientX, ...start };
887
+ setDragging(true);
888
+ const onMove = (moveEvent) => {
889
+ const drag = dragRef.current;
890
+ if (!drag) return;
891
+ resizeTo(drag.width + (moveEvent.clientX - drag.startX), drag.ratio, drag.maxWidth);
892
+ };
893
+ const onUp = () => {
894
+ dragRef.current = null;
895
+ setDragging(false);
896
+ window.removeEventListener("pointermove", onMove);
897
+ window.removeEventListener("pointerup", onUp);
898
+ };
899
+ window.addEventListener("pointermove", onMove);
900
+ window.addEventListener("pointerup", onUp);
901
+ },
902
+ [measure, resizeTo]
903
+ );
904
+ const onKeyDown = useCallback(
905
+ (event) => {
906
+ const step = event.key === "ArrowRight" ? KEYBOARD_STEP : event.key === "ArrowLeft" ? -KEYBOARD_STEP : 0;
907
+ if (step === 0) return;
908
+ event.preventDefault();
909
+ const start = measure(event.currentTarget);
910
+ resizeTo(start.width + step, start.ratio, start.maxWidth);
911
+ },
912
+ [measure, resizeTo]
913
+ );
914
+ const sizeStyle = width > 0 ? { width, height: height > 0 ? height : void 0, maxWidth: "100%" } : void 0;
915
+ return /* @__PURE__ */ jsxs(
916
+ NodeViewWrapper,
917
+ {
918
+ "data-slot": "resizable-image",
919
+ className: "group relative inline-block max-w-full",
920
+ children: [
921
+ /* @__PURE__ */ jsx(
922
+ "img",
923
+ {
924
+ ref: imageRef,
925
+ src,
926
+ alt,
927
+ className: "max-w-full rounded-md",
928
+ style: sizeStyle,
929
+ draggable: false
930
+ }
931
+ ),
932
+ /* @__PURE__ */ jsx(
933
+ "div",
934
+ {
935
+ role: "button",
936
+ tabIndex: 0,
937
+ "aria-label": resizeLabel,
938
+ title: resizeLabel,
939
+ "data-slot": "resizable-image-handle",
940
+ onPointerDown,
941
+ onKeyDown,
942
+ className: cn(
943
+ "border-primary absolute -right-1 -bottom-1 h-4 w-4 cursor-se-resize touch-none rounded-full border-2 bg-white transition-opacity",
944
+ "focus-visible:ring-ring/50 focus-visible:opacity-100 focus-visible:ring-2 focus-visible:outline-none",
945
+ dragging || selected ? "opacity-100" : "opacity-0 group-hover:opacity-100"
946
+ )
947
+ }
948
+ )
949
+ ]
950
+ }
951
+ );
952
+ }
953
+ function createResizableImage(options) {
954
+ return Image.extend({
955
+ addOptions() {
956
+ return { ...this.parent(), ...options };
957
+ },
958
+ // The size attributes belong to the extension, not to the consumer: the
959
+ // stock Image extension keeps width/height too, but they default to null
960
+ // and render as HTML attributes, which pins the image to a fixed size.
961
+ //
962
+ // A numeric default with `rendered: false` is what makes resizing work
963
+ // at all — Tiptap v3 gates DOM emission on `rendered`, so the size stays
964
+ // out of the markup and lives only in the document JSON, where a
965
+ // serializer can read it. Without these declarations the schema drops
966
+ // every updateAttributes({ width }) call and the handle does nothing.
967
+ addAttributes() {
968
+ return {
969
+ ...this.parent?.(),
970
+ width: { default: 0, rendered: false },
971
+ height: { default: 0, rendered: false }
972
+ };
973
+ },
974
+ addNodeView() {
975
+ return ReactNodeViewRenderer(ResizableImageView);
976
+ }
977
+ });
978
+ }
979
+ function LinkActions({ editor, labels, onEdit, onOpen }) {
980
+ const { href, canOpen } = useEditorState({
981
+ editor,
982
+ selector: ({ editor: current }) => {
983
+ const value = current.getAttributes("link").href ?? "";
984
+ return { href: value, canOpen: !!value && current.can().setLink({ href: value }) };
985
+ }
986
+ });
987
+ return /* @__PURE__ */ jsxs(
988
+ "div",
989
+ {
990
+ "data-slot": "link-bubble-menu",
991
+ className: "bg-popover text-popover-foreground flex items-center gap-1 rounded-md border p-1 shadow-md",
992
+ children: [
993
+ /* @__PURE__ */ jsx("span", { className: "text-muted-foreground max-w-[16rem] truncate px-2 text-xs", children: href }),
994
+ /* @__PURE__ */ jsx(
995
+ Button,
996
+ {
997
+ type: "button",
998
+ size: "sm",
999
+ variant: "ghost",
1000
+ title: labels.open,
1001
+ "aria-label": labels.open,
1002
+ disabled: !canOpen,
1003
+ onClick: () => {
1004
+ if (!canOpen) return;
1005
+ if (onOpen) {
1006
+ onOpen(href);
1007
+ return;
1008
+ }
1009
+ window.open(href, "_blank", "noopener,noreferrer");
1010
+ },
1011
+ children: /* @__PURE__ */ jsx(ExternalLink, { className: "h-4 w-4" })
1012
+ }
1013
+ ),
1014
+ /* @__PURE__ */ jsx(
1015
+ Button,
1016
+ {
1017
+ type: "button",
1018
+ size: "sm",
1019
+ variant: "ghost",
1020
+ title: labels.edit,
1021
+ "aria-label": labels.edit,
1022
+ onClick: onEdit,
1023
+ children: /* @__PURE__ */ jsx(Pencil, { className: "h-4 w-4" })
1024
+ }
1025
+ ),
1026
+ /* @__PURE__ */ jsx(
1027
+ Button,
1028
+ {
1029
+ type: "button",
1030
+ size: "sm",
1031
+ variant: "ghost",
1032
+ className: "text-destructive",
1033
+ title: labels.remove,
1034
+ "aria-label": labels.remove,
1035
+ onClick: () => editor.chain().focus().extendMarkRange("link").unsetLink().run(),
1036
+ children: /* @__PURE__ */ jsx(Unlink, { className: "h-4 w-4" })
1037
+ }
1038
+ )
1039
+ ]
1040
+ }
1041
+ );
1042
+ }
1043
+ function LinkBubbleMenu({ editor, labels, onEdit, onOpen }) {
1044
+ const shouldShow = useCallback(
1045
+ // Only while the caret is actually in a link — otherwise this bar would
1046
+ // compete with any other bubble menu the consumer adds.
1047
+ ({ editor: current }) => current.isActive("link"),
1048
+ []
1049
+ );
1050
+ const options = useMemo(() => ({ placement: "bottom", offset: 8 }), []);
1051
+ if (!editor) return null;
1052
+ return /* @__PURE__ */ jsx(BubbleMenu, { editor, shouldShow, options, children: /* @__PURE__ */ jsx(LinkActions, { editor, labels, onEdit, onOpen }) });
1053
+ }
1054
+ function ListMenu({ editor, triggerLabel, icon, options }) {
1055
+ const active = options.find((option) => editor?.isActive(option.node));
1056
+ return /* @__PURE__ */ jsxs(DropdownMenu, { children: [
1057
+ /* @__PURE__ */ jsx(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(
1058
+ Button,
1059
+ {
1060
+ type: "button",
1061
+ size: "sm",
1062
+ variant: active ? "secondary" : "ghost",
1063
+ disabled: !editor,
1064
+ title: active ? active.label : triggerLabel,
1065
+ "aria-label": active ? active.label : triggerLabel,
1066
+ "data-slot": "list-menu-trigger",
1067
+ children: [
1068
+ active ? active.icon : icon,
1069
+ /* @__PURE__ */ jsx(ChevronDown, { className: "h-3 w-3 opacity-60" })
1070
+ ]
1071
+ }
1072
+ ) }),
1073
+ /* @__PURE__ */ jsx(DropdownMenuContent, { align: "start", children: options.map((option) => {
1074
+ const isActive = editor?.isActive(option.node) ?? false;
1075
+ return /* @__PURE__ */ jsxs(
1076
+ DropdownMenuItem,
1077
+ {
1078
+ disabled: !editor,
1079
+ className: "cursor-pointer",
1080
+ onSelect: () => editor && option.run(editor),
1081
+ children: [
1082
+ option.icon,
1083
+ /* @__PURE__ */ jsx("span", { className: "flex-1", children: option.label }),
1084
+ /* @__PURE__ */ jsx(
1085
+ Check,
1086
+ {
1087
+ "aria-hidden": "true",
1088
+ className: cn("h-4 w-4", isActive ? "opacity-100" : "opacity-0")
1089
+ }
1090
+ )
1091
+ ]
1092
+ },
1093
+ option.node
1094
+ );
1095
+ }) })
1096
+ ] });
1097
+ }
1098
+ function useRichTextEditor({
1099
+ labels,
1100
+ onUpdate,
1101
+ content,
1102
+ onInsertLink,
1103
+ onInsertImage,
1104
+ extraExtensions = [],
1105
+ extraToolbar = [],
1106
+ replaceToolbar,
1107
+ contentClassName,
1108
+ allowBase64Images = false,
1109
+ editorProps
1110
+ }) {
1111
+ const [linkNeedsText, setLinkNeedsText] = useState(false);
1112
+ const editor = useEditor({
1113
+ extensions: [
1114
+ // openOnClick false: a plain click has to place the caret so the
1115
+ // author can edit link text. Left at its default, Tiptap navigates
1116
+ // the current tab and throws the author out of the editor.
1117
+ StarterKit.configure({ link: { openOnClick: false } }),
1118
+ Highlight,
1119
+ TableKit.configure({ table: { resizable: true } }),
1120
+ createResizableImage({ resizeLabel: labels.resizeImage }).configure({
1121
+ inline: false,
1122
+ allowBase64: allowBase64Images
1123
+ }),
1124
+ ...extraExtensions
1125
+ ],
1126
+ content,
1127
+ immediatelyRender: false,
1128
+ // Required for the toolbar's pressed state and the image node view to
1129
+ // repaint as the selection and attributes change.
1130
+ shouldRerenderOnTransaction: true,
1131
+ editorProps: {
1132
+ ...editorProps,
1133
+ attributes: {
1134
+ // ProseMirror's editable div needs its own accessible name: a
1135
+ // visible <Label> beside it is decoration, and a screen reader
1136
+ // would announce an unnamed box.
1137
+ role: "textbox",
1138
+ "aria-label": labels.editor,
1139
+ class: contentClassName ? `${EDITOR_CONTENT_STYLE} ${contentClassName}` : EDITOR_CONTENT_STYLE
1140
+ },
1141
+ // Ctrl/Cmd-click opens the link in a new tab and leaves the author
1142
+ // where they were: the shortcut is a deliberate gesture, and keeping
1143
+ // the editor open means no unsaved work is at risk. A plain click
1144
+ // falls through to ProseMirror and just moves the caret.
1145
+ handleClick(_view, _pos, event) {
1146
+ if (!event.ctrlKey && !event.metaKey) return false;
1147
+ const href = event.target?.closest("a")?.getAttribute("href");
1148
+ if (!href) return false;
1149
+ event.preventDefault();
1150
+ window.open(href, "_blank", "noopener,noreferrer");
1151
+ return true;
1152
+ }
1153
+ },
1154
+ onUpdate: onUpdate ? ({ editor: current }) => onUpdate(current.getJSON()) : void 0
1155
+ });
1156
+ const currentLinkHref = useCallback(
1157
+ () => editor?.getAttributes("link").href ?? "",
1158
+ [editor]
1159
+ );
1160
+ const openLink = useCallback(() => {
1161
+ if (editor) {
1162
+ const { from, to } = editor.state.selection;
1163
+ setLinkNeedsText(from === to && !editor.isActive("link"));
1164
+ }
1165
+ onInsertLink?.();
1166
+ }, [editor, onInsertLink]);
1167
+ const applyLink = useCallback(
1168
+ (href, text) => {
1169
+ if (!editor) return;
1170
+ const url = href.trim();
1171
+ if (url && !editor.can().setLink({ href: url })) return;
1172
+ if (linkNeedsText) {
1173
+ if (url && text?.trim()) {
1174
+ editor.chain().focus().insertContent({
1175
+ type: "text",
1176
+ text: text.trim(),
1177
+ marks: [{ type: "link", attrs: { href: url } }]
1178
+ }).run();
1179
+ }
1180
+ return;
1181
+ }
1182
+ if (url) editor.chain().focus().extendMarkRange("link").setLink({ href: url }).run();
1183
+ else editor.chain().focus().extendMarkRange("link").unsetLink().run();
1184
+ },
1185
+ [editor, linkNeedsText]
1186
+ );
1187
+ const toolbar = useMemo(() => {
1188
+ const builtIn = [
1189
+ {
1190
+ kind: "button",
1191
+ id: "bold",
1192
+ icon: /* @__PURE__ */ jsx(Bold, { className: "h-4 w-4" }),
1193
+ label: labels.bold,
1194
+ active: { name: "bold" },
1195
+ run: (e) => e.chain().focus().toggleBold().run()
1196
+ },
1197
+ {
1198
+ kind: "button",
1199
+ id: "italic",
1200
+ icon: /* @__PURE__ */ jsx(Italic, { className: "h-4 w-4" }),
1201
+ label: labels.italic,
1202
+ active: { name: "italic" },
1203
+ run: (e) => e.chain().focus().toggleItalic().run()
1204
+ },
1205
+ {
1206
+ kind: "button",
1207
+ id: "heading",
1208
+ icon: /* @__PURE__ */ jsx(Heading2, { className: "h-4 w-4" }),
1209
+ label: labels.heading,
1210
+ active: { name: "heading", attrs: { level: 2 } },
1211
+ run: (e) => e.chain().focus().toggleHeading({ level: 2 }).run()
1212
+ },
1213
+ { kind: "separator" },
1214
+ {
1215
+ kind: "button",
1216
+ id: "highlight",
1217
+ icon: /* @__PURE__ */ jsx(Highlighter, { className: "h-4 w-4" }),
1218
+ label: labels.highlight,
1219
+ active: { name: "highlight" },
1220
+ run: (e) => e.chain().focus().toggleHighlight().run()
1221
+ },
1222
+ {
1223
+ kind: "button",
1224
+ id: "underline",
1225
+ icon: /* @__PURE__ */ jsx(Underline, { className: "h-4 w-4" }),
1226
+ label: labels.underline,
1227
+ active: { name: "underline" },
1228
+ run: (e) => e.chain().focus().toggleMark("underline").run()
1229
+ },
1230
+ {
1231
+ kind: "button",
1232
+ id: "strike",
1233
+ icon: /* @__PURE__ */ jsx(Strikethrough, { className: "h-4 w-4" }),
1234
+ label: labels.strikethrough,
1235
+ active: { name: "strike" },
1236
+ run: (e) => e.chain().focus().toggleMark("strike").run()
1237
+ },
1238
+ { kind: "separator" },
1239
+ {
1240
+ kind: "button",
1241
+ id: "quote",
1242
+ icon: /* @__PURE__ */ jsx(Quote, { className: "h-4 w-4" }),
1243
+ label: labels.quote,
1244
+ active: { name: "blockquote" },
1245
+ run: (e) => e.chain().focus().toggleBlockquote().run()
1246
+ },
1247
+ {
1248
+ kind: "custom",
1249
+ id: "lists",
1250
+ render: (current) => /* @__PURE__ */ jsx(
1251
+ ListMenu,
1252
+ {
1253
+ editor: current,
1254
+ triggerLabel: labels.lists.trigger,
1255
+ icon: /* @__PURE__ */ jsx(List, { className: "h-4 w-4" }),
1256
+ options: [
1257
+ {
1258
+ node: "bulletList",
1259
+ label: labels.lists.bullet,
1260
+ icon: /* @__PURE__ */ jsx(List, { className: "h-4 w-4" }),
1261
+ run: (e) => e.chain().focus().toggleBulletList().run()
1262
+ },
1263
+ {
1264
+ node: "orderedList",
1265
+ label: labels.lists.ordered,
1266
+ icon: /* @__PURE__ */ jsx(ListOrdered, { className: "h-4 w-4" }),
1267
+ run: (e) => e.chain().focus().toggleOrderedList().run()
1268
+ }
1269
+ ]
1270
+ }
1271
+ )
1272
+ },
1273
+ { kind: "separator" },
1274
+ {
1275
+ kind: "button",
1276
+ id: "link",
1277
+ icon: /* @__PURE__ */ jsx(Link, { className: "h-4 w-4" }),
1278
+ label: labels.insertLink,
1279
+ active: { name: "link" },
1280
+ run: () => openLink()
1281
+ },
1282
+ {
1283
+ kind: "button",
1284
+ id: "image",
1285
+ icon: /* @__PURE__ */ jsx(ImagePlus, { className: "h-4 w-4" }),
1286
+ label: labels.insertImage,
1287
+ // No `active`: this opens a dialog, it is never a pressed state.
1288
+ run: () => onInsertImage?.()
1289
+ },
1290
+ {
1291
+ kind: "custom",
1292
+ id: "table",
1293
+ render: (current) => /* @__PURE__ */ jsx(
1294
+ TableMenu,
1295
+ {
1296
+ editor: current,
1297
+ icon: /* @__PURE__ */ jsx(Table, { className: "h-4 w-4" }),
1298
+ labels: labels.table
1299
+ }
1300
+ )
1301
+ }
1302
+ ];
1303
+ let items = replaceToolbar ? builtIn.map(
1304
+ (item) => item.kind !== "separator" && replaceToolbar[item.id] ? replaceToolbar[item.id] : item
1305
+ ) : builtIn;
1306
+ const appended = [];
1307
+ for (const group of extraToolbar) {
1308
+ if (!group.after) {
1309
+ appended.push(...group.items);
1310
+ continue;
1311
+ }
1312
+ const at = items.findIndex(
1313
+ (item) => item.kind !== "separator" && item.id === group.after
1314
+ );
1315
+ if (at === -1) {
1316
+ appended.push(...group.items);
1317
+ continue;
1318
+ }
1319
+ items = [...items.slice(0, at + 1), ...group.items, ...items.slice(at + 1)];
1320
+ }
1321
+ return [...items, ...appended];
1322
+ }, [labels, extraToolbar, replaceToolbar, onInsertImage, openLink]);
1323
+ const linkMenu = /* @__PURE__ */ jsx(LinkBubbleMenu, { editor, labels: labels.link, onEdit: openLink });
1324
+ return { editor, toolbar, linkMenu, linkNeedsText, currentLinkHref, applyLink };
1325
+ }
1326
+ function ColorMenu({ editor, labels, icon, swatches }) {
1327
+ const active = swatches.find((swatch) => editor?.isActive(swatch.mark));
1328
+ function apply(mark) {
1329
+ if (!editor) return;
1330
+ const chain = editor.chain().focus();
1331
+ for (const swatch of swatches) chain.unsetMark(swatch.mark);
1332
+ if (mark) chain.setMark(mark);
1333
+ chain.run();
1334
+ }
1335
+ return /* @__PURE__ */ jsxs(DropdownMenu, { children: [
1336
+ /* @__PURE__ */ jsx(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsx(
1337
+ Button,
1338
+ {
1339
+ type: "button",
1340
+ size: "sm",
1341
+ variant: active ? "secondary" : "ghost",
1342
+ disabled: !editor,
1343
+ title: labels.trigger,
1344
+ "aria-label": labels.trigger,
1345
+ "data-slot": "color-menu-trigger",
1346
+ children: icon
1347
+ }
1348
+ ) }),
1349
+ /* @__PURE__ */ jsx(DropdownMenuContent, { align: "start", className: "w-auto", children: /* @__PURE__ */ jsxs(
1350
+ "div",
1351
+ {
1352
+ role: "group",
1353
+ "aria-label": labels.heading,
1354
+ className: "flex items-center gap-1.5 px-1.5 py-1",
1355
+ children: [
1356
+ swatches.map((swatch) => /* @__PURE__ */ jsx(
1357
+ "button",
1358
+ {
1359
+ type: "button",
1360
+ "aria-label": swatch.label,
1361
+ "aria-pressed": editor?.isActive(swatch.mark) ?? false,
1362
+ title: swatch.label,
1363
+ style: { backgroundColor: swatch.preview },
1364
+ className: cn(
1365
+ "h-7 w-7 cursor-pointer rounded-full border transition-transform",
1366
+ "focus-visible:ring-ring/50 focus-visible:ring-2 focus-visible:outline-none",
1367
+ editor?.isActive(swatch.mark) ? "border-primary scale-110 border-2" : "border-border/60 hover:scale-110"
1368
+ ),
1369
+ onClick: () => apply(swatch.mark)
1370
+ },
1371
+ swatch.mark
1372
+ )),
1373
+ /* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: "bg-border mx-0.5 h-5 w-px" }),
1374
+ /* @__PURE__ */ jsx(
1375
+ "button",
1376
+ {
1377
+ type: "button",
1378
+ "aria-label": labels.clear,
1379
+ title: labels.clear,
1380
+ disabled: !active,
1381
+ className: cn(
1382
+ "text-muted-foreground flex h-7 w-7 cursor-pointer items-center justify-center rounded-full",
1383
+ "focus-visible:ring-ring/50 focus-visible:ring-2 focus-visible:outline-none",
1384
+ "hover:text-foreground disabled:cursor-default disabled:opacity-40"
1385
+ ),
1386
+ onClick: () => apply(""),
1387
+ children: /* @__PURE__ */ jsx(Ban, { className: "h-4 w-4" })
1388
+ }
1389
+ )
1390
+ ]
1391
+ }
1392
+ ) })
1393
+ ] });
1394
+ }
1395
+ function InsertLinkDialog({
1396
+ open,
1397
+ onOpenChange,
1398
+ labels,
1399
+ url,
1400
+ onUrlChange,
1401
+ text = "",
1402
+ onTextChange,
1403
+ needsText = false,
1404
+ onApply,
1405
+ onCancel
1406
+ }) {
1407
+ const urlId = useId();
1408
+ const textId = useId();
1409
+ const canApply = needsText ? Boolean(url.trim()) && Boolean(text.trim()) : true;
1410
+ function submitOnEnter(event) {
1411
+ if (event.key === "Enter" && canApply) {
1412
+ event.preventDefault();
1413
+ onApply();
1414
+ }
1415
+ }
1416
+ return /* @__PURE__ */ jsx(Dialog, { open, onOpenChange, children: /* @__PURE__ */ jsxs(DialogContent, { className: "px-4 py-2", "data-slot": "insert-link-dialog", children: [
1417
+ /* @__PURE__ */ jsx(DialogHeader, { children: /* @__PURE__ */ jsx(DialogTitle, { children: labels.title }) }),
1418
+ /* @__PURE__ */ jsxs("div", { className: "mt-4 flex flex-col gap-3", children: [
1419
+ needsText && onTextChange && /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
1420
+ /* @__PURE__ */ jsx(Label, { htmlFor: textId, children: labels.textLabel }),
1421
+ /* @__PURE__ */ jsx(
1422
+ Input,
1423
+ {
1424
+ id: textId,
1425
+ placeholder: labels.textPlaceholder,
1426
+ value: text,
1427
+ onChange: (event) => onTextChange(event.target.value),
1428
+ onKeyDown: submitOnEnter
1429
+ }
1430
+ )
1431
+ ] }),
1432
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
1433
+ /* @__PURE__ */ jsx(Label, { htmlFor: urlId, children: labels.urlLabel }),
1434
+ /* @__PURE__ */ jsx(
1435
+ Input,
1436
+ {
1437
+ id: urlId,
1438
+ placeholder: labels.urlPlaceholder,
1439
+ value: url,
1440
+ onChange: (event) => onUrlChange(event.target.value),
1441
+ onKeyDown: submitOnEnter
1442
+ }
1443
+ ),
1444
+ !needsText && /* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-xs", children: labels.hint })
1445
+ ] })
1446
+ ] }),
1447
+ /* @__PURE__ */ jsxs(DialogFooter, { children: [
1448
+ /* @__PURE__ */ jsx(
1449
+ Button,
1450
+ {
1451
+ type: "button",
1452
+ variant: "outline",
1453
+ onClick: () => {
1454
+ if (onCancel) onCancel();
1455
+ else onOpenChange(false);
1456
+ },
1457
+ children: labels.cancel
1458
+ }
1459
+ ),
1460
+ /* @__PURE__ */ jsx(Button, { type: "button", onClick: onApply, disabled: !canApply, children: labels.apply })
1461
+ ] })
1462
+ ] }) });
1463
+ }
1464
+
1465
+ export { ColorMenu, EDITOR_CONTENT_STYLE, InsertImageDialog, InsertLinkDialog, LinkBubbleMenu, ListMenu, RichTextEditor, RichTextToolbar, TableMenu, createResizableImage, useRichTextEditor };
1466
+ //# sourceMappingURL=editor.js.map
1467
+ //# sourceMappingURL=editor.js.map