@krak-stack/registry 0.10.5 → 0.10.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2677 @@
1
+ // ../../src/components/ui/email-editor.tsx
2
+ import { Schema as Schema7 } from "effect";
3
+ import { Popover } from "@base-ui/react/popover";
4
+ import {
5
+ useId as useId3,
6
+ useMemo,
7
+ useRef as useRef4,
8
+ useState as useState4
9
+ } from "react";
10
+ import {
11
+ Type,
12
+ Image,
13
+ MousePointer2,
14
+ Minus,
15
+ Space,
16
+ KeyRound,
17
+ ScanLine,
18
+ Trash2,
19
+ Copy,
20
+ Pencil,
21
+ X,
22
+ ChevronDown as ChevronDown2,
23
+ Plus,
24
+ Palette
25
+ } from "lucide-react";
26
+
27
+ // ../../src/components/ui/button.tsx
28
+ import { Button as ButtonPrimitive } from "@base-ui/react/button";
29
+ import { cva } from "class-variance-authority";
30
+
31
+ // ../../src/lib/utils.ts
32
+ import { cn } from "cn";
33
+
34
+ // ../../src/components/ui/button.tsx
35
+ import { jsx } from "react/jsx-runtime";
36
+ var buttonVariants = cva("group/button inline-flex shrink-0 items-center justify-center rounded-md border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 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 [&_svg:not([class*='size-'])]:size-4", {
37
+ variants: {
38
+ variant: {
39
+ default: "bg-primary text-primary-foreground hover:bg-primary/80",
40
+ outline: "border-border bg-background shadow-xs hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
41
+ secondary: "bg-secondary text-secondary-foreground hover:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)] aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
42
+ ghost: "hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
43
+ destructive: "bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
44
+ link: "text-primary underline-offset-4 hover:underline"
45
+ },
46
+ size: {
47
+ default: "h-9 gap-1.5 px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
48
+ xs: "h-6 gap-1 rounded-[min(var(--radius-md),8px)] px-2 text-xs in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
49
+ sm: "h-8 gap-1 rounded-[min(var(--radius-md),10px)] px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5",
50
+ lg: "h-10 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
51
+ icon: "size-9",
52
+ "icon-xs": "size-6 rounded-[min(var(--radius-md),8px)] in-data-[slot=button-group]:rounded-md [&_svg:not([class*='size-'])]:size-3",
53
+ "icon-sm": "size-8 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-md",
54
+ "icon-lg": "size-10"
55
+ }
56
+ },
57
+ defaultVariants: {
58
+ variant: "default",
59
+ size: "default"
60
+ }
61
+ });
62
+ function Button({
63
+ className,
64
+ variant = "default",
65
+ size = "default",
66
+ ...props
67
+ }) {
68
+ return /* @__PURE__ */ jsx(ButtonPrimitive, {
69
+ "data-slot": "button",
70
+ className: cn(buttonVariants({ variant, size, className })),
71
+ ...props
72
+ });
73
+ }
74
+
75
+ // ../../src/components/ui/input.tsx
76
+ import { Input as InputPrimitive } from "@base-ui/react/input";
77
+ import { jsx as jsx2 } from "react/jsx-runtime";
78
+ function Input({ className, type, ...props }) {
79
+ return /* @__PURE__ */ jsx2(InputPrimitive, {
80
+ type,
81
+ "data-slot": "input",
82
+ className: cn("h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-2.5 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40", className),
83
+ ...props
84
+ });
85
+ }
86
+
87
+ // ../../src/components/ui/select.tsx
88
+ import { Select as SelectPrimitive } from "@base-ui/react/select";
89
+ import { ChevronDownIcon, CheckIcon, ChevronUpIcon } from "lucide-react";
90
+ import { jsx as jsx3, jsxs } from "react/jsx-runtime";
91
+ "use client";
92
+ var Select = SelectPrimitive.Root;
93
+ function SelectValue({ className, ...props }) {
94
+ return /* @__PURE__ */ jsx3(SelectPrimitive.Value, {
95
+ "data-slot": "select-value",
96
+ className: cn("flex flex-1 text-left", className),
97
+ ...props
98
+ });
99
+ }
100
+ function SelectTrigger({
101
+ className,
102
+ size = "default",
103
+ children,
104
+ ...props
105
+ }) {
106
+ return /* @__PURE__ */ jsxs(SelectPrimitive.Trigger, {
107
+ "data-slot": "select-trigger",
108
+ "data-size": size,
109
+ className: cn("flex w-fit items-center justify-between gap-1.5 rounded-md border border-input bg-transparent py-2 pr-2 pl-2.5 text-sm whitespace-nowrap shadow-xs transition-[color,box-shadow] outline-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 data-placeholder:text-muted-foreground data-[size=default]:h-9 data-[size=sm]:h-8 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-1.5 dark:bg-input/30 dark:hover:bg-input/50 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", className),
110
+ ...props,
111
+ children: [
112
+ children,
113
+ /* @__PURE__ */ jsx3(SelectPrimitive.Icon, {
114
+ render: /* @__PURE__ */ jsx3(ChevronDownIcon, {
115
+ className: "text-muted-foreground pointer-events-none size-4"
116
+ })
117
+ })
118
+ ]
119
+ });
120
+ }
121
+ function SelectContent({
122
+ className,
123
+ children,
124
+ side = "bottom",
125
+ sideOffset = 4,
126
+ align = "center",
127
+ alignOffset = 0,
128
+ alignItemWithTrigger = true,
129
+ ...props
130
+ }) {
131
+ return /* @__PURE__ */ jsx3(SelectPrimitive.Portal, {
132
+ children: /* @__PURE__ */ jsx3(SelectPrimitive.Positioner, {
133
+ side,
134
+ sideOffset,
135
+ align,
136
+ alignOffset,
137
+ alignItemWithTrigger,
138
+ className: "isolate z-50",
139
+ children: /* @__PURE__ */ jsxs(SelectPrimitive.Popup, {
140
+ "data-slot": "select-content",
141
+ "data-align-trigger": alignItemWithTrigger,
142
+ className: cn("relative isolate z-50 max-h-(--available-height) w-(--anchor-width) min-w-36 origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-md bg-popover text-popover-foreground shadow-md ring-1 ring-foreground/10 duration-100 data-[align-trigger=true]:animate-none data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-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 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95", className),
143
+ ...props,
144
+ children: [
145
+ /* @__PURE__ */ jsx3(SelectScrollUpButton, {}),
146
+ /* @__PURE__ */ jsx3(SelectPrimitive.List, {
147
+ children
148
+ }),
149
+ /* @__PURE__ */ jsx3(SelectScrollDownButton, {})
150
+ ]
151
+ })
152
+ })
153
+ });
154
+ }
155
+ function SelectItem({
156
+ className,
157
+ children,
158
+ ...props
159
+ }) {
160
+ return /* @__PURE__ */ jsxs(SelectPrimitive.Item, {
161
+ "data-slot": "select-item",
162
+ className: cn("relative flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 pr-8 pl-2 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2", className),
163
+ ...props,
164
+ children: [
165
+ /* @__PURE__ */ jsx3(SelectPrimitive.ItemText, {
166
+ className: "flex flex-1 shrink-0 gap-2 whitespace-nowrap",
167
+ children
168
+ }),
169
+ /* @__PURE__ */ jsx3(SelectPrimitive.ItemIndicator, {
170
+ render: /* @__PURE__ */ jsx3("span", {
171
+ className: "pointer-events-none absolute right-2 flex size-4 items-center justify-center"
172
+ }),
173
+ children: /* @__PURE__ */ jsx3(CheckIcon, {
174
+ className: "pointer-events-none"
175
+ })
176
+ })
177
+ ]
178
+ });
179
+ }
180
+ function SelectScrollUpButton({
181
+ className,
182
+ ...props
183
+ }) {
184
+ return /* @__PURE__ */ jsx3(SelectPrimitive.ScrollUpArrow, {
185
+ "data-slot": "select-scroll-up-button",
186
+ className: cn("top-0 z-10 flex w-full cursor-default items-center justify-center bg-popover py-1 [&_svg:not([class*='size-'])]:size-4", className),
187
+ ...props,
188
+ children: /* @__PURE__ */ jsx3(ChevronUpIcon, {})
189
+ });
190
+ }
191
+ function SelectScrollDownButton({
192
+ className,
193
+ ...props
194
+ }) {
195
+ return /* @__PURE__ */ jsx3(SelectPrimitive.ScrollDownArrow, {
196
+ "data-slot": "select-scroll-down-button",
197
+ className: cn("bottom-0 z-10 flex w-full cursor-default items-center justify-center bg-popover py-1 [&_svg:not([class*='size-'])]:size-4", className),
198
+ ...props,
199
+ children: /* @__PURE__ */ jsx3(ChevronDownIcon, {})
200
+ });
201
+ }
202
+
203
+ // ../../src/components/ui/popover.tsx
204
+ import { Popover as PopoverPrimitive } from "@base-ui/react/popover";
205
+ import { jsx as jsx4 } from "react/jsx-runtime";
206
+ function PopoverTrigger({ ...props }) {
207
+ return /* @__PURE__ */ jsx4(PopoverPrimitive.Trigger, {
208
+ "data-slot": "popover-trigger",
209
+ ...props
210
+ });
211
+ }
212
+ function PopoverContent({
213
+ className,
214
+ align = "center",
215
+ alignOffset = 0,
216
+ side = "bottom",
217
+ sideOffset = 4,
218
+ ...props
219
+ }) {
220
+ return /* @__PURE__ */ jsx4(PopoverPrimitive.Portal, {
221
+ children: /* @__PURE__ */ jsx4(PopoverPrimitive.Positioner, {
222
+ align,
223
+ alignOffset,
224
+ side,
225
+ sideOffset,
226
+ className: "isolate z-50",
227
+ children: /* @__PURE__ */ jsx4(PopoverPrimitive.Popup, {
228
+ "data-slot": "popover-content",
229
+ className: cn("z-50 flex w-72 origin-(--transform-origin) flex-col gap-4 rounded-md bg-popover p-4 text-sm text-popover-foreground shadow-md ring-1 ring-foreground/10 outline-hidden duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-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 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95", className),
230
+ ...props
231
+ })
232
+ })
233
+ });
234
+ }
235
+ function PopoverTitle({ className, ...props }) {
236
+ return /* @__PURE__ */ jsx4(PopoverPrimitive.Title, {
237
+ "data-slot": "popover-title",
238
+ className: cn("font-medium", className),
239
+ ...props
240
+ });
241
+ }
242
+
243
+ // ../../src/components/ui/animated-list.tsx
244
+ import {
245
+ useEffect as useEffect2,
246
+ useEffectEvent as useEffectEvent2,
247
+ useId,
248
+ useRef as useRef2,
249
+ useState as useState2
250
+ } from "react";
251
+ import { GripVertical } from "lucide-react";
252
+ import { cn as cn2 } from "cn";
253
+ import { useKrakstackLocale } from "@krak-stack/registry/krakstack-provider";
254
+
255
+ // ../../src/lib/krakmotion.ts
256
+ import {
257
+ useEffect,
258
+ useEffectEvent,
259
+ useLayoutEffect,
260
+ useRef,
261
+ useState,
262
+ useSyncExternalStore
263
+ } from "react";
264
+ var useBrowserLayoutEffect = globalThis.window ? useLayoutEffect : useEffect;
265
+ var reducedMotionQuery = "(prefers-reduced-motion: reduce)";
266
+ var subscribeReducedMotion = (notify) => {
267
+ const query = window.matchMedia?.(reducedMotionQuery);
268
+ query?.addEventListener("change", notify);
269
+ return () => query?.removeEventListener("change", notify);
270
+ };
271
+ var useReducedMotion = () => useSyncExternalStore(subscribeReducedMotion, () => window.matchMedia?.(reducedMotionQuery).matches ?? true, () => true);
272
+ var animateElement = (element, frames, duration, complete) => {
273
+ if (duration <= 0 || !element.animate) {
274
+ complete?.();
275
+ return () => {};
276
+ }
277
+ const animation = element.animate(frames, {
278
+ duration,
279
+ easing: "cubic-bezier(0.2, 0, 0, 1)"
280
+ });
281
+ animation.onfinish = () => complete?.();
282
+ return () => {
283
+ animation.onfinish = null;
284
+ animation.cancel();
285
+ };
286
+ };
287
+ var useKeyedPresence = (items, getKey) => {
288
+ const [previous, setPrevious] = useState(items);
289
+ const [retained, setRetained] = useState(items);
290
+ if (previous !== items) {
291
+ const keys = new Set(items.map(getKey));
292
+ const next = [...items];
293
+ retained.forEach((item, index) => {
294
+ if (!keys.has(getKey(item)))
295
+ next.splice(Math.min(index, next.length), 0, item);
296
+ });
297
+ setPrevious(items);
298
+ setRetained(next);
299
+ }
300
+ const present = new Set(items.map(getKey));
301
+ return {
302
+ items: retained,
303
+ present,
304
+ remove: (key) => setRetained((current) => current.filter((item) => getKey(item) !== key))
305
+ };
306
+ };
307
+ var useListLayout = (ref, duration) => {
308
+ const previous = useRef(new Map);
309
+ const animations = useRef(new Map);
310
+ const reduced = useReducedMotion();
311
+ useBrowserLayoutEffect(() => {
312
+ const next = new Map;
313
+ for (const element of ref.current?.children ?? []) {
314
+ if (!(element instanceof HTMLElement))
315
+ continue;
316
+ const key = element.dataset.motionKey;
317
+ if (!key)
318
+ continue;
319
+ const position = { top: element.offsetTop, left: element.offsetLeft };
320
+ next.set(key, position);
321
+ const old = previous.current.get(key);
322
+ if (old && (old.top !== position.top || old.left !== position.left)) {
323
+ const transform = getComputedStyle(element).transform;
324
+ const interrupted = transform === "none" ? null : new DOMMatrixReadOnly(transform);
325
+ animations.current.get(element)?.();
326
+ if (!element.hasAttribute("data-dragging")) {
327
+ animations.current.set(element, animateElement(element, [
328
+ {
329
+ transform: `translate(${old.left - position.left + (interrupted?.m41 ?? 0)}px, ${old.top - position.top + (interrupted?.m42 ?? 0)}px)`
330
+ },
331
+ { transform: "none" }
332
+ ], reduced ? 0 : Math.max(0, duration)));
333
+ }
334
+ }
335
+ }
336
+ for (const [element, cancel] of animations.current) {
337
+ if (!element.isConnected) {
338
+ cancel();
339
+ animations.current.delete(element);
340
+ }
341
+ }
342
+ previous.current = next;
343
+ });
344
+ useEffect(() => () => {
345
+ for (const cancel of animations.current.values())
346
+ cancel();
347
+ }, []);
348
+ };
349
+ var setDragStyle = (element, translate, active) => {
350
+ element.style.translate = translate;
351
+ element.style.zIndex = active ? "1" : "";
352
+ element.toggleAttribute("data-dragging", active);
353
+ };
354
+ var useVerticalDrag = ({
355
+ ref,
356
+ container,
357
+ enabled,
358
+ overflow,
359
+ onMove
360
+ }) => {
361
+ const session = useRef(null);
362
+ const update = () => {
363
+ const drag = session.current;
364
+ const element = drag?.element ?? ref.current;
365
+ const list = container.current;
366
+ if (!drag?.active || !element || !list)
367
+ return;
368
+ const bounds = list.getBoundingClientRect();
369
+ const top = bounds.top + list.clientTop - list.scrollTop;
370
+ const allowance = Math.max(0, overflow);
371
+ const desired = Math.max(bounds.top - allowance, Math.min(bounds.bottom - element.offsetHeight + allowance, drag.y - drag.grab));
372
+ setDragStyle(element, `0 ${desired - top - element.offsetTop}px`, true);
373
+ const bottom = desired + element.offsetHeight;
374
+ const siblings = [...list.children].filter((child) => child instanceof HTMLElement && child.getAttribute("aria-hidden") !== "true");
375
+ const index = siblings.indexOf(element);
376
+ let target = index;
377
+ for (let i = 0;i < siblings.length; i++) {
378
+ const sibling = siblings[i];
379
+ if (!(sibling instanceof HTMLElement) || sibling === element)
380
+ continue;
381
+ const middle = top + sibling.offsetTop + sibling.offsetHeight / 2;
382
+ if (i > index && bottom > middle)
383
+ target = i;
384
+ if (i < index && desired < middle) {
385
+ target = i;
386
+ break;
387
+ }
388
+ }
389
+ if (target !== index)
390
+ onMove(target);
391
+ };
392
+ const end = () => {
393
+ const drag = session.current;
394
+ session.current = null;
395
+ const element = drag?.element ?? ref.current;
396
+ if (element)
397
+ setDragStyle(element, "", false);
398
+ if (drag?.capture.hasPointerCapture?.(drag.pointer))
399
+ drag.capture.releasePointerCapture(drag.pointer);
400
+ };
401
+ const updateDrag = useEffectEvent(update);
402
+ const endDrag = useEffectEvent(end);
403
+ const connection = useRef({ generation: 0 });
404
+ useBrowserLayoutEffect(() => {
405
+ if (!enabled)
406
+ end();
407
+ else
408
+ update();
409
+ });
410
+ useEffect(() => {
411
+ const lifecycle = connection.current;
412
+ const generation = ++lifecycle.generation;
413
+ const scroll = () => updateDrag();
414
+ const move = (event) => {
415
+ const drag = session.current;
416
+ const element = ref.current;
417
+ if (!drag || drag.pointer !== event.pointerId || !element)
418
+ return;
419
+ drag.y = event.clientY;
420
+ if (!drag.active && Math.abs(drag.y - drag.start) < 4)
421
+ return;
422
+ drag.active = true;
423
+ element.dataset.dragging = "";
424
+ element.style.zIndex = "1";
425
+ updateDrag();
426
+ };
427
+ const stop = (event) => {
428
+ if (session.current?.pointer === event.pointerId)
429
+ endDrag();
430
+ };
431
+ document.addEventListener("scroll", scroll, true);
432
+ document.addEventListener("pointermove", move);
433
+ document.addEventListener("pointerup", stop);
434
+ document.addEventListener("pointercancel", stop);
435
+ document.addEventListener("lostpointercapture", stop);
436
+ window.addEventListener("resize", endDrag);
437
+ return () => {
438
+ document.removeEventListener("scroll", scroll, true);
439
+ document.removeEventListener("pointermove", move);
440
+ document.removeEventListener("pointerup", stop);
441
+ document.removeEventListener("pointercancel", stop);
442
+ document.removeEventListener("lostpointercapture", stop);
443
+ window.removeEventListener("resize", endDrag);
444
+ queueMicrotask(() => {
445
+ if (lifecycle.generation === generation)
446
+ endDrag();
447
+ });
448
+ };
449
+ }, [ref]);
450
+ return {
451
+ onPointerDown: (event) => {
452
+ if (!enabled || event.button !== 0 || session.current)
453
+ return;
454
+ const element = ref.current;
455
+ const list = container.current;
456
+ if (!element || !list)
457
+ return;
458
+ list.setPointerCapture(event.pointerId);
459
+ session.current = {
460
+ pointer: event.pointerId,
461
+ start: event.clientY,
462
+ y: event.clientY,
463
+ grab: event.clientY - element.getBoundingClientRect().top,
464
+ active: false,
465
+ capture: list,
466
+ element
467
+ };
468
+ }
469
+ };
470
+ };
471
+
472
+ // ../../src/components/ui/animated-list.tsx
473
+ import { jsx as jsx5, jsxs as jsxs2, Fragment } from "react/jsx-runtime";
474
+ var labels = {
475
+ en: {
476
+ reorder: "Reorder",
477
+ instructions: "Drag to reorder, or use Arrow Up, Arrow Down, Home and End on the handle.",
478
+ position: (label, position, total) => `${label}, position ${position} of ${total}.`
479
+ },
480
+ fr: {
481
+ reorder: "Réordonner",
482
+ instructions: "Faites glisser pour réordonner, ou utilisez les flèches haut et bas, Début et Fin sur la poignée.",
483
+ position: (label, position, total) => `${label}, position ${position} sur ${total}.`
484
+ }
485
+ };
486
+ var AnimatedList = ({
487
+ items,
488
+ getKey,
489
+ children,
490
+ duration = 180,
491
+ className,
492
+ itemClassName,
493
+ label,
494
+ onReorder,
495
+ dragOverflow = 12,
496
+ getItemLabel,
497
+ locale: override,
498
+ messages
499
+ }) => {
500
+ const locale = useKrakstackLocale(override);
501
+ const copy = {
502
+ ...locale.startsWith("fr") ? labels.fr : labels.en,
503
+ ...messages?.[locale.split("-")[0] ?? locale],
504
+ ...messages?.[locale]
505
+ };
506
+ const instructions = useId();
507
+ const container = useRef2(null);
508
+ const [announcedKey, setAnnouncedKey] = useState2(null);
509
+ const retained = useKeyedPresence(items, getKey);
510
+ const [initial, setInitial] = useState2(true);
511
+ useEffect2(() => setInitial(false), []);
512
+ useListLayout(container, duration);
513
+ const announcement = items.map((item, index) => getKey(item) === announcedKey ? copy.position(getItemLabel?.(item) ?? getKey(item), index + 1, items.length) : "").join("");
514
+ const move = (key, target) => {
515
+ const index = items.findIndex((item) => getKey(item) === key);
516
+ if (index < 0 || index === target || !onReorder)
517
+ return;
518
+ const next = [...items];
519
+ const [item] = next.splice(index, 1);
520
+ if (item === undefined)
521
+ return;
522
+ next.splice(target, 0, item);
523
+ setAnnouncedKey(key);
524
+ onReorder(next);
525
+ };
526
+ return /* @__PURE__ */ jsxs2(Fragment, {
527
+ children: [
528
+ /* @__PURE__ */ jsx5("ul", {
529
+ ref: container,
530
+ "data-slot": "animated-list",
531
+ className: cn2("relative grid gap-3", className),
532
+ "aria-label": label,
533
+ children: retained.items.map((item) => {
534
+ const key = getKey(item);
535
+ const index = items.findIndex((candidate) => getKey(candidate) === key);
536
+ return /* @__PURE__ */ jsx5(ListItem, {
537
+ itemKey: key,
538
+ present: retained.present.has(key),
539
+ initial,
540
+ duration,
541
+ className: itemClassName,
542
+ instructions,
543
+ handleLabel: `${copy.reorder}: ${getItemLabel?.(item) ?? key}`,
544
+ reorderable: !!onReorder,
545
+ disabled: items.length < 2,
546
+ container,
547
+ dragOverflow,
548
+ onExit: () => retained.remove(key),
549
+ onMove: (target) => move(key, target),
550
+ onKeyMove: (direction) => move(key, direction === "Home" ? 0 : direction === "End" ? items.length - 1 : Math.max(0, Math.min(items.length - 1, index + (direction === "ArrowUp" ? -1 : 1)))),
551
+ children: children(item)
552
+ }, key);
553
+ })
554
+ }),
555
+ onReorder ? /* @__PURE__ */ jsxs2(Fragment, {
556
+ children: [
557
+ /* @__PURE__ */ jsx5("p", {
558
+ id: instructions,
559
+ className: "sr-only",
560
+ children: copy.instructions
561
+ }),
562
+ /* @__PURE__ */ jsx5("p", {
563
+ role: "status",
564
+ "aria-live": "polite",
565
+ "aria-atomic": "true",
566
+ className: "sr-only",
567
+ children: announcement
568
+ })
569
+ ]
570
+ }) : null
571
+ ]
572
+ });
573
+ };
574
+ var ListItem = ({
575
+ itemKey,
576
+ present,
577
+ initial,
578
+ duration,
579
+ className,
580
+ instructions,
581
+ handleLabel,
582
+ reorderable,
583
+ disabled,
584
+ container,
585
+ dragOverflow,
586
+ onExit,
587
+ onMove,
588
+ onKeyMove,
589
+ children
590
+ }) => {
591
+ const ref = useRef2(null);
592
+ const mounted = useRef2(false);
593
+ const lastPresent = useRef2(present);
594
+ const transitioning = useRef2(false);
595
+ const exit = useEffectEvent2(onExit);
596
+ const reduced = useReducedMotion();
597
+ const drag = useVerticalDrag({
598
+ ref,
599
+ container,
600
+ enabled: reorderable && !disabled && present,
601
+ overflow: dragOverflow,
602
+ onMove
603
+ });
604
+ useEffect2(() => {
605
+ const element = ref.current;
606
+ const first = !mounted.current;
607
+ mounted.current = true;
608
+ if (!element || first && initial && present)
609
+ return;
610
+ if (!first && lastPresent.current === present && !transitioning.current)
611
+ return;
612
+ lastPresent.current = present;
613
+ transitioning.current = true;
614
+ return animateElement(element, [{ opacity: present ? 0 : 1 }, { opacity: present ? 1 : 0 }], reduced ? 0 : Math.max(0, duration), () => {
615
+ transitioning.current = false;
616
+ if (!present)
617
+ exit();
618
+ });
619
+ }, [present, initial, duration, reduced]);
620
+ return /* @__PURE__ */ jsxs2("li", {
621
+ ref,
622
+ "data-slot": "animated-list-item",
623
+ "data-motion-key": itemKey,
624
+ className: cn2("relative", className),
625
+ inert: !present,
626
+ "aria-hidden": !present ? true : undefined,
627
+ children: [
628
+ reorderable ? /* @__PURE__ */ jsx5("button", {
629
+ "data-slot": "animated-list-handle",
630
+ type: "button",
631
+ disabled: disabled || !present,
632
+ "aria-label": handleLabel,
633
+ "aria-describedby": instructions,
634
+ className: "text-muted-foreground focus-visible:ring-ring inline-flex size-8 shrink-0 cursor-grab touch-none items-center justify-center rounded-md outline-none select-none focus-visible:ring-2 active:cursor-grabbing disabled:cursor-default disabled:opacity-50",
635
+ ...drag,
636
+ onKeyDown: (event) => {
637
+ if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)
638
+ return;
639
+ if (event.key === "ArrowUp" || event.key === "ArrowDown" || event.key === "Home" || event.key === "End") {
640
+ event.preventDefault();
641
+ event.stopPropagation();
642
+ onKeyMove(event.key);
643
+ }
644
+ },
645
+ children: /* @__PURE__ */ jsx5(GripVertical, {
646
+ className: "size-4",
647
+ "aria-hidden": "true"
648
+ })
649
+ }) : null,
650
+ children
651
+ ]
652
+ });
653
+ };
654
+
655
+ // ../../src/components/ui/rich-text-editor.tsx
656
+ import { Schema as Schema4 } from "effect";
657
+ import {
658
+ useEffect as useEffect3,
659
+ useEffectEvent as useEffectEvent3,
660
+ useId as useId2,
661
+ useRef as useRef3,
662
+ useState as useState3
663
+ } from "react";
664
+ import {
665
+ Bold,
666
+ Italic,
667
+ Underline,
668
+ Undo2,
669
+ Redo2,
670
+ Link2,
671
+ Unlink,
672
+ Braces,
673
+ ChevronDown
674
+ } from "lucide-react";
675
+
676
+ // ../../src/components/ui/dropdown-menu.tsx
677
+ import { Menu as MenuPrimitive } from "@base-ui/react/menu";
678
+ import { ChevronRightIcon, CheckIcon as CheckIcon2 } from "lucide-react";
679
+ import { jsx as jsx6, jsxs as jsxs3 } from "react/jsx-runtime";
680
+ function DropdownMenu({ ...props }) {
681
+ return /* @__PURE__ */ jsx6(MenuPrimitive.Root, {
682
+ "data-slot": "dropdown-menu",
683
+ ...props
684
+ });
685
+ }
686
+ function DropdownMenuTrigger({ ...props }) {
687
+ return /* @__PURE__ */ jsx6(MenuPrimitive.Trigger, {
688
+ "data-slot": "dropdown-menu-trigger",
689
+ ...props
690
+ });
691
+ }
692
+ function DropdownMenuContent({
693
+ align = "start",
694
+ alignOffset = 0,
695
+ side = "bottom",
696
+ sideOffset = 4,
697
+ className,
698
+ ...props
699
+ }) {
700
+ return /* @__PURE__ */ jsx6(MenuPrimitive.Portal, {
701
+ children: /* @__PURE__ */ jsx6(MenuPrimitive.Positioner, {
702
+ className: "isolate z-50 outline-none",
703
+ align,
704
+ alignOffset,
705
+ side,
706
+ sideOffset,
707
+ children: /* @__PURE__ */ jsx6(MenuPrimitive.Popup, {
708
+ "data-slot": "dropdown-menu-content",
709
+ className: cn("z-50 max-h-(--available-height) w-(--anchor-width) min-w-32 origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-md bg-popover p-1 text-popover-foreground shadow-md ring-1 ring-foreground/10 duration-100 outline-none data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-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 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:overflow-hidden data-closed:fade-out-0 data-closed:zoom-out-95", className),
710
+ ...props
711
+ })
712
+ })
713
+ });
714
+ }
715
+ function DropdownMenuItem({
716
+ className,
717
+ inset,
718
+ variant = "default",
719
+ ...props
720
+ }) {
721
+ return /* @__PURE__ */ jsx6(MenuPrimitive.Item, {
722
+ "data-slot": "dropdown-menu-item",
723
+ "data-inset": inset,
724
+ "data-variant": variant,
725
+ className: cn("group/dropdown-menu-item relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-inset:pl-8 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-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 data-[variant=destructive]:*:[svg]:text-destructive", className),
726
+ ...props
727
+ });
728
+ }
729
+
730
+ // ../../src/components/ui/rich-text-editor.tsx
731
+ import { useKrakstackLocale as useKrakstackLocale2 } from "@krak-stack/registry/krakstack-provider";
732
+
733
+ // ../../src/lib/rich-text/schema.ts
734
+ import { Schema } from "effect";
735
+ var RichTextLink = Schema.String.check(Schema.isMaxLength(2000), Schema.makeFilter((value) => {
736
+ if (/[\s\p{Cc}\\{}]/u.test(value))
737
+ return false;
738
+ const url = URL.parse(value);
739
+ return url !== null && ["https:", "mailto:"].includes(url.protocol) && !url.username && !url.password;
740
+ })).annotate({ identifier: "RichTextLink" });
741
+ var RichTextMarks = Schema.Struct({
742
+ bold: Schema.optional(Schema.Boolean),
743
+ italic: Schema.optional(Schema.Boolean),
744
+ underline: Schema.optional(Schema.Boolean),
745
+ href: Schema.optional(RichTextLink)
746
+ }).annotate({ identifier: "RichTextMarks" });
747
+ var RichTextInline = Schema.Union([
748
+ Schema.Struct({
749
+ type: Schema.Literal("text"),
750
+ text: Schema.String.check(Schema.isMaxLength(20000)),
751
+ marks: Schema.optional(RichTextMarks)
752
+ }),
753
+ Schema.Struct({
754
+ type: Schema.Literal("variable"),
755
+ key: Schema.String.check(Schema.isPattern(/^[a-zA-Z][a-zA-Z0-9_.]{0,79}$/)),
756
+ marks: Schema.optional(RichTextMarks)
757
+ })
758
+ ]).annotate({ identifier: "RichTextInline" });
759
+ var RichTextBlockKind = Schema.Literals([
760
+ "paragraph",
761
+ "heading",
762
+ "bullet",
763
+ "numbered"
764
+ ]).annotate({ identifier: "RichTextBlockKind" });
765
+ var RichTextDocument = Schema.Struct({
766
+ version: Schema.Literal(1),
767
+ blocks: Schema.Array(Schema.Struct({
768
+ type: RichTextBlockKind,
769
+ content: Schema.Array(RichTextInline).check(Schema.isMaxLength(2000))
770
+ })).check(Schema.isMinLength(1), Schema.isMaxLength(100))
771
+ }).check(Schema.makeFilter((value) => value.blocks.reduce((size, block) => size + block.content.reduce((count, inline) => count + (inline.type === "text" ? inline.text.length : 1), 0), 0) <= 20000)).annotate({ identifier: "RichTextDocument" });
772
+ var RichTextVariable = Schema.Struct({
773
+ key: Schema.String.check(Schema.isPattern(/^[a-zA-Z][a-zA-Z0-9_.]{0,79}$/)),
774
+ label: Schema.String,
775
+ type: Schema.Literals(["text", "url"]),
776
+ sample: Schema.String
777
+ }).annotate({ identifier: "RichTextVariable" });
778
+
779
+ // ../../src/lib/rich-text/model.ts
780
+ import { Schema as Schema2 } from "effect";
781
+ var sameMarks = Schema2.toEquivalence(RichTextMarks);
782
+ var emptyRichText = () => ({
783
+ version: 1,
784
+ blocks: [{ type: "paragraph", content: [] }]
785
+ });
786
+ var richTextFromString = (text) => ({
787
+ version: 1,
788
+ blocks: text.split(/\r\n?|\n/).map((line) => ({
789
+ type: "paragraph",
790
+ content: line ? [{ type: "text", text: line }] : []
791
+ }))
792
+ });
793
+ var richTextPlainText = (value, variables) => value.blocks.map((block) => block.content.map((inline) => inline.type === "text" ? inline.text : variables?.[inline.key] ?? `{{${inline.key}}}`).join("")).join(`
794
+ `);
795
+ var units = (value) => value.blocks.flatMap((block, index) => [
796
+ ...index ? [{ type: "break", next: block.type }] : [],
797
+ ...block.content.flatMap((inline) => inline.type === "text" ? inline.text.split("").map((text) => ({ ...inline, text })) : [inline])
798
+ ]);
799
+ var fromUnits = (content, first) => {
800
+ const blocks = [{ type: first, content: [] }];
801
+ for (const unit of content) {
802
+ if (unit.type === "break") {
803
+ blocks.push({ type: unit.next, content: [] });
804
+ continue;
805
+ }
806
+ const target = blocks.at(-1);
807
+ if (!target)
808
+ continue;
809
+ const previous = target.content.at(-1);
810
+ if (unit.type === "text" && previous?.type === "text" && sameMarks(previous.marks ?? {}, unit.marks ?? {})) {
811
+ target.content[target.content.length - 1] = {
812
+ ...previous,
813
+ text: previous.text + unit.text
814
+ };
815
+ } else
816
+ target.content.push(unit);
817
+ }
818
+ return { version: 1, blocks };
819
+ };
820
+ var replaceRichText = (value, selection, inserted) => {
821
+ const content = units(value);
822
+ const from = Math.max(0, Math.min(selection.from, content.length));
823
+ const to = Math.max(from, Math.min(selection.to, content.length));
824
+ const additions = inserted.type === "text" ? inserted.text.replace(/\r\n?/g, `
825
+ `).split("").map((text) => text === `
826
+ ` ? { type: "break", next: "paragraph" } : { type: "text", text, marks: inserted.marks }) : [inserted];
827
+ const next = fromUnits([...content.slice(0, from), ...additions, ...content.slice(to)], value.blocks[0]?.type ?? "paragraph");
828
+ const position = from + additions.length;
829
+ return { value: next, selection: { from: position, to: position } };
830
+ };
831
+ var formatRichText = (value, selection, mark, setting) => fromUnits(units(value).map((unit, index) => {
832
+ if (unit.type === "break" || index < selection.from || index >= selection.to)
833
+ return unit;
834
+ const marks = { ...unit.marks, [mark]: setting };
835
+ return { ...unit, marks };
836
+ }), value.blocks[0]?.type ?? "paragraph");
837
+ var setRichTextBlock = (value, selection, type) => {
838
+ let offset = 0;
839
+ return {
840
+ ...value,
841
+ blocks: value.blocks.map((block) => {
842
+ const start = offset;
843
+ offset += block.content.reduce((size, inline) => size + (inline.type === "text" ? inline.text.length : 1), 0) + 1;
844
+ return selection.from < offset && selection.to >= start ? { ...block, type } : block;
845
+ })
846
+ };
847
+ };
848
+ var richTextMarksAt = (value, position) => {
849
+ const content = units(value);
850
+ const unit = content[Math.max(0, position - 1)];
851
+ return unit && unit.type !== "break" ? unit.marks ?? {} : {};
852
+ };
853
+
854
+ // ../../src/lib/rich-text/dom.ts
855
+ import { Option, Schema as Schema3 } from "effect";
856
+ var blocks = new Set(["P", "DIV", "H1", "H2", "H3", "LI"]);
857
+ var sameMarks2 = Schema3.toEquivalence(RichTextMarks);
858
+ var pointAfter = (node) => ({
859
+ node: node.parentNode ?? node,
860
+ offset: Array.from(node.parentNode?.childNodes ?? []).indexOf(node) + 1
861
+ });
862
+ var readRichTextDOM = (root) => {
863
+ const result = [];
864
+ const points = [{ node: root, offset: 0 }];
865
+ let current;
866
+ const begin = (type, point) => {
867
+ if (current)
868
+ result.push(current);
869
+ if (result.length)
870
+ points.push(point);
871
+ else
872
+ points[0] = point;
873
+ current = { type, content: [] };
874
+ };
875
+ const append = (inline, point) => {
876
+ if (!current)
877
+ begin("paragraph", point);
878
+ const previous = current?.content.at(-1);
879
+ if (inline.type === "text" && previous?.type === "text" && sameMarks2(inline.marks ?? {}, previous.marks ?? {})) {
880
+ if (current)
881
+ current.content[current.content.length - 1] = {
882
+ ...previous,
883
+ text: previous.text + inline.text
884
+ };
885
+ } else
886
+ current?.content.push(inline);
887
+ points.push(point);
888
+ };
889
+ const visit = (node, inherited = {}, kind = "paragraph") => {
890
+ if (node instanceof Text) {
891
+ if (!current)
892
+ begin(kind, { node, offset: 0 });
893
+ for (let index = 0;index < node.data.length; index++)
894
+ append({ type: "text", text: node.data[index] ?? "", marks: inherited }, { node, offset: index + 1 });
895
+ return;
896
+ }
897
+ if (!(node instanceof HTMLElement) || ["SCRIPT", "STYLE", "IFRAME"].includes(node.tagName))
898
+ return;
899
+ const key = node.dataset.richVariable;
900
+ if (key) {
901
+ const variable = { type: "variable", key, marks: inherited };
902
+ append(variable, pointAfter(node));
903
+ return;
904
+ }
905
+ const href = node.getAttribute("href");
906
+ const marks = {
907
+ bold: inherited.bold,
908
+ italic: inherited.italic,
909
+ underline: inherited.underline,
910
+ href: inherited.href
911
+ };
912
+ if (["B", "STRONG"].includes(node.tagName) || node.style.fontWeight === "700")
913
+ marks.bold = true;
914
+ if (["I", "EM"].includes(node.tagName) || node.style.fontStyle === "italic")
915
+ marks.italic = true;
916
+ if (node.tagName === "U" || node.style.textDecoration.includes("underline"))
917
+ marks.underline = true;
918
+ if (href && Schema3.is(RichTextLink)(href))
919
+ marks.href = href;
920
+ const type = node.dataset.richKind === "bullet" ? "bullet" : node.dataset.richKind === "numbered" ? "numbered" : /^H[123]$/.test(node.tagName) ? "heading" : node.tagName === "LI" ? node.parentElement?.tagName === "OL" ? "numbered" : "bullet" : kind;
921
+ const isBlock = blocks.has(node.tagName);
922
+ const container = isBlock && [...node.children].some((child) => blocks.has(child.tagName));
923
+ if (isBlock && !container)
924
+ begin(type, { node, offset: 0 });
925
+ if (node.tagName === "BR") {
926
+ if (node.nextSibling)
927
+ begin(kind, pointAfter(node));
928
+ return;
929
+ }
930
+ for (const child of node.childNodes)
931
+ visit(child, marks, type);
932
+ };
933
+ for (const child of root.childNodes)
934
+ visit(child);
935
+ if (current)
936
+ result.push(current);
937
+ const value = Option.getOrUndefined(Schema3.decodeOption(RichTextDocument)({
938
+ version: 1,
939
+ blocks: result.length ? result : emptyRichText().blocks
940
+ }));
941
+ return { value, points };
942
+ };
943
+ var readEditorSelection = (root, points) => {
944
+ const selection = root.ownerDocument.getSelection();
945
+ if (!selection?.anchorNode || !selection.focusNode || !root.contains(selection.anchorNode) || !root.contains(selection.focusNode))
946
+ return;
947
+ const position = (node, offset) => {
948
+ const range = root.ownerDocument.createRange();
949
+ range.setStart(node, offset);
950
+ range.collapse(true);
951
+ let result = 0;
952
+ points.forEach((point, index) => {
953
+ if (root.contains(point.node) && range.comparePoint(point.node, point.offset) <= 0)
954
+ result = index;
955
+ });
956
+ return result;
957
+ };
958
+ const anchor = position(selection.anchorNode, selection.anchorOffset);
959
+ const focus = position(selection.focusNode, selection.focusOffset);
960
+ return { from: Math.min(anchor, focus), to: Math.max(anchor, focus) };
961
+ };
962
+ var restoreRichTextSelection = (root, selection) => {
963
+ const { points } = readRichTextDOM(root);
964
+ const start = points[Math.min(selection.from, points.length - 1)];
965
+ const end = points[Math.min(selection.to, points.length - 1)];
966
+ if (!start || !end)
967
+ return;
968
+ const range = root.ownerDocument.createRange();
969
+ range.setStart(start.node, start.offset);
970
+ range.setEnd(end.node, end.offset);
971
+ root.ownerDocument.getSelection()?.removeAllRanges();
972
+ root.ownerDocument.getSelection()?.addRange(range);
973
+ };
974
+ var writeRichTextDOM = (root, value, variables) => {
975
+ const children = value.blocks.map((block) => {
976
+ const paragraph = root.ownerDocument.createElement(block.type === "heading" ? "h2" : "div");
977
+ if (block.type === "heading")
978
+ paragraph.style.cssText = "font-size:1.4em;font-weight:700";
979
+ if (block.type === "bullet" || block.type === "numbered") {
980
+ paragraph.dataset.richKind = block.type;
981
+ paragraph.style.cssText = `display:list-item;margin-left:1.5em;list-style-type:${block.type === "bullet" ? "disc" : "decimal"}`;
982
+ }
983
+ for (const inline of block.content) {
984
+ const span = root.ownerDocument.createElement(inline.marks?.href ? "a" : "span");
985
+ if (inline.type === "variable") {
986
+ span.dataset.richVariable = inline.key;
987
+ span.contentEditable = "false";
988
+ span.textContent = `{{${variables.find((item) => item.key === inline.key)?.label ?? inline.key}}}`;
989
+ span.className = "mx-0.5 inline-block rounded bg-indigo-50 px-1 font-mono text-[0.85em] text-indigo-800";
990
+ } else
991
+ span.textContent = inline.text;
992
+ if (inline.marks?.bold)
993
+ span.style.fontWeight = "700";
994
+ if (inline.marks?.italic)
995
+ span.style.fontStyle = "italic";
996
+ if (inline.marks?.underline)
997
+ span.style.textDecoration = "underline";
998
+ if (inline.marks?.href) {
999
+ span.setAttribute("href", inline.marks.href);
1000
+ span.style.textDecoration = "underline";
1001
+ }
1002
+ paragraph.append(span);
1003
+ }
1004
+ if (!block.content.length)
1005
+ paragraph.append(root.ownerDocument.createElement("br"));
1006
+ return paragraph;
1007
+ });
1008
+ root.replaceChildren(...children);
1009
+ };
1010
+
1011
+ // ../../src/components/ui/rich-text-editor.tsx
1012
+ import { jsx as jsx7, jsxs as jsxs4, Fragment as Fragment2 } from "react/jsx-runtime";
1013
+ var labels2 = {
1014
+ en: {
1015
+ bold: "Bold",
1016
+ italic: "Italic",
1017
+ underline: "Underline",
1018
+ undo: "Undo",
1019
+ redo: "Redo",
1020
+ link: "Link",
1021
+ unlink: "Remove link",
1022
+ linkUrl: "Link URL",
1023
+ apply: "Apply link",
1024
+ variable: "Insert variable",
1025
+ paragraph: "Paragraph",
1026
+ heading: "Heading",
1027
+ bullet: "Bulleted list",
1028
+ numbered: "Numbered list",
1029
+ toolbar: "Text formatting",
1030
+ invalid: "The content is too long or contains an unsupported value.",
1031
+ invalidLink: "Enter an HTTPS or mailto link.",
1032
+ cancel: "Cancel"
1033
+ },
1034
+ fr: {
1035
+ bold: "Gras",
1036
+ italic: "Italique",
1037
+ underline: "Souligné",
1038
+ undo: "Annuler",
1039
+ redo: "Rétablir",
1040
+ link: "Lien",
1041
+ unlink: "Retirer le lien",
1042
+ linkUrl: "URL du lien",
1043
+ apply: "Appliquer le lien",
1044
+ variable: "Insérer une variable",
1045
+ paragraph: "Paragraphe",
1046
+ heading: "Titre",
1047
+ bullet: "Liste à puces",
1048
+ numbered: "Liste numérotée",
1049
+ toolbar: "Mise en forme",
1050
+ invalid: "Le contenu est trop long ou contient une valeur non prise en charge.",
1051
+ invalidLink: "Saisissez un lien HTTPS ou mailto.",
1052
+ cancel: "Annuler"
1053
+ }
1054
+ };
1055
+ var equivalent = Schema4.toEquivalence(RichTextDocument);
1056
+ var ToolButton = ({
1057
+ label,
1058
+ children,
1059
+ onClick,
1060
+ pressed,
1061
+ disabled
1062
+ }) => /* @__PURE__ */ jsx7(Button, {
1063
+ type: "button",
1064
+ size: "icon-sm",
1065
+ variant: pressed ? "secondary" : "ghost",
1066
+ "aria-label": label,
1067
+ title: label,
1068
+ "aria-pressed": pressed,
1069
+ disabled,
1070
+ onPointerDown: (event) => event.preventDefault(),
1071
+ onClick,
1072
+ children
1073
+ });
1074
+ var inlineContent = (inline, variables, values) => {
1075
+ let content = inline.type === "text" ? inline.text : values?.[inline.key] ?? /* @__PURE__ */ jsx7("span", {
1076
+ contentEditable: false,
1077
+ "data-rich-variable": inline.key,
1078
+ className: "inline-block rounded bg-indigo-50 px-1.5 font-mono text-[0.85em] text-indigo-800 select-all",
1079
+ children: `{{${variables.find((variable) => variable.key === inline.key)?.label ?? inline.key}}}`
1080
+ });
1081
+ if (inline.marks?.bold)
1082
+ content = /* @__PURE__ */ jsx7("strong", {
1083
+ children: content
1084
+ });
1085
+ if (inline.marks?.italic)
1086
+ content = /* @__PURE__ */ jsx7("em", {
1087
+ children: content
1088
+ });
1089
+ if (inline.marks?.underline)
1090
+ content = /* @__PURE__ */ jsx7("u", {
1091
+ children: content
1092
+ });
1093
+ if (inline.marks?.href)
1094
+ content = /* @__PURE__ */ jsx7("a", {
1095
+ href: inline.marks.href,
1096
+ className: "underline",
1097
+ children: content
1098
+ });
1099
+ return content;
1100
+ };
1101
+ var RichTextContent = ({
1102
+ value,
1103
+ variables = [],
1104
+ values
1105
+ }) => /* @__PURE__ */ jsx7(Fragment2, {
1106
+ children: value.blocks.map((block, index) => {
1107
+ const content = block.content.map((inline, key) => /* @__PURE__ */ jsx7("span", {
1108
+ children: inlineContent(inline, variables, values)
1109
+ }, key));
1110
+ if (block.type === "heading")
1111
+ return /* @__PURE__ */ jsx7("h2", {
1112
+ className: "text-xl font-semibold",
1113
+ children: content.length ? content : /* @__PURE__ */ jsx7("br", {})
1114
+ }, index);
1115
+ return /* @__PURE__ */ jsx7("div", {
1116
+ "data-rich-kind": block.type,
1117
+ style: block.type === "bullet" || block.type === "numbered" ? {
1118
+ display: "list-item",
1119
+ marginLeft: "1.5em",
1120
+ listStyleType: block.type === "bullet" ? "disc" : "decimal"
1121
+ } : undefined,
1122
+ children: content.length ? content : /* @__PURE__ */ jsx7("br", {})
1123
+ }, index);
1124
+ })
1125
+ });
1126
+ var RichTextEditor = ({
1127
+ value,
1128
+ onChange,
1129
+ label,
1130
+ variables = [],
1131
+ disabled,
1132
+ locale: override,
1133
+ messages,
1134
+ className,
1135
+ contentStyle,
1136
+ singleLine = false,
1137
+ maxLength
1138
+ }) => {
1139
+ const locale = useKrakstackLocale2(override);
1140
+ const copy = {
1141
+ ...locale.startsWith("fr") ? labels2.fr : labels2.en,
1142
+ ...messages?.[locale.split("-")[0] ?? locale],
1143
+ ...messages?.[locale]
1144
+ };
1145
+ const root = useRef3(null);
1146
+ const model = useRef3(value);
1147
+ const composing = useRef3(false);
1148
+ const selected = useRef3({ from: 0, to: 0 });
1149
+ const history = useRef3([
1150
+ { value, selection: { from: 0, to: 0 } }
1151
+ ]);
1152
+ const cursor = useRef3(0);
1153
+ const [marks, setMarks] = useState3({});
1154
+ const pendingMarks = useRef3(null);
1155
+ const [error, setError] = useState3(false);
1156
+ const [linkOpen, setLinkOpen] = useState3(false);
1157
+ const [link, setLink] = useState3("");
1158
+ const [linkError, setLinkError] = useState3(false);
1159
+ const [revision, setRevision] = useState3(0);
1160
+ const [historyState, setHistoryState] = useState3({
1161
+ undo: false,
1162
+ redo: false
1163
+ });
1164
+ const id = useId2();
1165
+ const [initial] = useState3(() => /* @__PURE__ */ jsx7(RichTextContent, {
1166
+ value,
1167
+ variables
1168
+ }));
1169
+ const selection = () => {
1170
+ const element = root.current;
1171
+ if (!element)
1172
+ return selected.current;
1173
+ const next = readEditorSelection(element, readRichTextDOM(element).points);
1174
+ if (next)
1175
+ selected.current = next;
1176
+ return selected.current;
1177
+ };
1178
+ const focusEditor = () => {
1179
+ const element = root.current;
1180
+ if (element) {
1181
+ element.focus();
1182
+ restoreRichTextSelection(element, selected.current);
1183
+ }
1184
+ return false;
1185
+ };
1186
+ const commit = (next, range, write = true, record = true) => {
1187
+ const element = root.current;
1188
+ if (!element || disabled)
1189
+ return;
1190
+ const original = next;
1191
+ if (singleLine) {
1192
+ next = {
1193
+ version: 1,
1194
+ blocks: [
1195
+ {
1196
+ type: "paragraph",
1197
+ content: next.blocks.flatMap((block, index) => [
1198
+ ...index ? [{ type: "text", text: " " }] : [],
1199
+ ...block.content.map((inline) => inline.type === "variable" ? { type: "variable", key: inline.key } : {
1200
+ type: "text",
1201
+ text: inline.text.replace(/[\r\n]+/g, " ")
1202
+ })
1203
+ ])
1204
+ }
1205
+ ]
1206
+ };
1207
+ if (!equivalent(original, next))
1208
+ write = true;
1209
+ }
1210
+ if (!Schema4.is(RichTextDocument)(next) || maxLength !== undefined && richTextPlainText(next).length > maxLength) {
1211
+ setError(true);
1212
+ writeRichTextDOM(element, model.current, variables);
1213
+ element.focus();
1214
+ restoreRichTextSelection(element, selected.current);
1215
+ return;
1216
+ }
1217
+ setError(false);
1218
+ if (record && !equivalent(model.current, next)) {
1219
+ history.current[cursor.current] = {
1220
+ value: model.current,
1221
+ selection: selected.current
1222
+ };
1223
+ history.current = [
1224
+ ...history.current.slice(0, cursor.current + 1),
1225
+ { value: next, selection: range }
1226
+ ].slice(-100);
1227
+ cursor.current = history.current.length - 1;
1228
+ }
1229
+ model.current = next;
1230
+ selected.current = range;
1231
+ if (write) {
1232
+ writeRichTextDOM(element, next, variables);
1233
+ element.focus();
1234
+ restoreRichTextSelection(element, range);
1235
+ }
1236
+ setRevision((current) => current + 1);
1237
+ setHistoryState({
1238
+ undo: cursor.current > 0,
1239
+ redo: cursor.current < history.current.length - 1
1240
+ });
1241
+ onChange(next);
1242
+ };
1243
+ const travel = (direction) => {
1244
+ const index = Math.max(0, Math.min(history.current.length - 1, cursor.current + direction));
1245
+ const snapshot = history.current[index];
1246
+ if (!snapshot || index === cursor.current)
1247
+ return;
1248
+ cursor.current = index;
1249
+ pendingMarks.current = null;
1250
+ commit(snapshot.value, snapshot.selection, true, false);
1251
+ };
1252
+ const insert = (inline) => {
1253
+ const result = replaceRichText(model.current, selection(), inline);
1254
+ commit(result.value, result.selection);
1255
+ };
1256
+ const format = (mark) => {
1257
+ const range = selection();
1258
+ const next = {
1259
+ ...pendingMarks.current ?? richTextMarksAt(model.current, range.to),
1260
+ [mark]: !marks[mark]
1261
+ };
1262
+ setMarks(next);
1263
+ if (range.from === range.to) {
1264
+ pendingMarks.current = next;
1265
+ root.current?.focus();
1266
+ } else
1267
+ commit(formatRichText(model.current, range, mark, next[mark]), range);
1268
+ };
1269
+ const syncInput = () => {
1270
+ const element = root.current;
1271
+ if (!element || composing.current)
1272
+ return;
1273
+ const parsed = readRichTextDOM(element);
1274
+ const range = readEditorSelection(element, parsed.points) ?? selected.current;
1275
+ if (parsed.value)
1276
+ commit(parsed.value, range, false);
1277
+ else {
1278
+ setError(true);
1279
+ writeRichTextDOM(element, model.current, variables);
1280
+ restoreRichTextSelection(element, selected.current);
1281
+ }
1282
+ };
1283
+ const beforeInput = useEffectEvent3((event) => {
1284
+ if (disabled || composing.current || event.isComposing)
1285
+ return;
1286
+ if (event.inputType === "historyUndo" || event.inputType === "historyRedo") {
1287
+ event.preventDefault();
1288
+ travel(event.inputType === "historyUndo" ? -1 : 1);
1289
+ } else if (["insertText", "insertParagraph", "insertLineBreak"].includes(event.inputType)) {
1290
+ event.preventDefault();
1291
+ if (singleLine && event.inputType !== "insertText")
1292
+ return;
1293
+ const range = selection();
1294
+ insert({
1295
+ type: "text",
1296
+ text: event.inputType === "insertText" ? event.data ?? "" : `
1297
+ `,
1298
+ marks: pendingMarks.current ?? richTextMarksAt(model.current, range.to)
1299
+ });
1300
+ } else if (event.inputType === "formatBold" || event.inputType === "formatItalic" || event.inputType === "formatUnderline") {
1301
+ event.preventDefault();
1302
+ if (!singleLine)
1303
+ format(event.inputType === "formatBold" ? "bold" : event.inputType === "formatItalic" ? "italic" : "underline");
1304
+ }
1305
+ });
1306
+ useEffect3(() => {
1307
+ const element = root.current;
1308
+ if (!element)
1309
+ return;
1310
+ const listener = (event) => beforeInput(event);
1311
+ element.addEventListener("beforeinput", listener);
1312
+ return () => element.removeEventListener("beforeinput", listener);
1313
+ }, []);
1314
+ const remember = useEffectEvent3(() => {
1315
+ const element = root.current;
1316
+ if (!element || document.activeElement !== element || composing.current)
1317
+ return;
1318
+ const previous = selected.current;
1319
+ const range = selection();
1320
+ if (previous.from !== range.from || previous.to !== range.to)
1321
+ pendingMarks.current = null;
1322
+ setMarks(pendingMarks.current ?? richTextMarksAt(model.current, range.to));
1323
+ });
1324
+ useEffect3(() => {
1325
+ document.addEventListener("selectionchange", remember);
1326
+ return () => document.removeEventListener("selectionchange", remember);
1327
+ }, []);
1328
+ useEffect3(() => {
1329
+ const element = root.current;
1330
+ if (!element || composing.current)
1331
+ return;
1332
+ if (!equivalent(model.current, value)) {
1333
+ model.current = value;
1334
+ history.current = [{ value, selection: { from: 0, to: 0 } }];
1335
+ cursor.current = 0;
1336
+ selected.current = { from: 0, to: 0 };
1337
+ writeRichTextDOM(element, value, variables);
1338
+ setRevision((current) => current + 1);
1339
+ setHistoryState({ undo: false, redo: false });
1340
+ }
1341
+ }, [value, variables]);
1342
+ return /* @__PURE__ */ jsxs4("div", {
1343
+ "data-slot": "rich-text-editor",
1344
+ className: `bg-background text-foreground overflow-hidden rounded-xl border ${className ?? ""}`,
1345
+ "data-revision": revision,
1346
+ children: [
1347
+ /* @__PURE__ */ jsxs4("div", {
1348
+ role: "group",
1349
+ "aria-label": copy.toolbar,
1350
+ className: "bg-muted/30 flex flex-wrap items-center gap-1 border-b p-2",
1351
+ children: [
1352
+ /* @__PURE__ */ jsx7(ToolButton, {
1353
+ label: copy.undo,
1354
+ disabled: disabled || !historyState.undo,
1355
+ onClick: () => travel(-1),
1356
+ children: /* @__PURE__ */ jsx7(Undo2, {})
1357
+ }),
1358
+ /* @__PURE__ */ jsx7(ToolButton, {
1359
+ label: copy.redo,
1360
+ disabled: disabled || !historyState.redo,
1361
+ onClick: () => travel(1),
1362
+ children: /* @__PURE__ */ jsx7(Redo2, {})
1363
+ }),
1364
+ !singleLine ? /* @__PURE__ */ jsxs4(Fragment2, {
1365
+ children: [
1366
+ /* @__PURE__ */ jsx7("span", {
1367
+ className: "mx-1 h-5 border-l",
1368
+ "aria-hidden": true
1369
+ }),
1370
+ /* @__PURE__ */ jsxs4(Select, {
1371
+ disabled,
1372
+ defaultValue: "paragraph",
1373
+ items: [
1374
+ { value: "paragraph", label: copy.paragraph },
1375
+ { value: "heading", label: copy.heading },
1376
+ { value: "bullet", label: copy.bullet },
1377
+ { value: "numbered", label: copy.numbered }
1378
+ ],
1379
+ onOpenChange: (open) => {
1380
+ if (open)
1381
+ selection();
1382
+ },
1383
+ onValueChange: (value) => {
1384
+ const type = Schema4.decodeUnknownSync(RichTextBlockKind)(value);
1385
+ const range = selection();
1386
+ commit(setRichTextBlock(model.current, range, type), range);
1387
+ },
1388
+ children: [
1389
+ /* @__PURE__ */ jsx7(SelectTrigger, {
1390
+ size: "sm",
1391
+ "aria-label": copy.paragraph,
1392
+ className: "max-w-40 text-xs",
1393
+ onPointerDown: selection,
1394
+ onFocus: selection,
1395
+ children: /* @__PURE__ */ jsx7(SelectValue, {})
1396
+ }),
1397
+ /* @__PURE__ */ jsxs4(SelectContent, {
1398
+ align: "start",
1399
+ alignItemWithTrigger: false,
1400
+ finalFocus: focusEditor,
1401
+ children: [
1402
+ /* @__PURE__ */ jsx7(SelectItem, {
1403
+ value: "paragraph",
1404
+ children: copy.paragraph
1405
+ }),
1406
+ /* @__PURE__ */ jsx7(SelectItem, {
1407
+ value: "heading",
1408
+ children: copy.heading
1409
+ }),
1410
+ /* @__PURE__ */ jsx7(SelectItem, {
1411
+ value: "bullet",
1412
+ children: copy.bullet
1413
+ }),
1414
+ /* @__PURE__ */ jsx7(SelectItem, {
1415
+ value: "numbered",
1416
+ children: copy.numbered
1417
+ })
1418
+ ]
1419
+ })
1420
+ ]
1421
+ }),
1422
+ /* @__PURE__ */ jsx7(ToolButton, {
1423
+ label: copy.bold,
1424
+ disabled,
1425
+ pressed: !!marks.bold,
1426
+ onClick: () => format("bold"),
1427
+ children: /* @__PURE__ */ jsx7(Bold, {})
1428
+ }),
1429
+ /* @__PURE__ */ jsx7(ToolButton, {
1430
+ label: copy.italic,
1431
+ disabled,
1432
+ pressed: !!marks.italic,
1433
+ onClick: () => format("italic"),
1434
+ children: /* @__PURE__ */ jsx7(Italic, {})
1435
+ }),
1436
+ /* @__PURE__ */ jsx7(ToolButton, {
1437
+ label: copy.underline,
1438
+ disabled,
1439
+ pressed: !!marks.underline,
1440
+ onClick: () => format("underline"),
1441
+ children: /* @__PURE__ */ jsx7(Underline, {})
1442
+ }),
1443
+ /* @__PURE__ */ jsx7(ToolButton, {
1444
+ label: copy.link,
1445
+ disabled,
1446
+ pressed: linkOpen || !!marks.href,
1447
+ onClick: () => {
1448
+ selection();
1449
+ setLink(marks.href ?? "");
1450
+ setLinkError(false);
1451
+ setLinkOpen(!linkOpen);
1452
+ },
1453
+ children: /* @__PURE__ */ jsx7(Link2, {})
1454
+ })
1455
+ ]
1456
+ }) : null,
1457
+ variables.length ? /* @__PURE__ */ jsxs4(DropdownMenu, {
1458
+ onOpenChange: (open) => {
1459
+ if (open)
1460
+ selection();
1461
+ },
1462
+ children: [
1463
+ /* @__PURE__ */ jsxs4(DropdownMenuTrigger, {
1464
+ disabled,
1465
+ onPointerDown: selection,
1466
+ onFocus: selection,
1467
+ render: /* @__PURE__ */ jsx7(Button, {
1468
+ type: "button",
1469
+ variant: "outline",
1470
+ size: "sm",
1471
+ className: "ml-auto text-xs"
1472
+ }),
1473
+ children: [
1474
+ /* @__PURE__ */ jsx7(Braces, {
1475
+ className: "size-3.5",
1476
+ "aria-hidden": true
1477
+ }),
1478
+ copy.variable,
1479
+ /* @__PURE__ */ jsx7(ChevronDown, {
1480
+ className: "size-3.5",
1481
+ "aria-hidden": true
1482
+ })
1483
+ ]
1484
+ }),
1485
+ /* @__PURE__ */ jsx7(DropdownMenuContent, {
1486
+ align: "end",
1487
+ finalFocus: focusEditor,
1488
+ children: variables.map((variable) => /* @__PURE__ */ jsx7(DropdownMenuItem, {
1489
+ onClick: () => insert({ type: "variable", key: variable.key }),
1490
+ children: variable.label
1491
+ }, variable.key))
1492
+ })
1493
+ ]
1494
+ }) : null
1495
+ ]
1496
+ }),
1497
+ linkOpen ? /* @__PURE__ */ jsxs4("div", {
1498
+ className: "flex flex-wrap items-end gap-2 border-b p-3",
1499
+ children: [
1500
+ /* @__PURE__ */ jsxs4("label", {
1501
+ className: "grid min-w-0 flex-1 gap-1 text-xs",
1502
+ children: [
1503
+ copy.linkUrl,
1504
+ /* @__PURE__ */ jsx7(Input, {
1505
+ value: link,
1506
+ "aria-invalid": linkError,
1507
+ placeholder: "https://",
1508
+ onChange: (event) => setLink(event.target.value)
1509
+ })
1510
+ ]
1511
+ }),
1512
+ /* @__PURE__ */ jsx7(Button, {
1513
+ type: "button",
1514
+ size: "sm",
1515
+ disabled,
1516
+ onClick: () => {
1517
+ if (!Schema4.is(RichTextLink)(link)) {
1518
+ setLinkError(true);
1519
+ return;
1520
+ }
1521
+ commit(formatRichText(model.current, selected.current, "href", link), selected.current);
1522
+ setLinkOpen(false);
1523
+ },
1524
+ children: copy.apply
1525
+ }),
1526
+ marks.href ? /* @__PURE__ */ jsxs4(Button, {
1527
+ type: "button",
1528
+ size: "sm",
1529
+ variant: "outline",
1530
+ disabled,
1531
+ onClick: () => {
1532
+ commit(formatRichText(model.current, selected.current, "href", undefined), selected.current);
1533
+ setLinkOpen(false);
1534
+ setLinkError(false);
1535
+ },
1536
+ children: [
1537
+ /* @__PURE__ */ jsx7(Unlink, {
1538
+ "aria-hidden": true
1539
+ }),
1540
+ copy.unlink
1541
+ ]
1542
+ }) : null,
1543
+ /* @__PURE__ */ jsx7(Button, {
1544
+ type: "button",
1545
+ size: "sm",
1546
+ variant: "ghost",
1547
+ onClick: () => setLinkOpen(false),
1548
+ children: copy.cancel
1549
+ }),
1550
+ linkError ? /* @__PURE__ */ jsx7("p", {
1551
+ role: "alert",
1552
+ className: "text-destructive w-full text-xs",
1553
+ children: copy.invalidLink
1554
+ }) : null
1555
+ ]
1556
+ }) : null,
1557
+ /* @__PURE__ */ jsx7("div", {
1558
+ ref: root,
1559
+ style: contentStyle,
1560
+ role: "textbox",
1561
+ "aria-label": label,
1562
+ "aria-multiline": !singleLine,
1563
+ "aria-disabled": disabled,
1564
+ "aria-describedby": error ? `${id}-error` : undefined,
1565
+ contentEditable: !disabled,
1566
+ suppressContentEditableWarning: true,
1567
+ className: `focus-visible:ring-ring ${singleLine ? "min-h-10" : "min-h-32"} px-4 py-3 text-sm leading-relaxed outline-none focus-visible:ring-2 focus-visible:ring-inset [&>div]:min-h-[1.5em]`,
1568
+ onInput: syncInput,
1569
+ onKeyUp: selection,
1570
+ onPointerUp: selection,
1571
+ onBlur: selection,
1572
+ onCompositionStart: () => {
1573
+ composing.current = true;
1574
+ },
1575
+ onCompositionEnd: () => {
1576
+ composing.current = false;
1577
+ syncInput();
1578
+ },
1579
+ onPaste: (event) => {
1580
+ event.preventDefault();
1581
+ const text = event.clipboardData.getData("text/plain");
1582
+ insert({
1583
+ type: "text",
1584
+ text: singleLine ? text.replace(/[\r\n]+/g, " ") : text
1585
+ });
1586
+ },
1587
+ onDrop: (event) => event.preventDefault(),
1588
+ onClick: (event) => {
1589
+ if (event.target instanceof HTMLElement && event.target.closest("a"))
1590
+ event.preventDefault();
1591
+ },
1592
+ onKeyDown: (event) => {
1593
+ if (event.nativeEvent.isComposing)
1594
+ return;
1595
+ if ((event.ctrlKey || event.metaKey) && !event.altKey) {
1596
+ const key = event.key.toLowerCase();
1597
+ if (["b", "i", "u", "z", "y"].includes(key)) {
1598
+ event.preventDefault();
1599
+ if (key === "z" || key === "y")
1600
+ travel(key === "y" || event.shiftKey ? 1 : -1);
1601
+ else if (!singleLine)
1602
+ format(key === "b" ? "bold" : key === "i" ? "italic" : "underline");
1603
+ }
1604
+ }
1605
+ },
1606
+ children: initial
1607
+ }),
1608
+ error ? /* @__PURE__ */ jsx7("p", {
1609
+ id: `${id}-error`,
1610
+ role: "alert",
1611
+ className: "text-destructive px-4 pb-3 text-xs",
1612
+ children: copy.invalid
1613
+ }) : null
1614
+ ]
1615
+ });
1616
+ };
1617
+
1618
+ // ../../src/components/ui/tabs.tsx
1619
+ import { Tabs as TabsPrimitive } from "@base-ui/react/tabs";
1620
+ import { cva as cva2 } from "class-variance-authority";
1621
+ import { jsx as jsx8 } from "react/jsx-runtime";
1622
+ function Tabs({
1623
+ className,
1624
+ orientation = "horizontal",
1625
+ ...props
1626
+ }) {
1627
+ return /* @__PURE__ */ jsx8(TabsPrimitive.Root, {
1628
+ "data-slot": "tabs",
1629
+ "data-orientation": orientation,
1630
+ className: cn("group/tabs flex gap-2 data-horizontal:flex-col", className),
1631
+ ...props
1632
+ });
1633
+ }
1634
+ var tabsListVariants = cva2("group/tabs-list inline-flex w-fit items-center justify-center rounded-lg p-[3px] text-muted-foreground group-data-horizontal/tabs:h-9 group-data-vertical/tabs:h-fit group-data-vertical/tabs:flex-col data-[variant=line]:rounded-none", {
1635
+ variants: {
1636
+ variant: {
1637
+ default: "bg-muted",
1638
+ line: "gap-1 bg-transparent"
1639
+ }
1640
+ },
1641
+ defaultVariants: {
1642
+ variant: "default"
1643
+ }
1644
+ });
1645
+ function TabsList({
1646
+ className,
1647
+ variant = "default",
1648
+ ...props
1649
+ }) {
1650
+ return /* @__PURE__ */ jsx8(TabsPrimitive.List, {
1651
+ "data-slot": "tabs-list",
1652
+ "data-variant": variant,
1653
+ className: cn(tabsListVariants({ variant }), className),
1654
+ ...props
1655
+ });
1656
+ }
1657
+ function TabsTrigger({ className, ...props }) {
1658
+ return /* @__PURE__ */ jsx8(TabsPrimitive.Tab, {
1659
+ "data-slot": "tabs-trigger",
1660
+ className: cn("relative inline-flex h-[calc(100%-1px)] flex-1 items-center justify-center gap-1.5 rounded-md border border-transparent px-2 py-1 text-sm font-medium whitespace-nowrap text-foreground/60 transition-all group-data-vertical/tabs:w-full group-data-vertical/tabs:justify-start hover:text-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:outline-1 focus-visible:outline-ring disabled:pointer-events-none disabled:opacity-50 has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 aria-disabled:pointer-events-none aria-disabled:opacity-50 dark:text-muted-foreground dark:hover:text-foreground group-data-[variant=default]/tabs-list:data-active:shadow-sm group-data-[variant=line]/tabs-list:data-active:shadow-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", "group-data-[variant=line]/tabs-list:bg-transparent group-data-[variant=line]/tabs-list:data-active:bg-transparent dark:group-data-[variant=line]/tabs-list:data-active:border-transparent dark:group-data-[variant=line]/tabs-list:data-active:bg-transparent", "data-active:bg-background data-active:text-foreground dark:data-active:border-input dark:data-active:bg-input/30 dark:data-active:text-foreground", "after:absolute after:bg-foreground after:opacity-0 after:transition-opacity group-data-horizontal/tabs:after:inset-x-0 group-data-horizontal/tabs:after:bottom-[-5px] group-data-horizontal/tabs:after:h-0.5 group-data-vertical/tabs:after:inset-y-0 group-data-vertical/tabs:after:-right-1 group-data-vertical/tabs:after:w-0.5 group-data-[variant=line]/tabs-list:data-active:after:opacity-100", className),
1661
+ ...props
1662
+ });
1663
+ }
1664
+ function TabsContent({ className, ...props }) {
1665
+ return /* @__PURE__ */ jsx8(TabsPrimitive.Panel, {
1666
+ "data-slot": "tabs-content",
1667
+ className: cn("flex-1 text-sm outline-none", className),
1668
+ ...props
1669
+ });
1670
+ }
1671
+
1672
+ // ../../src/components/ui/email-editor.tsx
1673
+ import { useKrakstackLocale as useKrakstackLocale3 } from "@krak-stack/registry/krakstack-provider";
1674
+
1675
+ // ../../src/lib/email-document/schema.ts
1676
+ import { Schema as Schema5 } from "effect";
1677
+ var EmailImageUrl = RichTextLink.check(Schema5.isPattern(/^https:\/\//)).annotate({ identifier: "EmailImageUrl" });
1678
+ var EmailColor = Schema5.String.check(Schema5.isPattern(/^#[0-9a-fA-F]{6}$/)).annotate({ identifier: "EmailColor" });
1679
+ var EmailAlignment = Schema5.Literals([
1680
+ "left",
1681
+ "center",
1682
+ "right"
1683
+ ]).annotate({ identifier: "EmailAlignment" });
1684
+ var EmailFont = Schema5.Literals(["sans", "serif", "mono"]).annotate({
1685
+ identifier: "EmailFont"
1686
+ });
1687
+ var spacing = Schema5.Int.check(Schema5.isBetween({ minimum: 0, maximum: 96 }));
1688
+ var common = {
1689
+ id: Schema5.NonEmptyString.check(Schema5.isMaxLength(80)),
1690
+ alignment: EmailAlignment,
1691
+ paddingTop: spacing,
1692
+ paddingBottom: spacing
1693
+ };
1694
+ var EmailDestination = Schema5.Union([
1695
+ Schema5.Struct({ type: Schema5.Literal("url"), url: RichTextLink }),
1696
+ Schema5.Struct({
1697
+ type: Schema5.Literal("variable"),
1698
+ key: RichTextVariable.fields.key
1699
+ })
1700
+ ]).annotate({ identifier: "EmailDestination" });
1701
+ var EmailElement = Schema5.Union([
1702
+ Schema5.Struct({
1703
+ ...common,
1704
+ type: Schema5.Literal("text"),
1705
+ content: RichTextDocument
1706
+ }),
1707
+ Schema5.Struct({
1708
+ ...common,
1709
+ type: Schema5.Literal("button"),
1710
+ label: Schema5.String.check(Schema5.isMaxLength(200)),
1711
+ destination: EmailDestination
1712
+ }),
1713
+ Schema5.Struct({
1714
+ ...common,
1715
+ type: Schema5.Literal("code"),
1716
+ variable: RichTextVariable.fields.key
1717
+ }),
1718
+ Schema5.Struct({
1719
+ ...common,
1720
+ type: Schema5.Literals(["logo", "image"]),
1721
+ src: EmailImageUrl,
1722
+ alt: Schema5.String.check(Schema5.isMaxLength(200)),
1723
+ width: Schema5.Int.check(Schema5.isBetween({ minimum: 32, maximum: 560 }))
1724
+ }),
1725
+ Schema5.Struct({ ...common, type: Schema5.Literal("divider") }),
1726
+ Schema5.Struct({ ...common, type: Schema5.Literal("spacer"), height: spacing })
1727
+ ]).annotate({ identifier: "EmailElement" });
1728
+ var subject = Schema5.String.check(Schema5.isMaxLength(200), Schema5.isPattern(/^[^\r\n]*$/));
1729
+ var EmailDocument = Schema5.Struct({
1730
+ version: Schema5.Literal(1),
1731
+ subject,
1732
+ preview: subject,
1733
+ appearance: Schema5.Struct({
1734
+ background: EmailColor,
1735
+ surface: EmailColor,
1736
+ text: EmailColor,
1737
+ primary: EmailColor,
1738
+ primaryText: EmailColor,
1739
+ font: EmailFont,
1740
+ radius: Schema5.Int.check(Schema5.isBetween({ minimum: 0, maximum: 32 }))
1741
+ }),
1742
+ elements: Schema5.Array(EmailElement).check(Schema5.isMaxLength(40))
1743
+ }).check(Schema5.makeFilter((document2) => new Set(document2.elements.map((element) => element.id)).size === document2.elements.length)).annotate({ identifier: "EmailDocument" });
1744
+ var EmailRenderInput = Schema5.Struct({
1745
+ document: EmailDocument,
1746
+ variables: Schema5.Array(RichTextVariable),
1747
+ values: Schema5.Record(Schema5.String, Schema5.String),
1748
+ requiredVariables: Schema5.optional(Schema5.Array(Schema5.String))
1749
+ }).annotate({ identifier: "EmailRenderInput" });
1750
+ var EmailRenderResult = Schema5.Struct({
1751
+ subject: Schema5.String,
1752
+ html: Schema5.String,
1753
+ text: Schema5.String
1754
+ }).annotate({ identifier: "EmailRenderResult" });
1755
+
1756
+ // ../../src/lib/email-document/model.ts
1757
+ import { Schema as Schema6 } from "effect";
1758
+ var emailElementTypes = [
1759
+ "text",
1760
+ "button",
1761
+ "code",
1762
+ "logo",
1763
+ "image",
1764
+ "divider",
1765
+ "spacer"
1766
+ ];
1767
+ var newEmailElement = (type, input) => {
1768
+ const common = {
1769
+ id: input.id,
1770
+ alignment: "left",
1771
+ paddingTop: 8,
1772
+ paddingBottom: 8
1773
+ };
1774
+ switch (type) {
1775
+ case "text":
1776
+ return { ...common, type, content: richTextFromString(input.text) };
1777
+ case "button":
1778
+ return {
1779
+ ...common,
1780
+ type,
1781
+ label: input.button,
1782
+ destination: input.urlVariable ? { type: "variable", key: input.urlVariable } : { type: "url", url: "https://example.com" }
1783
+ };
1784
+ case "code":
1785
+ return { ...common, type, variable: input.codeVariable ?? "code" };
1786
+ case "logo":
1787
+ case "image":
1788
+ return {
1789
+ ...common,
1790
+ type,
1791
+ src: "https://krakstack.net/favicon.svg",
1792
+ alt: input.imageAlt,
1793
+ width: type === "logo" ? 48 : 160
1794
+ };
1795
+ case "divider":
1796
+ return { ...common, type };
1797
+ case "spacer":
1798
+ return { ...common, type, height: 24 };
1799
+ }
1800
+ };
1801
+ var richTextFromEmailText = (text) => ({
1802
+ version: 1,
1803
+ blocks: [
1804
+ {
1805
+ type: "paragraph",
1806
+ content: text.split(/(\{\{[a-zA-Z][a-zA-Z0-9_.]*\}\})/g).filter(Boolean).map((part) => /^\{\{[a-zA-Z][a-zA-Z0-9_.]*\}\}$/.test(part) ? { type: "variable", key: part.slice(2, -2) } : { type: "text", text: part })
1807
+ }
1808
+ ]
1809
+ });
1810
+ var textReferences = (text) => [...text.matchAll(/\{\{([a-zA-Z][a-zA-Z0-9_.]*)\}\}/g)].map((match) => match[1] ?? "");
1811
+ var emailDocumentReferences = (document2) => [
1812
+ ...new Set([
1813
+ ...textReferences(document2.subject),
1814
+ ...textReferences(document2.preview),
1815
+ ...document2.elements.flatMap((element) => {
1816
+ switch (element.type) {
1817
+ case "text":
1818
+ return element.content.blocks.flatMap((block) => block.content.flatMap((inline) => inline.type === "variable" ? [inline.key] : []));
1819
+ case "code":
1820
+ return [element.variable];
1821
+ case "button":
1822
+ return [
1823
+ ...textReferences(element.label),
1824
+ ...element.destination.type === "variable" ? [element.destination.key] : []
1825
+ ];
1826
+ default:
1827
+ return [];
1828
+ }
1829
+ })
1830
+ ])
1831
+ ];
1832
+ var validateEmailDocument = ({
1833
+ document: document2,
1834
+ variables,
1835
+ values,
1836
+ requiredVariables = []
1837
+ }) => {
1838
+ if (!Schema6.is(EmailDocument)(document2))
1839
+ return [{ code: "invalid-document" }];
1840
+ const references = emailDocumentReferences(document2);
1841
+ const issues = [];
1842
+ for (const key of references) {
1843
+ if (!variables.some((variable) => variable.key === key))
1844
+ issues.push({ code: "unknown-variable", key });
1845
+ if (values && !values[key])
1846
+ issues.push({ code: "missing-variable", key });
1847
+ }
1848
+ for (const element of document2.elements) {
1849
+ if (element.type === "button" && element.destination.type === "variable") {
1850
+ const key = element.destination.key;
1851
+ if (variables.find((variable) => variable.key === key)?.type !== "url" || values && !Schema6.is(RichTextLink)(values[key]))
1852
+ issues.push({ code: "invalid-url", key });
1853
+ }
1854
+ }
1855
+ for (const key of requiredVariables)
1856
+ if (!document2.elements.some((element) => element.type === "code" && element.variable === key || element.type === "button" && element.destination.type === "variable" && element.destination.key === key || element.type === "text" && element.content.blocks.some((block) => block.content.some((inline) => inline.type === "variable" && inline.key === key))))
1857
+ issues.push({ code: "missing-required-variable", key });
1858
+ return issues;
1859
+ };
1860
+
1861
+ // ../../src/components/ui/email-editor.tsx
1862
+ import { jsx as jsx9, jsxs as jsxs5, Fragment as Fragment3 } from "react/jsx-runtime";
1863
+ var EmailEditorSelect = ({
1864
+ label,
1865
+ value,
1866
+ options,
1867
+ disabled,
1868
+ onValueChange
1869
+ }) => /* @__PURE__ */ jsxs5(Select, {
1870
+ items: options,
1871
+ value,
1872
+ disabled,
1873
+ onValueChange: (value) => {
1874
+ if (value !== null)
1875
+ onValueChange(value);
1876
+ },
1877
+ children: [
1878
+ /* @__PURE__ */ jsx9(SelectTrigger, {
1879
+ "aria-label": label,
1880
+ className: "w-full min-w-0 text-xs",
1881
+ children: /* @__PURE__ */ jsx9(SelectValue, {})
1882
+ }),
1883
+ /* @__PURE__ */ jsx9(SelectContent, {
1884
+ align: "start",
1885
+ alignItemWithTrigger: false,
1886
+ children: options.map((option) => /* @__PURE__ */ jsx9(SelectItem, {
1887
+ value: option.value,
1888
+ children: option.label
1889
+ }, option.value))
1890
+ })
1891
+ ]
1892
+ });
1893
+ var labels3 = {
1894
+ en: {
1895
+ edit: "Design",
1896
+ preview: "Preview",
1897
+ subject: "Subject",
1898
+ preheader: "Preview text",
1899
+ add: "Add element",
1900
+ text: "Text",
1901
+ button: "Button",
1902
+ code: "Verification code",
1903
+ logo: "Logo",
1904
+ image: "Image",
1905
+ divider: "Divider",
1906
+ spacer: "Spacer",
1907
+ content: "Email content",
1908
+ properties: "Element settings",
1909
+ select: "Select an element to edit its settings.",
1910
+ alignment: "Alignment",
1911
+ left: "Left",
1912
+ center: "Center",
1913
+ right: "Right",
1914
+ paddingTop: "Space above",
1915
+ paddingBottom: "Space below",
1916
+ width: "Width",
1917
+ height: "Height",
1918
+ src: "Image URL",
1919
+ alt: "Alternative text",
1920
+ label: "Button label",
1921
+ destination: "Destination",
1922
+ url: "Custom URL",
1923
+ variable: "Variable",
1924
+ insertVariable: "Insert variable",
1925
+ close: "Close settings",
1926
+ duplicate: "Duplicate element",
1927
+ remove: "Remove element",
1928
+ background: "Canvas",
1929
+ surface: "Paper",
1930
+ primary: "Accent",
1931
+ primaryText: "Button text",
1932
+ foreground: "Text color",
1933
+ font: "Font",
1934
+ sans: "Sans serif",
1935
+ serif: "Serif",
1936
+ mono: "Monospace",
1937
+ branding: "Branding",
1938
+ invalid: "Fix invalid settings or missing variables before using this email.",
1939
+ newText: "Write your message here.",
1940
+ newButton: "Continue",
1941
+ newImage: "Your image",
1942
+ empty: "Start with an element. Everything can be moved.",
1943
+ editElement: "Edit element",
1944
+ local: "Drag by the grip or use the arrow keys on it. Your changes stay in this editor."
1945
+ },
1946
+ fr: {
1947
+ edit: "Création",
1948
+ preview: "Aperçu",
1949
+ subject: "Objet",
1950
+ preheader: "Texte d’aperçu",
1951
+ add: "Ajouter un élément",
1952
+ text: "Texte",
1953
+ button: "Bouton",
1954
+ code: "Code de vérification",
1955
+ logo: "Logo",
1956
+ image: "Image",
1957
+ divider: "Séparateur",
1958
+ spacer: "Espacement",
1959
+ content: "Contenu du courriel",
1960
+ properties: "Réglages de l’élément",
1961
+ select: "Sélectionnez un élément pour modifier ses réglages.",
1962
+ alignment: "Alignement",
1963
+ left: "Gauche",
1964
+ center: "Centre",
1965
+ right: "Droite",
1966
+ paddingTop: "Espace au-dessus",
1967
+ paddingBottom: "Espace en dessous",
1968
+ width: "Largeur",
1969
+ height: "Hauteur",
1970
+ src: "URL de l’image",
1971
+ alt: "Texte de remplacement",
1972
+ label: "Libellé du bouton",
1973
+ destination: "Destination",
1974
+ url: "URL personnalisée",
1975
+ variable: "Variable",
1976
+ insertVariable: "Insérer une variable",
1977
+ close: "Fermer les réglages",
1978
+ duplicate: "Dupliquer l’élément",
1979
+ remove: "Retirer l’élément",
1980
+ background: "Fond",
1981
+ surface: "Papier",
1982
+ primary: "Accent",
1983
+ primaryText: "Texte du bouton",
1984
+ foreground: "Couleur du texte",
1985
+ font: "Police",
1986
+ sans: "Sans empattement",
1987
+ serif: "Avec empattement",
1988
+ mono: "Chasse fixe",
1989
+ branding: "Identité visuelle",
1990
+ invalid: "Corrigez les réglages invalides ou les variables manquantes avant d’utiliser ce courriel.",
1991
+ newText: "Rédigez votre message ici.",
1992
+ newButton: "Continuer",
1993
+ newImage: "Votre image",
1994
+ empty: "Commencez par un élément. Tout peut être déplacé.",
1995
+ editElement: "Modifier l’élément",
1996
+ local: "Glissez avec la poignée ou utilisez ses touches fléchées. Vos modifications restent dans cet éditeur."
1997
+ }
1998
+ };
1999
+ var icons = {
2000
+ text: Type,
2001
+ button: MousePointer2,
2002
+ code: KeyRound,
2003
+ logo: ScanLine,
2004
+ image: Image,
2005
+ divider: Minus,
2006
+ spacer: Space
2007
+ };
2008
+ var fonts = {
2009
+ sans: "Arial, sans-serif",
2010
+ serif: "Georgia, serif",
2011
+ mono: "'Courier New', monospace"
2012
+ };
2013
+ var EmailEditor = ({
2014
+ value,
2015
+ onChange,
2016
+ variables = [],
2017
+ requiredVariables = [],
2018
+ preview,
2019
+ disabled,
2020
+ locale: override,
2021
+ messages
2022
+ }) => {
2023
+ const locale = useKrakstackLocale3(override);
2024
+ const copy = {
2025
+ ...locale.startsWith("fr") ? labels3.fr : labels3.en,
2026
+ ...messages?.[locale.split("-")[0] ?? locale],
2027
+ ...messages?.[locale]
2028
+ };
2029
+ const [active, setActive] = useState4(value.elements.find((element) => element.type === "text")?.id ?? null);
2030
+ const selected = value.elements.find((element) => element.id === active);
2031
+ const [view, setView] = useState4("design");
2032
+ const canvas = useRef4(null);
2033
+ const elementSurfaces = useRef4(new Map);
2034
+ const [settingsOpen, setSettingsOpen] = useState4(false);
2035
+ const settingsAnchor = useMemo(() => settingsOpen ? {
2036
+ get contextElement() {
2037
+ return canvas.current ?? undefined;
2038
+ },
2039
+ getBoundingClientRect: () => {
2040
+ const canvasBounds = canvas.current?.getBoundingClientRect();
2041
+ const elementBounds = active ? elementSurfaces.current.get(active)?.getBoundingClientRect() : undefined;
2042
+ return new DOMRect(canvasBounds?.x, elementBounds?.y ?? canvasBounds?.y, canvasBounds?.width, elementBounds?.height ?? canvasBounds?.height);
2043
+ }
2044
+ } : null, [active, settingsOpen]);
2045
+ const id = useId3();
2046
+ const [insertOpen, setInsertOpen] = useState4(false);
2047
+ const update = (element) => {
2048
+ if (!disabled)
2049
+ onChange({
2050
+ ...value,
2051
+ elements: value.elements.map((item) => item.id === element.id ? element : item)
2052
+ });
2053
+ };
2054
+ const add = (type) => {
2055
+ const element = newEmailElement(type, {
2056
+ id: crypto.randomUUID(),
2057
+ text: copy.newText,
2058
+ button: copy.newButton,
2059
+ imageAlt: copy.newImage,
2060
+ codeVariable: variables.find((variable) => variable.key === "code")?.key ?? variables.find((variable) => variable.type === "text")?.key,
2061
+ urlVariable: variables.find((variable) => variable.type === "url")?.key
2062
+ });
2063
+ onChange({ ...value, elements: [...value.elements, element] });
2064
+ setActive(element.id);
2065
+ };
2066
+ const field = (label, control) => /* @__PURE__ */ jsxs5("label", {
2067
+ className: "grid min-w-0 gap-1.5 text-xs font-medium",
2068
+ children: [
2069
+ label,
2070
+ control
2071
+ ]
2072
+ });
2073
+ const invalid = validateEmailDocument({ document: value, variables, requiredVariables }).length > 0;
2074
+ const canvasVariables = {
2075
+ "--email-paper": value.appearance.surface,
2076
+ "--email-hover": `color-mix(in srgb, ${value.appearance.surface} 94%, ${value.appearance.text})`
2077
+ };
2078
+ return /* @__PURE__ */ jsxs5("div", {
2079
+ "data-slot": "email-editor",
2080
+ className: "min-w-0 space-y-4",
2081
+ children: [
2082
+ /* @__PURE__ */ jsxs5(Tabs, {
2083
+ value: view,
2084
+ onValueChange: setView,
2085
+ children: [
2086
+ /* @__PURE__ */ jsxs5(TabsList, {
2087
+ children: [
2088
+ /* @__PURE__ */ jsx9(TabsTrigger, {
2089
+ value: "design",
2090
+ children: copy.edit
2091
+ }),
2092
+ /* @__PURE__ */ jsx9(TabsTrigger, {
2093
+ value: "preview",
2094
+ disabled: !preview,
2095
+ children: copy.preview
2096
+ })
2097
+ ]
2098
+ }),
2099
+ /* @__PURE__ */ jsx9(TabsContent, {
2100
+ value: "preview",
2101
+ className: "mt-4",
2102
+ children: preview
2103
+ }),
2104
+ /* @__PURE__ */ jsxs5(TabsContent, {
2105
+ value: "design",
2106
+ className: "mt-4 space-y-4",
2107
+ children: [
2108
+ /* @__PURE__ */ jsx9("div", {
2109
+ className: "grid gap-3",
2110
+ children: ["subject", "preview"].map((key) => {
2111
+ const label = key === "subject" ? copy.subject : copy.preheader;
2112
+ return /* @__PURE__ */ jsxs5("div", {
2113
+ role: "group",
2114
+ "aria-label": label,
2115
+ className: "min-w-0 space-y-1.5",
2116
+ children: [
2117
+ /* @__PURE__ */ jsx9("p", {
2118
+ className: "text-xs font-medium",
2119
+ children: label
2120
+ }),
2121
+ /* @__PURE__ */ jsx9(RichTextEditor, {
2122
+ label,
2123
+ value: richTextFromEmailText(value[key]),
2124
+ onChange: (content) => onChange({ ...value, [key]: richTextPlainText(content) }),
2125
+ variables,
2126
+ locale,
2127
+ disabled,
2128
+ singleLine: true,
2129
+ maxLength: 200
2130
+ })
2131
+ ]
2132
+ }, key);
2133
+ })
2134
+ }),
2135
+ /* @__PURE__ */ jsxs5("div", {
2136
+ className: "bg-muted/20 flex flex-wrap items-center justify-between gap-2 rounded-xl border p-2",
2137
+ children: [
2138
+ /* @__PURE__ */ jsxs5(Popover.Root, {
2139
+ open: insertOpen,
2140
+ onOpenChange: setInsertOpen,
2141
+ children: [
2142
+ /* @__PURE__ */ jsxs5(PopoverTrigger, {
2143
+ id: `${id}-add`,
2144
+ render: /* @__PURE__ */ jsx9(Button, {
2145
+ type: "button",
2146
+ size: "sm",
2147
+ disabled: disabled || value.elements.length >= 40
2148
+ }),
2149
+ children: [
2150
+ /* @__PURE__ */ jsx9(Plus, {
2151
+ className: "size-3.5",
2152
+ "aria-hidden": true
2153
+ }),
2154
+ copy.add,
2155
+ /* @__PURE__ */ jsx9(ChevronDown2, {
2156
+ className: "size-3.5",
2157
+ "aria-hidden": true
2158
+ })
2159
+ ]
2160
+ }),
2161
+ /* @__PURE__ */ jsxs5(PopoverContent, {
2162
+ align: "start",
2163
+ className: "w-64",
2164
+ children: [
2165
+ /* @__PURE__ */ jsx9(PopoverTitle, {
2166
+ children: copy.add
2167
+ }),
2168
+ /* @__PURE__ */ jsx9("div", {
2169
+ className: "grid gap-1",
2170
+ children: emailElementTypes.map((type) => {
2171
+ const Icon = icons[type];
2172
+ return /* @__PURE__ */ jsxs5(Button, {
2173
+ type: "button",
2174
+ variant: "ghost",
2175
+ size: "sm",
2176
+ className: "h-auto min-h-8 min-w-0 justify-start text-left whitespace-normal",
2177
+ disabled: disabled || value.elements.length >= 40,
2178
+ onClick: () => {
2179
+ add(type);
2180
+ setInsertOpen(false);
2181
+ },
2182
+ children: [
2183
+ /* @__PURE__ */ jsx9(Icon, {
2184
+ className: "size-3.5",
2185
+ "aria-hidden": true
2186
+ }),
2187
+ copy[type]
2188
+ ]
2189
+ }, type);
2190
+ })
2191
+ })
2192
+ ]
2193
+ })
2194
+ ]
2195
+ }),
2196
+ /* @__PURE__ */ jsxs5(Popover.Root, {
2197
+ children: [
2198
+ /* @__PURE__ */ jsxs5(PopoverTrigger, {
2199
+ render: /* @__PURE__ */ jsx9(Button, {
2200
+ type: "button",
2201
+ variant: "outline",
2202
+ size: "sm",
2203
+ disabled
2204
+ }),
2205
+ children: [
2206
+ /* @__PURE__ */ jsx9(Palette, {
2207
+ className: "size-3.5",
2208
+ "aria-hidden": true
2209
+ }),
2210
+ copy.branding,
2211
+ /* @__PURE__ */ jsx9(ChevronDown2, {
2212
+ className: "size-3.5",
2213
+ "aria-hidden": true
2214
+ })
2215
+ ]
2216
+ }),
2217
+ /* @__PURE__ */ jsxs5(PopoverContent, {
2218
+ align: "end",
2219
+ className: "w-[min(380px,calc(100vw-2rem))]",
2220
+ children: [
2221
+ /* @__PURE__ */ jsx9(PopoverTitle, {
2222
+ children: copy.branding
2223
+ }),
2224
+ /* @__PURE__ */ jsxs5("div", {
2225
+ className: "grid grid-cols-2 gap-4",
2226
+ children: [
2227
+ [
2228
+ "background",
2229
+ "surface",
2230
+ "primary",
2231
+ "primaryText",
2232
+ "text"
2233
+ ].map((key) => /* @__PURE__ */ jsxs5("label", {
2234
+ className: "flex items-center justify-between gap-2 text-xs",
2235
+ children: [
2236
+ key === "text" ? copy.foreground : copy[key],
2237
+ /* @__PURE__ */ jsx9("input", {
2238
+ type: "color",
2239
+ value: value.appearance[key],
2240
+ disabled,
2241
+ className: "h-8 w-9 cursor-pointer rounded border p-0.5",
2242
+ onChange: (event) => onChange({
2243
+ ...value,
2244
+ appearance: {
2245
+ ...value.appearance,
2246
+ [key]: event.target.value
2247
+ }
2248
+ })
2249
+ })
2250
+ ]
2251
+ }, key)),
2252
+ field(copy.font, /* @__PURE__ */ jsx9(EmailEditorSelect, {
2253
+ label: copy.font,
2254
+ value: value.appearance.font,
2255
+ disabled,
2256
+ options: ["sans", "serif", "mono"].map((font) => ({ value: font, label: copy[font] })),
2257
+ onValueChange: (font) => onChange({
2258
+ ...value,
2259
+ appearance: {
2260
+ ...value.appearance,
2261
+ font: Schema7.decodeUnknownSync(EmailFont)(font)
2262
+ }
2263
+ })
2264
+ }))
2265
+ ]
2266
+ })
2267
+ ]
2268
+ })
2269
+ ]
2270
+ })
2271
+ ]
2272
+ }),
2273
+ /* @__PURE__ */ jsxs5("div", {
2274
+ className: "flex min-w-0 flex-col gap-4",
2275
+ children: [
2276
+ /* @__PURE__ */ jsx9("div", {
2277
+ ref: canvas,
2278
+ "data-slot": "email-editor-canvas",
2279
+ className: "min-w-0 rounded-xl border p-3 sm:p-4",
2280
+ style: {
2281
+ backgroundColor: Schema7.is(EmailColor)(value.appearance.background) ? value.appearance.background : "#f4f4f5"
2282
+ },
2283
+ children: /* @__PURE__ */ jsxs5("div", {
2284
+ className: "min-w-0 rounded-lg p-2 shadow-sm sm:p-3",
2285
+ style: {
2286
+ ...canvasVariables,
2287
+ backgroundColor: Schema7.is(EmailColor)(value.appearance.surface) ? value.appearance.surface : "#ffffff",
2288
+ color: Schema7.is(EmailColor)(value.appearance.text) ? value.appearance.text : "#18181b",
2289
+ fontFamily: fonts[value.appearance.font]
2290
+ },
2291
+ children: [
2292
+ !value.elements.length ? /* @__PURE__ */ jsx9("p", {
2293
+ className: "p-8 text-center text-sm",
2294
+ children: copy.empty
2295
+ }) : null,
2296
+ /* @__PURE__ */ jsx9(AnimatedList, {
2297
+ items: value.elements,
2298
+ getKey: (element) => element.id,
2299
+ label: copy.content,
2300
+ locale,
2301
+ getItemLabel: (element) => copy[element.type],
2302
+ duration: 140,
2303
+ onReorder: disabled ? undefined : (elements) => onChange({ ...value, elements }),
2304
+ itemClassName: "group flex min-w-0 items-start gap-1 rounded-lg bg-[var(--email-paper)] transition-[background-color,box-shadow] hover:bg-[var(--email-hover)] data-[dragging]:bg-[var(--email-hover)] data-[dragging]:shadow-lg [&>[data-slot=animated-list-handle]]:text-inherit",
2305
+ children: (element) => /* @__PURE__ */ jsxs5("div", {
2306
+ ref: (surface) => {
2307
+ if (surface)
2308
+ elementSurfaces.current.set(element.id, surface);
2309
+ else
2310
+ elementSurfaces.current.delete(element.id);
2311
+ },
2312
+ className: "min-w-0 flex-1 rounded-lg border border-transparent transition-colors hover:border-current",
2313
+ onClick: () => {
2314
+ if (!disabled) {
2315
+ setActive(element.id);
2316
+ setSettingsOpen(true);
2317
+ }
2318
+ },
2319
+ onFocusCapture: () => {
2320
+ if (!disabled)
2321
+ setActive(element.id);
2322
+ },
2323
+ style: active === element.id ? { borderColor: value.appearance.primary } : undefined,
2324
+ children: [
2325
+ /* @__PURE__ */ jsx9("div", {
2326
+ className: "flex items-center justify-between gap-2 px-2 py-1 text-xs",
2327
+ children: /* @__PURE__ */ jsxs5("button", {
2328
+ type: "button",
2329
+ disabled,
2330
+ "aria-label": `${copy.editElement}: ${copy[element.type]}`,
2331
+ onClick: () => {
2332
+ setActive(element.id);
2333
+ setSettingsOpen(true);
2334
+ },
2335
+ className: "inline-flex items-center gap-1 rounded px-1 py-0.5 hover:underline",
2336
+ children: [
2337
+ /* @__PURE__ */ jsx9(Pencil, {
2338
+ className: "size-3",
2339
+ "aria-hidden": true
2340
+ }),
2341
+ copy[element.type]
2342
+ ]
2343
+ })
2344
+ }),
2345
+ /* @__PURE__ */ jsx9("div", {
2346
+ style: {
2347
+ textAlign: element.alignment,
2348
+ paddingTop: element.paddingTop,
2349
+ paddingBottom: element.paddingBottom
2350
+ },
2351
+ className: "min-w-0 px-2",
2352
+ children: element.type === "text" ? active === element.id ? /* @__PURE__ */ jsx9(RichTextEditor, {
2353
+ value: element.content,
2354
+ variables,
2355
+ locale,
2356
+ label: copy.text,
2357
+ disabled,
2358
+ contentStyle: {
2359
+ color: value.appearance.text,
2360
+ backgroundColor: value.appearance.surface
2361
+ },
2362
+ onChange: (content) => update({ ...element, content })
2363
+ }) : /* @__PURE__ */ jsx9("div", {
2364
+ role: "button",
2365
+ tabIndex: disabled ? -1 : 0,
2366
+ "aria-label": `${copy.editElement}: ${copy.text}`,
2367
+ className: "cursor-text text-sm leading-relaxed",
2368
+ onClick: (event) => {
2369
+ event.preventDefault();
2370
+ setActive(element.id);
2371
+ },
2372
+ onKeyDown: (event) => {
2373
+ if (event.key === "Enter" || event.key === " ") {
2374
+ event.preventDefault();
2375
+ setActive(element.id);
2376
+ setSettingsOpen(true);
2377
+ }
2378
+ },
2379
+ children: /* @__PURE__ */ jsx9(RichTextContent, {
2380
+ value: element.content,
2381
+ variables
2382
+ })
2383
+ }) : element.type === "button" ? /* @__PURE__ */ jsx9("button", {
2384
+ type: "button",
2385
+ disabled,
2386
+ onClick: () => setActive(element.id),
2387
+ className: "max-w-full rounded-md px-5 py-3 text-sm font-semibold break-words",
2388
+ style: {
2389
+ backgroundColor: value.appearance.primary,
2390
+ color: value.appearance.primaryText
2391
+ },
2392
+ children: element.label
2393
+ }) : element.type === "code" ? /* @__PURE__ */ jsx9("button", {
2394
+ type: "button",
2395
+ disabled,
2396
+ onClick: () => setActive(element.id),
2397
+ className: "w-full rounded-lg border border-[#e5e7eb] p-4 font-mono text-xl tracking-[0.2em]",
2398
+ children: `{{${element.variable}}}`
2399
+ }) : element.type === "image" || element.type === "logo" ? Schema7.is(RichTextLink)(element.src) ? /* @__PURE__ */ jsx9("img", {
2400
+ src: element.src,
2401
+ alt: element.alt,
2402
+ width: element.width,
2403
+ className: "inline-block max-w-full"
2404
+ }) : /* @__PURE__ */ jsx9(Image, {
2405
+ className: "mx-auto size-8"
2406
+ }) : element.type === "divider" ? /* @__PURE__ */ jsx9("hr", {
2407
+ className: "border-[#e5e7eb]"
2408
+ }) : element.type === "spacer" ? /* @__PURE__ */ jsx9("div", {
2409
+ style: { height: element.height },
2410
+ className: "min-h-2 rounded border border-dashed border-[#e5e7eb]"
2411
+ }) : null
2412
+ })
2413
+ ]
2414
+ })
2415
+ })
2416
+ ]
2417
+ })
2418
+ }),
2419
+ /* @__PURE__ */ jsx9(Popover.Root, {
2420
+ open: settingsOpen && !!selected,
2421
+ onOpenChange: setSettingsOpen,
2422
+ children: /* @__PURE__ */ jsx9(Popover.Portal, {
2423
+ children: /* @__PURE__ */ jsx9(Popover.Positioner, {
2424
+ anchor: settingsAnchor,
2425
+ side: "right",
2426
+ align: "start",
2427
+ sideOffset: 8,
2428
+ collisionAvoidance: {
2429
+ side: "none",
2430
+ align: "none",
2431
+ fallbackAxisSide: "none"
2432
+ },
2433
+ className: "z-50",
2434
+ children: /* @__PURE__ */ jsxs5(Popover.Popup, {
2435
+ initialFocus: false,
2436
+ className: "bg-popover text-popover-foreground max-h-[min(480px,80dvh)] w-[min(380px,calc(100vw-2rem))] space-y-3 overflow-y-auto rounded-xl border p-4 shadow-lg outline-none",
2437
+ children: [
2438
+ /* @__PURE__ */ jsxs5("div", {
2439
+ className: "flex items-center justify-between gap-2",
2440
+ children: [
2441
+ /* @__PURE__ */ jsx9(Popover.Title, {
2442
+ className: "text-xs font-semibold",
2443
+ children: selected ? copy[selected.type] : copy.properties
2444
+ }),
2445
+ /* @__PURE__ */ jsx9(Popover.Close, {
2446
+ "aria-label": copy.close,
2447
+ className: "hover:bg-muted rounded p-1",
2448
+ children: /* @__PURE__ */ jsx9(X, {
2449
+ className: "size-4"
2450
+ })
2451
+ })
2452
+ ]
2453
+ }),
2454
+ !selected ? /* @__PURE__ */ jsx9("p", {
2455
+ className: "text-muted-foreground text-xs",
2456
+ children: copy.select
2457
+ }) : /* @__PURE__ */ jsxs5("div", {
2458
+ className: "grid min-w-0 items-end gap-3 sm:grid-cols-2 [&>label]:min-w-0",
2459
+ children: [
2460
+ field(copy.alignment, /* @__PURE__ */ jsx9(EmailEditorSelect, {
2461
+ label: copy.alignment,
2462
+ value: selected.alignment,
2463
+ disabled,
2464
+ options: ["left", "center", "right"].map((alignment) => ({
2465
+ value: alignment,
2466
+ label: copy[alignment]
2467
+ })),
2468
+ onValueChange: (alignment) => update({
2469
+ ...selected,
2470
+ alignment: Schema7.decodeUnknownSync(EmailAlignment)(alignment)
2471
+ })
2472
+ })),
2473
+ /* @__PURE__ */ jsx9("div", {
2474
+ className: "grid grid-cols-2 gap-2",
2475
+ children: ["paddingTop", "paddingBottom"].map((key) => /* @__PURE__ */ jsxs5("label", {
2476
+ className: "grid gap-1.5 text-[11px]",
2477
+ children: [
2478
+ copy[key],
2479
+ /* @__PURE__ */ jsx9(Input, {
2480
+ type: "number",
2481
+ min: 0,
2482
+ max: 96,
2483
+ value: selected[key],
2484
+ disabled,
2485
+ onChange: (event) => update({
2486
+ ...selected,
2487
+ [key]: event.target.valueAsNumber
2488
+ })
2489
+ })
2490
+ ]
2491
+ }, key))
2492
+ }),
2493
+ selected.type === "button" ? /* @__PURE__ */ jsxs5(Fragment3, {
2494
+ children: [
2495
+ field(copy.label, /* @__PURE__ */ jsx9(Input, {
2496
+ value: selected.label,
2497
+ disabled,
2498
+ onChange: (event) => update({
2499
+ ...selected,
2500
+ label: event.target.value
2501
+ })
2502
+ })),
2503
+ field(copy.destination, /* @__PURE__ */ jsx9(EmailEditorSelect, {
2504
+ label: copy.destination,
2505
+ disabled,
2506
+ options: [
2507
+ { value: "__url", label: copy.url },
2508
+ ...variables.filter((variable) => variable.type === "url").map((variable) => ({
2509
+ value: variable.key,
2510
+ label: variable.label
2511
+ }))
2512
+ ],
2513
+ value: selected.destination.type === "variable" ? selected.destination.key : "__url",
2514
+ onValueChange: (destination) => update({
2515
+ ...selected,
2516
+ destination: destination === "__url" ? {
2517
+ type: "url",
2518
+ url: "https://example.com"
2519
+ } : {
2520
+ type: "variable",
2521
+ key: destination
2522
+ }
2523
+ })
2524
+ })),
2525
+ selected.destination.type === "url" ? field(copy.url, /* @__PURE__ */ jsx9(Input, {
2526
+ type: "url",
2527
+ value: selected.destination.url,
2528
+ disabled,
2529
+ onChange: (event) => update({
2530
+ ...selected,
2531
+ destination: {
2532
+ type: "url",
2533
+ url: event.target.value
2534
+ }
2535
+ })
2536
+ })) : null
2537
+ ]
2538
+ }) : null,
2539
+ selected.type === "code" ? field(copy.variable, /* @__PURE__ */ jsx9(EmailEditorSelect, {
2540
+ label: copy.variable,
2541
+ disabled,
2542
+ value: selected.variable,
2543
+ options: variables.filter((variable) => variable.type === "text").map((variable) => ({
2544
+ value: variable.key,
2545
+ label: variable.label
2546
+ })),
2547
+ onValueChange: (variable) => update({
2548
+ ...selected,
2549
+ variable
2550
+ })
2551
+ })) : null,
2552
+ selected.type === "logo" || selected.type === "image" ? /* @__PURE__ */ jsxs5(Fragment3, {
2553
+ children: [
2554
+ field(copy.src, /* @__PURE__ */ jsx9(Input, {
2555
+ type: "url",
2556
+ value: selected.src,
2557
+ disabled,
2558
+ onChange: (event) => update({
2559
+ ...selected,
2560
+ src: event.target.value
2561
+ })
2562
+ })),
2563
+ field(copy.alt, /* @__PURE__ */ jsx9(Input, {
2564
+ value: selected.alt,
2565
+ disabled,
2566
+ onChange: (event) => update({
2567
+ ...selected,
2568
+ alt: event.target.value
2569
+ })
2570
+ })),
2571
+ field(copy.width, /* @__PURE__ */ jsx9(Input, {
2572
+ type: "number",
2573
+ min: 32,
2574
+ max: 560,
2575
+ value: selected.width,
2576
+ disabled,
2577
+ onChange: (event) => update({
2578
+ ...selected,
2579
+ width: event.target.valueAsNumber
2580
+ })
2581
+ }))
2582
+ ]
2583
+ }) : null,
2584
+ selected.type === "spacer" ? field(copy.height, /* @__PURE__ */ jsx9(Input, {
2585
+ type: "number",
2586
+ min: 0,
2587
+ max: 96,
2588
+ value: selected.height,
2589
+ disabled,
2590
+ onChange: (event) => update({
2591
+ ...selected,
2592
+ height: event.target.valueAsNumber
2593
+ })
2594
+ })) : null,
2595
+ /* @__PURE__ */ jsxs5("div", {
2596
+ className: "flex flex-wrap items-center gap-2 sm:col-span-2",
2597
+ children: [
2598
+ /* @__PURE__ */ jsxs5(Button, {
2599
+ type: "button",
2600
+ size: "sm",
2601
+ variant: "outline",
2602
+ disabled: disabled || value.elements.length >= 40,
2603
+ onClick: () => {
2604
+ const duplicate = {
2605
+ ...selected,
2606
+ id: crypto.randomUUID()
2607
+ };
2608
+ const index = value.elements.findIndex((element) => element.id === selected.id);
2609
+ onChange({
2610
+ ...value,
2611
+ elements: [
2612
+ ...value.elements.slice(0, index + 1),
2613
+ duplicate,
2614
+ ...value.elements.slice(index + 1)
2615
+ ]
2616
+ });
2617
+ setActive(duplicate.id);
2618
+ },
2619
+ children: [
2620
+ /* @__PURE__ */ jsx9(Copy, {
2621
+ className: "size-3.5"
2622
+ }),
2623
+ copy.duplicate
2624
+ ]
2625
+ }),
2626
+ /* @__PURE__ */ jsxs5(Button, {
2627
+ type: "button",
2628
+ size: "sm",
2629
+ variant: "ghost",
2630
+ className: "text-destructive",
2631
+ disabled,
2632
+ onClick: () => {
2633
+ document.getElementById(`${id}-add`)?.focus();
2634
+ onChange({
2635
+ ...value,
2636
+ elements: value.elements.filter((element) => element.id !== selected.id)
2637
+ });
2638
+ setActive(null);
2639
+ setSettingsOpen(false);
2640
+ },
2641
+ children: [
2642
+ /* @__PURE__ */ jsx9(Trash2, {
2643
+ className: "size-3.5"
2644
+ }),
2645
+ copy.remove
2646
+ ]
2647
+ })
2648
+ ]
2649
+ })
2650
+ ]
2651
+ })
2652
+ ]
2653
+ })
2654
+ })
2655
+ })
2656
+ })
2657
+ ]
2658
+ }),
2659
+ /* @__PURE__ */ jsx9("p", {
2660
+ className: "text-muted-foreground text-xs",
2661
+ children: copy.local
2662
+ })
2663
+ ]
2664
+ })
2665
+ ]
2666
+ }),
2667
+ invalid ? /* @__PURE__ */ jsx9("p", {
2668
+ role: "alert",
2669
+ className: "text-destructive text-xs",
2670
+ children: copy.invalid
2671
+ }) : null
2672
+ ]
2673
+ });
2674
+ };
2675
+ export {
2676
+ EmailEditor
2677
+ };