@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,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
+ // 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 }
@@ -0,0 +1,119 @@
1
+ "use client"
2
+
3
+ import type { Editor } from "@tiptap/core"
4
+ import { isTextSelection } from "@tiptap/core"
5
+ import { useEditorState } from "@tiptap/react"
6
+ import { BubbleMenu } from "@tiptap/react/menus"
7
+
8
+ import type { IconName } from "../icon"
9
+ import { IconButton } from "../icon-button"
10
+ import type { EditorLabels } from "./labels"
11
+ import { mintPartKey } from "./saved-parts"
12
+
13
+ /**
14
+ * The selection bubble (EditorInline): after a short pause above a text
15
+ * selection, bold, italic, underline, strikethrough, code · link, highlight.
16
+ * · Comment (M5) · Rewrite with an agent (M6, EditorAgents): the agent's
17
+ * version arrives as a suggestion. Without edit rights (Viewing) only Comment
18
+ * and Rewrite show.
19
+ */
20
+ function SelectionBubble({
21
+ editor,
22
+ labels,
23
+ onLink,
24
+ onComment,
25
+ onRewrite,
26
+ perDocument,
27
+ }: {
28
+ editor: Editor
29
+ labels: EditorLabels
30
+ onLink: () => void
31
+ /** M5: Comment on the selection; with it the bubble shows even where nothing can be edited. */
32
+ onComment?: () => void
33
+ /** M6: ask an agent to rewrite the selection; its version comes back as a suggestion. */
34
+ onRewrite?: () => void
35
+ /** M7, E.23: editing a saved block's original, mark text as filled per document. */
36
+ perDocument?: boolean
37
+ }) {
38
+ const s = useEditorState({
39
+ editor,
40
+ selector: ({ editor: e }) => ({
41
+ bold: e.isActive("bold"),
42
+ italic: e.isActive("italic"),
43
+ underline: e.isActive("underline"),
44
+ strike: e.isActive("strike"),
45
+ code: e.isActive("code"),
46
+ link: e.isActive("link"),
47
+ highlight: e.isActive("highlight"),
48
+ perDocument: e.isActive("perDocument"),
49
+ editable: e.isEditable,
50
+ }),
51
+ })
52
+ const b = (icon: IconName, label: string, on: boolean, onClick: () => void, shortcut?: string) => (
53
+ <IconButton
54
+ icon={icon}
55
+ label={label}
56
+ size="sm"
57
+ shortcut={shortcut}
58
+ pressed={on}
59
+ onMouseDown={(e) => e.preventDefault()}
60
+ onClick={onClick}
61
+ />
62
+ )
63
+ const mark = (name: string) => () => editor.chain().focus().toggleMark(name).run()
64
+ // E.23a: marking mints the part's key once; unmarking deletes it, and marking again mints a new one.
65
+ const togglePart = () =>
66
+ s.perDocument
67
+ ? editor.chain().focus().unsetMark("perDocument").run()
68
+ : editor.chain().focus().setMark("perDocument", { key: mintPartKey() }).run()
69
+ return (
70
+ <BubbleMenu
71
+ editor={editor}
72
+ updateDelay={250}
73
+ shouldShow={({ editor: e, state, from, to }) =>
74
+ (e.isEditable || !!onComment || !!onRewrite) && isTextSelection(state.selection) && from !== to && !e.isActive("codeBlock")
75
+ }
76
+ options={{ placement: "top-start", offset: 8 }}
77
+ >
78
+ <div
79
+ role="toolbar"
80
+ aria-label={labels.selection}
81
+ data-slot="selection-bubble"
82
+ className="flex items-center gap-0.5 rounded-control border border-border bg-popover p-[3px] whitespace-nowrap shadow-float"
83
+ >
84
+ {s.editable && (
85
+ <>
86
+ {b("bold", labels.bold, s.bold, mark("bold"), "⌘B")}
87
+ {b("italic", labels.italic, s.italic, mark("italic"), "⌘I")}
88
+ {b("underline", labels.underline, s.underline, mark("underline"), "⌘U")}
89
+ {b("strike", labels.strike, s.strike, mark("strike"), "⇧⌘X")}
90
+ {b("code", labels.code, s.code, mark("code"), "⌘E")}
91
+ <span aria-hidden="true" className="mx-[3px] h-[18px] w-px bg-border" />
92
+ {b("link", labels.link, s.link, onLink, "⌘K")}
93
+ {b("highlight", labels.highlight, s.highlight, mark("highlight"))}
94
+ {perDocument && (
95
+ <>
96
+ <span aria-hidden="true" className="mx-[3px] h-[18px] w-px bg-border" />
97
+ {b("field", labels.perDocumentMark, s.perDocument, togglePart)}
98
+ </>
99
+ )}
100
+ </>
101
+ )}
102
+ {onComment && (
103
+ <>
104
+ {s.editable && <span aria-hidden="true" className="mx-[3px] h-[18px] w-px bg-border" />}
105
+ {b("comment-add", labels.comment, false, onComment, "⌥⌘M")}
106
+ </>
107
+ )}
108
+ {onRewrite && (
109
+ <>
110
+ {(s.editable || onComment) && <span aria-hidden="true" className="mx-[3px] h-[18px] w-px bg-border" />}
111
+ {b("robot", labels.agentRewrite, false, onRewrite)}
112
+ </>
113
+ )}
114
+ </div>
115
+ </BubbleMenu>
116
+ )
117
+ }
118
+
119
+ export { SelectionBubble }