@whity/ui 0.0.0-stage → 0.5.0

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 (169) hide show
  1. package/README.md +107 -3
  2. package/package.json +192 -6
  3. package/src/access-denied.stories.tsx +179 -0
  4. package/src/access-denied.tsx +305 -0
  5. package/src/action-proposal.tsx +302 -0
  6. package/src/actor-chip.tsx +71 -0
  7. package/src/agent-activity.tsx +163 -0
  8. package/src/agent-collaboration.stories.tsx +156 -0
  9. package/src/agent-run-status.tsx +196 -0
  10. package/src/alert-dialog.stories.tsx +45 -0
  11. package/src/alert-dialog.tsx +147 -0
  12. package/src/alert.stories.tsx +91 -0
  13. package/src/alert.tsx +89 -0
  14. package/src/app-sidebar.stories.tsx +195 -0
  15. package/src/app-sidebar.tsx +500 -0
  16. package/src/approval-queue.tsx +108 -0
  17. package/src/audience-group-picker.stories.tsx +198 -0
  18. package/src/audience-group-picker.tsx +362 -0
  19. package/src/audience-people-picker.stories.tsx +102 -0
  20. package/src/audience-people-picker.tsx +279 -0
  21. package/src/avatar.tsx +83 -0
  22. package/src/badge.stories.tsx +105 -0
  23. package/src/badge.tsx +68 -0
  24. package/src/beta-warning.stories.tsx +79 -0
  25. package/src/beta-warning.tsx +189 -0
  26. package/src/bilingual-input.stories.tsx +82 -0
  27. package/src/bilingual-input.tsx +183 -0
  28. package/src/breadcrumb.stories.tsx +51 -0
  29. package/src/breadcrumb.tsx +90 -0
  30. package/src/button.stories.tsx +87 -0
  31. package/src/button.tsx +118 -0
  32. package/src/card.stories.tsx +423 -0
  33. package/src/card.tsx +180 -0
  34. package/src/chart.stories.tsx +66 -0
  35. package/src/chart.tsx +228 -0
  36. package/src/checkbox.stories.tsx +62 -0
  37. package/src/checkbox.tsx +54 -0
  38. package/src/collapsible.stories.tsx +48 -0
  39. package/src/collapsible.tsx +34 -0
  40. package/src/color-card.stories.tsx +61 -0
  41. package/src/color-card.tsx +270 -0
  42. package/src/color-picker.stories.tsx +65 -0
  43. package/src/color-picker.tsx +425 -0
  44. package/src/command-palette.tsx +306 -0
  45. package/src/copy-block.stories.tsx +38 -0
  46. package/src/copy-block.tsx +85 -0
  47. package/src/data-table.stories.tsx +200 -0
  48. package/src/data-table.tsx +1106 -0
  49. package/src/date-picker.stories.tsx +61 -0
  50. package/src/date-picker.tsx +422 -0
  51. package/src/design-cards.stories.tsx +458 -0
  52. package/src/dialog.stories.tsx +48 -0
  53. package/src/dialog.tsx +166 -0
  54. package/src/documents/barcode-svg.stories.tsx +72 -0
  55. package/src/documents/barcode-svg.tsx +79 -0
  56. package/src/dropdown-menu.stories.tsx +77 -0
  57. package/src/dropdown-menu.tsx +271 -0
  58. package/src/editor/behaviour.ts +279 -0
  59. package/src/editor/block-ids.ts +44 -0
  60. package/src/editor/block-list.tsx +101 -0
  61. package/src/editor/block-toolbar.tsx +477 -0
  62. package/src/editor/bubble.tsx +93 -0
  63. package/src/editor/catalogue.ts +103 -0
  64. package/src/editor/collab-rail.tsx +746 -0
  65. package/src/editor/collab.stories.tsx +207 -0
  66. package/src/editor/collab.ts +87 -0
  67. package/src/editor/contract.ts +408 -0
  68. package/src/editor/convert.ts +362 -0
  69. package/src/editor/doc-editor.stories.tsx +98 -0
  70. package/src/editor/doc-editor.tsx +1287 -0
  71. package/src/editor/editor.css +536 -0
  72. package/src/editor/extensions.ts +86 -0
  73. package/src/editor/field-picker.tsx +107 -0
  74. package/src/editor/fields.ts +100 -0
  75. package/src/editor/gutter.tsx +367 -0
  76. package/src/editor/index.ts +22 -0
  77. package/src/editor/label-sheet.tsx +289 -0
  78. package/src/editor/labels.ts +310 -0
  79. package/src/editor/link-tools.tsx +141 -0
  80. package/src/editor/node-views.tsx +450 -0
  81. package/src/editor/page-view.tsx +353 -0
  82. package/src/editor/publish-check.tsx +182 -0
  83. package/src/editor/rich-blocks.stories.tsx +146 -0
  84. package/src/editor/sample.ts +40 -0
  85. package/src/editor/schema-rich.ts +150 -0
  86. package/src/editor/schema.ts +315 -0
  87. package/src/editor/services.tsx +127 -0
  88. package/src/editor/slash.ts +64 -0
  89. package/src/editor/suggesting.ts +372 -0
  90. package/src/editor/suggestions.ts +763 -0
  91. package/src/editor/table-keys.ts +66 -0
  92. package/src/editor/templates.stories.tsx +324 -0
  93. package/src/editor/toolbar.tsx +358 -0
  94. package/src/empty-state.stories.tsx +51 -0
  95. package/src/empty-state.tsx +100 -0
  96. package/src/empty-state.usage-notes.md +30 -0
  97. package/src/form.stories.tsx +112 -0
  98. package/src/form.tsx +176 -0
  99. package/src/globals.css +20 -0
  100. package/src/icon-button.tsx +85 -0
  101. package/src/icon-map.ts +244 -0
  102. package/src/icon.tsx +41 -0
  103. package/src/index.ts +67 -0
  104. package/src/input.stories.tsx +157 -0
  105. package/src/input.tsx +759 -0
  106. package/src/locked-screen.stories.tsx +29 -0
  107. package/src/locked-screen.tsx +61 -0
  108. package/src/mark.tsx +62 -0
  109. package/src/math-text.stories.tsx +33 -0
  110. package/src/math-text.tsx +43 -0
  111. package/src/menu-bar.stories.tsx +198 -0
  112. package/src/menu-bar.tsx +373 -0
  113. package/src/money/currency.ts +230 -0
  114. package/src/page-header.stories.tsx +95 -0
  115. package/src/page-header.tsx +122 -0
  116. package/src/page-shell.stories.tsx +42 -0
  117. package/src/page-shell.tsx +32 -0
  118. package/src/pagination.stories.tsx +45 -0
  119. package/src/pagination.tsx +98 -0
  120. package/src/plugin-card.stories.tsx +180 -0
  121. package/src/plugin-card.tsx +848 -0
  122. package/src/popover.stories.tsx +48 -0
  123. package/src/popover.tsx +55 -0
  124. package/src/radio-group.stories.tsx +26 -0
  125. package/src/radio-group.tsx +44 -0
  126. package/src/route-flow/editor.tsx +1014 -0
  127. package/src/route-flow/model.ts +716 -0
  128. package/src/scope-grant.tsx +139 -0
  129. package/src/screen-too-small.stories.tsx +61 -0
  130. package/src/screen-too-small.tsx +104 -0
  131. package/src/select.stories.tsx +44 -0
  132. package/src/select.tsx +197 -0
  133. package/src/separator.stories.tsx +34 -0
  134. package/src/separator.tsx +34 -0
  135. package/src/sheet.stories.tsx +49 -0
  136. package/src/sheet.tsx +168 -0
  137. package/src/skeleton.stories.tsx +28 -0
  138. package/src/skeleton.tsx +19 -0
  139. package/src/slider.stories.tsx +78 -0
  140. package/src/slider.tsx +300 -0
  141. package/src/spinner.stories.tsx +33 -0
  142. package/src/spinner.tsx +55 -0
  143. package/src/switch.stories.tsx +42 -0
  144. package/src/switch.tsx +41 -0
  145. package/src/switcher.stories.tsx +111 -0
  146. package/src/switcher.tsx +131 -0
  147. package/src/table.stories.tsx +48 -0
  148. package/src/table.tsx +121 -0
  149. package/src/tabs.stories.tsx +74 -0
  150. package/src/tabs.tsx +90 -0
  151. package/src/tag-input.stories.tsx +67 -0
  152. package/src/tag-input.tsx +117 -0
  153. package/src/tenant-logo.tsx +63 -0
  154. package/src/tenant-switcher.stories.tsx +38 -0
  155. package/src/tenant-switcher.tsx +164 -0
  156. package/src/textarea.stories.tsx +27 -0
  157. package/src/textarea.tsx +18 -0
  158. package/src/theme-mode.ts +52 -0
  159. package/src/time-picker.stories.tsx +62 -0
  160. package/src/time-picker.tsx +615 -0
  161. package/src/toggle-group.tsx +138 -0
  162. package/src/tooltip.stories.tsx +43 -0
  163. package/src/tooltip.tsx +73 -0
  164. package/src/top-nav.stories.tsx +58 -0
  165. package/src/top-nav.tsx +324 -0
  166. package/src/undo-bar.tsx +107 -0
  167. package/src/utils.ts +45 -0
  168. package/src/view-toggle.stories.tsx +84 -0
  169. package/src/view-toggle.tsx +110 -0
@@ -0,0 +1,107 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+
5
+ import { Icon } from "../icon"
6
+ import { cn } from "../utils"
7
+ import type { FieldDef, FieldGroup } from "./fields"
8
+ import type { EditorLabels } from "./labels"
9
+
10
+ /**
11
+ * The field picker (EditorInline): a searchable list grouped by record, each
12
+ * field as its chip with a sample value. Typing {{ fills the query from the
13
+ * text; Field ▾ in the toolbar gives it a search box instead.
14
+ */
15
+ function FieldPicker({
16
+ groups,
17
+ query,
18
+ active,
19
+ onActive,
20
+ onPick,
21
+ labels,
22
+ search,
23
+ footer,
24
+ id,
25
+ }: {
26
+ /** Already filtered by the query. */
27
+ groups: FieldGroup[]
28
+ /** What was typed after {{, shown at the top. */
29
+ query?: string
30
+ active: number
31
+ onActive: (index: number) => void
32
+ onPick: (field: FieldDef) => void
33
+ labels: EditorLabels
34
+ /** Field ▾'s own search box, in place of the query line. */
35
+ search?: React.ReactNode
36
+ /** Below the list: Field ▾'s "New field". */
37
+ footer?: React.ReactNode
38
+ id: string
39
+ }) {
40
+ const listRef = React.useRef<HTMLDivElement>(null)
41
+ React.useEffect(() => {
42
+ listRef.current?.querySelector(`[data-index="${active}"]`)?.scrollIntoView({ block: "nearest" })
43
+ }, [active])
44
+
45
+ let index = -1
46
+ return (
47
+ <div
48
+ data-slot="field-picker"
49
+ className="wy-fpick grid w-[300px] gap-0.5 rounded-lg border border-border bg-popover p-1 text-sm text-popover-foreground shadow-float"
50
+ >
51
+ {search ??
52
+ (query !== undefined && (
53
+ <div className="flex items-center gap-1.5 px-2 pt-1 pb-1.5 text-xs text-muted-foreground">
54
+ <Icon name="search" size={14} />
55
+ {query ? <b className="font-medium text-foreground">{query}</b> : labels.fieldSearch}
56
+ </div>
57
+ ))}
58
+ <div ref={listRef} id={id} role="listbox" aria-label={labels.fieldPicker} className="grid max-h-[320px] gap-0.5 overflow-y-auto">
59
+ {groups.length === 0 && <div className="px-2 py-3 text-muted-foreground">{labels.noFields}</div>}
60
+ {groups.map((group) => (
61
+ <div key={group.group} role="group" aria-label={group.group} className="grid gap-0.5">
62
+ <div role="presentation" className="px-2 pt-1.5 pb-0.5 text-xs font-medium text-muted-foreground">
63
+ {group.group}
64
+ </div>
65
+ {group.items.map((field) => {
66
+ index += 1
67
+ const i = index
68
+ const on = i === active
69
+ return (
70
+ <div
71
+ key={field.path}
72
+ role="option"
73
+ id={`${id}-${i}`}
74
+ aria-selected={on}
75
+ data-index={i}
76
+ title={field.path}
77
+ onMouseDown={(e) => e.preventDefault()}
78
+ onMouseMove={() => onActive(i)}
79
+ onClick={() => onPick(field)}
80
+ className={cn(
81
+ "wy-fpick__it flex cursor-pointer items-center gap-2 rounded-control px-2 py-1.5",
82
+ on && "bg-primary-tint text-primary-tint-foreground"
83
+ )}
84
+ >
85
+ <span className="wy-mf">{field.label}</span>
86
+ {field.sample && (
87
+ <span dir="auto" className="ms-auto max-w-[120px] truncate text-xs text-muted-foreground">
88
+ {field.sample}
89
+ </span>
90
+ )}
91
+ </div>
92
+ )
93
+ })}
94
+ </div>
95
+ ))}
96
+ </div>
97
+ {footer}
98
+ </div>
99
+ )
100
+ }
101
+
102
+ /** The fields in picker order, so the active index maps back to one. */
103
+ function flatFields(groups: FieldGroup[]): FieldDef[] {
104
+ return groups.flatMap((g) => g.items)
105
+ }
106
+
107
+ export { FieldPicker, flatFields }
@@ -0,0 +1,100 @@
1
+ import { Extension, type Range } from "@tiptap/core"
2
+ import { PluginKey } from "@tiptap/pm/state"
3
+ import Suggestion, { exitSuggestion, type SuggestionKeyDownProps } from "@tiptap/suggestion"
4
+
5
+ /** One merge field a template can use (E.26), with a sample value for the picker. */
6
+ export interface FieldDef {
7
+ /** "customer.name" */
8
+ path: string
9
+ /** "Customer name" */
10
+ label: string
11
+ /** "Example Clinic": what the picker shows beside it. */
12
+ sample?: string
13
+ /** E.27: required fields block issuing; fiscal fields are always required. */
14
+ required?: boolean
15
+ }
16
+
17
+ /** Fields grouped by the record they come from: Customer, Invoice, Company, Today (contract DocEditorProps.fields). */
18
+ export interface FieldGroup {
19
+ /** The group's name as shown: "Customer". */
20
+ group: string
21
+ items: FieldDef[]
22
+ }
23
+
24
+ /** What the {{ picker shows; the React side (field-picker.tsx) renders it. */
25
+ export interface FieldTriggerState {
26
+ open: boolean
27
+ query: string
28
+ range: Range | null
29
+ rect: DOMRect | null
30
+ }
31
+
32
+ export interface FieldTriggerOptions {
33
+ onState: (state: FieldTriggerState) => void
34
+ /** Returns true when the picker handled the key (arrows, Enter). */
35
+ onKeyDown: (event: KeyboardEvent) => boolean
36
+ }
37
+
38
+ export const fieldTriggerKey = new PluginKey("fieldTrigger")
39
+
40
+ /** The groups narrowed to fields whose name or path matches what was typed. */
41
+ export function filterFields(groups: FieldGroup[], query: string): FieldGroup[] {
42
+ const q = query.trim().toLowerCase()
43
+ if (!q) return groups.filter((g) => g.items.length > 0)
44
+ return groups
45
+ .map((g) => ({
46
+ ...g,
47
+ items: g.items.filter((f) => f.label.toLowerCase().includes(q) || f.path.toLowerCase().includes(q) || g.group.toLowerCase().includes(q)),
48
+ }))
49
+ .filter((g) => g.items.length > 0)
50
+ }
51
+
52
+ /** Every path a record type offers, for the publish check (E.37). */
53
+ export function fieldPaths(groups: FieldGroup[]): Set<string> {
54
+ return new Set(groups.flatMap((g) => g.items.map((f) => f.path)))
55
+ }
56
+
57
+ /**
58
+ * Typing {{ opens the field picker (EditorInline). Spaces are allowed so a
59
+ * field can be found by its plain name; Esc closes it and keeps what was typed.
60
+ */
61
+ export const FieldTrigger = Extension.create<FieldTriggerOptions>({
62
+ name: "fieldTrigger",
63
+ addOptions: () => ({ onState: () => {}, onKeyDown: () => false }),
64
+ addProseMirrorPlugins() {
65
+ const { onState, onKeyDown } = this.options
66
+ const publish = (open: boolean, props?: { query: string; range: Range; clientRect?: (() => DOMRect | null) | null }) =>
67
+ onState({
68
+ open,
69
+ query: props?.query ?? "",
70
+ range: props?.range ?? null,
71
+ rect: props?.clientRect?.() ?? null,
72
+ })
73
+ return [
74
+ Suggestion({
75
+ editor: this.editor,
76
+ pluginKey: fieldTriggerKey,
77
+ char: "{{",
78
+ allowSpaces: true,
79
+ startOfLine: false,
80
+ allowedPrefixes: null,
81
+ // Not inside code, where {{ is just text.
82
+ allow: ({ state, range }) => state.doc.resolve(range.from).parent.type.name !== "codeBlock",
83
+ items: () => [],
84
+ render: () => ({
85
+ onStart: (props) => publish(true, props),
86
+ onUpdate: (props) => publish(true, props),
87
+ onExit: () => publish(false),
88
+ onKeyDown: ({ event }: SuggestionKeyDownProps) => {
89
+ if (event.key === "Escape") {
90
+ exitSuggestion(this.editor.view, fieldTriggerKey)
91
+ publish(false)
92
+ return true
93
+ }
94
+ return onKeyDown(event)
95
+ },
96
+ }),
97
+ }),
98
+ ]
99
+ },
100
+ })
@@ -0,0 +1,367 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import type { Editor } from "@tiptap/core"
5
+ import { NodeSelection } from "@tiptap/pm/state"
6
+
7
+ import {
8
+ DropdownMenu,
9
+ DropdownMenuContent,
10
+ DropdownMenuItem,
11
+ DropdownMenuSeparator,
12
+ DropdownMenuShortcut,
13
+ DropdownMenuSub,
14
+ DropdownMenuSubContent,
15
+ DropdownMenuSubTrigger,
16
+ DropdownMenuTrigger,
17
+ } from "../dropdown-menu"
18
+ import { Icon } from "../icon"
19
+ import { cn } from "../utils"
20
+ import { deleteBlock, duplicateBlock, moveBlockTo, turnInto } from "./behaviour"
21
+ import { BlockList } from "./block-list"
22
+ import { CATALOGUE, applyItem, filterCatalogue, type CatalogueItem } from "./catalogue"
23
+ import { EDITOR_LABELS, type EditorLabels } from "./labels"
24
+
25
+ /** Top padding of a block before its first line, so the gutter sits on that line. */
26
+ function firstLineOffset(el: HTMLElement): number {
27
+ return parseFloat(getComputedStyle(el).paddingTop) || 0
28
+ }
29
+
30
+ interface Hovered {
31
+ pos: number
32
+ index: number
33
+ top: number
34
+ height: number
35
+ }
36
+
37
+ /**
38
+ * The block gutter (E.8, EditorBlockChrome): hovering a block, or putting the
39
+ * caret in it, shows + and the drag handle in the margin at its first line.
40
+ * + opens the inserter for a new block below; the handle drags the block or,
41
+ * clicked, opens its menu. Agents never use it: they only suggest (E.4).
42
+ */
43
+ function BlockGutter({
44
+ editor,
45
+ labels,
46
+ container,
47
+ menuAt,
48
+ onMenuAtChange,
49
+ blockUrl,
50
+ }: {
51
+ editor: Editor
52
+ labels: EditorLabels
53
+ /** The positioned element around the editable content. */
54
+ container: React.RefObject<HTMLDivElement | null>
55
+ /** A block position whose menu should open (Esc then Enter). */
56
+ menuAt: number | null
57
+ onMenuAtChange: (pos: number | null) => void
58
+ /** The address "Copy link to block" copies. */
59
+ blockUrl: (blockId: string) => string
60
+ }) {
61
+ const [hovered, setHovered] = React.useState<Hovered | null>(null)
62
+ const [inserter, setInserter] = React.useState<{ pos: number; query: string; active: number } | null>(null)
63
+ const [drag, setDrag] = React.useState<{ from: number; x: number; y: number; dropIndex: number; dropTop: number; text: string } | null>(null)
64
+ const pointerDown = React.useRef<{ x: number; y: number; pos: number } | null>(null)
65
+
66
+ const measure = React.useCallback(
67
+ (index: number): Hovered | null => {
68
+ const wrap = container.current
69
+ const el = editor.view.dom.children[index] as HTMLElement | undefined
70
+ if (!wrap || !el) return null
71
+ let pos = 0
72
+ for (let i = 0; i < index; i++) pos += editor.state.doc.child(i).nodeSize
73
+ const box = wrap.getBoundingClientRect()
74
+ const r = el.getBoundingClientRect()
75
+ return { pos, index, top: r.top - box.top + firstLineOffset(el), height: r.height }
76
+ },
77
+ [editor, container]
78
+ )
79
+
80
+ // Hovering a block shows its gutter.
81
+ React.useEffect(() => {
82
+ const wrap = container.current
83
+ if (!wrap) return
84
+ const onMove = (e: PointerEvent) => {
85
+ if (drag) return
86
+ const children = Array.from(editor.view.dom.children) as HTMLElement[]
87
+ const index = children.findIndex((el) => {
88
+ const r = el.getBoundingClientRect()
89
+ return e.clientY >= r.top - 2 && e.clientY <= r.bottom + 2
90
+ })
91
+ if (index >= 0) setHovered(measure(index))
92
+ }
93
+ wrap.addEventListener("pointermove", onMove)
94
+ return () => wrap.removeEventListener("pointermove", onMove)
95
+ }, [editor, container, measure, drag])
96
+
97
+ // So does putting the caret in it.
98
+ React.useEffect(() => {
99
+ const onSelection = () => {
100
+ if (!editor.isFocused) return
101
+ const { $from } = editor.state.selection
102
+ setHovered(measure($from.depth === 0 ? $from.index(0) : $from.index(0)))
103
+ }
104
+ editor.on("selectionUpdate", onSelection)
105
+ editor.on("update", onSelection)
106
+ return () => {
107
+ editor.off("selectionUpdate", onSelection)
108
+ editor.off("update", onSelection)
109
+ }
110
+ }, [editor, measure])
111
+
112
+ // Esc then Enter on a block opens its menu.
113
+ React.useEffect(() => {
114
+ if (menuAt === null) return
115
+ const index = editor.state.doc.resolve(menuAt).index(0)
116
+ setHovered(measure(index))
117
+ }, [menuAt, editor, measure])
118
+
119
+ if (!editor.isEditable) return null
120
+
121
+ const dropIndexAt = (clientY: number) => {
122
+ const wrap = container.current!
123
+ const box = wrap.getBoundingClientRect()
124
+ const children = Array.from(editor.view.dom.children) as HTMLElement[]
125
+ let index = children.length
126
+ let top = children.length ? children[children.length - 1].getBoundingClientRect().bottom - box.top : 0
127
+ for (let i = 0; i < children.length; i++) {
128
+ const r = children[i].getBoundingClientRect()
129
+ if (clientY < r.top + r.height / 2) {
130
+ index = i
131
+ top = r.top - box.top
132
+ break
133
+ }
134
+ }
135
+ return { index, top }
136
+ }
137
+
138
+ const startPointer = (e: React.PointerEvent) => {
139
+ if (!hovered || e.button !== 0) return
140
+ pointerDown.current = { x: e.clientX, y: e.clientY, pos: hovered.pos }
141
+ ;(e.target as HTMLElement).setPointerCapture(e.pointerId)
142
+ }
143
+ const movePointer = (e: React.PointerEvent) => {
144
+ const down = pointerDown.current
145
+ if (!down) return
146
+ if (!drag && Math.hypot(e.clientX - down.x, e.clientY - down.y) < 4) return
147
+ const node = editor.state.doc.nodeAt(down.pos)
148
+ const box = container.current!.getBoundingClientRect()
149
+ const { index, top } = dropIndexAt(e.clientY)
150
+ const el = editor.view.nodeDOM(down.pos) as HTMLElement | null
151
+ el?.classList.add("is-dragging")
152
+ setDrag({ from: down.pos, x: e.clientX - box.left, y: e.clientY - box.top, dropIndex: index, dropTop: top, text: node?.textContent.slice(0, 80) ?? "" })
153
+ }
154
+ const endPointer = () => {
155
+ const down = pointerDown.current
156
+ pointerDown.current = null
157
+ if (!down) return
158
+ if (!drag) {
159
+ onMenuAtChange(down.pos)
160
+ return
161
+ }
162
+ ;(editor.view.nodeDOM(down.pos) as HTMLElement | null)?.classList.remove("is-dragging")
163
+ const fromIndex = editor.state.doc.resolve(down.pos).index(0)
164
+ // Dropping below its own place: the block isn't counted once it's lifted out.
165
+ const target = drag.dropIndex > fromIndex ? drag.dropIndex - 1 : drag.dropIndex
166
+ setDrag(null)
167
+ if (target !== fromIndex) moveBlockTo(editor, down.pos, target)
168
+ }
169
+
170
+ const blockAt = (pos: number) => editor.state.doc.nodeAt(pos)
171
+ const select = (pos: number) => editor.chain().focus().command(({ tr }) => {
172
+ tr.setSelection(NodeSelection.create(tr.doc, pos))
173
+ return true
174
+ }).run()
175
+ const inside = (pos: number) => editor.chain().focus().setTextSelection(pos + 1).run()
176
+
177
+ const menuOpen = menuAt !== null
178
+ const menuBlock = menuOpen ? blockAt(menuAt) : null
179
+ const inserterItems: CatalogueItem[] = inserter ? filterCatalogue(inserter.query, labels, EDITOR_LABELS) : []
180
+
181
+ return (
182
+ <>
183
+ {hovered && !drag && (
184
+ <div
185
+ data-slot="block-gutter"
186
+ className="absolute end-full z-10 flex pe-1.5"
187
+ style={{ top: hovered.top }}
188
+ onPointerDown={(e) => e.stopPropagation()}
189
+ >
190
+ <button
191
+ type="button"
192
+ aria-label={labels.addBelow}
193
+ className={cn(
194
+ "grid size-6 place-items-center rounded-[6px] text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring",
195
+ inserter && "bg-muted text-foreground"
196
+ )}
197
+ onMouseDown={(e) => e.preventDefault()}
198
+ onClick={() => setInserter({ pos: hovered.pos, query: "", active: 0 })}
199
+ >
200
+ <Icon name="plus" />
201
+ </button>
202
+ <DropdownMenu
203
+ modal={false}
204
+ open={menuOpen && menuAt === hovered.pos}
205
+ onOpenChange={(open) => onMenuAtChange(open ? hovered.pos : null)}
206
+ >
207
+ <DropdownMenuTrigger asChild>
208
+ <button
209
+ type="button"
210
+ aria-label={labels.dragHandle}
211
+ className="grid h-6 w-[18px] cursor-grab touch-none place-items-center rounded-[6px] text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring"
212
+ onPointerDown={(e) => {
213
+ e.preventDefault()
214
+ startPointer(e)
215
+ }}
216
+ onPointerMove={movePointer}
217
+ onPointerUp={endPointer}
218
+ onKeyDown={(e) => {
219
+ if (e.key === "Enter" || e.key === " ") {
220
+ e.preventDefault()
221
+ onMenuAtChange(hovered.pos)
222
+ }
223
+ }}
224
+ >
225
+ <Icon name="drag" />
226
+ </button>
227
+ </DropdownMenuTrigger>
228
+ {menuBlock && menuAt !== null && (
229
+ <DropdownMenuContent
230
+ align="start"
231
+ aria-label={labels.blockMenu}
232
+ onCloseAutoFocus={(e) => {
233
+ e.preventDefault()
234
+ editor.commands.focus()
235
+ }}
236
+ >
237
+ {menuBlock.isTextblock && (
238
+ <DropdownMenuSub>
239
+ <DropdownMenuSubTrigger>
240
+ <Icon name="sync" /> {labels.turnInto}
241
+ </DropdownMenuSubTrigger>
242
+ <DropdownMenuSubContent>
243
+ {CATALOGUE.filter((i) => i.textBlock).map((item) => (
244
+ <DropdownMenuItem
245
+ key={item.id}
246
+ onSelect={() => {
247
+ inside(menuAt)
248
+ turnInto(editor, item.node.type, item.node.attrs)
249
+ }}
250
+ >
251
+ <Icon name={item.icon} /> {labels[item.label]}
252
+ </DropdownMenuItem>
253
+ ))}
254
+ </DropdownMenuSubContent>
255
+ </DropdownMenuSub>
256
+ )}
257
+ <DropdownMenuItem onSelect={() => duplicateBlock(editor, menuAt)}>
258
+ <Icon name="copy" /> {labels.duplicate}
259
+ <DropdownMenuShortcut>⌘D</DropdownMenuShortcut>
260
+ </DropdownMenuItem>
261
+ <DropdownMenuItem
262
+ onSelect={() => {
263
+ const id = menuBlock.attrs.id as string | null
264
+ if (id) void navigator.clipboard?.writeText(blockUrl(id))
265
+ }}
266
+ >
267
+ <Icon name="link" /> {labels.copyLinkToBlock}
268
+ </DropdownMenuItem>
269
+ <DropdownMenuSeparator />
270
+ <DropdownMenuItem
271
+ disabled={editor.state.doc.resolve(menuAt).index(0) === 0}
272
+ onSelect={() => moveBlockTo(editor, menuAt, editor.state.doc.resolve(menuAt).index(0) - 1)}
273
+ >
274
+ <Icon name="arrow-up" /> {labels.moveUp}
275
+ <DropdownMenuShortcut>⌥⇧↑</DropdownMenuShortcut>
276
+ </DropdownMenuItem>
277
+ <DropdownMenuItem
278
+ disabled={editor.state.doc.resolve(menuAt).index(0) >= editor.state.doc.childCount - 1}
279
+ onSelect={() => moveBlockTo(editor, menuAt, editor.state.doc.resolve(menuAt).index(0) + 1)}
280
+ >
281
+ <Icon name="arrow-down" /> {labels.moveDown}
282
+ <DropdownMenuShortcut>⌥⇧↓</DropdownMenuShortcut>
283
+ </DropdownMenuItem>
284
+ <DropdownMenuSeparator />
285
+ <DropdownMenuItem variant="destructive" onSelect={() => deleteBlock(editor, menuAt)}>
286
+ <Icon name="delete" /> {labels.delete}
287
+ <DropdownMenuShortcut>Del</DropdownMenuShortcut>
288
+ </DropdownMenuItem>
289
+ </DropdownMenuContent>
290
+ )}
291
+ </DropdownMenu>
292
+ </div>
293
+ )}
294
+
295
+ {inserter && hovered && (
296
+ <div className="absolute start-0 z-30" style={{ top: hovered.top + 28 }}>
297
+ <BlockList
298
+ id="wy-inserter"
299
+ items={inserterItems}
300
+ active={Math.min(inserter.active, Math.max(0, inserterItems.length - 1))}
301
+ onActive={(active) => setInserter({ ...inserter, active })}
302
+ onPick={(item) => {
303
+ const node = blockAt(inserter.pos)
304
+ setInserter(null)
305
+ if (node) applyItem(editor, item, { after: inserter.pos + node.nodeSize })
306
+ }}
307
+ labels={labels}
308
+ footer={
309
+ <>
310
+ <span>{labels.slashMove}</span>
311
+ <span>{labels.slashInsert}</span>
312
+ <span>{labels.slashClose}</span>
313
+ </>
314
+ }
315
+ search={
316
+ <div className="-mx-1 -mt-1 mb-1 flex h-9 items-center gap-2 border-b border-border px-2.5 text-muted-foreground">
317
+ <Icon name="search" size={14} />
318
+ <input
319
+ autoFocus
320
+ aria-label={labels.searchBlocks}
321
+ aria-controls="wy-inserter"
322
+ placeholder={labels.searchBlocks}
323
+ value={inserter.query}
324
+ onChange={(e) => setInserter({ ...inserter, query: e.target.value, active: 0 })}
325
+ onBlur={() => setInserter(null)}
326
+ onKeyDown={(e) => {
327
+ if (e.key === "ArrowDown" || e.key === "ArrowUp") {
328
+ e.preventDefault()
329
+ const n = inserterItems.length || 1
330
+ setInserter({ ...inserter, active: (inserter.active + (e.key === "ArrowDown" ? 1 : n - 1)) % n })
331
+ } else if (e.key === "Enter") {
332
+ e.preventDefault()
333
+ const item = inserterItems[inserter.active]
334
+ const node = blockAt(inserter.pos)
335
+ setInserter(null)
336
+ if (item && node) applyItem(editor, item, { after: inserter.pos + node.nodeSize })
337
+ } else if (e.key === "Escape") {
338
+ e.preventDefault()
339
+ setInserter(null)
340
+ select(inserter.pos)
341
+ }
342
+ }}
343
+ className="h-full min-w-0 flex-1 bg-transparent text-sm text-foreground outline-none placeholder:text-muted-foreground"
344
+ />
345
+ </div>
346
+ }
347
+ />
348
+ </div>
349
+ )}
350
+
351
+ {drag && (
352
+ <>
353
+ <div aria-hidden="true" className="pointer-events-none absolute inset-x-0 z-20 h-0.5 rounded-[1px] bg-primary" style={{ top: drag.dropTop - 1 }} />
354
+ <div
355
+ aria-hidden="true"
356
+ className="pointer-events-none absolute z-30 max-w-[420px] -rotate-1 truncate rounded-control border border-border bg-card px-2.5 py-1.5 text-base shadow-float"
357
+ style={{ top: drag.y + 8, left: drag.x + 12 }}
358
+ >
359
+ {drag.text || "…"}
360
+ </div>
361
+ </>
362
+ )}
363
+ </>
364
+ )
365
+ }
366
+
367
+ export { BlockGutter }
@@ -0,0 +1,22 @@
1
+ export * from "./contract"
2
+ export { DocEditor, type DocEditorProps, type SaveState } from "./doc-editor"
3
+ export { EDITOR_LABELS, type EditorLabels } from "./labels"
4
+ export { fromEditorContent, normalizeBlocks, toEditorContent } from "./convert"
5
+ export { documentExtensions } from "./extensions"
6
+ export { EditorServicesProvider, type EditorServices, type RecordView, type ChartData, type UploadedFile } from "./services"
7
+ export { type FieldDef, type FieldGroup } from "./fields"
8
+ export { type Letterhead } from "./page-view"
9
+ export { type FieldPreview } from "./services"
10
+ export * from "./collab"
11
+ export {
12
+ collectPatches,
13
+ renderSuggestions,
14
+ stripSuggestions,
15
+ suggestEdit,
16
+ summarize,
17
+ type FormatEdit,
18
+ type LocalChange,
19
+ type PatchedSuggestion,
20
+ type SuggestionKind,
21
+ type SuggestionPatch,
22
+ } from "./suggestions"