@krak-stack/registry 0.10.18 → 0.10.20

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.
Files changed (47) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +18 -0
  3. package/dist/components/ui/code-block.js +7 -7
  4. package/dist/components/ui/copy-button.js +7 -7
  5. package/dist/components/ui/copy-value.d.ts +4 -1
  6. package/dist/components/ui/copy-value.js +49 -16
  7. package/dist/components/ui/data-table.d.ts +3 -3
  8. package/dist/components/ui/data-table.js +23 -17
  9. package/dist/components/ui/editing-locale-switcher.js +7 -7
  10. package/dist/components/ui/effect-form.js +8 -8
  11. package/dist/components/ui/email-composer.js +871 -0
  12. package/dist/components/ui/email-editor.d.ts +3 -1
  13. package/dist/components/ui/email-editor.js +294 -471
  14. package/dist/components/ui/error-component.js +7 -7
  15. package/dist/components/ui/form.d.ts +7 -7
  16. package/dist/components/ui/form.js +18 -14
  17. package/dist/components/ui/icon-input.js +1 -1
  18. package/dist/components/ui/map-menu.d.ts +1 -1
  19. package/dist/components/ui/map-menu.js +183 -60
  20. package/dist/components/ui/pagination.js +7 -7
  21. package/dist/components/ui/phone-number-input.js +1 -1
  22. package/dist/components/ui/rich-text-editor.d.ts +15 -13
  23. package/dist/components/ui/rich-text-editor.js +280 -661
  24. package/dist/components/ui/select-with-custom-input.js +7 -7
  25. package/dist/components/ui/select.js +159 -0
  26. package/dist/components/ui/timezone-select.js +1 -1
  27. package/dist/components/ui/virtualized-combobox.js +1 -1
  28. package/dist/lib/docs.js +7 -7
  29. package/dist/lib/email-composer/index.js +110 -0
  30. package/dist/lib/email-composer/schema.js +13 -0
  31. package/dist/lib/email-document/legacy-rich-text/editor.d.ts +70 -0
  32. package/dist/lib/email-document/legacy-rich-text/schema.d.ts +65 -0
  33. package/dist/lib/email-document/legacy-rich-text/tiptap.d.ts +39 -0
  34. package/dist/lib/email-document/model.d.ts +1 -1
  35. package/dist/lib/email-document/model.js +2 -2
  36. package/dist/lib/email-document/render.js +2 -2
  37. package/dist/lib/email-document/schema.js +1 -1
  38. package/dist/lib/rich-text/model.js +79 -111
  39. package/dist/lib/rich-text/schema.d.ts +5 -55
  40. package/dist/lib/rich-text/schema.js +17 -36
  41. package/dist/lib/rich-text/tiptap.d.ts +12 -0
  42. package/dist/services/agent/client/index.js +7 -7
  43. package/dist/services/events/client/audit-log-table.js +23 -17
  44. package/dist/services/webhooks/client/delivery-table.js +30 -24
  45. package/package.json +24 -1
  46. package/dist/lib/rich-text/dom.d.ts +0 -40
  47. /package/dist/lib/{rich-text → email-document/legacy-rich-text}/model.d.ts +0 -0
@@ -1,10 +1,13 @@
1
1
  // ../../src/components/ui/rich-text-editor.tsx
2
- import { Schema as Schema4 } from "effect";
2
+ import { Schema as Schema3 } from "effect";
3
+ import { EditorContent, useEditor } from "@tiptap/react";
4
+ import { EditorState } from "@tiptap/pm/state";
5
+ import { closeHistory } from "@tiptap/pm/history";
6
+ import { renderToReactElement } from "@tiptap/static-renderer/pm/react";
3
7
  import {
4
8
  useEffect,
5
9
  useEffectEvent,
6
10
  useId,
7
- useRef,
8
11
  useState
9
12
  } from "react";
10
13
  import {
@@ -15,8 +18,7 @@ import {
15
18
  Redo2,
16
19
  Link2,
17
20
  Unlink,
18
- Braces,
19
- ChevronDown
21
+ Braces
20
22
  } from "lucide-react";
21
23
 
22
24
  // ../../src/components/ui/button.tsx
@@ -101,7 +103,7 @@ function SelectTrigger({
101
103
  return /* @__PURE__ */ jsxs(SelectPrimitive.Trigger, {
102
104
  "data-slot": "select-trigger",
103
105
  "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),
106
+ className: cn("border-input focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 data-placeholder:text-muted-foreground dark:bg-input/30 dark:hover:bg-input/50 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 flex w-fit items-center justify-between gap-1.5 rounded-md border bg-transparent py-2 pr-2 pl-2.5 text-sm whitespace-nowrap shadow-xs transition-[color,box-shadow] outline-none focus-visible:ring-3 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:ring-3 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 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", className),
105
107
  ...props,
106
108
  children: [
107
109
  children,
@@ -118,9 +120,9 @@ function SelectContent({
118
120
  children,
119
121
  side = "bottom",
120
122
  sideOffset = 4,
121
- align = "center",
123
+ align = "start",
122
124
  alignOffset = 0,
123
- alignItemWithTrigger = true,
125
+ alignItemWithTrigger = false,
124
126
  ...props
125
127
  }) {
126
128
  return /* @__PURE__ */ jsx3(SelectPrimitive.Portal, {
@@ -134,7 +136,7 @@ function SelectContent({
134
136
  children: /* @__PURE__ */ jsxs(SelectPrimitive.Popup, {
135
137
  "data-slot": "select-content",
136
138
  "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),
139
+ className: cn("bg-popover text-popover-foreground ring-foreground/10 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 relative isolate z-50 max-h-(--available-height) w-(--anchor-width) max-w-(--available-width) min-w-0 origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-md shadow-md ring-1 duration-100 data-[align-trigger=true]:animate-none", className),
138
140
  ...props,
139
141
  children: [
140
142
  /* @__PURE__ */ jsx3(SelectScrollUpButton, {}),
@@ -154,7 +156,7 @@ function SelectItem({
154
156
  }) {
155
157
  return /* @__PURE__ */ jsxs(SelectPrimitive.Item, {
156
158
  "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),
159
+ className: cn("focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground 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 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
160
  ...props,
159
161
  children: [
160
162
  /* @__PURE__ */ jsx3(SelectPrimitive.ItemText, {
@@ -178,7 +180,7 @@ function SelectScrollUpButton({
178
180
  }) {
179
181
  return /* @__PURE__ */ jsx3(SelectPrimitive.ScrollUpArrow, {
180
182
  "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),
183
+ className: cn("bg-popover top-0 z-10 flex w-full cursor-default items-center justify-center py-1 [&_svg:not([class*='size-'])]:size-4", className),
182
184
  ...props,
183
185
  children: /* @__PURE__ */ jsx3(ChevronUpIcon, {})
184
186
  });
@@ -189,7 +191,7 @@ function SelectScrollDownButton({
189
191
  }) {
190
192
  return /* @__PURE__ */ jsx3(SelectPrimitive.ScrollDownArrow, {
191
193
  "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),
194
+ className: cn("bg-popover bottom-0 z-10 flex w-full cursor-default items-center justify-center py-1 [&_svg:not([class*='size-'])]:size-4", className),
193
195
  ...props,
194
196
  children: /* @__PURE__ */ jsx3(ChevronDownIcon, {})
195
197
  });
@@ -260,271 +262,94 @@ var RichTextLink = Schema.String.check(Schema.isMaxLength(2000), Schema.makeFilt
260
262
  const url = URL.parse(value);
261
263
  return url !== null && ["https:", "mailto:"].includes(url.protocol) && !url.username && !url.password;
262
264
  })).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" });
265
+ var attributes = Schema.Record(Schema.String, Schema.Json);
266
+ var variableKey = Schema.String.check(Schema.isPattern(/^[a-zA-Z][a-zA-Z0-9_.]{0,79}$/));
267
+ var mark = Schema.Struct({
268
+ type: Schema.String,
269
+ attrs: Schema.optional(attributes)
270
+ }).check(Schema.makeFilter((value) => value.type !== "link" || Schema.is(RichTextLink)(value.attrs?.href)));
271
+ var TiptapNode = Schema.Struct({
272
+ type: Schema.String,
273
+ attrs: Schema.optional(attributes),
274
+ content: Schema.optional(Schema.mutable(Schema.Array(Schema.suspend(() => TiptapNode)))),
275
+ marks: Schema.optional(Schema.mutable(Schema.Array(mark))),
276
+ text: Schema.optional(Schema.String)
277
+ }).check(Schema.makeFilter((node) => node.type !== "mention" || Schema.is(variableKey)(node.attrs?.id))).annotate({ identifier: "TiptapNode" });
278
+ var RichTextDocument = TiptapNode.check(Schema.makeFilter((node) => node.type === "doc")).annotate({ identifier: "TiptapDocument" });
294
279
  var RichTextVariable = Schema.Struct({
295
- key: Schema.String.check(Schema.isPattern(/^[a-zA-Z][a-zA-Z0-9_.]{0,79}$/)),
280
+ key: variableKey,
296
281
  label: Schema.String,
297
282
  type: Schema.Literals(["text", "url"]),
298
283
  sample: Schema.String
299
284
  }).annotate({ identifier: "RichTextVariable" });
300
285
 
301
- // ../../src/lib/rich-text/model.ts
286
+ // ../../src/lib/rich-text/tiptap.ts
287
+ import { Extension } from "@tiptap/core";
288
+ import Mention from "@tiptap/extension-mention";
289
+ import StarterKit from "@tiptap/starter-kit";
290
+ import { Plugin } from "@tiptap/pm/state";
302
291
  import { Schema as Schema2 } from "effect";
303
- var sameMarks = Schema2.toEquivalence(RichTextMarks);
304
- var emptyRichText = () => ({
305
- version: 1,
306
- blocks: [{ type: "paragraph", content: [] }]
292
+ var richTextMention = (variables = () => []) => Mention.configure({
293
+ deleteTriggerWithBackspace: true,
294
+ HTMLAttributes: {
295
+ class: "rounded bg-primary/10 px-1 font-mono text-primary",
296
+ contenteditable: "false"
297
+ },
298
+ renderText: ({ node }) => `{{${node.attrs.id}}}`,
299
+ renderHTML: ({ node }) => {
300
+ const id = Schema2.decodeUnknownSync(RichTextVariable.fields.key)(node.attrs.id);
301
+ const label = variables().find((variable) => variable.key === id)?.label ?? node.attrs.label ?? id;
302
+ return [
303
+ "span",
304
+ {
305
+ "data-type": "mention",
306
+ "data-id": id,
307
+ contenteditable: "false",
308
+ class: "rounded bg-primary/10 px-1 font-mono text-primary"
309
+ },
310
+ `{{${label}}}`
311
+ ];
312
+ },
313
+ suggestion: { items: () => [] }
307
314
  });
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;
315
+ var richTextExtensions = ({
316
+ variables = () => []
317
+ } = {}) => [
318
+ StarterKit.configure({
319
+ link: {
320
+ openOnClick: false,
321
+ autolink: false,
322
+ linkOnPaste: false,
323
+ isAllowedUri: (url) => Schema2.is(RichTextLink)(url)
324
+ },
325
+ undoRedo: { depth: 100 },
326
+ heading: { levels: [1, 2, 3] }
327
+ }),
328
+ richTextMention(variables)
329
+ ];
330
+ var richTextValidation = ({
331
+ maxLength,
332
+ onInvalid
333
+ }) => Extension.create({
334
+ name: "richTextValidation",
335
+ addProseMirrorPlugins: () => [
336
+ new Plugin({
337
+ filterTransaction: (transaction) => {
338
+ if (!transaction.docChanged)
339
+ return true;
340
+ const limit = maxLength();
341
+ const text = transaction.doc.textBetween(0, transaction.doc.content.size, `
342
+ `, (node) => node.type.name === "mention" ? `{{${node.attrs.id}}}` : "");
343
+ const valid = Schema2.is(RichTextDocument)(transaction.doc.toJSON()) && (limit === undefined || text.length <= limit);
344
+ onInvalid(!valid);
345
+ return valid;
346
+ }
360
347
  })
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
348
+ ]
376
349
  });
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
350
 
526
351
  // ../../src/components/ui/rich-text-editor.tsx
527
- import { jsx as jsx5, jsxs as jsxs3, Fragment } from "react/jsx-runtime";
352
+ import { jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
528
353
  var labels = {
529
354
  en: {
530
355
  bold: "Bold",
@@ -536,6 +361,7 @@ var labels = {
536
361
  unlink: "Remove link",
537
362
  linkUrl: "Link URL",
538
363
  apply: "Apply link",
364
+ cancel: "Cancel",
539
365
  variable: "Insert variable",
540
366
  paragraph: "Paragraph",
541
367
  heading: "Heading",
@@ -543,8 +369,7 @@ var labels = {
543
369
  numbered: "Numbered list",
544
370
  toolbar: "Text formatting",
545
371
  invalid: "The content is too long or contains an unsupported value.",
546
- invalidLink: "Enter an HTTPS or mailto link.",
547
- cancel: "Cancel"
372
+ invalidLink: "Enter an HTTPS or mailto link."
548
373
  },
549
374
  fr: {
550
375
  bold: "Gras",
@@ -556,6 +381,7 @@ var labels = {
556
381
  unlink: "Retirer le lien",
557
382
  linkUrl: "URL du lien",
558
383
  apply: "Appliquer le lien",
384
+ cancel: "Annuler",
559
385
  variable: "Insérer une variable",
560
386
  paragraph: "Paragraphe",
561
387
  heading: "Titre",
@@ -563,11 +389,10 @@ var labels = {
563
389
  numbered: "Liste numérotée",
564
390
  toolbar: "Mise en forme",
565
391
  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"
392
+ invalidLink: "Saisissez un lien HTTPS ou mailto."
568
393
  }
569
394
  };
570
- var equivalent = Schema4.toEquivalence(RichTextDocument);
395
+ var replacementMeta = "krakstackExternalReplacement";
571
396
  var ToolButton = ({
572
397
  label,
573
398
  children,
@@ -586,57 +411,21 @@ var ToolButton = ({
586
411
  onClick,
587
412
  children
588
413
  });
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
414
  var RichTextContent = ({
617
415
  value,
618
416
  variables = [],
619
417
  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
- })
418
+ }) => renderToReactElement({
419
+ extensions: richTextExtensions({ variables: () => variables }),
420
+ content: Schema3.decodeSync(RichTextDocument)(value),
421
+ options: {
422
+ nodeMapping: {
423
+ mention: ({ node }) => {
424
+ const key = Schema3.decodeUnknownSync(RichTextVariable.fields.key)(node.attrs.id);
425
+ return values?.[key] ?? `{{${variables.find((variable) => variable.key === key)?.label ?? key}}}`;
426
+ }
427
+ }
428
+ }
640
429
  });
641
430
  var RichTextEditor = ({
642
431
  value,
@@ -648,216 +437,94 @@ var RichTextEditor = ({
648
437
  messages,
649
438
  className,
650
439
  contentStyle,
651
- singleLine = false,
652
- maxLength
440
+ maxLength,
441
+ extensions,
442
+ onReady
653
443
  }) => {
654
444
  const locale = useKrakstackLocale(override);
445
+ const language = locale.startsWith("fr") ? "fr" : "en";
655
446
  const copy = {
656
- ...locale.startsWith("fr") ? labels.fr : labels.en,
657
- ...messages?.[locale.split("-")[0] ?? locale],
447
+ ...labels[language],
448
+ ...messages?.[language],
658
449
  ...messages?.[locale]
659
450
  };
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
451
  const [error, setError] = useState(false);
671
452
  const [linkOpen, setLinkOpen] = useState(false);
672
453
  const [link, setLink] = useState("");
673
454
  const [linkError, setLinkError] = useState(false);
674
455
  const [revision, setRevision] = useState(0);
675
- const [historyState, setHistoryState] = useState({
676
- undo: false,
677
- redo: false
678
- });
679
456
  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);
457
+ const getMaxLength = useEffectEvent(() => maxLength);
458
+ const getVariables = useEffectEvent(() => variables);
459
+ const editor = useEditor({
460
+ immediatelyRender: false,
461
+ extensions: [
462
+ ...extensions ?? richTextExtensions({ variables: () => getVariables() }),
463
+ richTextValidation({
464
+ maxLength: () => getMaxLength(),
465
+ onInvalid: setError
466
+ })
467
+ ],
468
+ content: Schema3.decodeSync(RichTextDocument)(value),
469
+ editable: !disabled,
470
+ onCreate: ({ editor }) => onReady?.(editor),
471
+ onUpdate: ({ editor }) => onChange(editor.getJSON()),
472
+ onTransaction: ({ transaction }) => {
473
+ const replacement = transaction.getMeta(replacementMeta) === true;
474
+ if (replacement) {
475
+ setError(false);
476
+ setLinkOpen(false);
477
+ }
478
+ if (replacement || transaction.docChanged || transaction.selectionSet || transaction.storedMarksSet)
479
+ setRevision((current) => current + 1);
480
+ },
481
+ editorProps: {
482
+ attributes: {
483
+ role: "textbox",
484
+ "aria-label": label,
485
+ "aria-multiline": "true",
486
+ "aria-disabled": String(!!disabled),
487
+ "aria-describedby": error ? `${id}-error` : "",
488
+ class: `min-h-40 px-4 py-3 outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset ${contentStyle?.fontSize === undefined ? "text-sm" : ""} ${contentStyle?.lineHeight === undefined ? "leading-relaxed" : ""} [&_h1]:text-2xl [&_h2]:text-xl [&_h3]:text-lg [&_h1]:font-semibold [&_h2]:font-semibold [&_h3]:font-semibold [&_ul]:list-disc [&_ol]:list-decimal [&_ul]:pl-6 [&_ol]:pl-6 [&_a]:underline [&_blockquote]:border-l-2 [&_blockquote]:pl-4 [&_pre]:rounded [&_pre]:bg-muted [&_pre]:p-3`
489
+ }
698
490
  }
491
+ }, [extensions]);
492
+ const focusEditor = () => {
493
+ editor?.view.focus();
699
494
  return false;
700
495
  };
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
496
  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)
497
+ if (!editor)
832
498
  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
- });
499
+ const incoming = editor.schema.nodeFromJSON(Schema3.decodeSync(RichTextDocument)(value));
500
+ incoming.check();
501
+ if (!editor.state.doc.eq(incoming)) {
502
+ editor.view.updateState(EditorState.create({
503
+ schema: editor.schema,
504
+ doc: incoming,
505
+ plugins: editor.state.plugins
506
+ }));
507
+ editor.view.dispatch(editor.state.tr.setMeta(replacementMeta, true));
508
+ }
509
+ }, [editor, value]);
839
510
  useEffect(() => {
840
- document.addEventListener("selectionchange", remember);
841
- return () => document.removeEventListener("selectionchange", remember);
842
- }, []);
511
+ editor?.setEditable(!disabled, false);
512
+ }, [editor, disabled]);
843
513
  useEffect(() => {
844
- const element = root.current;
845
- if (!element || composing.current)
514
+ if (!editor)
846
515
  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 });
516
+ for (const chip of editor.view.dom.querySelectorAll('[data-type="mention"]')) {
517
+ const key = chip.getAttribute("data-id");
518
+ const text = `{{${variables.find((variable) => variable.key === key)?.label ?? key}}}`;
519
+ if (chip.textContent !== text)
520
+ chip.textContent = text;
855
521
  }
856
- }, [value, variables]);
522
+ }, [editor, variables]);
523
+ const block = editor?.isActive("bulletList") ? "bullet" : editor?.isActive("orderedList") ? "numbered" : editor?.isActive("heading") ? "heading" : "paragraph";
857
524
  return /* @__PURE__ */ jsxs3("div", {
858
525
  "data-slot": "rich-text-editor",
859
- className: `bg-background text-foreground overflow-hidden rounded-xl border ${className ?? ""}`,
860
526
  "data-revision": revision,
527
+ className: `bg-background text-foreground overflow-hidden rounded-xl border ${className ?? ""}`,
861
528
  children: [
862
529
  /* @__PURE__ */ jsxs3("div", {
863
530
  role: "group",
@@ -866,119 +533,115 @@ var RichTextEditor = ({
866
533
  children: [
867
534
  /* @__PURE__ */ jsx5(ToolButton, {
868
535
  label: copy.undo,
869
- disabled: disabled || !historyState.undo,
870
- onClick: () => travel(-1),
536
+ disabled: disabled || !editor?.can().undo(),
537
+ onClick: () => {
538
+ editor?.chain().focus().undo().run();
539
+ },
871
540
  children: /* @__PURE__ */ jsx5(Undo2, {})
872
541
  }),
873
542
  /* @__PURE__ */ jsx5(ToolButton, {
874
543
  label: copy.redo,
875
- disabled: disabled || !historyState.redo,
876
- onClick: () => travel(1),
544
+ disabled: disabled || !editor?.can().redo(),
545
+ onClick: () => {
546
+ editor?.chain().focus().redo().run();
547
+ },
877
548
  children: /* @__PURE__ */ jsx5(Redo2, {})
878
549
  }),
879
- !singleLine ? /* @__PURE__ */ jsxs3(Fragment, {
550
+ /* @__PURE__ */ jsxs3(Select, {
551
+ disabled: disabled || !editor,
552
+ value: block,
553
+ items: [
554
+ { value: "paragraph", label: copy.paragraph },
555
+ { value: "heading", label: copy.heading },
556
+ { value: "bullet", label: copy.bullet },
557
+ { value: "numbered", label: copy.numbered }
558
+ ],
559
+ onValueChange: (kind) => {
560
+ if (disabled || !editor)
561
+ return;
562
+ if (kind === "bullet")
563
+ editor.chain().focus().toggleBulletList().run();
564
+ else if (kind === "numbered")
565
+ editor.chain().focus().toggleOrderedList().run();
566
+ else if (kind === "heading")
567
+ editor.chain().focus().clearNodes().setHeading({ level: 2 }).run();
568
+ else
569
+ editor.chain().focus().clearNodes().setParagraph().run();
570
+ },
880
571
  children: [
881
- /* @__PURE__ */ jsx5("span", {
882
- className: "mx-1 h-5 border-l",
883
- "aria-hidden": true
572
+ /* @__PURE__ */ jsx5(SelectTrigger, {
573
+ size: "sm",
574
+ "aria-label": copy.paragraph,
575
+ className: "max-w-40 text-xs",
576
+ children: /* @__PURE__ */ jsx5(SelectValue, {})
884
577
  }),
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
- },
578
+ /* @__PURE__ */ jsxs3(SelectContent, {
579
+ align: "start",
580
+ alignItemWithTrigger: false,
581
+ finalFocus: focusEditor,
903
582
  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, {})
583
+ /* @__PURE__ */ jsx5(SelectItem, {
584
+ value: "paragraph",
585
+ children: copy.paragraph
911
586
  }),
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
- ]
587
+ /* @__PURE__ */ jsx5(SelectItem, {
588
+ value: "heading",
589
+ children: copy.heading
590
+ }),
591
+ /* @__PURE__ */ jsx5(SelectItem, {
592
+ value: "bullet",
593
+ children: copy.bullet
594
+ }),
595
+ /* @__PURE__ */ jsx5(SelectItem, {
596
+ value: "numbered",
597
+ children: copy.numbered
934
598
  })
935
599
  ]
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
600
  })
970
601
  ]
971
- }) : null,
972
- variables.length ? /* @__PURE__ */ jsxs3(DropdownMenu, {
973
- onOpenChange: (open) => {
974
- if (open)
975
- selection();
602
+ }),
603
+ /* @__PURE__ */ jsx5(ToolButton, {
604
+ label: copy.bold,
605
+ disabled: disabled || !editor,
606
+ pressed: editor?.isActive("bold"),
607
+ onClick: () => {
608
+ editor?.chain().focus().toggleBold().run();
609
+ },
610
+ children: /* @__PURE__ */ jsx5(Bold, {})
611
+ }),
612
+ /* @__PURE__ */ jsx5(ToolButton, {
613
+ label: copy.italic,
614
+ disabled: disabled || !editor,
615
+ pressed: editor?.isActive("italic"),
616
+ onClick: () => {
617
+ editor?.chain().focus().toggleItalic().run();
618
+ },
619
+ children: /* @__PURE__ */ jsx5(Italic, {})
620
+ }),
621
+ /* @__PURE__ */ jsx5(ToolButton, {
622
+ label: copy.underline,
623
+ disabled: disabled || !editor,
624
+ pressed: editor?.isActive("underline"),
625
+ onClick: () => {
626
+ editor?.chain().focus().toggleUnderline().run();
627
+ },
628
+ children: /* @__PURE__ */ jsx5(Underline, {})
629
+ }),
630
+ /* @__PURE__ */ jsx5(ToolButton, {
631
+ label: copy.link,
632
+ disabled: disabled || !editor,
633
+ pressed: linkOpen || editor?.isActive("link"),
634
+ onClick: () => {
635
+ setLink(editor?.getAttributes("link").href ?? "");
636
+ setLinkError(false);
637
+ setLinkOpen(!linkOpen);
976
638
  },
639
+ children: /* @__PURE__ */ jsx5(Link2, {})
640
+ }),
641
+ variables.length ? /* @__PURE__ */ jsxs3(DropdownMenu, {
977
642
  children: [
978
643
  /* @__PURE__ */ jsxs3(DropdownMenuTrigger, {
979
- disabled,
980
- onPointerDown: selection,
981
- onFocus: selection,
644
+ disabled: disabled || !editor,
982
645
  render: /* @__PURE__ */ jsx5(Button, {
983
646
  type: "button",
984
647
  variant: "outline",
@@ -987,21 +650,25 @@ var RichTextEditor = ({
987
650
  }),
988
651
  children: [
989
652
  /* @__PURE__ */ jsx5(Braces, {
990
- className: "size-3.5",
991
653
  "aria-hidden": true
992
654
  }),
993
- copy.variable,
994
- /* @__PURE__ */ jsx5(ChevronDown, {
995
- className: "size-3.5",
996
- "aria-hidden": true
997
- })
655
+ copy.variable
998
656
  ]
999
657
  }),
1000
658
  /* @__PURE__ */ jsx5(DropdownMenuContent, {
1001
659
  align: "end",
1002
660
  finalFocus: focusEditor,
1003
661
  children: variables.map((variable) => /* @__PURE__ */ jsx5(DropdownMenuItem, {
1004
- onClick: () => insert({ type: "variable", key: variable.key }),
662
+ onClick: () => {
663
+ if (disabled || !editor)
664
+ return;
665
+ editor.view.dispatch(closeHistory(editor.state.tr));
666
+ editor.commands.insertContent({
667
+ type: "mention",
668
+ attrs: { id: variable.key, label: variable.label }
669
+ });
670
+ editor.view.dispatch(closeHistory(editor.state.tr));
671
+ },
1005
672
  children: variable.label
1006
673
  }, variable.key))
1007
674
  })
@@ -1029,24 +696,23 @@ var RichTextEditor = ({
1029
696
  size: "sm",
1030
697
  disabled,
1031
698
  onClick: () => {
1032
- if (!Schema4.is(RichTextLink)(link)) {
699
+ if (!Schema3.is(RichTextLink)(link)) {
1033
700
  setLinkError(true);
1034
701
  return;
1035
702
  }
1036
- commit(formatRichText(model.current, selected.current, "href", link), selected.current);
703
+ editor?.chain().focus().extendMarkRange("link").setLink({ href: link }).run();
1037
704
  setLinkOpen(false);
1038
705
  },
1039
706
  children: copy.apply
1040
707
  }),
1041
- marks.href ? /* @__PURE__ */ jsxs3(Button, {
708
+ editor?.isActive("link") ? /* @__PURE__ */ jsxs3(Button, {
1042
709
  type: "button",
1043
710
  size: "sm",
1044
711
  variant: "outline",
1045
712
  disabled,
1046
713
  onClick: () => {
1047
- commit(formatRichText(model.current, selected.current, "href", undefined), selected.current);
714
+ editor.chain().focus().extendMarkRange("link").unsetLink().run();
1048
715
  setLinkOpen(false);
1049
- setLinkError(false);
1050
716
  },
1051
717
  children: [
1052
718
  /* @__PURE__ */ jsx5(Unlink, {
@@ -1069,56 +735,9 @@ var RichTextEditor = ({
1069
735
  }) : null
1070
736
  ]
1071
737
  }) : 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
738
+ /* @__PURE__ */ jsx5(EditorContent, {
739
+ editor,
740
+ style: contentStyle
1122
741
  }),
1123
742
  error ? /* @__PURE__ */ jsx5("p", {
1124
743
  id: `${id}-error`,