@whity/ui 0.5.0 → 1.0.1

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 (208) hide show
  1. package/README.md +107 -107
  2. package/package.json +223 -192
  3. package/src/access-denied.stories.tsx +179 -179
  4. package/src/access-denied.tsx +305 -305
  5. package/src/action-proposal.tsx +338 -302
  6. package/src/actor-chip.tsx +71 -71
  7. package/src/agent-activity.tsx +163 -163
  8. package/src/agent-collaboration.stories.tsx +156 -156
  9. package/src/agent-run-status.tsx +196 -196
  10. package/src/alert-dialog.stories.tsx +45 -45
  11. package/src/alert-dialog.tsx +147 -147
  12. package/src/alert.stories.tsx +91 -91
  13. package/src/alert.tsx +89 -89
  14. package/src/app-sidebar.stories.tsx +195 -195
  15. package/src/app-sidebar.tsx +604 -500
  16. package/src/approval-queue.tsx +109 -108
  17. package/src/audience-group-picker.stories.tsx +198 -198
  18. package/src/audience-group-picker.tsx +362 -362
  19. package/src/audience-people-picker.stories.tsx +102 -102
  20. package/src/audience-people-picker.tsx +279 -279
  21. package/src/avatar-pattern.ts +138 -0
  22. package/src/avatar.tsx +125 -83
  23. package/src/badge.stories.tsx +105 -105
  24. package/src/badge.tsx +68 -68
  25. package/src/beta-warning.stories.tsx +79 -79
  26. package/src/beta-warning.tsx +189 -189
  27. package/src/bilingual-input.stories.tsx +82 -82
  28. package/src/bilingual-input.tsx +188 -183
  29. package/src/board.tsx +216 -0
  30. package/src/breadcrumb.stories.tsx +51 -51
  31. package/src/breadcrumb.tsx +90 -90
  32. package/src/built-on-whity.tsx +52 -0
  33. package/src/button.stories.tsx +87 -87
  34. package/src/button.tsx +125 -118
  35. package/src/card.stories.tsx +423 -423
  36. package/src/card.tsx +180 -180
  37. package/src/chart.stories.tsx +66 -66
  38. package/src/chart.tsx +228 -228
  39. package/src/checkbox.stories.tsx +62 -62
  40. package/src/checkbox.tsx +54 -54
  41. package/src/collapsible.stories.tsx +48 -48
  42. package/src/collapsible.tsx +34 -34
  43. package/src/color-card.stories.tsx +61 -61
  44. package/src/color-card.tsx +270 -270
  45. package/src/color-picker.stories.tsx +65 -65
  46. package/src/color-picker.tsx +425 -425
  47. package/src/column-matcher.tsx +84 -0
  48. package/src/command-palette.tsx +399 -306
  49. package/src/confirm-action.tsx +98 -0
  50. package/src/copy-block.stories.tsx +38 -38
  51. package/src/copy-block.tsx +85 -85
  52. package/src/data-table.stories.tsx +200 -200
  53. package/src/data-table.tsx +1687 -1106
  54. package/src/date-picker.stories.tsx +61 -61
  55. package/src/date-picker.tsx +457 -422
  56. package/src/design-cards.stories.tsx +551 -458
  57. package/src/dialog.stories.tsx +48 -48
  58. package/src/dialog.tsx +179 -166
  59. package/src/display-preferences.tsx +59 -0
  60. package/src/documents/barcode-svg.stories.tsx +72 -72
  61. package/src/documents/barcode-svg.tsx +79 -79
  62. package/src/dropdown-menu.stories.tsx +77 -77
  63. package/src/dropdown-menu.tsx +271 -271
  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 -279
  68. package/src/editor/block-ids.ts +44 -44
  69. package/src/editor/block-list.tsx +104 -101
  70. package/src/editor/block-toolbar.tsx +490 -477
  71. package/src/editor/bubble.tsx +119 -93
  72. package/src/editor/catalogue.ts +169 -103
  73. package/src/editor/collab-rail.tsx +746 -746
  74. package/src/editor/collab.stories.tsx +207 -207
  75. package/src/editor/collab.ts +87 -87
  76. package/src/editor/contract.ts +476 -408
  77. package/src/editor/convert.ts +414 -362
  78. package/src/editor/doc-editor.stories.tsx +98 -98
  79. package/src/editor/doc-editor.tsx +1492 -1287
  80. package/src/editor/editor.css +636 -536
  81. package/src/editor/extensions.ts +86 -86
  82. package/src/editor/field-bar.tsx +154 -0
  83. package/src/editor/field-picker.tsx +109 -107
  84. package/src/editor/fields.ts +100 -100
  85. package/src/editor/gutter.tsx +443 -367
  86. package/src/editor/index.ts +23 -22
  87. package/src/editor/label-sheet.tsx +289 -289
  88. package/src/editor/labels.ts +394 -310
  89. package/src/editor/link-tools.tsx +141 -141
  90. package/src/editor/node-views.tsx +461 -450
  91. package/src/editor/page-view.tsx +372 -353
  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 -182
  97. package/src/editor/rich-blocks.stories.tsx +146 -146
  98. package/src/editor/sample.ts +40 -40
  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 -150
  104. package/src/editor/schema.ts +368 -315
  105. package/src/editor/services.tsx +205 -127
  106. package/src/editor/slash.ts +64 -64
  107. package/src/editor/suggesting.ts +372 -372
  108. package/src/editor/suggestions.ts +763 -763
  109. package/src/editor/table-keys.ts +66 -66
  110. package/src/editor/templates.stories.tsx +324 -324
  111. package/src/editor/toolbar.tsx +364 -358
  112. package/src/empty-state.stories.tsx +51 -51
  113. package/src/empty-state.tsx +126 -100
  114. package/src/empty-state.usage-notes.md +30 -30
  115. package/src/form.stories.tsx +112 -112
  116. package/src/form.tsx +176 -176
  117. package/src/globals.css +20 -20
  118. package/src/icon-button.tsx +85 -85
  119. package/src/icon-map.ts +335 -244
  120. package/src/icon.tsx +41 -41
  121. package/src/index.ts +92 -67
  122. package/src/input.stories.tsx +157 -157
  123. package/src/input.tsx +768 -759
  124. package/src/kpi-strip.tsx +47 -0
  125. package/src/localized-text.ts +76 -0
  126. package/src/locked-screen.stories.tsx +29 -29
  127. package/src/locked-screen.tsx +75 -61
  128. package/src/lockup.tsx +42 -0
  129. package/src/lottie.tsx +441 -0
  130. package/src/mark.tsx +62 -62
  131. package/src/math-text.stories.tsx +33 -33
  132. package/src/math-text.tsx +43 -43
  133. package/src/menu-bar.stories.tsx +198 -198
  134. package/src/menu-bar.tsx +373 -373
  135. package/src/message-thread.tsx +181 -0
  136. package/src/money/currency.ts +244 -230
  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 -95
  142. package/src/page-header.tsx +122 -122
  143. package/src/page-shell.stories.tsx +42 -42
  144. package/src/page-shell.tsx +32 -32
  145. package/src/page-states.tsx +277 -0
  146. package/src/pagination.stories.tsx +45 -45
  147. package/src/pagination.tsx +98 -98
  148. package/src/paper-sheet.stories.tsx +88 -0
  149. package/src/paper-sheet.tsx +85 -0
  150. package/src/phone-input.tsx +252 -0
  151. package/src/phone-tab-bar.tsx +152 -0
  152. package/src/plugin-card.stories.tsx +208 -180
  153. package/src/plugin-card.tsx +931 -848
  154. package/src/popover.stories.tsx +48 -48
  155. package/src/popover.tsx +55 -55
  156. package/src/radio-group.stories.tsx +26 -26
  157. package/src/radio-group.tsx +44 -44
  158. package/src/role-summary.stories.tsx +143 -0
  159. package/src/role-summary.tsx +120 -0
  160. package/src/route-flow/editor.tsx +1087 -1014
  161. package/src/route-flow/model.ts +771 -716
  162. package/src/save-bar.tsx +94 -0
  163. package/src/scope-grant.tsx +139 -139
  164. package/src/screen-too-small.stories.tsx +61 -61
  165. package/src/screen-too-small.tsx +104 -104
  166. package/src/search-field.tsx +65 -0
  167. package/src/select.stories.tsx +44 -44
  168. package/src/select.tsx +197 -197
  169. package/src/separator.stories.tsx +34 -34
  170. package/src/separator.tsx +34 -34
  171. package/src/sheet.stories.tsx +49 -49
  172. package/src/sheet.tsx +168 -168
  173. package/src/skeleton.stories.tsx +28 -28
  174. package/src/skeleton.tsx +19 -19
  175. package/src/slider.stories.tsx +78 -78
  176. package/src/slider.tsx +300 -300
  177. package/src/spinner.stories.tsx +33 -33
  178. package/src/spinner.tsx +55 -55
  179. package/src/stepper.tsx +85 -0
  180. package/src/switch.stories.tsx +42 -42
  181. package/src/switch.tsx +41 -41
  182. package/src/switcher.stories.tsx +111 -111
  183. package/src/switcher.tsx +131 -131
  184. package/src/system-state.tsx +55 -0
  185. package/src/table.stories.tsx +48 -48
  186. package/src/table.tsx +121 -121
  187. package/src/tabs.stories.tsx +74 -74
  188. package/src/tabs.tsx +90 -90
  189. package/src/tag-input.stories.tsx +67 -67
  190. package/src/tag-input.tsx +117 -117
  191. package/src/tenant-logo.tsx +63 -63
  192. package/src/tenant-switcher.stories.tsx +38 -38
  193. package/src/tenant-switcher.tsx +185 -164
  194. package/src/textarea.stories.tsx +27 -27
  195. package/src/textarea.tsx +18 -18
  196. package/src/theme-mode.ts +74 -52
  197. package/src/time-grid.tsx +163 -0
  198. package/src/time-picker.stories.tsx +62 -62
  199. package/src/time-picker.tsx +616 -615
  200. package/src/toggle-group.tsx +139 -138
  201. package/src/tooltip.stories.tsx +43 -43
  202. package/src/tooltip.tsx +73 -73
  203. package/src/top-nav.stories.tsx +58 -58
  204. package/src/top-nav.tsx +324 -324
  205. package/src/undo-bar.tsx +107 -107
  206. package/src/utils.ts +45 -45
  207. package/src/view-toggle.stories.tsx +84 -84
  208. package/src/view-toggle.tsx +111 -110
@@ -1,477 +1,490 @@
1
- "use client"
2
-
3
- import * as React from "react"
4
- import type { Editor } from "@tiptap/core"
5
- import type { Node as PmNode } from "@tiptap/pm/model"
6
- import { NodeSelection, TextSelection } from "@tiptap/pm/state"
7
- import { useEditorState } from "@tiptap/react"
8
-
9
- import { Button } from "../button"
10
- import { IconButton } from "../icon-button"
11
- import { ToggleGroup } from "../toggle-group"
12
- import { cn } from "../utils"
13
- import type { Inline } from "./contract"
14
- import { deleteBlock } from "./behaviour"
15
- import { fill, type EditorLabels } from "./labels"
16
- import { inlineText, textToInline, useEditorServices } from "./services"
17
-
18
- const RICH = new Set(["callout", "table", "image", "file", "record", "chart", "signature", "barcode", "qr", "math"])
19
-
20
- /** The rich block the selection is on or in: its node and position. */
21
- function selectedRich(editor: Editor): { node: PmNode; pos: number } | null {
22
- const sel = editor.state.selection
23
- if (sel instanceof NodeSelection && RICH.has(sel.node.type.name)) return { node: sel.node, pos: sel.from }
24
- const { $from } = sel
25
- for (let d = $from.depth; d > 0; d--) {
26
- const node = $from.node(d)
27
- if (node.type.name === "table" || node.type.name === "callout") return { node, pos: $from.before(d) }
28
- }
29
- return null
30
- }
31
-
32
- function Sep() {
33
- return <span aria-hidden="true" className="mx-[3px] h-[18px] w-px bg-border" />
34
- }
35
-
36
- /** A labelled text field the toolbar opens for one attribute; Enter keeps it, Esc leaves it. */
37
- function FieldRow({ label, value, onDone, onCancel, mono }: { label: string; value: string; onDone: (v: string) => void; onCancel: () => void; mono?: boolean }) {
38
- const [text, setText] = React.useState(value)
39
- const ref = React.useRef<HTMLInputElement>(null)
40
- React.useEffect(() => ref.current?.focus(), [])
41
- const { labels } = useEditorServices()
42
- return (
43
- <div className="flex items-center gap-1.5 border-t border-border pt-1">
44
- <label className="flex flex-1 items-center gap-2 text-[0.8125rem] text-muted-foreground">
45
- {label}
46
- <input
47
- ref={ref}
48
- value={text}
49
- dir="auto"
50
- onChange={(e) => setText(e.target.value)}
51
- onKeyDown={(e) => {
52
- if (e.key === "Enter") {
53
- e.preventDefault()
54
- onDone(text)
55
- } else if (e.key === "Escape") {
56
- e.preventDefault()
57
- onCancel()
58
- }
59
- }}
60
- className={cn(
61
- "h-7 min-w-0 flex-1 rounded-control-sm border border-input bg-card px-2 text-[0.8125rem] text-foreground outline-none focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring",
62
- mono && "font-mono"
63
- )}
64
- />
65
- </label>
66
- <Button size="sm" variant="outline" onClick={() => onDone(text)}>
67
- {labels.done}
68
- </Button>
69
- </div>
70
- )
71
- }
72
-
73
- type Editing = { key: string; label: string; value: string; apply: (v: string) => void; mono?: boolean } | null
74
-
75
- /**
76
- * A selected rich block's own toolbar, above it (E.8, EditorBlockChrome):
77
- * image width, caption, alt text, replace; table header row, rows and columns;
78
- * callout kind; record and chart source; signature signers and stamp; barcode
79
- * and QR value; the maths formula. Agents never use it (E.4).
80
- */
81
- function BlockToolbar({ editor, labels, container }: { editor: Editor; labels: EditorLabels; container: React.RefObject<HTMLDivElement | null> }) {
82
- const { services } = useEditorServices()
83
- const target = useEditorState({ editor, selector: ({ editor: e }) => (e.isEditable ? selectedRich(e) : null) })
84
- const [editing, setEditing] = React.useState<Editing>(null)
85
- const [busy, setBusy] = React.useState(false)
86
- const [error, setError] = React.useState<string | null>(null)
87
- const fileInput = React.useRef<HTMLInputElement>(null)
88
- const [top, setTop] = React.useState<number | null>(null)
89
- // On a label sheet (E.43) the label is small and its neighbours repeat it, so
90
- // the bar opens below the block, where it covers nothing the author needs.
91
- const [below, setBelow] = React.useState(false)
92
-
93
- const pos = target?.pos
94
- React.useEffect(() => setEditing(null), [pos])
95
- React.useLayoutEffect(() => {
96
- if (pos === undefined || !container.current) {
97
- setTop(null)
98
- return
99
- }
100
- const dom = editor.view.nodeDOM(pos) as HTMLElement | null
101
- if (!dom) return
102
- const box = container.current.getBoundingClientRect()
103
- const rect = dom.getBoundingClientRect()
104
- const inLabel = !!dom.closest(".wy-ls__label")
105
- setBelow(inLabel)
106
- setTop((inLabel ? rect.bottom + 10 : rect.top) - box.top)
107
- }, [editor, pos, container, target?.node])
108
-
109
- if (!target || top === null) return null
110
- const { node } = target
111
- const type = node.type.name
112
- const attrs = node.attrs as Record<string, unknown>
113
- const set = (next: Record<string, unknown>) => {
114
- const current = editor.state.doc.nodeAt(target.pos)
115
- if (!current) return
116
- const tr = editor.state.tr.setNodeMarkup(target.pos, undefined, { ...current.attrs, ...next })
117
- if (editor.state.selection instanceof NodeSelection) tr.setSelection(NodeSelection.create(tr.doc, target.pos))
118
- editor.view.dispatch(tr)
119
- }
120
- const edit = (key: string, label: string, value: string, apply: (v: string) => void, mono?: boolean) => setEditing({ key, label, value, apply, mono })
121
- const remove = () => deleteBlock(editor, target.pos)
122
- const del = <IconButton icon="delete" label={labels.deleteBlock} size="sm" onClick={remove} />
123
-
124
- const upload = async (file: File) => {
125
- if (!services.uploadFile) return
126
- setBusy(true)
127
- setError(null)
128
- try {
129
- const up = await services.uploadFile(file)
130
- if (type === "image") set({ src: up.url, alt: (attrs.alt as string) || file.name.replace(/\.[^.]+$/, "") })
131
- else set({ fileId: up.fileId, name: up.name, size: up.size, mime: up.mime })
132
- } catch {
133
- setError(labels.uploadFailed)
134
- } finally {
135
- setBusy(false)
136
- }
137
- }
138
-
139
- let controls: React.ReactNode = null
140
- if (type === "callout") {
141
- controls = (
142
- <ToggleGroup
143
- size="sm"
144
- label={labels.blockCallout}
145
- required
146
- value={attrs.kind === "important" ? "important" : "note"}
147
- onChange={(v) => set({ kind: v })}
148
- options={[
149
- { value: "note", label: labels.calloutNote, icon: "info" },
150
- { value: "important", label: labels.calloutImportant, icon: "alert" },
151
- ]}
152
- />
153
- )
154
- } else if (type === "image" || type === "file") {
155
- controls = (
156
- <>
157
- {type === "image" && (
158
- <>
159
- <ToggleGroup
160
- size="sm"
161
- label={labels.blockImage}
162
- required
163
- value={String(attrs.width ?? "full")}
164
- onChange={(v) => set({ width: v })}
165
- options={[
166
- { value: "half", label: labels.widthHalf, icon: "columns", iconOnly: true },
167
- { value: "wide", label: labels.widthWide, icon: "continuous", iconOnly: true },
168
- { value: "full", label: labels.widthFull, icon: "page", iconOnly: true },
169
- ]}
170
- />
171
- <Sep />
172
- <Button variant="ghost" size="sm" onClick={() => edit("caption", labels.caption, inlineText(attrs.caption as Inline[] | null), (v) => set({ caption: v.trim() ? textToInline(v, attrs.caption as Inline[] | null) : null }))}>
173
- {labels.caption}
174
- </Button>
175
- <Button variant="ghost" size="sm" onClick={() => edit("alt", labels.altText, String(attrs.alt ?? ""), (v) => set({ alt: v.trim() }))}>
176
- {labels.altText}
177
- </Button>
178
- </>
179
- )}
180
- {services.uploadFile && (
181
- <Button variant="ghost" size="sm" loading={busy} onClick={() => fileInput.current?.click()}>
182
- {busy ? labels.uploading : (type === "image" ? attrs.src : attrs.fileId) ? labels.replace : labels.upload}
183
- </Button>
184
- )}
185
- <input
186
- ref={fileInput}
187
- type="file"
188
- hidden
189
- /* What the server stores (FormUploadPolicy): PNG and JPEG images, and PDF for files. */
190
- accept={type === "image" ? "image/png,image/jpeg" : "application/pdf,image/png,image/jpeg"}
191
- onChange={(e) => {
192
- const file = e.target.files?.[0]
193
- e.target.value = ""
194
- if (file) void upload(file)
195
- }}
196
- />
197
- <Sep />
198
- {del}
199
- </>
200
- )
201
- } else if (type === "table") {
202
- const sel = editor.state.selection
203
- const cellPos = (() => {
204
- const { $from } = sel
205
- for (let d = $from.depth; d > 0; d--) if ($from.node(d).type.name === "tableCell") return { row: $from.index(d - 2), col: $from.index(d - 1) }
206
- return null
207
- })()
208
- const columns = ((attrs.columns as { align: string; widthPct?: number }[]) ?? []).slice()
209
- const width = Math.max(columns.length, ...Array.from({ length: node.childCount }, (_, i) => node.child(i).childCount))
210
- const setRows = (mutate: (rows: Inline[][][]) => Inline[][][], nextColumns = columns) => {
211
- // Rebuild through the contract so cells keep their content and marks.
212
- const json = node.toJSON() as { content?: { content?: { content?: unknown[] }[] }[] }
213
- const rows: Inline[][][] = (json.content ?? []).map((r) => (r.content ?? []).map((c) => (c.content ?? []) as Inline[]))
214
- const next = mutate(rows)
215
- const table = editor.schema.nodeFromJSON({
216
- type: "table",
217
- attrs: { ...node.attrs, columns: nextColumns },
218
- content: next.map((r) => ({ type: "tableRow", content: r.map((c) => ({ type: "tableCell", ...(c.length ? { content: c } : {}) })) })),
219
- })
220
- const tr = editor.state.tr.replaceWith(target.pos, target.pos + node.nodeSize, table)
221
- tr.setSelection(TextSelection.near(tr.doc.resolve(Math.min(target.pos + 3, tr.doc.content.size))))
222
- editor.view.dispatch(tr)
223
- }
224
- const blankRow = () => Array.from({ length: width }, () => [] as Inline[])
225
- controls = (
226
- <>
227
- <Button variant="ghost" size="sm" icon={attrs.headerRow ? "check" : undefined} active={Boolean(attrs.headerRow)} onClick={() => set({ headerRow: !attrs.headerRow })}>
228
- {labels.headerRow}
229
- </Button>
230
- <Sep />
231
- <Button variant="ghost" size="sm" icon="plus" onClick={() => setRows((rows) => { const r = rows.slice(); r.splice(cellPos ? cellPos.row + 1 : r.length, 0, blankRow()); return r })}>
232
- {labels.addRow}
233
- </Button>
234
- <Button
235
- variant="ghost"
236
- size="sm"
237
- icon="plus"
238
- onClick={() => {
239
- const at = cellPos ? cellPos.col + 1 : width
240
- const nextColumns = Array.from({ length: width }, (_, i) => columns[i] ?? { align: "start" as const })
241
- nextColumns.splice(at, 0, { align: "start" })
242
- setRows((rows) => rows.map((r) => { const c = r.slice(); while (c.length < width) c.push([]); c.splice(at, 0, []); return c }), nextColumns)
243
- }}
244
- >
245
- {labels.addColumn}
246
- </Button>
247
- {cellPos && (
248
- <>
249
- <Sep />
250
- <ToggleGroup
251
- size="sm"
252
- label={labels.columnAlign}
253
- required
254
- value={columns[cellPos.col]?.align === "end" ? "end" : "start"}
255
- onChange={(v) => {
256
- const next = Array.from({ length: width }, (_, i) => columns[i] ?? { align: "start" as const })
257
- next[cellPos.col] = { ...next[cellPos.col], align: v === "end" ? "end" : "start" }
258
- set({ columns: next })
259
- }}
260
- options={[
261
- { value: "start", label: labels.alignStart },
262
- { value: "end", label: labels.alignEnd },
263
- ]}
264
- />
265
- <IconButton icon="minus" label={labels.deleteRow} size="sm" disabled={node.childCount <= 1} onClick={() => setRows((rows) => rows.filter((_, i) => i !== cellPos.row))} />
266
- <IconButton
267
- icon="close"
268
- label={labels.deleteColumn}
269
- size="sm"
270
- disabled={width <= 1}
271
- onClick={() => setRows((rows) => rows.map((r) => r.filter((_, i) => i !== cellPos.col)), columns.filter((_, i) => i !== cellPos.col))}
272
- />
273
- </>
274
- )}
275
- <Sep />
276
- {del}
277
- </>
278
- )
279
- } else if (type === "record") {
280
- const ref = attrs.ref as { type: string; id: string } | null
281
- controls = (
282
- <>
283
- {services.recordTypes && services.recordTypes.length > 0 && (
284
- <select
285
- aria-label={labels.recordType}
286
- value={ref?.type ?? ""}
287
- onChange={(e) => set({ ref: e.target.value ? { type: e.target.value, id: ref?.id ?? "" } : null })}
288
- className="h-7 rounded-control-sm border border-input bg-card px-1.5 text-[0.8125rem]"
289
- >
290
- <option value="">{labels.recordType}</option>
291
- {services.recordTypes.map((t) => (
292
- <option key={t.key} value={t.key}>
293
- {t.label}
294
- </option>
295
- ))}
296
- </select>
297
- )}
298
- <Button variant="ghost" size="sm" disabled={!ref?.type} onClick={() => edit("record", labels.recordId, ref?.id ?? "", (v) => set({ ref: { type: ref!.type, id: v.trim() } }), true)}>
299
- {labels.recordChoose}
300
- </Button>
301
- <Sep />
302
- <ToggleGroup
303
- size="sm"
304
- label={labels.blockRecord}
305
- required
306
- value={attrs.view === "summary" ? "summary" : "card"}
307
- onChange={(v) => set({ view: v })}
308
- options={[
309
- { value: "card", label: labels.viewCard },
310
- { value: "summary", label: labels.viewSummary },
311
- ]}
312
- />
313
- <Sep />
314
- {del}
315
- </>
316
- )
317
- } else if (type === "chart") {
318
- const source = attrs.source as { query: string; params?: Record<string, string> } | null
319
- controls = (
320
- <>
321
- <ToggleGroup
322
- size="sm"
323
- label={labels.blockChart}
324
- required
325
- value={String(attrs.chart ?? "bar")}
326
- onChange={(v) => set({ chart: v })}
327
- options={[
328
- { value: "bar", label: labels.chartBar },
329
- { value: "line", label: labels.chartLine },
330
- { value: "donut", label: labels.chartDonut },
331
- ]}
332
- />
333
- {services.chartSources && services.chartSources.length > 0 && (
334
- <>
335
- <Sep />
336
- <select
337
- aria-label={labels.chartSource}
338
- value={source?.query ?? ""}
339
- onChange={(e) => set({ source: e.target.value ? { query: e.target.value } : null })}
340
- className="h-7 rounded-control-sm border border-input bg-card px-1.5 text-[0.8125rem]"
341
- >
342
- <option value="">{labels.chartSource}</option>
343
- {services.chartSources.map((s) => (
344
- <option key={s.key} value={s.key}>
345
- {s.label}
346
- </option>
347
- ))}
348
- </select>
349
- </>
350
- )}
351
- <Button variant="ghost" size="sm" onClick={() => edit("title", labels.chartTitle, String(attrs.title ?? ""), (v) => set({ title: v.trim() || null }))}>
352
- {labels.chartTitle}
353
- </Button>
354
- <Sep />
355
- {del}
356
- </>
357
- )
358
- } else if (type === "signature") {
359
- const signers = (attrs.signers as { name: Inline[]; role: Inline[] }[]) ?? []
360
- controls = (
361
- <>
362
- {signers.map((s, i) => (
363
- <Button
364
- key={i}
365
- variant="ghost"
366
- size="sm"
367
- onClick={() =>
368
- edit(`signer-${i}`, fill(labels.signatureName, {}), `${inlineText(s.name)} | ${inlineText(s.role)}`, (v) => {
369
- const [name, role = ""] = v.split("|").map((x) => x.trim())
370
- const next = signers.slice()
371
- next[i] = { name: textToInline(name, s.name), role: textToInline(role, s.role) }
372
- set({ signers: next })
373
- })
374
- }
375
- >
376
- {inlineText(s.name) || labels.signatureName}
377
- </Button>
378
- ))}
379
- <Button variant="ghost" size="sm" icon="plus" disabled={signers.length >= 3} onClick={() => set({ signers: [...signers, { name: [], role: [] }] })}>
380
- {labels.addSigner}
381
- </Button>
382
- <Button variant="ghost" size="sm" onClick={() => edit("date", labels.signatureDate, inlineText(attrs.date as Inline[]), (v) => set({ date: textToInline(v, attrs.date as Inline[]) }))}>
383
- {/* @date-display-ignore: the signature date is text the author typed or a merge field (Inline[]), not a record timestamp; this is its label. */}
384
- {labels.signatureDate}
385
- </Button>
386
- <Button variant="ghost" size="sm" active={Boolean(attrs.stamp)} onClick={() => set({ stamp: !attrs.stamp })}>
387
- {labels.stamp}
388
- </Button>
389
- <Sep />
390
- {del}
391
- </>
392
- )
393
- } else if (type === "barcode" || type === "qr") {
394
- controls = (
395
- <>
396
- {type === "barcode" && (
397
- <select
398
- aria-label={labels.symbology}
399
- value={String(attrs.symbology ?? "code128")}
400
- onChange={(e) => set({ symbology: e.target.value })}
401
- className="h-7 rounded-control-sm border border-input bg-card px-1.5 text-[0.8125rem]"
402
- >
403
- <option value="code128">Code 128</option>
404
- <option value="ean13">EAN-13</option>
405
- <option value="code39">Code 39</option>
406
- </select>
407
- )}
408
- <Button variant="ghost" size="sm" onClick={() => edit("value", labels.value, inlineText(attrs.value as Inline[]), (v) => set({ value: textToInline(v, attrs.value as Inline[]) }), true)}>
409
- {labels.value}
410
- </Button>
411
- {type === "barcode" ? (
412
- <Button variant="ghost" size="sm" active={Boolean(attrs.showText)} onClick={() => set({ showText: !attrs.showText })}>
413
- {labels.showText}
414
- </Button>
415
- ) : (
416
- <Button variant="ghost" size="sm" onClick={() => edit("caption", labels.caption, inlineText(attrs.caption as Inline[] | null), (v) => set({ caption: v.trim() ? textToInline(v, attrs.caption as Inline[] | null) : null }))}>
417
- {labels.caption}
418
- </Button>
419
- )}
420
- <Sep />
421
- {del}
422
- </>
423
- )
424
- } else if (type === "math") {
425
- controls = (
426
- <>
427
- <Button variant="ghost" size="sm" onClick={() => edit("tex", labels.formula, String(attrs.tex ?? ""), (v) => set({ tex: v }), true)}>
428
- {labels.formula}
429
- </Button>
430
- <Sep />
431
- {del}
432
- </>
433
- )
434
- }
435
- if (!controls) return null
436
-
437
- return (
438
- <div
439
- role="toolbar"
440
- aria-label={labels.blockToolbar}
441
- data-slot="block-toolbar"
442
- className={cn(
443
- "absolute start-0 z-20 grid gap-1 rounded-control border border-border bg-popover p-[3px] shadow-float",
444
- below ? "w-max max-w-[420px]" : "max-w-full"
445
- )}
446
- style={below ? { top } : { top, transform: "translateY(calc(-100% - 10px))" }}
447
- onMouseDown={(e) => {
448
- if ((e.target as HTMLElement).tagName !== "INPUT" && (e.target as HTMLElement).tagName !== "SELECT") e.preventDefault()
449
- }}
450
- >
451
- <div className="flex flex-wrap items-center gap-0.5">{controls}</div>
452
- {error && (
453
- <div role="alert" className="px-1.5 pb-0.5 text-xs text-error-text">
454
- {error}
455
- </div>
456
- )}
457
- {editing && (
458
- <FieldRow
459
- key={editing.key}
460
- label={editing.label}
461
- value={editing.value}
462
- mono={editing.mono}
463
- onCancel={() => {
464
- setEditing(null)
465
- editor.commands.focus()
466
- }}
467
- onDone={(v) => {
468
- editing.apply(v)
469
- setEditing(null)
470
- }}
471
- />
472
- )}
473
- </div>
474
- )
475
- }
476
-
477
- export { BlockToolbar }
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import type { Editor } from "@tiptap/core"
5
+ import type { Node as PmNode } from "@tiptap/pm/model"
6
+ import { NodeSelection, TextSelection } from "@tiptap/pm/state"
7
+ import { useEditorState } from "@tiptap/react"
8
+
9
+ import { Button } from "../button"
10
+ import { IconButton } from "../icon-button"
11
+ import { ToggleGroup } from "../toggle-group"
12
+ import { cn } from "../utils"
13
+ import type { Inline } from "./contract"
14
+ import { deleteBlock } from "./behaviour"
15
+ import { fill, type EditorLabels } from "./labels"
16
+ import { inlineText, textToInline, useEditorServices } from "./services"
17
+
18
+ const RICH = new Set(["callout", "table", "image", "file", "record", "chart", "signature", "barcode", "qr", "math"])
19
+
20
+ /** The rich block the selection is on or in: its node and position. */
21
+ function selectedRich(editor: Editor): { node: PmNode; pos: number } | null {
22
+ const sel = editor.state.selection
23
+ if (sel instanceof NodeSelection && RICH.has(sel.node.type.name)) return { node: sel.node, pos: sel.from }
24
+ const { $from } = sel
25
+ for (let d = $from.depth; d > 0; d--) {
26
+ const node = $from.node(d)
27
+ if (node.type.name === "table" || node.type.name === "callout") return { node, pos: $from.before(d) }
28
+ }
29
+ return null
30
+ }
31
+
32
+ function Sep() {
33
+ return <span aria-hidden="true" className="mx-[3px] h-[18px] w-px bg-border" />
34
+ }
35
+
36
+ /** A labelled text field the toolbar opens for one attribute; Enter keeps it, Esc leaves it. */
37
+ function FieldRow({ label, value, onDone, onCancel, mono }: { label: string; value: string; onDone: (v: string) => void; onCancel: () => void; mono?: boolean }) {
38
+ const [text, setText] = React.useState(value)
39
+ const ref = React.useRef<HTMLInputElement>(null)
40
+ React.useEffect(() => ref.current?.focus(), [])
41
+ const { labels } = useEditorServices()
42
+ return (
43
+ <div className="flex items-center gap-1.5 border-t border-border pt-1">
44
+ <label className="flex flex-1 items-center gap-2 text-[0.8125rem] text-muted-foreground">
45
+ {label}
46
+ <input
47
+ ref={ref}
48
+ value={text}
49
+ dir="auto"
50
+ onChange={(e) => setText(e.target.value)}
51
+ onKeyDown={(e) => {
52
+ if (e.key === "Enter") {
53
+ e.preventDefault()
54
+ onDone(text)
55
+ } else if (e.key === "Escape") {
56
+ e.preventDefault()
57
+ onCancel()
58
+ }
59
+ }}
60
+ className={cn(
61
+ "h-7 min-w-0 flex-1 rounded-control-sm border border-input bg-card px-2 text-[0.8125rem] text-foreground outline-none focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring",
62
+ mono && "font-mono"
63
+ )}
64
+ />
65
+ </label>
66
+ <Button size="sm" variant="outline" onClick={() => onDone(text)}>
67
+ {labels.done}
68
+ </Button>
69
+ </div>
70
+ )
71
+ }
72
+
73
+ type Editing = { key: string; label: string; value: string; apply: (v: string) => void; mono?: boolean } | null
74
+
75
+ /**
76
+ * A selected rich block's own toolbar, above it (E.8, EditorBlockChrome):
77
+ * image width, caption, alt text, replace; table header row, rows and columns;
78
+ * callout kind; record and chart source; signature signers and stamp; barcode
79
+ * and QR value; the maths formula. Agents never use it (E.4).
80
+ */
81
+ function BlockToolbar({ editor, labels, container }: { editor: Editor; labels: EditorLabels; container: React.RefObject<HTMLDivElement | null> }) {
82
+ const { services } = useEditorServices()
83
+ const target = useEditorState({ editor, selector: ({ editor: e }) => (e.isEditable ? selectedRich(e) : null) })
84
+ // A document opens with its caret at the start, which in most starters is
85
+ // inside the first table: shown then, the bar sat over the letterhead before
86
+ // the author had touched anything. It waits for the first focus and stays
87
+ // after it, so typing in its own fields (which blurs the editor) keeps it.
88
+ const [engaged, setEngaged] = React.useState(() => editor.isFocused)
89
+ React.useEffect(() => {
90
+ if (engaged) return
91
+ const onFocus = () => setEngaged(true)
92
+ editor.on("focus", onFocus)
93
+ return () => {
94
+ editor.off("focus", onFocus)
95
+ }
96
+ }, [editor, engaged])
97
+ const [editing, setEditing] = React.useState<Editing>(null)
98
+ const [busy, setBusy] = React.useState(false)
99
+ const [error, setError] = React.useState<string | null>(null)
100
+ const fileInput = React.useRef<HTMLInputElement>(null)
101
+ const [top, setTop] = React.useState<number | null>(null)
102
+ // On a label sheet (E.43) the label is small and its neighbours repeat it, so
103
+ // the bar opens below the block, where it covers nothing the author needs.
104
+ const [below, setBelow] = React.useState(false)
105
+
106
+ const pos = target?.pos
107
+ React.useEffect(() => setEditing(null), [pos])
108
+ React.useLayoutEffect(() => {
109
+ if (pos === undefined || !container.current) {
110
+ setTop(null)
111
+ return
112
+ }
113
+ const dom = editor.view.nodeDOM(pos) as HTMLElement | null
114
+ if (!dom) return
115
+ const box = container.current.getBoundingClientRect()
116
+ const rect = dom.getBoundingClientRect()
117
+ const inLabel = !!dom.closest(".wy-ls__label")
118
+ setBelow(inLabel)
119
+ setTop((inLabel ? rect.bottom + 10 : rect.top) - box.top)
120
+ }, [editor, pos, container, target?.node])
121
+
122
+ if (!engaged || !target || top === null) return null
123
+ const { node } = target
124
+ const type = node.type.name
125
+ const attrs = node.attrs as Record<string, unknown>
126
+ const set = (next: Record<string, unknown>) => {
127
+ const current = editor.state.doc.nodeAt(target.pos)
128
+ if (!current) return
129
+ const tr = editor.state.tr.setNodeMarkup(target.pos, undefined, { ...current.attrs, ...next })
130
+ if (editor.state.selection instanceof NodeSelection) tr.setSelection(NodeSelection.create(tr.doc, target.pos))
131
+ editor.view.dispatch(tr)
132
+ }
133
+ const edit = (key: string, label: string, value: string, apply: (v: string) => void, mono?: boolean) => setEditing({ key, label, value, apply, mono })
134
+ const remove = () => deleteBlock(editor, target.pos)
135
+ const del = <IconButton icon="delete" label={labels.deleteBlock} size="sm" onClick={remove} />
136
+
137
+ const upload = async (file: File) => {
138
+ if (!services.uploadFile) return
139
+ setBusy(true)
140
+ setError(null)
141
+ try {
142
+ const up = await services.uploadFile(file)
143
+ if (type === "image") set({ src: up.url, alt: (attrs.alt as string) || file.name.replace(/\.[^.]+$/, "") })
144
+ else set({ fileId: up.fileId, name: up.name, size: up.size, mime: up.mime })
145
+ } catch {
146
+ setError(labels.uploadFailed)
147
+ } finally {
148
+ setBusy(false)
149
+ }
150
+ }
151
+
152
+ let controls: React.ReactNode = null
153
+ if (type === "callout") {
154
+ controls = (
155
+ <ToggleGroup
156
+ size="sm"
157
+ label={labels.blockCallout}
158
+ required
159
+ value={attrs.kind === "important" ? "important" : "note"}
160
+ onChange={(v) => set({ kind: v })}
161
+ options={[
162
+ { value: "note", label: labels.calloutNote, icon: "info" },
163
+ { value: "important", label: labels.calloutImportant, icon: "alert" },
164
+ ]}
165
+ />
166
+ )
167
+ } else if (type === "image" || type === "file") {
168
+ controls = (
169
+ <>
170
+ {type === "image" && (
171
+ <>
172
+ <ToggleGroup
173
+ size="sm"
174
+ label={labels.blockImage}
175
+ required
176
+ value={String(attrs.width ?? "full")}
177
+ onChange={(v) => set({ width: v })}
178
+ options={[
179
+ { value: "half", label: labels.widthHalf, icon: "columns", iconOnly: true },
180
+ { value: "wide", label: labels.widthWide, icon: "continuous", iconOnly: true },
181
+ { value: "full", label: labels.widthFull, icon: "page", iconOnly: true },
182
+ ]}
183
+ />
184
+ <Sep />
185
+ <Button variant="ghost" size="sm" onClick={() => edit("caption", labels.caption, inlineText(attrs.caption as Inline[] | null), (v) => set({ caption: v.trim() ? textToInline(v, attrs.caption as Inline[] | null) : null }))}>
186
+ {labels.caption}
187
+ </Button>
188
+ <Button variant="ghost" size="sm" onClick={() => edit("alt", labels.altText, String(attrs.alt ?? ""), (v) => set({ alt: v.trim() }))}>
189
+ {labels.altText}
190
+ </Button>
191
+ </>
192
+ )}
193
+ {services.uploadFile && (
194
+ <Button variant="ghost" size="sm" loading={busy} onClick={() => fileInput.current?.click()}>
195
+ {busy ? labels.uploading : (type === "image" ? attrs.src : attrs.fileId) ? labels.replace : labels.upload}
196
+ </Button>
197
+ )}
198
+ <input
199
+ ref={fileInput}
200
+ type="file"
201
+ hidden
202
+ /* What the server stores (FormUploadPolicy): PNG and JPEG images, and PDF for files. */
203
+ accept={type === "image" ? "image/png,image/jpeg" : "application/pdf,image/png,image/jpeg"}
204
+ onChange={(e) => {
205
+ const file = e.target.files?.[0]
206
+ e.target.value = ""
207
+ if (file) void upload(file)
208
+ }}
209
+ />
210
+ <Sep />
211
+ {del}
212
+ </>
213
+ )
214
+ } else if (type === "table") {
215
+ const sel = editor.state.selection
216
+ const cellPos = (() => {
217
+ const { $from } = sel
218
+ for (let d = $from.depth; d > 0; d--) if ($from.node(d).type.name === "tableCell") return { row: $from.index(d - 2), col: $from.index(d - 1) }
219
+ return null
220
+ })()
221
+ const columns = ((attrs.columns as { align: string; widthPct?: number }[]) ?? []).slice()
222
+ const width = Math.max(columns.length, ...Array.from({ length: node.childCount }, (_, i) => node.child(i).childCount))
223
+ const setRows = (mutate: (rows: Inline[][][]) => Inline[][][], nextColumns = columns) => {
224
+ // Rebuild through the contract so cells keep their content and marks.
225
+ const json = node.toJSON() as { content?: { content?: { content?: unknown[] }[] }[] }
226
+ const rows: Inline[][][] = (json.content ?? []).map((r) => (r.content ?? []).map((c) => (c.content ?? []) as Inline[]))
227
+ const next = mutate(rows)
228
+ const table = editor.schema.nodeFromJSON({
229
+ type: "table",
230
+ attrs: { ...node.attrs, columns: nextColumns },
231
+ content: next.map((r) => ({ type: "tableRow", content: r.map((c) => ({ type: "tableCell", ...(c.length ? { content: c } : {}) })) })),
232
+ })
233
+ const tr = editor.state.tr.replaceWith(target.pos, target.pos + node.nodeSize, table)
234
+ tr.setSelection(TextSelection.near(tr.doc.resolve(Math.min(target.pos + 3, tr.doc.content.size))))
235
+ editor.view.dispatch(tr)
236
+ }
237
+ const blankRow = () => Array.from({ length: width }, () => [] as Inline[])
238
+ controls = (
239
+ <>
240
+ <Button variant="ghost" size="sm" icon={attrs.headerRow ? "check" : undefined} active={Boolean(attrs.headerRow)} onClick={() => set({ headerRow: !attrs.headerRow })}>
241
+ {labels.headerRow}
242
+ </Button>
243
+ <Sep />
244
+ <Button variant="ghost" size="sm" icon="plus" onClick={() => setRows((rows) => { const r = rows.slice(); r.splice(cellPos ? cellPos.row + 1 : r.length, 0, blankRow()); return r })}>
245
+ {labels.addRow}
246
+ </Button>
247
+ <Button
248
+ variant="ghost"
249
+ size="sm"
250
+ icon="plus"
251
+ onClick={() => {
252
+ const at = cellPos ? cellPos.col + 1 : width
253
+ const nextColumns = Array.from({ length: width }, (_, i) => columns[i] ?? { align: "start" as const })
254
+ nextColumns.splice(at, 0, { align: "start" })
255
+ setRows((rows) => rows.map((r) => { const c = r.slice(); while (c.length < width) c.push([]); c.splice(at, 0, []); return c }), nextColumns)
256
+ }}
257
+ >
258
+ {labels.addColumn}
259
+ </Button>
260
+ {cellPos && (
261
+ <>
262
+ <Sep />
263
+ <ToggleGroup
264
+ size="sm"
265
+ label={labels.columnAlign}
266
+ required
267
+ value={columns[cellPos.col]?.align === "end" ? "end" : "start"}
268
+ onChange={(v) => {
269
+ const next = Array.from({ length: width }, (_, i) => columns[i] ?? { align: "start" as const })
270
+ next[cellPos.col] = { ...next[cellPos.col], align: v === "end" ? "end" : "start" }
271
+ set({ columns: next })
272
+ }}
273
+ options={[
274
+ { value: "start", label: labels.alignStart },
275
+ { value: "end", label: labels.alignEnd },
276
+ ]}
277
+ />
278
+ <IconButton icon="minus" label={labels.deleteRow} size="sm" disabled={node.childCount <= 1} onClick={() => setRows((rows) => rows.filter((_, i) => i !== cellPos.row))} />
279
+ <IconButton
280
+ icon="close"
281
+ label={labels.deleteColumn}
282
+ size="sm"
283
+ disabled={width <= 1}
284
+ onClick={() => setRows((rows) => rows.map((r) => r.filter((_, i) => i !== cellPos.col)), columns.filter((_, i) => i !== cellPos.col))}
285
+ />
286
+ </>
287
+ )}
288
+ <Sep />
289
+ {del}
290
+ </>
291
+ )
292
+ } else if (type === "record") {
293
+ const ref = attrs.ref as { type: string; id: string } | null
294
+ controls = (
295
+ <>
296
+ {services.recordTypes && services.recordTypes.length > 0 && (
297
+ <select
298
+ aria-label={labels.recordType}
299
+ value={ref?.type ?? ""}
300
+ onChange={(e) => set({ ref: e.target.value ? { type: e.target.value, id: ref?.id ?? "" } : null })}
301
+ className="h-7 rounded-control-sm border border-input bg-card px-1.5 text-[0.8125rem]"
302
+ >
303
+ <option value="">{labels.recordType}</option>
304
+ {services.recordTypes.map((t) => (
305
+ <option key={t.key} value={t.key}>
306
+ {t.label}
307
+ </option>
308
+ ))}
309
+ </select>
310
+ )}
311
+ <Button variant="ghost" size="sm" disabled={!ref?.type} onClick={() => edit("record", labels.recordId, ref?.id ?? "", (v) => set({ ref: { type: ref!.type, id: v.trim() } }), true)}>
312
+ {labels.recordChoose}
313
+ </Button>
314
+ <Sep />
315
+ <ToggleGroup
316
+ size="sm"
317
+ label={labels.blockRecord}
318
+ required
319
+ value={attrs.view === "summary" ? "summary" : "card"}
320
+ onChange={(v) => set({ view: v })}
321
+ options={[
322
+ { value: "card", label: labels.viewCard },
323
+ { value: "summary", label: labels.viewSummary },
324
+ ]}
325
+ />
326
+ <Sep />
327
+ {del}
328
+ </>
329
+ )
330
+ } else if (type === "chart") {
331
+ const source = attrs.source as { query: string; params?: Record<string, string> } | null
332
+ controls = (
333
+ <>
334
+ <ToggleGroup
335
+ size="sm"
336
+ label={labels.blockChart}
337
+ required
338
+ value={String(attrs.chart ?? "bar")}
339
+ onChange={(v) => set({ chart: v })}
340
+ options={[
341
+ { value: "bar", label: labels.chartBar },
342
+ { value: "line", label: labels.chartLine },
343
+ { value: "donut", label: labels.chartDonut },
344
+ ]}
345
+ />
346
+ {services.chartSources && services.chartSources.length > 0 && (
347
+ <>
348
+ <Sep />
349
+ <select
350
+ aria-label={labels.chartSource}
351
+ value={source?.query ?? ""}
352
+ onChange={(e) => set({ source: e.target.value ? { query: e.target.value } : null })}
353
+ className="h-7 rounded-control-sm border border-input bg-card px-1.5 text-[0.8125rem]"
354
+ >
355
+ <option value="">{labels.chartSource}</option>
356
+ {services.chartSources.map((s) => (
357
+ <option key={s.key} value={s.key}>
358
+ {s.label}
359
+ </option>
360
+ ))}
361
+ </select>
362
+ </>
363
+ )}
364
+ <Button variant="ghost" size="sm" onClick={() => edit("title", labels.chartTitle, String(attrs.title ?? ""), (v) => set({ title: v.trim() || null }))}>
365
+ {labels.chartTitle}
366
+ </Button>
367
+ <Sep />
368
+ {del}
369
+ </>
370
+ )
371
+ } else if (type === "signature") {
372
+ const signers = (attrs.signers as { name: Inline[]; role: Inline[] }[]) ?? []
373
+ controls = (
374
+ <>
375
+ {signers.map((s, i) => (
376
+ <Button
377
+ key={i}
378
+ variant="ghost"
379
+ size="sm"
380
+ onClick={() =>
381
+ edit(`signer-${i}`, fill(labels.signatureName, {}), `${inlineText(s.name)} | ${inlineText(s.role)}`, (v) => {
382
+ const [name, role = ""] = v.split("|").map((x) => x.trim())
383
+ const next = signers.slice()
384
+ next[i] = { name: textToInline(name, s.name), role: textToInline(role, s.role) }
385
+ set({ signers: next })
386
+ })
387
+ }
388
+ >
389
+ {inlineText(s.name) || labels.signatureName}
390
+ </Button>
391
+ ))}
392
+ <Button variant="ghost" size="sm" icon="plus" disabled={signers.length >= 3} onClick={() => set({ signers: [...signers, { name: [], role: [] }] })}>
393
+ {labels.addSigner}
394
+ </Button>
395
+ <Button variant="ghost" size="sm" onClick={() => edit("date", labels.signatureDate, inlineText(attrs.date as Inline[]), (v) => set({ date: textToInline(v, attrs.date as Inline[]) }))}>
396
+ {/* @date-display-ignore: the signature date is text the author typed or a merge field (Inline[]), not a record timestamp; this is its label. */}
397
+ {labels.signatureDate}
398
+ </Button>
399
+ <Button variant="ghost" size="sm" active={Boolean(attrs.stamp)} onClick={() => set({ stamp: !attrs.stamp })}>
400
+ {labels.stamp}
401
+ </Button>
402
+ <Sep />
403
+ {del}
404
+ </>
405
+ )
406
+ } else if (type === "barcode" || type === "qr") {
407
+ controls = (
408
+ <>
409
+ {type === "barcode" && (
410
+ <select
411
+ aria-label={labels.symbology}
412
+ value={String(attrs.symbology ?? "code128")}
413
+ onChange={(e) => set({ symbology: e.target.value })}
414
+ className="h-7 rounded-control-sm border border-input bg-card px-1.5 text-[0.8125rem]"
415
+ >
416
+ <option value="code128">Code 128</option>
417
+ <option value="ean13">EAN-13</option>
418
+ <option value="code39">Code 39</option>
419
+ </select>
420
+ )}
421
+ <Button variant="ghost" size="sm" onClick={() => edit("value", labels.value, inlineText(attrs.value as Inline[]), (v) => set({ value: textToInline(v, attrs.value as Inline[]) }), true)}>
422
+ {labels.value}
423
+ </Button>
424
+ {type === "barcode" ? (
425
+ <Button variant="ghost" size="sm" active={Boolean(attrs.showText)} onClick={() => set({ showText: !attrs.showText })}>
426
+ {labels.showText}
427
+ </Button>
428
+ ) : (
429
+ <Button variant="ghost" size="sm" onClick={() => edit("caption", labels.caption, inlineText(attrs.caption as Inline[] | null), (v) => set({ caption: v.trim() ? textToInline(v, attrs.caption as Inline[] | null) : null }))}>
430
+ {labels.caption}
431
+ </Button>
432
+ )}
433
+ <Sep />
434
+ {del}
435
+ </>
436
+ )
437
+ } else if (type === "math") {
438
+ controls = (
439
+ <>
440
+ <Button variant="ghost" size="sm" onClick={() => edit("tex", labels.formula, String(attrs.tex ?? ""), (v) => set({ tex: v }), true)}>
441
+ {labels.formula}
442
+ </Button>
443
+ <Sep />
444
+ {del}
445
+ </>
446
+ )
447
+ }
448
+ if (!controls) return null
449
+
450
+ return (
451
+ <div
452
+ role="toolbar"
453
+ aria-label={labels.blockToolbar}
454
+ data-slot="block-toolbar"
455
+ className={cn(
456
+ "absolute start-0 z-20 grid gap-1 rounded-control border border-border bg-popover p-[3px] shadow-float",
457
+ below ? "w-max max-w-[420px]" : "max-w-full"
458
+ )}
459
+ style={below ? { top } : { top, transform: "translateY(calc(-100% - 10px))" }}
460
+ onMouseDown={(e) => {
461
+ if ((e.target as HTMLElement).tagName !== "INPUT" && (e.target as HTMLElement).tagName !== "SELECT") e.preventDefault()
462
+ }}
463
+ >
464
+ <div className="flex flex-wrap items-center gap-0.5">{controls}</div>
465
+ {error && (
466
+ <div role="alert" className="px-1.5 pb-0.5 text-xs text-error-text">
467
+ {error}
468
+ </div>
469
+ )}
470
+ {editing && (
471
+ <FieldRow
472
+ key={editing.key}
473
+ label={editing.label}
474
+ value={editing.value}
475
+ mono={editing.mono}
476
+ onCancel={() => {
477
+ setEditing(null)
478
+ editor.commands.focus()
479
+ }}
480
+ onDone={(v) => {
481
+ editing.apply(v)
482
+ setEditing(null)
483
+ }}
484
+ />
485
+ )}
486
+ </div>
487
+ )
488
+ }
489
+
490
+ export { BlockToolbar }