@bizapps/design-system-testing 1.0.11 → 1.7.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,1455 @@
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 flex h-9 w-full min-w-0 rounded-md border bg-transparent px-3 py-1 text-sm shadow-xs transition-[color,box-shadow] outline-none",
706
+ "placeholder:text-muted-foreground",
707
+ "focus-visible:border-ring focus-visible:ring-ring/30 focus-visible:ring-3",
708
+ "disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50",
709
+ "aria-invalid:border-destructive aria-invalid:ring-destructive/20 aria-invalid:ring-3",
710
+ "dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
711
+ className
712
+ ),
713
+ ...props
714
+ }
715
+ );
716
+ }
717
+ var DEFAULT_MAX_SIZE = 5 * 1024 * 1024;
718
+ var DEFAULT_ACCEPT = { "image/*": [".png", ".jpg", ".jpeg", ".webp"] };
719
+ function InsertImageDialog({
720
+ open,
721
+ onOpenChange,
722
+ labels,
723
+ url,
724
+ onUrlChange,
725
+ alt,
726
+ onAltChange,
727
+ file,
728
+ onFileChange,
729
+ uploading,
730
+ onApply,
731
+ onCancel,
732
+ removable = false,
733
+ onRemove,
734
+ maxSizeBytes = DEFAULT_MAX_SIZE,
735
+ accept = DEFAULT_ACCEPT
736
+ }) {
737
+ const urlId = useId();
738
+ const altId = useId();
739
+ const canApply = Boolean(file || url.trim()) && Boolean(alt.trim()) && !uploading;
740
+ return /* @__PURE__ */ jsx(Dialog, { open, onOpenChange, children: /* @__PURE__ */ jsxs(DialogContent, { className: "px-4 py-2", "data-slot": "insert-image-dialog", children: [
741
+ /* @__PURE__ */ jsx(DialogHeader, { children: /* @__PURE__ */ jsx(DialogTitle, { children: labels.title }) }),
742
+ /* @__PURE__ */ jsxs("div", { className: "mt-4 flex flex-col gap-3", children: [
743
+ /* @__PURE__ */ jsxs(
744
+ Dropzone,
745
+ {
746
+ accept,
747
+ maxFiles: 1,
748
+ maxSize: maxSizeBytes,
749
+ disabled: uploading,
750
+ src: file ? [file] : [],
751
+ onDrop: (files) => onFileChange(files[0] ?? null),
752
+ children: [
753
+ /* @__PURE__ */ jsx(DropzoneEmptyState, {}),
754
+ /* @__PURE__ */ jsx(DropzoneContent, {})
755
+ ]
756
+ }
757
+ ),
758
+ file && /* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-xs", children: file.name }),
759
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
760
+ /* @__PURE__ */ jsx(Label, { htmlFor: urlId, children: labels.urlLabel }),
761
+ /* @__PURE__ */ jsx(
762
+ Input,
763
+ {
764
+ id: urlId,
765
+ placeholder: labels.urlPlaceholder,
766
+ value: url,
767
+ onChange: (event) => onUrlChange(event.target.value),
768
+ onKeyDown: (event) => {
769
+ if (event.key === "Enter" && canApply) {
770
+ event.preventDefault();
771
+ onApply();
772
+ }
773
+ }
774
+ }
775
+ )
776
+ ] }),
777
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
778
+ /* @__PURE__ */ jsx(Label, { htmlFor: altId, children: labels.altLabel }),
779
+ /* @__PURE__ */ jsx(
780
+ Input,
781
+ {
782
+ id: altId,
783
+ value: alt,
784
+ onChange: (event) => onAltChange(event.target.value)
785
+ }
786
+ )
787
+ ] }),
788
+ /* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-xs", children: labels.hint })
789
+ ] }),
790
+ /* @__PURE__ */ jsxs(DialogFooter, { children: [
791
+ removable && onRemove && /* @__PURE__ */ jsxs(
792
+ Button,
793
+ {
794
+ type: "button",
795
+ variant: "ghost",
796
+ className: "text-destructive",
797
+ onClick: onRemove,
798
+ children: [
799
+ /* @__PURE__ */ jsx(Trash2, { className: "h-4 w-4" }),
800
+ labels.remove
801
+ ]
802
+ }
803
+ ),
804
+ /* @__PURE__ */ jsx(
805
+ Button,
806
+ {
807
+ type: "button",
808
+ variant: "outline",
809
+ onClick: () => {
810
+ if (onCancel) onCancel();
811
+ else onOpenChange(false);
812
+ },
813
+ children: labels.cancel
814
+ }
815
+ ),
816
+ /* @__PURE__ */ jsx(Button, { type: "button", onClick: onApply, disabled: !canApply, children: labels.apply })
817
+ ] })
818
+ ] }) });
819
+ }
820
+ var DEFAULT_MIN_WIDTH = 120;
821
+ var DEFAULT_START_WIDTH = 400;
822
+ var DEFAULT_RATIO = 4 / 3;
823
+ var KEYBOARD_STEP = 20;
824
+ function ResizableImageView({ node, updateAttributes, selected, extension }) {
825
+ const { src, alt, width, height } = node.attrs;
826
+ const sizeRef = useRef({ width, height });
827
+ sizeRef.current = { width, height };
828
+ const { resizeLabel, minWidth = DEFAULT_MIN_WIDTH } = extension.options;
829
+ const dragRef = useRef(null);
830
+ const [dragging, setDragging] = useState(false);
831
+ const resizeTo = useCallback(
832
+ (nextWidth, ratio, maxWidth) => {
833
+ const clamped = Math.max(minWidth, Math.min(Math.round(nextWidth), maxWidth));
834
+ updateAttributes({ width: clamped, height: Math.round(clamped / ratio) });
835
+ },
836
+ [minWidth, updateAttributes]
837
+ );
838
+ const measure = useCallback((handle) => {
839
+ const image = handle.parentElement?.querySelector("img") ?? null;
840
+ const current = sizeRef.current;
841
+ const editable = handle.closest(".ProseMirror");
842
+ const available = editable?.clientWidth ?? 0;
843
+ const naturalWidth = image?.naturalWidth ?? 0;
844
+ const naturalHeight = image?.naturalHeight ?? 0;
845
+ const ratio = current.width > 0 && current.height > 0 ? current.width / current.height : naturalWidth > 0 && naturalHeight > 0 ? naturalWidth / naturalHeight : DEFAULT_RATIO;
846
+ const unbounded = current.width > 0 ? current.width : naturalWidth || DEFAULT_START_WIDTH;
847
+ const startWidth = available > 0 ? Math.min(unbounded, available) : unbounded;
848
+ return {
849
+ width: startWidth,
850
+ ratio,
851
+ // jsdom reports clientWidth as 0, so a zero measure means "no
852
+ // constraint" — the real clamp only applies in a browser.
853
+ maxWidth: available > 0 ? available : Infinity
854
+ };
855
+ }, []);
856
+ const imageRef = useRef(null);
857
+ const fittedSrcRef = useRef(null);
858
+ useEffect(() => {
859
+ const img = imageRef.current;
860
+ if (!img) return;
861
+ if (sizeRef.current.width > 0 || fittedSrcRef.current === src) return;
862
+ const fit = () => {
863
+ const available = img.closest(".ProseMirror")?.clientWidth ?? 0;
864
+ if (!img.naturalWidth || !img.naturalHeight || available <= 0) return;
865
+ fittedSrcRef.current = src;
866
+ const nextWidth = Math.min(img.naturalWidth, available);
867
+ updateAttributes({
868
+ width: nextWidth,
869
+ height: Math.round(nextWidth / (img.naturalWidth / img.naturalHeight))
870
+ });
871
+ };
872
+ if (img.complete) {
873
+ fit();
874
+ return;
875
+ }
876
+ img.addEventListener("load", fit, { once: true });
877
+ return () => img.removeEventListener("load", fit);
878
+ }, [src, updateAttributes]);
879
+ const onPointerDown = useCallback(
880
+ (event) => {
881
+ event.preventDefault();
882
+ event.stopPropagation();
883
+ const start = measure(event.currentTarget);
884
+ dragRef.current = { startX: event.clientX, ...start };
885
+ setDragging(true);
886
+ const onMove = (moveEvent) => {
887
+ const drag = dragRef.current;
888
+ if (!drag) return;
889
+ resizeTo(drag.width + (moveEvent.clientX - drag.startX), drag.ratio, drag.maxWidth);
890
+ };
891
+ const onUp = () => {
892
+ dragRef.current = null;
893
+ setDragging(false);
894
+ window.removeEventListener("pointermove", onMove);
895
+ window.removeEventListener("pointerup", onUp);
896
+ };
897
+ window.addEventListener("pointermove", onMove);
898
+ window.addEventListener("pointerup", onUp);
899
+ },
900
+ [measure, resizeTo]
901
+ );
902
+ const onKeyDown = useCallback(
903
+ (event) => {
904
+ const step = event.key === "ArrowRight" ? KEYBOARD_STEP : event.key === "ArrowLeft" ? -KEYBOARD_STEP : 0;
905
+ if (step === 0) return;
906
+ event.preventDefault();
907
+ const start = measure(event.currentTarget);
908
+ resizeTo(start.width + step, start.ratio, start.maxWidth);
909
+ },
910
+ [measure, resizeTo]
911
+ );
912
+ const sizeStyle = width > 0 ? { width, height: height > 0 ? height : void 0, maxWidth: "100%" } : void 0;
913
+ return /* @__PURE__ */ jsxs(
914
+ NodeViewWrapper,
915
+ {
916
+ "data-slot": "resizable-image",
917
+ className: "group relative inline-block max-w-full",
918
+ children: [
919
+ /* @__PURE__ */ jsx(
920
+ "img",
921
+ {
922
+ ref: imageRef,
923
+ src,
924
+ alt,
925
+ className: "max-w-full rounded-md",
926
+ style: sizeStyle,
927
+ draggable: false
928
+ }
929
+ ),
930
+ /* @__PURE__ */ jsx(
931
+ "div",
932
+ {
933
+ role: "button",
934
+ tabIndex: 0,
935
+ "aria-label": resizeLabel,
936
+ title: resizeLabel,
937
+ "data-slot": "resizable-image-handle",
938
+ onPointerDown,
939
+ onKeyDown,
940
+ className: cn(
941
+ "border-primary absolute -right-1 -bottom-1 h-4 w-4 cursor-se-resize touch-none rounded-full border-2 bg-white transition-opacity",
942
+ "focus-visible:ring-ring/50 focus-visible:opacity-100 focus-visible:ring-2 focus-visible:outline-none",
943
+ dragging || selected ? "opacity-100" : "opacity-0 group-hover:opacity-100"
944
+ )
945
+ }
946
+ )
947
+ ]
948
+ }
949
+ );
950
+ }
951
+ function createResizableImage(options) {
952
+ return Image.extend({
953
+ addOptions() {
954
+ return { ...this.parent(), ...options };
955
+ },
956
+ // The size attributes belong to the extension, not to the consumer: the
957
+ // stock Image extension keeps width/height too, but they default to null
958
+ // and render as HTML attributes, which pins the image to a fixed size.
959
+ //
960
+ // A numeric default with `rendered: false` is what makes resizing work
961
+ // at all — Tiptap v3 gates DOM emission on `rendered`, so the size stays
962
+ // out of the markup and lives only in the document JSON, where a
963
+ // serializer can read it. Without these declarations the schema drops
964
+ // every updateAttributes({ width }) call and the handle does nothing.
965
+ addAttributes() {
966
+ return {
967
+ ...this.parent?.(),
968
+ width: { default: 0, rendered: false },
969
+ height: { default: 0, rendered: false }
970
+ };
971
+ },
972
+ addNodeView() {
973
+ return ReactNodeViewRenderer(ResizableImageView);
974
+ }
975
+ });
976
+ }
977
+ function ListMenu({ editor, triggerLabel, icon, options }) {
978
+ const active = options.find((option) => editor?.isActive(option.node));
979
+ return /* @__PURE__ */ jsxs(DropdownMenu, { children: [
980
+ /* @__PURE__ */ jsx(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(
981
+ Button,
982
+ {
983
+ type: "button",
984
+ size: "sm",
985
+ variant: active ? "secondary" : "ghost",
986
+ disabled: !editor,
987
+ title: active ? active.label : triggerLabel,
988
+ "aria-label": active ? active.label : triggerLabel,
989
+ "data-slot": "list-menu-trigger",
990
+ children: [
991
+ active ? active.icon : icon,
992
+ /* @__PURE__ */ jsx(ChevronDown, { className: "h-3 w-3 opacity-60" })
993
+ ]
994
+ }
995
+ ) }),
996
+ /* @__PURE__ */ jsx(DropdownMenuContent, { align: "start", children: options.map((option) => {
997
+ const isActive = editor?.isActive(option.node) ?? false;
998
+ return /* @__PURE__ */ jsxs(
999
+ DropdownMenuItem,
1000
+ {
1001
+ disabled: !editor,
1002
+ className: "cursor-pointer",
1003
+ onSelect: () => editor && option.run(editor),
1004
+ children: [
1005
+ option.icon,
1006
+ /* @__PURE__ */ jsx("span", { className: "flex-1", children: option.label }),
1007
+ /* @__PURE__ */ jsx(
1008
+ Check,
1009
+ {
1010
+ "aria-hidden": "true",
1011
+ className: cn("h-4 w-4", isActive ? "opacity-100" : "opacity-0")
1012
+ }
1013
+ )
1014
+ ]
1015
+ },
1016
+ option.node
1017
+ );
1018
+ }) })
1019
+ ] });
1020
+ }
1021
+ function useRichTextEditor({
1022
+ labels,
1023
+ onUpdate,
1024
+ content,
1025
+ onInsertLink,
1026
+ onInsertImage,
1027
+ extraExtensions = [],
1028
+ extraToolbar = [],
1029
+ contentClassName,
1030
+ allowBase64Images = false
1031
+ }) {
1032
+ const [linkNeedsText, setLinkNeedsText] = useState(false);
1033
+ const editor = useEditor({
1034
+ extensions: [
1035
+ // openOnClick false: a plain click has to place the caret so the
1036
+ // author can edit link text. Left at its default, Tiptap navigates
1037
+ // the current tab and throws the author out of the editor.
1038
+ StarterKit.configure({ link: { openOnClick: false } }),
1039
+ Highlight,
1040
+ TableKit.configure({ table: { resizable: true } }),
1041
+ createResizableImage({ resizeLabel: labels.resizeImage }).configure({
1042
+ inline: false,
1043
+ allowBase64: allowBase64Images
1044
+ }),
1045
+ ...extraExtensions
1046
+ ],
1047
+ content,
1048
+ immediatelyRender: false,
1049
+ // Required for the toolbar's pressed state and the image node view to
1050
+ // repaint as the selection and attributes change.
1051
+ shouldRerenderOnTransaction: true,
1052
+ editorProps: {
1053
+ attributes: {
1054
+ // ProseMirror's editable div needs its own accessible name: a
1055
+ // visible <Label> beside it is decoration, and a screen reader
1056
+ // would announce an unnamed box.
1057
+ role: "textbox",
1058
+ "aria-label": labels.editor,
1059
+ class: contentClassName ? `${EDITOR_CONTENT_STYLE} ${contentClassName}` : EDITOR_CONTENT_STYLE
1060
+ },
1061
+ // Ctrl/Cmd-click opens the link in a new tab and leaves the author
1062
+ // where they were: the shortcut is a deliberate gesture, and keeping
1063
+ // the editor open means no unsaved work is at risk. A plain click
1064
+ // falls through to ProseMirror and just moves the caret.
1065
+ handleClick(_view, _pos, event) {
1066
+ if (!event.ctrlKey && !event.metaKey) return false;
1067
+ const href = event.target?.closest("a")?.getAttribute("href");
1068
+ if (!href) return false;
1069
+ event.preventDefault();
1070
+ window.open(href, "_blank", "noopener,noreferrer");
1071
+ return true;
1072
+ }
1073
+ },
1074
+ onUpdate: onUpdate ? ({ editor: current }) => onUpdate(current.getJSON()) : void 0
1075
+ });
1076
+ const currentLinkHref = useCallback(
1077
+ () => editor?.getAttributes("link").href ?? "",
1078
+ [editor]
1079
+ );
1080
+ const openLink = useCallback(() => {
1081
+ if (editor) {
1082
+ const { from, to } = editor.state.selection;
1083
+ setLinkNeedsText(from === to && !editor.isActive("link"));
1084
+ }
1085
+ onInsertLink?.();
1086
+ }, [editor, onInsertLink]);
1087
+ const applyLink = useCallback(
1088
+ (href, text) => {
1089
+ if (!editor) return;
1090
+ const url = href.trim();
1091
+ if (linkNeedsText) {
1092
+ if (url && text?.trim()) {
1093
+ editor.chain().focus().insertContent({
1094
+ type: "text",
1095
+ text: text.trim(),
1096
+ marks: [{ type: "link", attrs: { href: url } }]
1097
+ }).run();
1098
+ }
1099
+ return;
1100
+ }
1101
+ if (url) editor.chain().focus().extendMarkRange("link").setLink({ href: url }).run();
1102
+ else editor.chain().focus().extendMarkRange("link").unsetLink().run();
1103
+ },
1104
+ [editor, linkNeedsText]
1105
+ );
1106
+ const toolbar = useMemo(() => {
1107
+ const builtIn = [
1108
+ {
1109
+ kind: "button",
1110
+ id: "bold",
1111
+ icon: /* @__PURE__ */ jsx(Bold, { className: "h-4 w-4" }),
1112
+ label: labels.bold,
1113
+ active: { name: "bold" },
1114
+ run: (e) => e.chain().focus().toggleBold().run()
1115
+ },
1116
+ {
1117
+ kind: "button",
1118
+ id: "italic",
1119
+ icon: /* @__PURE__ */ jsx(Italic, { className: "h-4 w-4" }),
1120
+ label: labels.italic,
1121
+ active: { name: "italic" },
1122
+ run: (e) => e.chain().focus().toggleItalic().run()
1123
+ },
1124
+ {
1125
+ kind: "button",
1126
+ id: "heading",
1127
+ icon: /* @__PURE__ */ jsx(Heading2, { className: "h-4 w-4" }),
1128
+ label: labels.heading,
1129
+ active: { name: "heading", attrs: { level: 2 } },
1130
+ run: (e) => e.chain().focus().toggleHeading({ level: 2 }).run()
1131
+ },
1132
+ { kind: "separator" },
1133
+ {
1134
+ kind: "button",
1135
+ id: "highlight",
1136
+ icon: /* @__PURE__ */ jsx(Highlighter, { className: "h-4 w-4" }),
1137
+ label: labels.highlight,
1138
+ active: { name: "highlight" },
1139
+ run: (e) => e.chain().focus().toggleHighlight().run()
1140
+ },
1141
+ {
1142
+ kind: "button",
1143
+ id: "underline",
1144
+ icon: /* @__PURE__ */ jsx(Underline, { className: "h-4 w-4" }),
1145
+ label: labels.underline,
1146
+ active: { name: "underline" },
1147
+ run: (e) => e.chain().focus().toggleMark("underline").run()
1148
+ },
1149
+ {
1150
+ kind: "button",
1151
+ id: "strike",
1152
+ icon: /* @__PURE__ */ jsx(Strikethrough, { className: "h-4 w-4" }),
1153
+ label: labels.strikethrough,
1154
+ active: { name: "strike" },
1155
+ run: (e) => e.chain().focus().toggleMark("strike").run()
1156
+ },
1157
+ { kind: "separator" },
1158
+ {
1159
+ kind: "button",
1160
+ id: "quote",
1161
+ icon: /* @__PURE__ */ jsx(Quote, { className: "h-4 w-4" }),
1162
+ label: labels.quote,
1163
+ active: { name: "blockquote" },
1164
+ run: (e) => e.chain().focus().toggleBlockquote().run()
1165
+ },
1166
+ {
1167
+ kind: "custom",
1168
+ id: "lists",
1169
+ render: (current) => /* @__PURE__ */ jsx(
1170
+ ListMenu,
1171
+ {
1172
+ editor: current,
1173
+ triggerLabel: labels.lists.trigger,
1174
+ icon: /* @__PURE__ */ jsx(List, { className: "h-4 w-4" }),
1175
+ options: [
1176
+ {
1177
+ node: "bulletList",
1178
+ label: labels.lists.bullet,
1179
+ icon: /* @__PURE__ */ jsx(List, { className: "h-4 w-4" }),
1180
+ run: (e) => e.chain().focus().toggleBulletList().run()
1181
+ },
1182
+ {
1183
+ node: "orderedList",
1184
+ label: labels.lists.ordered,
1185
+ icon: /* @__PURE__ */ jsx(ListOrdered, { className: "h-4 w-4" }),
1186
+ run: (e) => e.chain().focus().toggleOrderedList().run()
1187
+ }
1188
+ ]
1189
+ }
1190
+ )
1191
+ },
1192
+ { kind: "separator" },
1193
+ {
1194
+ kind: "button",
1195
+ id: "link",
1196
+ icon: /* @__PURE__ */ jsx(Link, { className: "h-4 w-4" }),
1197
+ label: labels.insertLink,
1198
+ active: { name: "link" },
1199
+ run: () => openLink()
1200
+ },
1201
+ {
1202
+ kind: "button",
1203
+ id: "image",
1204
+ icon: /* @__PURE__ */ jsx(ImagePlus, { className: "h-4 w-4" }),
1205
+ label: labels.insertImage,
1206
+ // No `active`: this opens a dialog, it is never a pressed state.
1207
+ run: () => onInsertImage?.()
1208
+ },
1209
+ {
1210
+ kind: "custom",
1211
+ id: "table",
1212
+ render: (current) => /* @__PURE__ */ jsx(
1213
+ TableMenu,
1214
+ {
1215
+ editor: current,
1216
+ icon: /* @__PURE__ */ jsx(Table, { className: "h-4 w-4" }),
1217
+ labels: labels.table
1218
+ }
1219
+ )
1220
+ }
1221
+ ];
1222
+ let items = builtIn;
1223
+ const appended = [];
1224
+ for (const group of extraToolbar) {
1225
+ if (!group.after) {
1226
+ appended.push(...group.items);
1227
+ continue;
1228
+ }
1229
+ const at = items.findIndex(
1230
+ (item) => item.kind !== "separator" && item.id === group.after
1231
+ );
1232
+ if (at === -1) {
1233
+ appended.push(...group.items);
1234
+ continue;
1235
+ }
1236
+ items = [...items.slice(0, at + 1), ...group.items, ...items.slice(at + 1)];
1237
+ }
1238
+ return [...items, ...appended];
1239
+ }, [labels, extraToolbar, onInsertImage, openLink]);
1240
+ return { editor, toolbar, linkNeedsText, currentLinkHref, applyLink };
1241
+ }
1242
+ function ColorMenu({ editor, labels, icon, swatches }) {
1243
+ const active = swatches.find((swatch) => editor?.isActive(swatch.mark));
1244
+ function apply(mark) {
1245
+ if (!editor) return;
1246
+ const chain = editor.chain().focus();
1247
+ for (const swatch of swatches) chain.unsetMark(swatch.mark);
1248
+ if (mark) chain.setMark(mark);
1249
+ chain.run();
1250
+ }
1251
+ return /* @__PURE__ */ jsxs(DropdownMenu, { children: [
1252
+ /* @__PURE__ */ jsx(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsx(
1253
+ Button,
1254
+ {
1255
+ type: "button",
1256
+ size: "sm",
1257
+ variant: active ? "secondary" : "ghost",
1258
+ disabled: !editor,
1259
+ title: labels.trigger,
1260
+ "aria-label": labels.trigger,
1261
+ "data-slot": "color-menu-trigger",
1262
+ children: icon
1263
+ }
1264
+ ) }),
1265
+ /* @__PURE__ */ jsx(DropdownMenuContent, { align: "start", className: "w-auto", children: /* @__PURE__ */ jsxs(
1266
+ "div",
1267
+ {
1268
+ role: "group",
1269
+ "aria-label": labels.heading,
1270
+ className: "flex items-center gap-1.5 px-1.5 py-1",
1271
+ children: [
1272
+ swatches.map((swatch) => /* @__PURE__ */ jsx(
1273
+ "button",
1274
+ {
1275
+ type: "button",
1276
+ "aria-label": swatch.label,
1277
+ "aria-pressed": editor?.isActive(swatch.mark) ?? false,
1278
+ title: swatch.label,
1279
+ style: { backgroundColor: swatch.preview },
1280
+ className: cn(
1281
+ "h-7 w-7 cursor-pointer rounded-full border transition-transform",
1282
+ "focus-visible:ring-ring/50 focus-visible:ring-2 focus-visible:outline-none",
1283
+ editor?.isActive(swatch.mark) ? "border-primary scale-110 border-2" : "border-border/60 hover:scale-110"
1284
+ ),
1285
+ onClick: () => apply(swatch.mark)
1286
+ },
1287
+ swatch.mark
1288
+ )),
1289
+ /* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: "bg-border mx-0.5 h-5 w-px" }),
1290
+ /* @__PURE__ */ jsx(
1291
+ "button",
1292
+ {
1293
+ type: "button",
1294
+ "aria-label": labels.clear,
1295
+ title: labels.clear,
1296
+ disabled: !active,
1297
+ className: cn(
1298
+ "text-muted-foreground flex h-7 w-7 cursor-pointer items-center justify-center rounded-full",
1299
+ "focus-visible:ring-ring/50 focus-visible:ring-2 focus-visible:outline-none",
1300
+ "hover:text-foreground disabled:cursor-default disabled:opacity-40"
1301
+ ),
1302
+ onClick: () => apply(""),
1303
+ children: /* @__PURE__ */ jsx(Ban, { className: "h-4 w-4" })
1304
+ }
1305
+ )
1306
+ ]
1307
+ }
1308
+ ) })
1309
+ ] });
1310
+ }
1311
+ function InsertLinkDialog({
1312
+ open,
1313
+ onOpenChange,
1314
+ labels,
1315
+ url,
1316
+ onUrlChange,
1317
+ text = "",
1318
+ onTextChange,
1319
+ needsText = false,
1320
+ onApply,
1321
+ onCancel
1322
+ }) {
1323
+ const urlId = useId();
1324
+ const textId = useId();
1325
+ const canApply = needsText ? Boolean(url.trim()) && Boolean(text.trim()) : true;
1326
+ function submitOnEnter(event) {
1327
+ if (event.key === "Enter" && canApply) {
1328
+ event.preventDefault();
1329
+ onApply();
1330
+ }
1331
+ }
1332
+ return /* @__PURE__ */ jsx(Dialog, { open, onOpenChange, children: /* @__PURE__ */ jsxs(DialogContent, { className: "px-4 py-2", "data-slot": "insert-link-dialog", children: [
1333
+ /* @__PURE__ */ jsx(DialogHeader, { children: /* @__PURE__ */ jsx(DialogTitle, { children: labels.title }) }),
1334
+ /* @__PURE__ */ jsxs("div", { className: "mt-4 flex flex-col gap-3", children: [
1335
+ needsText && onTextChange && /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
1336
+ /* @__PURE__ */ jsx(Label, { htmlFor: textId, children: labels.textLabel }),
1337
+ /* @__PURE__ */ jsx(
1338
+ Input,
1339
+ {
1340
+ id: textId,
1341
+ placeholder: labels.textPlaceholder,
1342
+ value: text,
1343
+ onChange: (event) => onTextChange(event.target.value),
1344
+ onKeyDown: submitOnEnter
1345
+ }
1346
+ )
1347
+ ] }),
1348
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
1349
+ /* @__PURE__ */ jsx(Label, { htmlFor: urlId, children: labels.urlLabel }),
1350
+ /* @__PURE__ */ jsx(
1351
+ Input,
1352
+ {
1353
+ id: urlId,
1354
+ placeholder: labels.urlPlaceholder,
1355
+ value: url,
1356
+ onChange: (event) => onUrlChange(event.target.value),
1357
+ onKeyDown: submitOnEnter
1358
+ }
1359
+ ),
1360
+ !needsText && /* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-xs", children: labels.hint })
1361
+ ] })
1362
+ ] }),
1363
+ /* @__PURE__ */ jsxs(DialogFooter, { children: [
1364
+ /* @__PURE__ */ jsx(
1365
+ Button,
1366
+ {
1367
+ type: "button",
1368
+ variant: "outline",
1369
+ onClick: () => {
1370
+ if (onCancel) onCancel();
1371
+ else onOpenChange(false);
1372
+ },
1373
+ children: labels.cancel
1374
+ }
1375
+ ),
1376
+ /* @__PURE__ */ jsx(Button, { type: "button", onClick: onApply, disabled: !canApply, children: labels.apply })
1377
+ ] })
1378
+ ] }) });
1379
+ }
1380
+ function LinkActions({ editor, labels, onEdit, onOpen }) {
1381
+ const href = useEditorState({
1382
+ editor,
1383
+ selector: ({ editor: current }) => current.getAttributes("link").href ?? ""
1384
+ });
1385
+ return /* @__PURE__ */ jsxs(
1386
+ "div",
1387
+ {
1388
+ "data-slot": "link-bubble-menu",
1389
+ className: "bg-popover text-popover-foreground flex items-center gap-1 rounded-md border p-1 shadow-md",
1390
+ children: [
1391
+ /* @__PURE__ */ jsx("span", { className: "text-muted-foreground max-w-[16rem] truncate px-2 text-xs", children: href }),
1392
+ /* @__PURE__ */ jsx(
1393
+ Button,
1394
+ {
1395
+ type: "button",
1396
+ size: "sm",
1397
+ variant: "ghost",
1398
+ title: labels.open,
1399
+ "aria-label": labels.open,
1400
+ disabled: !href,
1401
+ onClick: () => {
1402
+ if (!href) return;
1403
+ if (onOpen) {
1404
+ onOpen(href);
1405
+ return;
1406
+ }
1407
+ window.open(href, "_blank", "noopener,noreferrer");
1408
+ },
1409
+ children: /* @__PURE__ */ jsx(ExternalLink, { className: "h-4 w-4" })
1410
+ }
1411
+ ),
1412
+ /* @__PURE__ */ jsx(
1413
+ Button,
1414
+ {
1415
+ type: "button",
1416
+ size: "sm",
1417
+ variant: "ghost",
1418
+ title: labels.edit,
1419
+ "aria-label": labels.edit,
1420
+ onClick: onEdit,
1421
+ children: /* @__PURE__ */ jsx(Pencil, { className: "h-4 w-4" })
1422
+ }
1423
+ ),
1424
+ /* @__PURE__ */ jsx(
1425
+ Button,
1426
+ {
1427
+ type: "button",
1428
+ size: "sm",
1429
+ variant: "ghost",
1430
+ className: "text-destructive",
1431
+ title: labels.remove,
1432
+ "aria-label": labels.remove,
1433
+ onClick: () => editor.chain().focus().extendMarkRange("link").unsetLink().run(),
1434
+ children: /* @__PURE__ */ jsx(Unlink, { className: "h-4 w-4" })
1435
+ }
1436
+ )
1437
+ ]
1438
+ }
1439
+ );
1440
+ }
1441
+ function LinkBubbleMenu({ editor, labels, onEdit, onOpen }) {
1442
+ const shouldShow = useCallback(
1443
+ // Only while the caret is actually in a link — otherwise this bar would
1444
+ // compete with any other bubble menu the consumer adds.
1445
+ ({ editor: current }) => current.isActive("link"),
1446
+ []
1447
+ );
1448
+ const options = useMemo(() => ({ placement: "bottom", offset: 8 }), []);
1449
+ if (!editor) return null;
1450
+ return /* @__PURE__ */ jsx(BubbleMenu, { editor, shouldShow, options, children: /* @__PURE__ */ jsx(LinkActions, { editor, labels, onEdit, onOpen }) });
1451
+ }
1452
+
1453
+ export { ColorMenu, EDITOR_CONTENT_STYLE, InsertImageDialog, InsertLinkDialog, LinkBubbleMenu, ListMenu, RichTextEditor, RichTextToolbar, TableMenu, createResizableImage, useRichTextEditor };
1454
+ //# sourceMappingURL=editor.js.map
1455
+ //# sourceMappingURL=editor.js.map