@krak-stack/registry 0.10.4 → 0.10.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/dist/components/ui/animated-list.d.ts +6 -7
- package/dist/components/ui/animated-list.js +329 -139
- package/dist/components/ui/animated-number.js +47 -24
- package/dist/components/ui/auto-height.js +56 -15
- package/dist/components/ui/data-table.js +16 -13
- package/dist/components/ui/email-editor.d.ts +124 -0
- package/dist/components/ui/email-editor.js +2651 -0
- package/dist/components/ui/form.d.ts +5 -5
- package/dist/components/ui/presence-transition.d.ts +1 -1
- package/dist/components/ui/presence-transition.js +79 -26
- package/dist/components/ui/rich-text-editor.d.ts +70 -0
- package/dist/components/ui/rich-text-editor.js +1135 -0
- package/dist/components/ui/time-picker.d.ts +13 -4
- package/dist/components/ui/time-picker.js +141 -47
- package/dist/components/ui/time-picker.schema.d.ts +16 -2
- package/dist/lib/email-document/model.d.ts +26 -0
- package/dist/lib/email-document/model.js +253 -0
- package/dist/lib/email-document/render.d.ts +115 -0
- package/dist/lib/email-document/render.js +469 -0
- package/dist/lib/email-document/schema.d.ts +285 -0
- package/dist/lib/email-document/schema.js +139 -0
- package/dist/lib/krakmotion.d.ts +27 -0
- package/dist/lib/rich-text/dom.d.ts +40 -0
- package/dist/lib/rich-text/model.d.ts +19 -0
- package/dist/lib/rich-text/model.js +141 -0
- package/dist/lib/rich-text/schema.d.ts +64 -0
- package/dist/lib/rich-text/schema.js +53 -0
- package/dist/services/events/client/audit-log-table.js +16 -13
- package/dist/services/webhooks/client/delivery-table.js +16 -13
- package/package.json +29 -5
|
@@ -0,0 +1,1135 @@
|
|
|
1
|
+
// ../../src/components/ui/rich-text-editor.tsx
|
|
2
|
+
import { Schema as Schema4 } from "effect";
|
|
3
|
+
import {
|
|
4
|
+
useEffect,
|
|
5
|
+
useEffectEvent,
|
|
6
|
+
useId,
|
|
7
|
+
useRef,
|
|
8
|
+
useState
|
|
9
|
+
} from "react";
|
|
10
|
+
import {
|
|
11
|
+
Bold,
|
|
12
|
+
Italic,
|
|
13
|
+
Underline,
|
|
14
|
+
Undo2,
|
|
15
|
+
Redo2,
|
|
16
|
+
Link2,
|
|
17
|
+
Unlink,
|
|
18
|
+
Braces,
|
|
19
|
+
ChevronDown
|
|
20
|
+
} from "lucide-react";
|
|
21
|
+
|
|
22
|
+
// ../../src/components/ui/button.tsx
|
|
23
|
+
import { Button as ButtonPrimitive } from "@base-ui/react/button";
|
|
24
|
+
import { cva } from "class-variance-authority";
|
|
25
|
+
|
|
26
|
+
// ../../src/lib/utils.ts
|
|
27
|
+
import { cn } from "cn";
|
|
28
|
+
|
|
29
|
+
// ../../src/components/ui/button.tsx
|
|
30
|
+
import { jsx } from "react/jsx-runtime";
|
|
31
|
+
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", {
|
|
32
|
+
variants: {
|
|
33
|
+
variant: {
|
|
34
|
+
default: "bg-primary text-primary-foreground hover:bg-primary/80",
|
|
35
|
+
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",
|
|
36
|
+
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",
|
|
37
|
+
ghost: "hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
|
|
38
|
+
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",
|
|
39
|
+
link: "text-primary underline-offset-4 hover:underline"
|
|
40
|
+
},
|
|
41
|
+
size: {
|
|
42
|
+
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",
|
|
43
|
+
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",
|
|
44
|
+
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",
|
|
45
|
+
lg: "h-10 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
|
46
|
+
icon: "size-9",
|
|
47
|
+
"icon-xs": "size-6 rounded-[min(var(--radius-md),8px)] in-data-[slot=button-group]:rounded-md [&_svg:not([class*='size-'])]:size-3",
|
|
48
|
+
"icon-sm": "size-8 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-md",
|
|
49
|
+
"icon-lg": "size-10"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
defaultVariants: {
|
|
53
|
+
variant: "default",
|
|
54
|
+
size: "default"
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
function Button({
|
|
58
|
+
className,
|
|
59
|
+
variant = "default",
|
|
60
|
+
size = "default",
|
|
61
|
+
...props
|
|
62
|
+
}) {
|
|
63
|
+
return /* @__PURE__ */ jsx(ButtonPrimitive, {
|
|
64
|
+
"data-slot": "button",
|
|
65
|
+
className: cn(buttonVariants({ variant, size, className })),
|
|
66
|
+
...props
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ../../src/components/ui/input.tsx
|
|
71
|
+
import { Input as InputPrimitive } from "@base-ui/react/input";
|
|
72
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
73
|
+
function Input({ className, type, ...props }) {
|
|
74
|
+
return /* @__PURE__ */ jsx2(InputPrimitive, {
|
|
75
|
+
type,
|
|
76
|
+
"data-slot": "input",
|
|
77
|
+
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),
|
|
78
|
+
...props
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ../../src/components/ui/select.tsx
|
|
83
|
+
import { Select as SelectPrimitive } from "@base-ui/react/select";
|
|
84
|
+
import { ChevronDownIcon, CheckIcon, ChevronUpIcon } from "lucide-react";
|
|
85
|
+
import { jsx as jsx3, jsxs } from "react/jsx-runtime";
|
|
86
|
+
"use client";
|
|
87
|
+
var Select = SelectPrimitive.Root;
|
|
88
|
+
function SelectValue({ className, ...props }) {
|
|
89
|
+
return /* @__PURE__ */ jsx3(SelectPrimitive.Value, {
|
|
90
|
+
"data-slot": "select-value",
|
|
91
|
+
className: cn("flex flex-1 text-left", className),
|
|
92
|
+
...props
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
function SelectTrigger({
|
|
96
|
+
className,
|
|
97
|
+
size = "default",
|
|
98
|
+
children,
|
|
99
|
+
...props
|
|
100
|
+
}) {
|
|
101
|
+
return /* @__PURE__ */ jsxs(SelectPrimitive.Trigger, {
|
|
102
|
+
"data-slot": "select-trigger",
|
|
103
|
+
"data-size": size,
|
|
104
|
+
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),
|
|
105
|
+
...props,
|
|
106
|
+
children: [
|
|
107
|
+
children,
|
|
108
|
+
/* @__PURE__ */ jsx3(SelectPrimitive.Icon, {
|
|
109
|
+
render: /* @__PURE__ */ jsx3(ChevronDownIcon, {
|
|
110
|
+
className: "text-muted-foreground pointer-events-none size-4"
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
]
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
function SelectContent({
|
|
117
|
+
className,
|
|
118
|
+
children,
|
|
119
|
+
side = "bottom",
|
|
120
|
+
sideOffset = 4,
|
|
121
|
+
align = "center",
|
|
122
|
+
alignOffset = 0,
|
|
123
|
+
alignItemWithTrigger = true,
|
|
124
|
+
...props
|
|
125
|
+
}) {
|
|
126
|
+
return /* @__PURE__ */ jsx3(SelectPrimitive.Portal, {
|
|
127
|
+
children: /* @__PURE__ */ jsx3(SelectPrimitive.Positioner, {
|
|
128
|
+
side,
|
|
129
|
+
sideOffset,
|
|
130
|
+
align,
|
|
131
|
+
alignOffset,
|
|
132
|
+
alignItemWithTrigger,
|
|
133
|
+
className: "isolate z-50",
|
|
134
|
+
children: /* @__PURE__ */ jsxs(SelectPrimitive.Popup, {
|
|
135
|
+
"data-slot": "select-content",
|
|
136
|
+
"data-align-trigger": alignItemWithTrigger,
|
|
137
|
+
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),
|
|
138
|
+
...props,
|
|
139
|
+
children: [
|
|
140
|
+
/* @__PURE__ */ jsx3(SelectScrollUpButton, {}),
|
|
141
|
+
/* @__PURE__ */ jsx3(SelectPrimitive.List, {
|
|
142
|
+
children
|
|
143
|
+
}),
|
|
144
|
+
/* @__PURE__ */ jsx3(SelectScrollDownButton, {})
|
|
145
|
+
]
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
function SelectItem({
|
|
151
|
+
className,
|
|
152
|
+
children,
|
|
153
|
+
...props
|
|
154
|
+
}) {
|
|
155
|
+
return /* @__PURE__ */ jsxs(SelectPrimitive.Item, {
|
|
156
|
+
"data-slot": "select-item",
|
|
157
|
+
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),
|
|
158
|
+
...props,
|
|
159
|
+
children: [
|
|
160
|
+
/* @__PURE__ */ jsx3(SelectPrimitive.ItemText, {
|
|
161
|
+
className: "flex flex-1 shrink-0 gap-2 whitespace-nowrap",
|
|
162
|
+
children
|
|
163
|
+
}),
|
|
164
|
+
/* @__PURE__ */ jsx3(SelectPrimitive.ItemIndicator, {
|
|
165
|
+
render: /* @__PURE__ */ jsx3("span", {
|
|
166
|
+
className: "pointer-events-none absolute right-2 flex size-4 items-center justify-center"
|
|
167
|
+
}),
|
|
168
|
+
children: /* @__PURE__ */ jsx3(CheckIcon, {
|
|
169
|
+
className: "pointer-events-none"
|
|
170
|
+
})
|
|
171
|
+
})
|
|
172
|
+
]
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
function SelectScrollUpButton({
|
|
176
|
+
className,
|
|
177
|
+
...props
|
|
178
|
+
}) {
|
|
179
|
+
return /* @__PURE__ */ jsx3(SelectPrimitive.ScrollUpArrow, {
|
|
180
|
+
"data-slot": "select-scroll-up-button",
|
|
181
|
+
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),
|
|
182
|
+
...props,
|
|
183
|
+
children: /* @__PURE__ */ jsx3(ChevronUpIcon, {})
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
function SelectScrollDownButton({
|
|
187
|
+
className,
|
|
188
|
+
...props
|
|
189
|
+
}) {
|
|
190
|
+
return /* @__PURE__ */ jsx3(SelectPrimitive.ScrollDownArrow, {
|
|
191
|
+
"data-slot": "select-scroll-down-button",
|
|
192
|
+
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),
|
|
193
|
+
...props,
|
|
194
|
+
children: /* @__PURE__ */ jsx3(ChevronDownIcon, {})
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ../../src/components/ui/dropdown-menu.tsx
|
|
199
|
+
import { Menu as MenuPrimitive } from "@base-ui/react/menu";
|
|
200
|
+
import { ChevronRightIcon, CheckIcon as CheckIcon2 } from "lucide-react";
|
|
201
|
+
import { jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
202
|
+
function DropdownMenu({ ...props }) {
|
|
203
|
+
return /* @__PURE__ */ jsx4(MenuPrimitive.Root, {
|
|
204
|
+
"data-slot": "dropdown-menu",
|
|
205
|
+
...props
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
function DropdownMenuTrigger({ ...props }) {
|
|
209
|
+
return /* @__PURE__ */ jsx4(MenuPrimitive.Trigger, {
|
|
210
|
+
"data-slot": "dropdown-menu-trigger",
|
|
211
|
+
...props
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
function DropdownMenuContent({
|
|
215
|
+
align = "start",
|
|
216
|
+
alignOffset = 0,
|
|
217
|
+
side = "bottom",
|
|
218
|
+
sideOffset = 4,
|
|
219
|
+
className,
|
|
220
|
+
...props
|
|
221
|
+
}) {
|
|
222
|
+
return /* @__PURE__ */ jsx4(MenuPrimitive.Portal, {
|
|
223
|
+
children: /* @__PURE__ */ jsx4(MenuPrimitive.Positioner, {
|
|
224
|
+
className: "isolate z-50 outline-none",
|
|
225
|
+
align,
|
|
226
|
+
alignOffset,
|
|
227
|
+
side,
|
|
228
|
+
sideOffset,
|
|
229
|
+
children: /* @__PURE__ */ jsx4(MenuPrimitive.Popup, {
|
|
230
|
+
"data-slot": "dropdown-menu-content",
|
|
231
|
+
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),
|
|
232
|
+
...props
|
|
233
|
+
})
|
|
234
|
+
})
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
function DropdownMenuItem({
|
|
238
|
+
className,
|
|
239
|
+
inset,
|
|
240
|
+
variant = "default",
|
|
241
|
+
...props
|
|
242
|
+
}) {
|
|
243
|
+
return /* @__PURE__ */ jsx4(MenuPrimitive.Item, {
|
|
244
|
+
"data-slot": "dropdown-menu-item",
|
|
245
|
+
"data-inset": inset,
|
|
246
|
+
"data-variant": variant,
|
|
247
|
+
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),
|
|
248
|
+
...props
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ../../src/components/ui/rich-text-editor.tsx
|
|
253
|
+
import { useKrakstackLocale } from "@krak-stack/registry/krakstack-provider";
|
|
254
|
+
|
|
255
|
+
// ../../src/lib/rich-text/schema.ts
|
|
256
|
+
import { Schema } from "effect";
|
|
257
|
+
var RichTextLink = Schema.String.check(Schema.isMaxLength(2000), Schema.makeFilter((value) => {
|
|
258
|
+
if (/[\s\p{Cc}\\{}]/u.test(value))
|
|
259
|
+
return false;
|
|
260
|
+
const url = URL.parse(value);
|
|
261
|
+
return url !== null && ["https:", "mailto:"].includes(url.protocol) && !url.username && !url.password;
|
|
262
|
+
})).annotate({ identifier: "RichTextLink" });
|
|
263
|
+
var RichTextMarks = Schema.Struct({
|
|
264
|
+
bold: Schema.optional(Schema.Boolean),
|
|
265
|
+
italic: Schema.optional(Schema.Boolean),
|
|
266
|
+
underline: Schema.optional(Schema.Boolean),
|
|
267
|
+
href: Schema.optional(RichTextLink)
|
|
268
|
+
}).annotate({ identifier: "RichTextMarks" });
|
|
269
|
+
var RichTextInline = Schema.Union([
|
|
270
|
+
Schema.Struct({
|
|
271
|
+
type: Schema.Literal("text"),
|
|
272
|
+
text: Schema.String.check(Schema.isMaxLength(20000)),
|
|
273
|
+
marks: Schema.optional(RichTextMarks)
|
|
274
|
+
}),
|
|
275
|
+
Schema.Struct({
|
|
276
|
+
type: Schema.Literal("variable"),
|
|
277
|
+
key: Schema.String.check(Schema.isPattern(/^[a-zA-Z][a-zA-Z0-9_.]{0,79}$/)),
|
|
278
|
+
marks: Schema.optional(RichTextMarks)
|
|
279
|
+
})
|
|
280
|
+
]).annotate({ identifier: "RichTextInline" });
|
|
281
|
+
var RichTextBlockKind = Schema.Literals([
|
|
282
|
+
"paragraph",
|
|
283
|
+
"heading",
|
|
284
|
+
"bullet",
|
|
285
|
+
"numbered"
|
|
286
|
+
]).annotate({ identifier: "RichTextBlockKind" });
|
|
287
|
+
var RichTextDocument = Schema.Struct({
|
|
288
|
+
version: Schema.Literal(1),
|
|
289
|
+
blocks: Schema.Array(Schema.Struct({
|
|
290
|
+
type: RichTextBlockKind,
|
|
291
|
+
content: Schema.Array(RichTextInline).check(Schema.isMaxLength(2000))
|
|
292
|
+
})).check(Schema.isMinLength(1), Schema.isMaxLength(100))
|
|
293
|
+
}).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" });
|
|
294
|
+
var RichTextVariable = Schema.Struct({
|
|
295
|
+
key: Schema.String.check(Schema.isPattern(/^[a-zA-Z][a-zA-Z0-9_.]{0,79}$/)),
|
|
296
|
+
label: Schema.String,
|
|
297
|
+
type: Schema.Literals(["text", "url"]),
|
|
298
|
+
sample: Schema.String
|
|
299
|
+
}).annotate({ identifier: "RichTextVariable" });
|
|
300
|
+
|
|
301
|
+
// ../../src/lib/rich-text/model.ts
|
|
302
|
+
import { Schema as Schema2 } from "effect";
|
|
303
|
+
var sameMarks = Schema2.toEquivalence(RichTextMarks);
|
|
304
|
+
var emptyRichText = () => ({
|
|
305
|
+
version: 1,
|
|
306
|
+
blocks: [{ type: "paragraph", content: [] }]
|
|
307
|
+
});
|
|
308
|
+
var richTextPlainText = (value, variables) => value.blocks.map((block) => block.content.map((inline) => inline.type === "text" ? inline.text : variables?.[inline.key] ?? `{{${inline.key}}}`).join("")).join(`
|
|
309
|
+
`);
|
|
310
|
+
var units = (value) => value.blocks.flatMap((block, index) => [
|
|
311
|
+
...index ? [{ type: "break", next: block.type }] : [],
|
|
312
|
+
...block.content.flatMap((inline) => inline.type === "text" ? inline.text.split("").map((text) => ({ ...inline, text })) : [inline])
|
|
313
|
+
]);
|
|
314
|
+
var fromUnits = (content, first) => {
|
|
315
|
+
const blocks = [{ type: first, content: [] }];
|
|
316
|
+
for (const unit of content) {
|
|
317
|
+
if (unit.type === "break") {
|
|
318
|
+
blocks.push({ type: unit.next, content: [] });
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
const target = blocks.at(-1);
|
|
322
|
+
if (!target)
|
|
323
|
+
continue;
|
|
324
|
+
const previous = target.content.at(-1);
|
|
325
|
+
if (unit.type === "text" && previous?.type === "text" && sameMarks(previous.marks ?? {}, unit.marks ?? {})) {
|
|
326
|
+
target.content[target.content.length - 1] = {
|
|
327
|
+
...previous,
|
|
328
|
+
text: previous.text + unit.text
|
|
329
|
+
};
|
|
330
|
+
} else
|
|
331
|
+
target.content.push(unit);
|
|
332
|
+
}
|
|
333
|
+
return { version: 1, blocks };
|
|
334
|
+
};
|
|
335
|
+
var replaceRichText = (value, selection, inserted) => {
|
|
336
|
+
const content = units(value);
|
|
337
|
+
const from = Math.max(0, Math.min(selection.from, content.length));
|
|
338
|
+
const to = Math.max(from, Math.min(selection.to, content.length));
|
|
339
|
+
const additions = inserted.type === "text" ? inserted.text.replace(/\r\n?/g, `
|
|
340
|
+
`).split("").map((text) => text === `
|
|
341
|
+
` ? { type: "break", next: "paragraph" } : { type: "text", text, marks: inserted.marks }) : [inserted];
|
|
342
|
+
const next = fromUnits([...content.slice(0, from), ...additions, ...content.slice(to)], value.blocks[0]?.type ?? "paragraph");
|
|
343
|
+
const position = from + additions.length;
|
|
344
|
+
return { value: next, selection: { from: position, to: position } };
|
|
345
|
+
};
|
|
346
|
+
var formatRichText = (value, selection, mark, setting) => fromUnits(units(value).map((unit, index) => {
|
|
347
|
+
if (unit.type === "break" || index < selection.from || index >= selection.to)
|
|
348
|
+
return unit;
|
|
349
|
+
const marks = { ...unit.marks, [mark]: setting };
|
|
350
|
+
return { ...unit, marks };
|
|
351
|
+
}), value.blocks[0]?.type ?? "paragraph");
|
|
352
|
+
var setRichTextBlock = (value, selection, type) => {
|
|
353
|
+
let offset = 0;
|
|
354
|
+
return {
|
|
355
|
+
...value,
|
|
356
|
+
blocks: value.blocks.map((block) => {
|
|
357
|
+
const start = offset;
|
|
358
|
+
offset += block.content.reduce((size, inline) => size + (inline.type === "text" ? inline.text.length : 1), 0) + 1;
|
|
359
|
+
return selection.from < offset && selection.to >= start ? { ...block, type } : block;
|
|
360
|
+
})
|
|
361
|
+
};
|
|
362
|
+
};
|
|
363
|
+
var richTextMarksAt = (value, position) => {
|
|
364
|
+
const content = units(value);
|
|
365
|
+
const unit = content[Math.max(0, position - 1)];
|
|
366
|
+
return unit && unit.type !== "break" ? unit.marks ?? {} : {};
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
// ../../src/lib/rich-text/dom.ts
|
|
370
|
+
import { Option, Schema as Schema3 } from "effect";
|
|
371
|
+
var blocks = new Set(["P", "DIV", "H1", "H2", "H3", "LI"]);
|
|
372
|
+
var sameMarks2 = Schema3.toEquivalence(RichTextMarks);
|
|
373
|
+
var pointAfter = (node) => ({
|
|
374
|
+
node: node.parentNode ?? node,
|
|
375
|
+
offset: Array.from(node.parentNode?.childNodes ?? []).indexOf(node) + 1
|
|
376
|
+
});
|
|
377
|
+
var readRichTextDOM = (root) => {
|
|
378
|
+
const result = [];
|
|
379
|
+
const points = [{ node: root, offset: 0 }];
|
|
380
|
+
let current;
|
|
381
|
+
const begin = (type, point) => {
|
|
382
|
+
if (current)
|
|
383
|
+
result.push(current);
|
|
384
|
+
if (result.length)
|
|
385
|
+
points.push(point);
|
|
386
|
+
else
|
|
387
|
+
points[0] = point;
|
|
388
|
+
current = { type, content: [] };
|
|
389
|
+
};
|
|
390
|
+
const append = (inline, point) => {
|
|
391
|
+
if (!current)
|
|
392
|
+
begin("paragraph", point);
|
|
393
|
+
const previous = current?.content.at(-1);
|
|
394
|
+
if (inline.type === "text" && previous?.type === "text" && sameMarks2(inline.marks ?? {}, previous.marks ?? {})) {
|
|
395
|
+
if (current)
|
|
396
|
+
current.content[current.content.length - 1] = {
|
|
397
|
+
...previous,
|
|
398
|
+
text: previous.text + inline.text
|
|
399
|
+
};
|
|
400
|
+
} else
|
|
401
|
+
current?.content.push(inline);
|
|
402
|
+
points.push(point);
|
|
403
|
+
};
|
|
404
|
+
const visit = (node, inherited = {}, kind = "paragraph") => {
|
|
405
|
+
if (node instanceof Text) {
|
|
406
|
+
if (!current)
|
|
407
|
+
begin(kind, { node, offset: 0 });
|
|
408
|
+
for (let index = 0;index < node.data.length; index++)
|
|
409
|
+
append({ type: "text", text: node.data[index] ?? "", marks: inherited }, { node, offset: index + 1 });
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
if (!(node instanceof HTMLElement) || ["SCRIPT", "STYLE", "IFRAME"].includes(node.tagName))
|
|
413
|
+
return;
|
|
414
|
+
const key = node.dataset.richVariable;
|
|
415
|
+
if (key) {
|
|
416
|
+
const variable = { type: "variable", key, marks: inherited };
|
|
417
|
+
append(variable, pointAfter(node));
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
const href = node.getAttribute("href");
|
|
421
|
+
const marks = {
|
|
422
|
+
bold: inherited.bold,
|
|
423
|
+
italic: inherited.italic,
|
|
424
|
+
underline: inherited.underline,
|
|
425
|
+
href: inherited.href
|
|
426
|
+
};
|
|
427
|
+
if (["B", "STRONG"].includes(node.tagName) || node.style.fontWeight === "700")
|
|
428
|
+
marks.bold = true;
|
|
429
|
+
if (["I", "EM"].includes(node.tagName) || node.style.fontStyle === "italic")
|
|
430
|
+
marks.italic = true;
|
|
431
|
+
if (node.tagName === "U" || node.style.textDecoration.includes("underline"))
|
|
432
|
+
marks.underline = true;
|
|
433
|
+
if (href && Schema3.is(RichTextLink)(href))
|
|
434
|
+
marks.href = href;
|
|
435
|
+
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;
|
|
436
|
+
const isBlock = blocks.has(node.tagName);
|
|
437
|
+
const container = isBlock && [...node.children].some((child) => blocks.has(child.tagName));
|
|
438
|
+
if (isBlock && !container)
|
|
439
|
+
begin(type, { node, offset: 0 });
|
|
440
|
+
if (node.tagName === "BR") {
|
|
441
|
+
if (node.nextSibling)
|
|
442
|
+
begin(kind, pointAfter(node));
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
for (const child of node.childNodes)
|
|
446
|
+
visit(child, marks, type);
|
|
447
|
+
};
|
|
448
|
+
for (const child of root.childNodes)
|
|
449
|
+
visit(child);
|
|
450
|
+
if (current)
|
|
451
|
+
result.push(current);
|
|
452
|
+
const value = Option.getOrUndefined(Schema3.decodeOption(RichTextDocument)({
|
|
453
|
+
version: 1,
|
|
454
|
+
blocks: result.length ? result : emptyRichText().blocks
|
|
455
|
+
}));
|
|
456
|
+
return { value, points };
|
|
457
|
+
};
|
|
458
|
+
var readEditorSelection = (root, points) => {
|
|
459
|
+
const selection = root.ownerDocument.getSelection();
|
|
460
|
+
if (!selection?.anchorNode || !selection.focusNode || !root.contains(selection.anchorNode) || !root.contains(selection.focusNode))
|
|
461
|
+
return;
|
|
462
|
+
const position = (node, offset) => {
|
|
463
|
+
const range = root.ownerDocument.createRange();
|
|
464
|
+
range.setStart(node, offset);
|
|
465
|
+
range.collapse(true);
|
|
466
|
+
let result = 0;
|
|
467
|
+
points.forEach((point, index) => {
|
|
468
|
+
if (root.contains(point.node) && range.comparePoint(point.node, point.offset) <= 0)
|
|
469
|
+
result = index;
|
|
470
|
+
});
|
|
471
|
+
return result;
|
|
472
|
+
};
|
|
473
|
+
const anchor = position(selection.anchorNode, selection.anchorOffset);
|
|
474
|
+
const focus = position(selection.focusNode, selection.focusOffset);
|
|
475
|
+
return { from: Math.min(anchor, focus), to: Math.max(anchor, focus) };
|
|
476
|
+
};
|
|
477
|
+
var restoreRichTextSelection = (root, selection) => {
|
|
478
|
+
const { points } = readRichTextDOM(root);
|
|
479
|
+
const start = points[Math.min(selection.from, points.length - 1)];
|
|
480
|
+
const end = points[Math.min(selection.to, points.length - 1)];
|
|
481
|
+
if (!start || !end)
|
|
482
|
+
return;
|
|
483
|
+
const range = root.ownerDocument.createRange();
|
|
484
|
+
range.setStart(start.node, start.offset);
|
|
485
|
+
range.setEnd(end.node, end.offset);
|
|
486
|
+
root.ownerDocument.getSelection()?.removeAllRanges();
|
|
487
|
+
root.ownerDocument.getSelection()?.addRange(range);
|
|
488
|
+
};
|
|
489
|
+
var writeRichTextDOM = (root, value, variables) => {
|
|
490
|
+
const children = value.blocks.map((block) => {
|
|
491
|
+
const paragraph = root.ownerDocument.createElement(block.type === "heading" ? "h2" : "div");
|
|
492
|
+
if (block.type === "heading")
|
|
493
|
+
paragraph.style.cssText = "font-size:1.4em;font-weight:700";
|
|
494
|
+
if (block.type === "bullet" || block.type === "numbered") {
|
|
495
|
+
paragraph.dataset.richKind = block.type;
|
|
496
|
+
paragraph.style.cssText = `display:list-item;margin-left:1.5em;list-style-type:${block.type === "bullet" ? "disc" : "decimal"}`;
|
|
497
|
+
}
|
|
498
|
+
for (const inline of block.content) {
|
|
499
|
+
const span = root.ownerDocument.createElement(inline.marks?.href ? "a" : "span");
|
|
500
|
+
if (inline.type === "variable") {
|
|
501
|
+
span.dataset.richVariable = inline.key;
|
|
502
|
+
span.contentEditable = "false";
|
|
503
|
+
span.textContent = `{{${variables.find((item) => item.key === inline.key)?.label ?? inline.key}}}`;
|
|
504
|
+
span.className = "mx-0.5 inline-block rounded bg-indigo-50 px-1 font-mono text-[0.85em] text-indigo-800";
|
|
505
|
+
} else
|
|
506
|
+
span.textContent = inline.text;
|
|
507
|
+
if (inline.marks?.bold)
|
|
508
|
+
span.style.fontWeight = "700";
|
|
509
|
+
if (inline.marks?.italic)
|
|
510
|
+
span.style.fontStyle = "italic";
|
|
511
|
+
if (inline.marks?.underline)
|
|
512
|
+
span.style.textDecoration = "underline";
|
|
513
|
+
if (inline.marks?.href) {
|
|
514
|
+
span.setAttribute("href", inline.marks.href);
|
|
515
|
+
span.style.textDecoration = "underline";
|
|
516
|
+
}
|
|
517
|
+
paragraph.append(span);
|
|
518
|
+
}
|
|
519
|
+
if (!block.content.length)
|
|
520
|
+
paragraph.append(root.ownerDocument.createElement("br"));
|
|
521
|
+
return paragraph;
|
|
522
|
+
});
|
|
523
|
+
root.replaceChildren(...children);
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
// ../../src/components/ui/rich-text-editor.tsx
|
|
527
|
+
import { jsx as jsx5, jsxs as jsxs3, Fragment } from "react/jsx-runtime";
|
|
528
|
+
var labels = {
|
|
529
|
+
en: {
|
|
530
|
+
bold: "Bold",
|
|
531
|
+
italic: "Italic",
|
|
532
|
+
underline: "Underline",
|
|
533
|
+
undo: "Undo",
|
|
534
|
+
redo: "Redo",
|
|
535
|
+
link: "Link",
|
|
536
|
+
unlink: "Remove link",
|
|
537
|
+
linkUrl: "Link URL",
|
|
538
|
+
apply: "Apply link",
|
|
539
|
+
variable: "Insert variable",
|
|
540
|
+
paragraph: "Paragraph",
|
|
541
|
+
heading: "Heading",
|
|
542
|
+
bullet: "Bulleted list",
|
|
543
|
+
numbered: "Numbered list",
|
|
544
|
+
toolbar: "Text formatting",
|
|
545
|
+
invalid: "The content is too long or contains an unsupported value.",
|
|
546
|
+
invalidLink: "Enter an HTTPS or mailto link.",
|
|
547
|
+
cancel: "Cancel"
|
|
548
|
+
},
|
|
549
|
+
fr: {
|
|
550
|
+
bold: "Gras",
|
|
551
|
+
italic: "Italique",
|
|
552
|
+
underline: "Souligné",
|
|
553
|
+
undo: "Annuler",
|
|
554
|
+
redo: "Rétablir",
|
|
555
|
+
link: "Lien",
|
|
556
|
+
unlink: "Retirer le lien",
|
|
557
|
+
linkUrl: "URL du lien",
|
|
558
|
+
apply: "Appliquer le lien",
|
|
559
|
+
variable: "Insérer une variable",
|
|
560
|
+
paragraph: "Paragraphe",
|
|
561
|
+
heading: "Titre",
|
|
562
|
+
bullet: "Liste à puces",
|
|
563
|
+
numbered: "Liste numérotée",
|
|
564
|
+
toolbar: "Mise en forme",
|
|
565
|
+
invalid: "Le contenu est trop long ou contient une valeur non prise en charge.",
|
|
566
|
+
invalidLink: "Saisissez un lien HTTPS ou mailto.",
|
|
567
|
+
cancel: "Annuler"
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
var equivalent = Schema4.toEquivalence(RichTextDocument);
|
|
571
|
+
var ToolButton = ({
|
|
572
|
+
label,
|
|
573
|
+
children,
|
|
574
|
+
onClick,
|
|
575
|
+
pressed,
|
|
576
|
+
disabled
|
|
577
|
+
}) => /* @__PURE__ */ jsx5(Button, {
|
|
578
|
+
type: "button",
|
|
579
|
+
size: "icon-sm",
|
|
580
|
+
variant: pressed ? "secondary" : "ghost",
|
|
581
|
+
"aria-label": label,
|
|
582
|
+
title: label,
|
|
583
|
+
"aria-pressed": pressed,
|
|
584
|
+
disabled,
|
|
585
|
+
onPointerDown: (event) => event.preventDefault(),
|
|
586
|
+
onClick,
|
|
587
|
+
children
|
|
588
|
+
});
|
|
589
|
+
var inlineContent = (inline, variables, values) => {
|
|
590
|
+
let content = inline.type === "text" ? inline.text : values?.[inline.key] ?? /* @__PURE__ */ jsx5("span", {
|
|
591
|
+
contentEditable: false,
|
|
592
|
+
"data-rich-variable": inline.key,
|
|
593
|
+
className: "inline-block rounded bg-indigo-50 px-1.5 font-mono text-[0.85em] text-indigo-800 select-all",
|
|
594
|
+
children: `{{${variables.find((variable) => variable.key === inline.key)?.label ?? inline.key}}}`
|
|
595
|
+
});
|
|
596
|
+
if (inline.marks?.bold)
|
|
597
|
+
content = /* @__PURE__ */ jsx5("strong", {
|
|
598
|
+
children: content
|
|
599
|
+
});
|
|
600
|
+
if (inline.marks?.italic)
|
|
601
|
+
content = /* @__PURE__ */ jsx5("em", {
|
|
602
|
+
children: content
|
|
603
|
+
});
|
|
604
|
+
if (inline.marks?.underline)
|
|
605
|
+
content = /* @__PURE__ */ jsx5("u", {
|
|
606
|
+
children: content
|
|
607
|
+
});
|
|
608
|
+
if (inline.marks?.href)
|
|
609
|
+
content = /* @__PURE__ */ jsx5("a", {
|
|
610
|
+
href: inline.marks.href,
|
|
611
|
+
className: "underline",
|
|
612
|
+
children: content
|
|
613
|
+
});
|
|
614
|
+
return content;
|
|
615
|
+
};
|
|
616
|
+
var RichTextContent = ({
|
|
617
|
+
value,
|
|
618
|
+
variables = [],
|
|
619
|
+
values
|
|
620
|
+
}) => /* @__PURE__ */ jsx5(Fragment, {
|
|
621
|
+
children: value.blocks.map((block, index) => {
|
|
622
|
+
const content = block.content.map((inline, key) => /* @__PURE__ */ jsx5("span", {
|
|
623
|
+
children: inlineContent(inline, variables, values)
|
|
624
|
+
}, key));
|
|
625
|
+
if (block.type === "heading")
|
|
626
|
+
return /* @__PURE__ */ jsx5("h2", {
|
|
627
|
+
className: "text-xl font-semibold",
|
|
628
|
+
children: content.length ? content : /* @__PURE__ */ jsx5("br", {})
|
|
629
|
+
}, index);
|
|
630
|
+
return /* @__PURE__ */ jsx5("div", {
|
|
631
|
+
"data-rich-kind": block.type,
|
|
632
|
+
style: block.type === "bullet" || block.type === "numbered" ? {
|
|
633
|
+
display: "list-item",
|
|
634
|
+
marginLeft: "1.5em",
|
|
635
|
+
listStyleType: block.type === "bullet" ? "disc" : "decimal"
|
|
636
|
+
} : undefined,
|
|
637
|
+
children: content.length ? content : /* @__PURE__ */ jsx5("br", {})
|
|
638
|
+
}, index);
|
|
639
|
+
})
|
|
640
|
+
});
|
|
641
|
+
var RichTextEditor = ({
|
|
642
|
+
value,
|
|
643
|
+
onChange,
|
|
644
|
+
label,
|
|
645
|
+
variables = [],
|
|
646
|
+
disabled,
|
|
647
|
+
locale: override,
|
|
648
|
+
messages,
|
|
649
|
+
className,
|
|
650
|
+
contentStyle,
|
|
651
|
+
singleLine = false,
|
|
652
|
+
maxLength
|
|
653
|
+
}) => {
|
|
654
|
+
const locale = useKrakstackLocale(override);
|
|
655
|
+
const copy = {
|
|
656
|
+
...locale.startsWith("fr") ? labels.fr : labels.en,
|
|
657
|
+
...messages?.[locale.split("-")[0] ?? locale],
|
|
658
|
+
...messages?.[locale]
|
|
659
|
+
};
|
|
660
|
+
const root = useRef(null);
|
|
661
|
+
const model = useRef(value);
|
|
662
|
+
const composing = useRef(false);
|
|
663
|
+
const selected = useRef({ from: 0, to: 0 });
|
|
664
|
+
const history = useRef([
|
|
665
|
+
{ value, selection: { from: 0, to: 0 } }
|
|
666
|
+
]);
|
|
667
|
+
const cursor = useRef(0);
|
|
668
|
+
const [marks, setMarks] = useState({});
|
|
669
|
+
const pendingMarks = useRef(null);
|
|
670
|
+
const [error, setError] = useState(false);
|
|
671
|
+
const [linkOpen, setLinkOpen] = useState(false);
|
|
672
|
+
const [link, setLink] = useState("");
|
|
673
|
+
const [linkError, setLinkError] = useState(false);
|
|
674
|
+
const [revision, setRevision] = useState(0);
|
|
675
|
+
const [historyState, setHistoryState] = useState({
|
|
676
|
+
undo: false,
|
|
677
|
+
redo: false
|
|
678
|
+
});
|
|
679
|
+
const id = useId();
|
|
680
|
+
const [initial] = useState(() => /* @__PURE__ */ jsx5(RichTextContent, {
|
|
681
|
+
value,
|
|
682
|
+
variables
|
|
683
|
+
}));
|
|
684
|
+
const selection = () => {
|
|
685
|
+
const element = root.current;
|
|
686
|
+
if (!element)
|
|
687
|
+
return selected.current;
|
|
688
|
+
const next = readEditorSelection(element, readRichTextDOM(element).points);
|
|
689
|
+
if (next)
|
|
690
|
+
selected.current = next;
|
|
691
|
+
return selected.current;
|
|
692
|
+
};
|
|
693
|
+
const focusEditor = () => {
|
|
694
|
+
const element = root.current;
|
|
695
|
+
if (element) {
|
|
696
|
+
element.focus();
|
|
697
|
+
restoreRichTextSelection(element, selected.current);
|
|
698
|
+
}
|
|
699
|
+
return false;
|
|
700
|
+
};
|
|
701
|
+
const commit = (next, range, write = true, record = true) => {
|
|
702
|
+
const element = root.current;
|
|
703
|
+
if (!element || disabled)
|
|
704
|
+
return;
|
|
705
|
+
const original = next;
|
|
706
|
+
if (singleLine) {
|
|
707
|
+
next = {
|
|
708
|
+
version: 1,
|
|
709
|
+
blocks: [
|
|
710
|
+
{
|
|
711
|
+
type: "paragraph",
|
|
712
|
+
content: next.blocks.flatMap((block, index) => [
|
|
713
|
+
...index ? [{ type: "text", text: " " }] : [],
|
|
714
|
+
...block.content.map((inline) => inline.type === "variable" ? { type: "variable", key: inline.key } : {
|
|
715
|
+
type: "text",
|
|
716
|
+
text: inline.text.replace(/[\r\n]+/g, " ")
|
|
717
|
+
})
|
|
718
|
+
])
|
|
719
|
+
}
|
|
720
|
+
]
|
|
721
|
+
};
|
|
722
|
+
if (!equivalent(original, next))
|
|
723
|
+
write = true;
|
|
724
|
+
}
|
|
725
|
+
if (!Schema4.is(RichTextDocument)(next) || maxLength !== undefined && richTextPlainText(next).length > maxLength) {
|
|
726
|
+
setError(true);
|
|
727
|
+
writeRichTextDOM(element, model.current, variables);
|
|
728
|
+
element.focus();
|
|
729
|
+
restoreRichTextSelection(element, selected.current);
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
setError(false);
|
|
733
|
+
if (record && !equivalent(model.current, next)) {
|
|
734
|
+
history.current[cursor.current] = {
|
|
735
|
+
value: model.current,
|
|
736
|
+
selection: selected.current
|
|
737
|
+
};
|
|
738
|
+
history.current = [
|
|
739
|
+
...history.current.slice(0, cursor.current + 1),
|
|
740
|
+
{ value: next, selection: range }
|
|
741
|
+
].slice(-100);
|
|
742
|
+
cursor.current = history.current.length - 1;
|
|
743
|
+
}
|
|
744
|
+
model.current = next;
|
|
745
|
+
selected.current = range;
|
|
746
|
+
if (write) {
|
|
747
|
+
writeRichTextDOM(element, next, variables);
|
|
748
|
+
element.focus();
|
|
749
|
+
restoreRichTextSelection(element, range);
|
|
750
|
+
}
|
|
751
|
+
setRevision((current) => current + 1);
|
|
752
|
+
setHistoryState({
|
|
753
|
+
undo: cursor.current > 0,
|
|
754
|
+
redo: cursor.current < history.current.length - 1
|
|
755
|
+
});
|
|
756
|
+
onChange(next);
|
|
757
|
+
};
|
|
758
|
+
const travel = (direction) => {
|
|
759
|
+
const index = Math.max(0, Math.min(history.current.length - 1, cursor.current + direction));
|
|
760
|
+
const snapshot = history.current[index];
|
|
761
|
+
if (!snapshot || index === cursor.current)
|
|
762
|
+
return;
|
|
763
|
+
cursor.current = index;
|
|
764
|
+
pendingMarks.current = null;
|
|
765
|
+
commit(snapshot.value, snapshot.selection, true, false);
|
|
766
|
+
};
|
|
767
|
+
const insert = (inline) => {
|
|
768
|
+
const result = replaceRichText(model.current, selection(), inline);
|
|
769
|
+
commit(result.value, result.selection);
|
|
770
|
+
};
|
|
771
|
+
const format = (mark) => {
|
|
772
|
+
const range = selection();
|
|
773
|
+
const next = {
|
|
774
|
+
...pendingMarks.current ?? richTextMarksAt(model.current, range.to),
|
|
775
|
+
[mark]: !marks[mark]
|
|
776
|
+
};
|
|
777
|
+
setMarks(next);
|
|
778
|
+
if (range.from === range.to) {
|
|
779
|
+
pendingMarks.current = next;
|
|
780
|
+
root.current?.focus();
|
|
781
|
+
} else
|
|
782
|
+
commit(formatRichText(model.current, range, mark, next[mark]), range);
|
|
783
|
+
};
|
|
784
|
+
const syncInput = () => {
|
|
785
|
+
const element = root.current;
|
|
786
|
+
if (!element || composing.current)
|
|
787
|
+
return;
|
|
788
|
+
const parsed = readRichTextDOM(element);
|
|
789
|
+
const range = readEditorSelection(element, parsed.points) ?? selected.current;
|
|
790
|
+
if (parsed.value)
|
|
791
|
+
commit(parsed.value, range, false);
|
|
792
|
+
else {
|
|
793
|
+
setError(true);
|
|
794
|
+
writeRichTextDOM(element, model.current, variables);
|
|
795
|
+
restoreRichTextSelection(element, selected.current);
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
const beforeInput = useEffectEvent((event) => {
|
|
799
|
+
if (disabled || composing.current || event.isComposing)
|
|
800
|
+
return;
|
|
801
|
+
if (event.inputType === "historyUndo" || event.inputType === "historyRedo") {
|
|
802
|
+
event.preventDefault();
|
|
803
|
+
travel(event.inputType === "historyUndo" ? -1 : 1);
|
|
804
|
+
} else if (["insertText", "insertParagraph", "insertLineBreak"].includes(event.inputType)) {
|
|
805
|
+
event.preventDefault();
|
|
806
|
+
if (singleLine && event.inputType !== "insertText")
|
|
807
|
+
return;
|
|
808
|
+
const range = selection();
|
|
809
|
+
insert({
|
|
810
|
+
type: "text",
|
|
811
|
+
text: event.inputType === "insertText" ? event.data ?? "" : `
|
|
812
|
+
`,
|
|
813
|
+
marks: pendingMarks.current ?? richTextMarksAt(model.current, range.to)
|
|
814
|
+
});
|
|
815
|
+
} else if (event.inputType === "formatBold" || event.inputType === "formatItalic" || event.inputType === "formatUnderline") {
|
|
816
|
+
event.preventDefault();
|
|
817
|
+
if (!singleLine)
|
|
818
|
+
format(event.inputType === "formatBold" ? "bold" : event.inputType === "formatItalic" ? "italic" : "underline");
|
|
819
|
+
}
|
|
820
|
+
});
|
|
821
|
+
useEffect(() => {
|
|
822
|
+
const element = root.current;
|
|
823
|
+
if (!element)
|
|
824
|
+
return;
|
|
825
|
+
const listener = (event) => beforeInput(event);
|
|
826
|
+
element.addEventListener("beforeinput", listener);
|
|
827
|
+
return () => element.removeEventListener("beforeinput", listener);
|
|
828
|
+
}, []);
|
|
829
|
+
const remember = useEffectEvent(() => {
|
|
830
|
+
const element = root.current;
|
|
831
|
+
if (!element || document.activeElement !== element || composing.current)
|
|
832
|
+
return;
|
|
833
|
+
const previous = selected.current;
|
|
834
|
+
const range = selection();
|
|
835
|
+
if (previous.from !== range.from || previous.to !== range.to)
|
|
836
|
+
pendingMarks.current = null;
|
|
837
|
+
setMarks(pendingMarks.current ?? richTextMarksAt(model.current, range.to));
|
|
838
|
+
});
|
|
839
|
+
useEffect(() => {
|
|
840
|
+
document.addEventListener("selectionchange", remember);
|
|
841
|
+
return () => document.removeEventListener("selectionchange", remember);
|
|
842
|
+
}, []);
|
|
843
|
+
useEffect(() => {
|
|
844
|
+
const element = root.current;
|
|
845
|
+
if (!element || composing.current)
|
|
846
|
+
return;
|
|
847
|
+
if (!equivalent(model.current, value)) {
|
|
848
|
+
model.current = value;
|
|
849
|
+
history.current = [{ value, selection: { from: 0, to: 0 } }];
|
|
850
|
+
cursor.current = 0;
|
|
851
|
+
selected.current = { from: 0, to: 0 };
|
|
852
|
+
writeRichTextDOM(element, value, variables);
|
|
853
|
+
setRevision((current) => current + 1);
|
|
854
|
+
setHistoryState({ undo: false, redo: false });
|
|
855
|
+
}
|
|
856
|
+
}, [value, variables]);
|
|
857
|
+
return /* @__PURE__ */ jsxs3("div", {
|
|
858
|
+
"data-slot": "rich-text-editor",
|
|
859
|
+
className: `bg-background text-foreground overflow-hidden rounded-xl border ${className ?? ""}`,
|
|
860
|
+
"data-revision": revision,
|
|
861
|
+
children: [
|
|
862
|
+
/* @__PURE__ */ jsxs3("div", {
|
|
863
|
+
role: "group",
|
|
864
|
+
"aria-label": copy.toolbar,
|
|
865
|
+
className: "bg-muted/30 flex flex-wrap items-center gap-1 border-b p-2",
|
|
866
|
+
children: [
|
|
867
|
+
/* @__PURE__ */ jsx5(ToolButton, {
|
|
868
|
+
label: copy.undo,
|
|
869
|
+
disabled: disabled || !historyState.undo,
|
|
870
|
+
onClick: () => travel(-1),
|
|
871
|
+
children: /* @__PURE__ */ jsx5(Undo2, {})
|
|
872
|
+
}),
|
|
873
|
+
/* @__PURE__ */ jsx5(ToolButton, {
|
|
874
|
+
label: copy.redo,
|
|
875
|
+
disabled: disabled || !historyState.redo,
|
|
876
|
+
onClick: () => travel(1),
|
|
877
|
+
children: /* @__PURE__ */ jsx5(Redo2, {})
|
|
878
|
+
}),
|
|
879
|
+
!singleLine ? /* @__PURE__ */ jsxs3(Fragment, {
|
|
880
|
+
children: [
|
|
881
|
+
/* @__PURE__ */ jsx5("span", {
|
|
882
|
+
className: "mx-1 h-5 border-l",
|
|
883
|
+
"aria-hidden": true
|
|
884
|
+
}),
|
|
885
|
+
/* @__PURE__ */ jsxs3(Select, {
|
|
886
|
+
disabled,
|
|
887
|
+
defaultValue: "paragraph",
|
|
888
|
+
items: [
|
|
889
|
+
{ value: "paragraph", label: copy.paragraph },
|
|
890
|
+
{ value: "heading", label: copy.heading },
|
|
891
|
+
{ value: "bullet", label: copy.bullet },
|
|
892
|
+
{ value: "numbered", label: copy.numbered }
|
|
893
|
+
],
|
|
894
|
+
onOpenChange: (open) => {
|
|
895
|
+
if (open)
|
|
896
|
+
selection();
|
|
897
|
+
},
|
|
898
|
+
onValueChange: (value) => {
|
|
899
|
+
const type = Schema4.decodeUnknownSync(RichTextBlockKind)(value);
|
|
900
|
+
const range = selection();
|
|
901
|
+
commit(setRichTextBlock(model.current, range, type), range);
|
|
902
|
+
},
|
|
903
|
+
children: [
|
|
904
|
+
/* @__PURE__ */ jsx5(SelectTrigger, {
|
|
905
|
+
size: "sm",
|
|
906
|
+
"aria-label": copy.paragraph,
|
|
907
|
+
className: "max-w-40 text-xs",
|
|
908
|
+
onPointerDown: selection,
|
|
909
|
+
onFocus: selection,
|
|
910
|
+
children: /* @__PURE__ */ jsx5(SelectValue, {})
|
|
911
|
+
}),
|
|
912
|
+
/* @__PURE__ */ jsxs3(SelectContent, {
|
|
913
|
+
align: "start",
|
|
914
|
+
alignItemWithTrigger: false,
|
|
915
|
+
finalFocus: focusEditor,
|
|
916
|
+
children: [
|
|
917
|
+
/* @__PURE__ */ jsx5(SelectItem, {
|
|
918
|
+
value: "paragraph",
|
|
919
|
+
children: copy.paragraph
|
|
920
|
+
}),
|
|
921
|
+
/* @__PURE__ */ jsx5(SelectItem, {
|
|
922
|
+
value: "heading",
|
|
923
|
+
children: copy.heading
|
|
924
|
+
}),
|
|
925
|
+
/* @__PURE__ */ jsx5(SelectItem, {
|
|
926
|
+
value: "bullet",
|
|
927
|
+
children: copy.bullet
|
|
928
|
+
}),
|
|
929
|
+
/* @__PURE__ */ jsx5(SelectItem, {
|
|
930
|
+
value: "numbered",
|
|
931
|
+
children: copy.numbered
|
|
932
|
+
})
|
|
933
|
+
]
|
|
934
|
+
})
|
|
935
|
+
]
|
|
936
|
+
}),
|
|
937
|
+
/* @__PURE__ */ jsx5(ToolButton, {
|
|
938
|
+
label: copy.bold,
|
|
939
|
+
disabled,
|
|
940
|
+
pressed: !!marks.bold,
|
|
941
|
+
onClick: () => format("bold"),
|
|
942
|
+
children: /* @__PURE__ */ jsx5(Bold, {})
|
|
943
|
+
}),
|
|
944
|
+
/* @__PURE__ */ jsx5(ToolButton, {
|
|
945
|
+
label: copy.italic,
|
|
946
|
+
disabled,
|
|
947
|
+
pressed: !!marks.italic,
|
|
948
|
+
onClick: () => format("italic"),
|
|
949
|
+
children: /* @__PURE__ */ jsx5(Italic, {})
|
|
950
|
+
}),
|
|
951
|
+
/* @__PURE__ */ jsx5(ToolButton, {
|
|
952
|
+
label: copy.underline,
|
|
953
|
+
disabled,
|
|
954
|
+
pressed: !!marks.underline,
|
|
955
|
+
onClick: () => format("underline"),
|
|
956
|
+
children: /* @__PURE__ */ jsx5(Underline, {})
|
|
957
|
+
}),
|
|
958
|
+
/* @__PURE__ */ jsx5(ToolButton, {
|
|
959
|
+
label: copy.link,
|
|
960
|
+
disabled,
|
|
961
|
+
pressed: linkOpen || !!marks.href,
|
|
962
|
+
onClick: () => {
|
|
963
|
+
selection();
|
|
964
|
+
setLink(marks.href ?? "");
|
|
965
|
+
setLinkError(false);
|
|
966
|
+
setLinkOpen(!linkOpen);
|
|
967
|
+
},
|
|
968
|
+
children: /* @__PURE__ */ jsx5(Link2, {})
|
|
969
|
+
})
|
|
970
|
+
]
|
|
971
|
+
}) : null,
|
|
972
|
+
variables.length ? /* @__PURE__ */ jsxs3(DropdownMenu, {
|
|
973
|
+
onOpenChange: (open) => {
|
|
974
|
+
if (open)
|
|
975
|
+
selection();
|
|
976
|
+
},
|
|
977
|
+
children: [
|
|
978
|
+
/* @__PURE__ */ jsxs3(DropdownMenuTrigger, {
|
|
979
|
+
disabled,
|
|
980
|
+
onPointerDown: selection,
|
|
981
|
+
onFocus: selection,
|
|
982
|
+
render: /* @__PURE__ */ jsx5(Button, {
|
|
983
|
+
type: "button",
|
|
984
|
+
variant: "outline",
|
|
985
|
+
size: "sm",
|
|
986
|
+
className: "ml-auto text-xs"
|
|
987
|
+
}),
|
|
988
|
+
children: [
|
|
989
|
+
/* @__PURE__ */ jsx5(Braces, {
|
|
990
|
+
className: "size-3.5",
|
|
991
|
+
"aria-hidden": true
|
|
992
|
+
}),
|
|
993
|
+
copy.variable,
|
|
994
|
+
/* @__PURE__ */ jsx5(ChevronDown, {
|
|
995
|
+
className: "size-3.5",
|
|
996
|
+
"aria-hidden": true
|
|
997
|
+
})
|
|
998
|
+
]
|
|
999
|
+
}),
|
|
1000
|
+
/* @__PURE__ */ jsx5(DropdownMenuContent, {
|
|
1001
|
+
align: "end",
|
|
1002
|
+
finalFocus: focusEditor,
|
|
1003
|
+
children: variables.map((variable) => /* @__PURE__ */ jsx5(DropdownMenuItem, {
|
|
1004
|
+
onClick: () => insert({ type: "variable", key: variable.key }),
|
|
1005
|
+
children: variable.label
|
|
1006
|
+
}, variable.key))
|
|
1007
|
+
})
|
|
1008
|
+
]
|
|
1009
|
+
}) : null
|
|
1010
|
+
]
|
|
1011
|
+
}),
|
|
1012
|
+
linkOpen ? /* @__PURE__ */ jsxs3("div", {
|
|
1013
|
+
className: "flex flex-wrap items-end gap-2 border-b p-3",
|
|
1014
|
+
children: [
|
|
1015
|
+
/* @__PURE__ */ jsxs3("label", {
|
|
1016
|
+
className: "grid min-w-0 flex-1 gap-1 text-xs",
|
|
1017
|
+
children: [
|
|
1018
|
+
copy.linkUrl,
|
|
1019
|
+
/* @__PURE__ */ jsx5(Input, {
|
|
1020
|
+
value: link,
|
|
1021
|
+
"aria-invalid": linkError,
|
|
1022
|
+
placeholder: "https://",
|
|
1023
|
+
onChange: (event) => setLink(event.target.value)
|
|
1024
|
+
})
|
|
1025
|
+
]
|
|
1026
|
+
}),
|
|
1027
|
+
/* @__PURE__ */ jsx5(Button, {
|
|
1028
|
+
type: "button",
|
|
1029
|
+
size: "sm",
|
|
1030
|
+
disabled,
|
|
1031
|
+
onClick: () => {
|
|
1032
|
+
if (!Schema4.is(RichTextLink)(link)) {
|
|
1033
|
+
setLinkError(true);
|
|
1034
|
+
return;
|
|
1035
|
+
}
|
|
1036
|
+
commit(formatRichText(model.current, selected.current, "href", link), selected.current);
|
|
1037
|
+
setLinkOpen(false);
|
|
1038
|
+
},
|
|
1039
|
+
children: copy.apply
|
|
1040
|
+
}),
|
|
1041
|
+
marks.href ? /* @__PURE__ */ jsxs3(Button, {
|
|
1042
|
+
type: "button",
|
|
1043
|
+
size: "sm",
|
|
1044
|
+
variant: "outline",
|
|
1045
|
+
disabled,
|
|
1046
|
+
onClick: () => {
|
|
1047
|
+
commit(formatRichText(model.current, selected.current, "href", undefined), selected.current);
|
|
1048
|
+
setLinkOpen(false);
|
|
1049
|
+
setLinkError(false);
|
|
1050
|
+
},
|
|
1051
|
+
children: [
|
|
1052
|
+
/* @__PURE__ */ jsx5(Unlink, {
|
|
1053
|
+
"aria-hidden": true
|
|
1054
|
+
}),
|
|
1055
|
+
copy.unlink
|
|
1056
|
+
]
|
|
1057
|
+
}) : null,
|
|
1058
|
+
/* @__PURE__ */ jsx5(Button, {
|
|
1059
|
+
type: "button",
|
|
1060
|
+
size: "sm",
|
|
1061
|
+
variant: "ghost",
|
|
1062
|
+
onClick: () => setLinkOpen(false),
|
|
1063
|
+
children: copy.cancel
|
|
1064
|
+
}),
|
|
1065
|
+
linkError ? /* @__PURE__ */ jsx5("p", {
|
|
1066
|
+
role: "alert",
|
|
1067
|
+
className: "text-destructive w-full text-xs",
|
|
1068
|
+
children: copy.invalidLink
|
|
1069
|
+
}) : null
|
|
1070
|
+
]
|
|
1071
|
+
}) : null,
|
|
1072
|
+
/* @__PURE__ */ jsx5("div", {
|
|
1073
|
+
ref: root,
|
|
1074
|
+
style: contentStyle,
|
|
1075
|
+
role: "textbox",
|
|
1076
|
+
"aria-label": label,
|
|
1077
|
+
"aria-multiline": !singleLine,
|
|
1078
|
+
"aria-disabled": disabled,
|
|
1079
|
+
"aria-describedby": error ? `${id}-error` : undefined,
|
|
1080
|
+
contentEditable: !disabled,
|
|
1081
|
+
suppressContentEditableWarning: true,
|
|
1082
|
+
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]`,
|
|
1083
|
+
onInput: syncInput,
|
|
1084
|
+
onKeyUp: selection,
|
|
1085
|
+
onPointerUp: selection,
|
|
1086
|
+
onBlur: selection,
|
|
1087
|
+
onCompositionStart: () => {
|
|
1088
|
+
composing.current = true;
|
|
1089
|
+
},
|
|
1090
|
+
onCompositionEnd: () => {
|
|
1091
|
+
composing.current = false;
|
|
1092
|
+
syncInput();
|
|
1093
|
+
},
|
|
1094
|
+
onPaste: (event) => {
|
|
1095
|
+
event.preventDefault();
|
|
1096
|
+
const text = event.clipboardData.getData("text/plain");
|
|
1097
|
+
insert({
|
|
1098
|
+
type: "text",
|
|
1099
|
+
text: singleLine ? text.replace(/[\r\n]+/g, " ") : text
|
|
1100
|
+
});
|
|
1101
|
+
},
|
|
1102
|
+
onDrop: (event) => event.preventDefault(),
|
|
1103
|
+
onClick: (event) => {
|
|
1104
|
+
if (event.target instanceof HTMLElement && event.target.closest("a"))
|
|
1105
|
+
event.preventDefault();
|
|
1106
|
+
},
|
|
1107
|
+
onKeyDown: (event) => {
|
|
1108
|
+
if (event.nativeEvent.isComposing)
|
|
1109
|
+
return;
|
|
1110
|
+
if ((event.ctrlKey || event.metaKey) && !event.altKey) {
|
|
1111
|
+
const key = event.key.toLowerCase();
|
|
1112
|
+
if (["b", "i", "u", "z", "y"].includes(key)) {
|
|
1113
|
+
event.preventDefault();
|
|
1114
|
+
if (key === "z" || key === "y")
|
|
1115
|
+
travel(key === "y" || event.shiftKey ? 1 : -1);
|
|
1116
|
+
else if (!singleLine)
|
|
1117
|
+
format(key === "b" ? "bold" : key === "i" ? "italic" : "underline");
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
},
|
|
1121
|
+
children: initial
|
|
1122
|
+
}),
|
|
1123
|
+
error ? /* @__PURE__ */ jsx5("p", {
|
|
1124
|
+
id: `${id}-error`,
|
|
1125
|
+
role: "alert",
|
|
1126
|
+
className: "text-destructive px-4 pb-3 text-xs",
|
|
1127
|
+
children: copy.invalid
|
|
1128
|
+
}) : null
|
|
1129
|
+
]
|
|
1130
|
+
});
|
|
1131
|
+
};
|
|
1132
|
+
export {
|
|
1133
|
+
RichTextContent,
|
|
1134
|
+
RichTextEditor
|
|
1135
|
+
};
|