@whity/ui 0.0.0-stage → 1.0.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 (205) hide show
  1. package/README.md +106 -2
  2. package/package.json +219 -4
  3. package/src/access-denied.stories.tsx +179 -0
  4. package/src/access-denied.tsx +305 -0
  5. package/src/action-proposal.tsx +338 -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 +604 -0
  16. package/src/approval-queue.tsx +109 -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-pattern.ts +138 -0
  22. package/src/avatar.tsx +125 -0
  23. package/src/badge.stories.tsx +105 -0
  24. package/src/badge.tsx +68 -0
  25. package/src/beta-warning.stories.tsx +79 -0
  26. package/src/beta-warning.tsx +189 -0
  27. package/src/bilingual-input.stories.tsx +82 -0
  28. package/src/bilingual-input.tsx +188 -0
  29. package/src/board.tsx +216 -0
  30. package/src/breadcrumb.stories.tsx +51 -0
  31. package/src/breadcrumb.tsx +90 -0
  32. package/src/built-on-whity.tsx +52 -0
  33. package/src/button.stories.tsx +87 -0
  34. package/src/button.tsx +123 -0
  35. package/src/card.stories.tsx +423 -0
  36. package/src/card.tsx +180 -0
  37. package/src/chart.stories.tsx +66 -0
  38. package/src/chart.tsx +228 -0
  39. package/src/checkbox.stories.tsx +62 -0
  40. package/src/checkbox.tsx +54 -0
  41. package/src/collapsible.stories.tsx +48 -0
  42. package/src/collapsible.tsx +34 -0
  43. package/src/color-card.stories.tsx +61 -0
  44. package/src/color-card.tsx +270 -0
  45. package/src/color-picker.stories.tsx +65 -0
  46. package/src/color-picker.tsx +425 -0
  47. package/src/column-matcher.tsx +84 -0
  48. package/src/command-palette.tsx +398 -0
  49. package/src/confirm-action.tsx +98 -0
  50. package/src/copy-block.stories.tsx +38 -0
  51. package/src/copy-block.tsx +85 -0
  52. package/src/data-table.stories.tsx +200 -0
  53. package/src/data-table.tsx +1686 -0
  54. package/src/date-picker.stories.tsx +61 -0
  55. package/src/date-picker.tsx +456 -0
  56. package/src/design-cards.stories.tsx +551 -0
  57. package/src/dialog.stories.tsx +48 -0
  58. package/src/dialog.tsx +179 -0
  59. package/src/display-preferences.tsx +50 -0
  60. package/src/documents/barcode-svg.stories.tsx +72 -0
  61. package/src/documents/barcode-svg.tsx +79 -0
  62. package/src/dropdown-menu.stories.tsx +77 -0
  63. package/src/dropdown-menu.tsx +271 -0
  64. package/src/editor/agent-panel.tsx +222 -0
  65. package/src/editor/agents.stories.tsx +81 -0
  66. package/src/editor/agents.ts +55 -0
  67. package/src/editor/behaviour.ts +279 -0
  68. package/src/editor/block-ids.ts +44 -0
  69. package/src/editor/block-list.tsx +104 -0
  70. package/src/editor/block-toolbar.tsx +490 -0
  71. package/src/editor/bubble.tsx +119 -0
  72. package/src/editor/catalogue.ts +169 -0
  73. package/src/editor/collab-rail.tsx +746 -0
  74. package/src/editor/collab.stories.tsx +207 -0
  75. package/src/editor/collab.ts +87 -0
  76. package/src/editor/contract.ts +476 -0
  77. package/src/editor/convert.ts +414 -0
  78. package/src/editor/doc-editor.stories.tsx +98 -0
  79. package/src/editor/doc-editor.tsx +1492 -0
  80. package/src/editor/editor.css +636 -0
  81. package/src/editor/extensions.ts +86 -0
  82. package/src/editor/field-bar.tsx +154 -0
  83. package/src/editor/field-picker.tsx +109 -0
  84. package/src/editor/fields.ts +100 -0
  85. package/src/editor/gutter.tsx +443 -0
  86. package/src/editor/index.ts +23 -0
  87. package/src/editor/label-sheet.tsx +289 -0
  88. package/src/editor/labels.ts +394 -0
  89. package/src/editor/link-tools.tsx +141 -0
  90. package/src/editor/node-views.tsx +461 -0
  91. package/src/editor/page-view.tsx +372 -0
  92. package/src/editor/phone.tsx +310 -0
  93. package/src/editor/plugin-block-view.tsx +230 -0
  94. package/src/editor/plugin-blocks.stories.tsx +96 -0
  95. package/src/editor/plugin-blocks.ts +52 -0
  96. package/src/editor/publish-check.tsx +201 -0
  97. package/src/editor/rich-blocks.stories.tsx +146 -0
  98. package/src/editor/sample.ts +40 -0
  99. package/src/editor/save-block-dialog.tsx +141 -0
  100. package/src/editor/saved-block-view.tsx +205 -0
  101. package/src/editor/saved-blocks.stories.tsx +105 -0
  102. package/src/editor/saved-parts.ts +192 -0
  103. package/src/editor/schema-rich.ts +156 -0
  104. package/src/editor/schema.ts +368 -0
  105. package/src/editor/services.tsx +205 -0
  106. package/src/editor/slash.ts +64 -0
  107. package/src/editor/suggesting.ts +372 -0
  108. package/src/editor/suggestions.ts +763 -0
  109. package/src/editor/table-keys.ts +66 -0
  110. package/src/editor/templates.stories.tsx +324 -0
  111. package/src/editor/toolbar.tsx +364 -0
  112. package/src/empty-state.stories.tsx +51 -0
  113. package/src/empty-state.tsx +126 -0
  114. package/src/empty-state.usage-notes.md +30 -0
  115. package/src/form.stories.tsx +112 -0
  116. package/src/form.tsx +176 -0
  117. package/src/globals.css +20 -0
  118. package/src/icon-button.tsx +85 -0
  119. package/src/icon-map.ts +335 -0
  120. package/src/icon.tsx +41 -0
  121. package/src/index.ts +90 -0
  122. package/src/input.stories.tsx +157 -0
  123. package/src/input.tsx +768 -0
  124. package/src/kpi-strip.tsx +47 -0
  125. package/src/localized-text.ts +76 -0
  126. package/src/locked-screen.stories.tsx +29 -0
  127. package/src/locked-screen.tsx +75 -0
  128. package/src/lockup.tsx +42 -0
  129. package/src/lottie.tsx +441 -0
  130. package/src/mark.tsx +62 -0
  131. package/src/math-text.stories.tsx +33 -0
  132. package/src/math-text.tsx +43 -0
  133. package/src/menu-bar.stories.tsx +198 -0
  134. package/src/menu-bar.tsx +373 -0
  135. package/src/message-thread.tsx +181 -0
  136. package/src/money/currency.ts +244 -0
  137. package/src/money/money-input.tsx +131 -0
  138. package/src/money/money.tsx +33 -0
  139. package/src/open-on-computer.tsx +150 -0
  140. package/src/otp-input.tsx +161 -0
  141. package/src/page-header.stories.tsx +95 -0
  142. package/src/page-header.tsx +122 -0
  143. package/src/page-shell.stories.tsx +42 -0
  144. package/src/page-shell.tsx +32 -0
  145. package/src/page-states.tsx +277 -0
  146. package/src/pagination.stories.tsx +45 -0
  147. package/src/pagination.tsx +98 -0
  148. package/src/paper-sheet.stories.tsx +88 -0
  149. package/src/paper-sheet.tsx +85 -0
  150. package/src/phone-tab-bar.tsx +152 -0
  151. package/src/plugin-card.stories.tsx +208 -0
  152. package/src/plugin-card.tsx +931 -0
  153. package/src/popover.stories.tsx +48 -0
  154. package/src/popover.tsx +55 -0
  155. package/src/radio-group.stories.tsx +26 -0
  156. package/src/radio-group.tsx +44 -0
  157. package/src/route-flow/editor.tsx +1087 -0
  158. package/src/route-flow/model.ts +771 -0
  159. package/src/save-bar.tsx +94 -0
  160. package/src/scope-grant.tsx +139 -0
  161. package/src/screen-too-small.stories.tsx +61 -0
  162. package/src/screen-too-small.tsx +104 -0
  163. package/src/search-field.tsx +65 -0
  164. package/src/select.stories.tsx +44 -0
  165. package/src/select.tsx +197 -0
  166. package/src/separator.stories.tsx +34 -0
  167. package/src/separator.tsx +34 -0
  168. package/src/sheet.stories.tsx +49 -0
  169. package/src/sheet.tsx +168 -0
  170. package/src/skeleton.stories.tsx +28 -0
  171. package/src/skeleton.tsx +19 -0
  172. package/src/slider.stories.tsx +78 -0
  173. package/src/slider.tsx +300 -0
  174. package/src/spinner.stories.tsx +33 -0
  175. package/src/spinner.tsx +55 -0
  176. package/src/stepper.tsx +85 -0
  177. package/src/switch.stories.tsx +42 -0
  178. package/src/switch.tsx +41 -0
  179. package/src/switcher.stories.tsx +111 -0
  180. package/src/switcher.tsx +131 -0
  181. package/src/system-state.tsx +55 -0
  182. package/src/table.stories.tsx +48 -0
  183. package/src/table.tsx +121 -0
  184. package/src/tabs.stories.tsx +74 -0
  185. package/src/tabs.tsx +90 -0
  186. package/src/tag-input.stories.tsx +67 -0
  187. package/src/tag-input.tsx +117 -0
  188. package/src/tenant-logo.tsx +63 -0
  189. package/src/tenant-switcher.stories.tsx +38 -0
  190. package/src/tenant-switcher.tsx +164 -0
  191. package/src/textarea.stories.tsx +27 -0
  192. package/src/textarea.tsx +18 -0
  193. package/src/theme-mode.ts +52 -0
  194. package/src/time-grid.tsx +163 -0
  195. package/src/time-picker.stories.tsx +62 -0
  196. package/src/time-picker.tsx +615 -0
  197. package/src/toggle-group.tsx +138 -0
  198. package/src/tooltip.stories.tsx +43 -0
  199. package/src/tooltip.tsx +73 -0
  200. package/src/top-nav.stories.tsx +58 -0
  201. package/src/top-nav.tsx +324 -0
  202. package/src/undo-bar.tsx +107 -0
  203. package/src/utils.ts +45 -0
  204. package/src/view-toggle.stories.tsx +84 -0
  205. package/src/view-toggle.tsx +110 -0
@@ -0,0 +1,86 @@
1
+ import type { AnyExtension } from "@tiptap/core"
2
+ import { Gapcursor, Placeholder, UndoRedo } from "@tiptap/extensions"
3
+
4
+ import { BlockIds } from "./block-ids"
5
+ import { DocumentBehaviour } from "./behaviour"
6
+ import { richNodeViews } from "./node-views"
7
+ import { CheckItem, schemaExtensions } from "./schema"
8
+ import { Suggesting } from "./suggesting"
9
+ import { TableKeys } from "./table-keys"
10
+
11
+ /** The checklist box: a real checkbox button, outside the editable text. */
12
+ const CheckItemWithBox = CheckItem.extend({
13
+ addNodeView() {
14
+ return ({ node: initial, getPos, editor }) => {
15
+ let node = initial
16
+ const dom = document.createElement("div")
17
+ const box = document.createElement("button")
18
+ const contentDOM = document.createElement("div")
19
+ box.type = "button"
20
+ box.contentEditable = "false"
21
+ box.setAttribute("role", "checkbox")
22
+ box.className = "wy-check"
23
+ contentDOM.className = "wy-check-text"
24
+ dom.append(box, contentDOM)
25
+
26
+ const paint = () => {
27
+ dom.dataset.type = "check-item"
28
+ dom.dataset.level = String(node.attrs.level)
29
+ dom.dataset.checked = node.attrs.checked ? "true" : "false"
30
+ if (node.attrs.id) dom.dataset.blockId = node.attrs.id
31
+ box.setAttribute("aria-checked", node.attrs.checked ? "true" : "false")
32
+ box.setAttribute("aria-label", node.textContent || "Checklist item")
33
+ }
34
+ paint()
35
+
36
+ box.addEventListener("mousedown", (e) => e.preventDefault())
37
+ box.addEventListener("click", () => {
38
+ if (!editor.isEditable) return
39
+ const pos = typeof getPos === "function" ? getPos() : undefined
40
+ if (pos === undefined) return
41
+ editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, undefined, { ...node.attrs, checked: !node.attrs.checked }))
42
+ })
43
+
44
+ return {
45
+ dom,
46
+ contentDOM,
47
+ update(next) {
48
+ if (next.type !== node.type) return false
49
+ node = next
50
+ paint()
51
+ return true
52
+ },
53
+ ignoreMutation: (m) => m.target === box || box.contains(m.target as globalThis.Node),
54
+ }
55
+ }
56
+ },
57
+ })
58
+
59
+ export interface DocumentExtensionOptions {
60
+ /** The focused empty line's hint (EditorInsert card). */
61
+ placeholder?: string
62
+ }
63
+
64
+ /** Everything the document editor runs on, in one list. */
65
+ export function documentExtensions({ placeholder }: DocumentExtensionOptions = {}): AnyExtension[] {
66
+ return [
67
+ ...schemaExtensions.map((ext) => {
68
+ if (ext.name === "checkItem") return CheckItemWithBox
69
+ // M3 blocks get their editor views (node-views.tsx).
70
+ return richNodeViews.find((view) => view.name === ext.name) ?? ext
71
+ }),
72
+ Gapcursor,
73
+ TableKeys,
74
+ BlockIds,
75
+ DocumentBehaviour,
76
+ // Off until the editor turns Suggesting mode on (M5); also draws comment highlights.
77
+ Suggesting,
78
+ UndoRedo.configure({ depth: 200 }),
79
+ Placeholder.configure({
80
+ placeholder: placeholder ?? "Type / for blocks, {{ for fields, or just write",
81
+ showOnlyCurrent: true,
82
+ includeChildren: false,
83
+ emptyNodeClass: "is-empty",
84
+ }),
85
+ ]
86
+ }
@@ -0,0 +1,154 @@
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
+ import { useEditorState } from "@tiptap/react"
7
+
8
+ import { Button } from "../button"
9
+ import { IconButton } from "../icon-button"
10
+ import { FieldPicker, flatFields } from "./field-picker"
11
+ import { filterFields, type FieldDef, type FieldGroup } from "./fields"
12
+ import type { EditorLabels } from "./labels"
13
+
14
+ /**
15
+ * A selected merge field's own bar (E.26): what fills it (its group, its
16
+ * sample) and the two things an author does to one: change it for another
17
+ * field, or remove it. Before this, clicking a chip only selected it, so the
18
+ * only way to swap "Bill to" for "Customer name" was to delete it and find the
19
+ * new one in Field ▾.
20
+ */
21
+ function FieldBar({
22
+ editor,
23
+ labels,
24
+ groups,
25
+ container,
26
+ }: {
27
+ editor: Editor
28
+ labels: EditorLabels
29
+ groups: FieldGroup[] | undefined
30
+ container: React.RefObject<HTMLDivElement | null>
31
+ }) {
32
+ const target = useEditorState({
33
+ editor,
34
+ selector: ({ editor: e }) => {
35
+ const sel = e.state.selection
36
+ return e.isEditable && sel instanceof NodeSelection && sel.node.type.name === "field" ? { node: sel.node, pos: sel.from } : null
37
+ },
38
+ })
39
+ const [changing, setChanging] = React.useState(false)
40
+ const [query, setQuery] = React.useState("")
41
+ const [active, setActive] = React.useState(0)
42
+ const [place, setPlace] = React.useState<{ top: number; start: number } | null>(null)
43
+
44
+ const pos = target?.pos
45
+ React.useEffect(() => {
46
+ setChanging(false)
47
+ setQuery("")
48
+ }, [pos])
49
+ React.useLayoutEffect(() => {
50
+ if (pos === undefined || !container.current) {
51
+ setPlace(null)
52
+ return
53
+ }
54
+ const dom = editor.view.nodeDOM(pos) as HTMLElement | null
55
+ if (!dom) return
56
+ const box = container.current.getBoundingClientRect()
57
+ const rect = dom.getBoundingClientRect()
58
+ const rtl = getComputedStyle(container.current).direction === "rtl"
59
+ setPlace({ top: rect.bottom - box.top + 6, start: rtl ? box.right - rect.right : rect.left - box.left })
60
+ }, [editor, pos, container, target?.node])
61
+
62
+ const all = React.useMemo(() => flatFields(groups ?? []), [groups])
63
+ const shown = React.useMemo(() => filterFields(groups ?? [], query), [groups, query])
64
+ const flat = flatFields(shown)
65
+ React.useEffect(() => setActive(0), [query, changing])
66
+
67
+ if (!target || !place) return null
68
+ const path = String(target.node.attrs.path ?? "")
69
+ const def: FieldDef | undefined = all.find((f) => f.path === path)
70
+ const group = (groups ?? []).find((g) => g.items.some((f) => f.path === path))?.group
71
+ const label = String(target.node.attrs.label || def?.label || path)
72
+
73
+ const replace = (field: FieldDef) => {
74
+ editor
75
+ .chain()
76
+ .focus()
77
+ .command(({ tr }) => {
78
+ tr.setNodeMarkup(target.pos, undefined, { ...target.node.attrs, path: field.path, label: field.label, required: field.required ? true : null })
79
+ tr.setSelection(NodeSelection.create(tr.doc, target.pos))
80
+ return true
81
+ })
82
+ .run()
83
+ setChanging(false)
84
+ }
85
+ const remove = () => editor.chain().focus().deleteRange({ from: target.pos, to: target.pos + target.node.nodeSize }).run()
86
+
87
+ return (
88
+ <div
89
+ role="toolbar"
90
+ aria-label={labels.fieldBar}
91
+ data-slot="field-bar"
92
+ className="absolute z-20 grid w-max max-w-[360px] gap-1 rounded-control border border-border bg-popover p-1.5 text-sm text-popover-foreground shadow-float"
93
+ style={{ top: place.top, insetInlineStart: Math.max(0, place.start) }}
94
+ onMouseDown={(e) => {
95
+ if ((e.target as HTMLElement).tagName !== "INPUT") e.preventDefault()
96
+ }}
97
+ >
98
+ <div className="flex items-center gap-2 px-1">
99
+ <span className="min-w-0">
100
+ <span className="block truncate font-medium" dir="auto">
101
+ {label}
102
+ </span>
103
+ <span className="block truncate text-xs text-muted-foreground" dir="auto">
104
+ {[group, def?.sample ? `${labels.fieldExample} ${def.sample}` : null].filter(Boolean).join(" · ")}
105
+ </span>
106
+ </span>
107
+ <span className="ms-auto flex shrink-0 items-center gap-0.5">
108
+ {all.length > 0 && (
109
+ <Button variant="outline" size="sm" onClick={() => setChanging((v) => !v)} aria-expanded={changing}>
110
+ {labels.fieldChange}
111
+ </Button>
112
+ )}
113
+ <IconButton icon="delete" label={labels.fieldRemove} size="sm" onClick={remove} />
114
+ </span>
115
+ </div>
116
+ {changing && (
117
+ <FieldPicker
118
+ id="wy-fieldbar"
119
+ groups={shown}
120
+ active={Math.min(active, Math.max(0, flat.length - 1))}
121
+ onActive={setActive}
122
+ onPick={replace}
123
+ labels={labels}
124
+ search={
125
+ <input
126
+ autoFocus
127
+ aria-label={labels.fieldSearch}
128
+ placeholder={labels.fieldSearch}
129
+ value={query}
130
+ onChange={(e) => setQuery(e.target.value)}
131
+ onKeyDown={(e) => {
132
+ const n = flat.length || 1
133
+ if (e.key === "ArrowDown" || e.key === "ArrowUp") {
134
+ e.preventDefault()
135
+ setActive((i) => (i + (e.key === "ArrowDown" ? 1 : n - 1)) % n)
136
+ } else if (e.key === "Enter" && flat[active]) {
137
+ e.preventDefault()
138
+ replace(flat[active])
139
+ } else if (e.key === "Escape") {
140
+ e.preventDefault()
141
+ setChanging(false)
142
+ editor.commands.focus()
143
+ }
144
+ }}
145
+ className="mx-1 mb-1 h-8 rounded-control-sm border border-input bg-card px-2 text-sm outline-none focus-visible:border-ring"
146
+ />
147
+ }
148
+ />
149
+ )}
150
+ </div>
151
+ )
152
+ }
153
+
154
+ export { FieldBar }
@@ -0,0 +1,109 @@
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-[340px] max-w-[calc(100vw-2rem)] 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 shrink-0">{field.label}</span>
86
+ {/* min-w-0 lets the sample give way: without it a long chip
87
+ pushed "7 November 2026" past the picker's edge. */}
88
+ {field.sample && (
89
+ <span dir="auto" title={field.sample} className="ms-auto min-w-0 truncate text-xs text-muted-foreground">
90
+ {field.sample}
91
+ </span>
92
+ )}
93
+ </div>
94
+ )
95
+ })}
96
+ </div>
97
+ ))}
98
+ </div>
99
+ {footer}
100
+ </div>
101
+ )
102
+ }
103
+
104
+ /** The fields in picker order, so the active index maps back to one. */
105
+ function flatFields(groups: FieldGroup[]): FieldDef[] {
106
+ return groups.flatMap((g) => g.items)
107
+ }
108
+
109
+ 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
+ })