@whity/ui 0.5.0 → 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 +107 -107
  2. package/package.json +221 -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 +123 -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 +398 -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 +1686 -1106
  54. package/src/date-picker.stories.tsx +61 -61
  55. package/src/date-picker.tsx +456 -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 +50 -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 +90 -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-tab-bar.tsx +152 -0
  151. package/src/plugin-card.stories.tsx +208 -180
  152. package/src/plugin-card.tsx +931 -848
  153. package/src/popover.stories.tsx +48 -48
  154. package/src/popover.tsx +55 -55
  155. package/src/radio-group.stories.tsx +26 -26
  156. package/src/radio-group.tsx +44 -44
  157. package/src/route-flow/editor.tsx +1087 -1014
  158. package/src/route-flow/model.ts +771 -716
  159. package/src/save-bar.tsx +94 -0
  160. package/src/scope-grant.tsx +139 -139
  161. package/src/screen-too-small.stories.tsx +61 -61
  162. package/src/screen-too-small.tsx +104 -104
  163. package/src/search-field.tsx +65 -0
  164. package/src/select.stories.tsx +44 -44
  165. package/src/select.tsx +197 -197
  166. package/src/separator.stories.tsx +34 -34
  167. package/src/separator.tsx +34 -34
  168. package/src/sheet.stories.tsx +49 -49
  169. package/src/sheet.tsx +168 -168
  170. package/src/skeleton.stories.tsx +28 -28
  171. package/src/skeleton.tsx +19 -19
  172. package/src/slider.stories.tsx +78 -78
  173. package/src/slider.tsx +300 -300
  174. package/src/spinner.stories.tsx +33 -33
  175. package/src/spinner.tsx +55 -55
  176. package/src/stepper.tsx +85 -0
  177. package/src/switch.stories.tsx +42 -42
  178. package/src/switch.tsx +41 -41
  179. package/src/switcher.stories.tsx +111 -111
  180. package/src/switcher.tsx +131 -131
  181. package/src/system-state.tsx +55 -0
  182. package/src/table.stories.tsx +48 -48
  183. package/src/table.tsx +121 -121
  184. package/src/tabs.stories.tsx +74 -74
  185. package/src/tabs.tsx +90 -90
  186. package/src/tag-input.stories.tsx +67 -67
  187. package/src/tag-input.tsx +117 -117
  188. package/src/tenant-logo.tsx +63 -63
  189. package/src/tenant-switcher.stories.tsx +38 -38
  190. package/src/tenant-switcher.tsx +164 -164
  191. package/src/textarea.stories.tsx +27 -27
  192. package/src/textarea.tsx +18 -18
  193. package/src/theme-mode.ts +52 -52
  194. package/src/time-grid.tsx +163 -0
  195. package/src/time-picker.stories.tsx +62 -62
  196. package/src/time-picker.tsx +615 -615
  197. package/src/toggle-group.tsx +138 -138
  198. package/src/tooltip.stories.tsx +43 -43
  199. package/src/tooltip.tsx +73 -73
  200. package/src/top-nav.stories.tsx +58 -58
  201. package/src/top-nav.tsx +324 -324
  202. package/src/undo-bar.tsx +107 -107
  203. package/src/utils.ts +45 -45
  204. package/src/view-toggle.stories.tsx +84 -84
  205. package/src/view-toggle.tsx +110 -110
@@ -1,450 +1,461 @@
1
- "use client"
2
-
3
- import * as React from "react"
4
- import { NodeViewContent, NodeViewWrapper, ReactNodeViewRenderer, type ReactNodeViewProps } from "@tiptap/react"
5
-
6
- import { Badge } from "../badge"
7
- import { Chart } from "../chart"
8
- import { BarcodeSvg } from "../documents/barcode-svg"
9
- import { Icon } from "../icon"
10
- import { MathText } from "../math-text"
11
- import { cn } from "../utils"
12
- import type { Inline } from "./contract"
13
- import { fill } from "./labels"
14
- import {
15
- BarcodeBlock,
16
- Callout,
17
- ChartBlock,
18
- FileBlock,
19
- Image,
20
- MathBlock,
21
- PageBreak,
22
- QrBlock,
23
- RecordBlock,
24
- SignatureBlock,
25
- Table,
26
- Toggle,
27
- } from "./schema-rich"
28
- import { Field, MathInline } from "./schema"
29
- import { formatSize, inlineText, useEditorServices, type ChartData, type RecordView } from "./services"
30
-
31
- /**
32
- * How the rich blocks look in the editor (EditorBlocks, bundle.css
33
- * `.wy-blk--*`). Atoms are read-only here; their block toolbar edits them
34
- * (block-toolbar.tsx). Containers (callout, toggle) keep their text editable.
35
- */
36
-
37
- const blockAttrs = (props: ReactNodeViewProps, type: string) => ({
38
- "data-type": type,
39
- "data-block-id": props.node.attrs.id ?? undefined,
40
- className: cn("wy-rich", props.selected && "is-selected"),
41
- })
42
-
43
- function CalloutView(props: ReactNodeViewProps) {
44
- const important = props.node.attrs.kind === "important"
45
- return (
46
- <NodeViewWrapper {...blockAttrs(props, "callout")} data-kind={important ? "important" : "note"}>
47
- <span contentEditable={false} className="wy-callout-icon">
48
- <Icon name={important ? "alert" : "info"} />
49
- </span>
50
- <NodeViewContent as="div" className="wy-callout-text" />
51
- </NodeViewWrapper>
52
- )
53
- }
54
-
55
- function ToggleView(props: ReactNodeViewProps) {
56
- const { labels } = useEditorServices()
57
- const open = Boolean(props.node.attrs.open)
58
- return (
59
- <NodeViewWrapper {...blockAttrs(props, "toggle")} data-open={open ? "true" : "false"}>
60
- <button
61
- type="button"
62
- contentEditable={false}
63
- className="wy-toggle-button"
64
- aria-expanded={open}
65
- aria-label={open ? labels.toggleClose : labels.toggleOpen}
66
- onMouseDown={(e) => e.preventDefault()}
67
- onClick={() => props.updateAttributes({ open: !open })}
68
- >
69
- <Icon name="chevron-right" />
70
- </button>
71
- <NodeViewContent as="div" className="wy-toggle-content" />
72
- </NodeViewWrapper>
73
- )
74
- }
75
-
76
- function PageBreakView(props: ReactNodeViewProps) {
77
- const { labels } = useEditorServices()
78
- return (
79
- <NodeViewWrapper {...blockAttrs(props, "pageBreak")}>
80
- <div className="wy-pagebreak">{labels.blockPageBreak}</div>
81
- </NodeViewWrapper>
82
- )
83
- }
84
-
85
- function ImageView(props: ReactNodeViewProps) {
86
- const { labels } = useEditorServices()
87
- const { src, alt, width, caption } = props.node.attrs as { src: string; alt: string; width: string; caption: Inline[] | null }
88
- return (
89
- <NodeViewWrapper {...blockAttrs(props, "image")} data-width={width}>
90
- <figure className="wy-figure">
91
- {src ? <img src={src} alt={alt} draggable={false} /> : <div className="wy-placeholder">{labels.imageEmpty}</div>}
92
- {caption && caption.length > 0 && <figcaption dir="auto">{inlineText(caption)}</figcaption>}
93
- </figure>
94
- </NodeViewWrapper>
95
- )
96
- }
97
-
98
- function FileView(props: ReactNodeViewProps) {
99
- const { services, labels } = useEditorServices()
100
- const { fileId, name, size, mime } = props.node.attrs as { fileId: string; name: string; size: number; mime: string }
101
- const kind = (mime.split("/")[1] ?? "").toUpperCase()
102
- const href = fileId && services.fileUrl ? services.fileUrl(fileId) : undefined
103
- return (
104
- <NodeViewWrapper {...blockAttrs(props, "file")}>
105
- <div className="wy-file">
106
- <span className="wy-tile" aria-hidden="true">
107
- <Icon name="document" />
108
- </span>
109
- <span className="wy-file-meta">
110
- <span className="font-medium" dir="auto">
111
- {name || labels.fileEmpty}
112
- </span>
113
- {fileId && <span className="text-xs text-muted-foreground">{[formatSize(size), kind].filter(Boolean).join(" · ")}</span>}
114
- </span>
115
- {href && (
116
- <a href={href} download={name} className="wy-file-download" aria-label={fill(labels.fileDownload, { name })} contentEditable={false}>
117
- <Icon name="download" />
118
- </a>
119
- )}
120
- </div>
121
- </NodeViewWrapper>
122
- )
123
- }
124
-
125
- /** Loads live data for a block, again whenever the block's source changes. */
126
- function useLive<T>(key: string, load: (() => Promise<T | null>) | null): { state: "loading" | "ready" | "hidden" | "error" | "none"; data: T | null } {
127
- const [result, setResult] = React.useState<{ key: string; state: "loading" | "ready" | "hidden" | "error" | "none"; data: T | null }>({ key: "", state: "loading", data: null })
128
- React.useEffect(() => {
129
- if (!load) {
130
- setResult({ key, state: "none", data: null })
131
- return
132
- }
133
- let live = true
134
- setResult({ key, state: "loading", data: null })
135
- load()
136
- .then((data) => live && setResult({ key, state: data === null ? "hidden" : "ready", data }))
137
- .catch(() => live && setResult({ key, state: "error", data: null }))
138
- return () => {
139
- live = false
140
- }
141
- // `load` is rebuilt every render; the key says when the source really changed.
142
- // eslint-disable-next-line react-hooks/exhaustive-deps
143
- }, [key])
144
- return result.key === key ? result : { state: "loading", data: null }
145
- }
146
-
147
- function RecordView_(props: ReactNodeViewProps) {
148
- const { services, labels } = useEditorServices()
149
- const ref = props.node.attrs.ref as { type: string; id: string } | null
150
- const key = ref ? `${ref.type}:${ref.id}` : ""
151
- const { state, data } = useLive<RecordView>(key, ref && services.resolveRecord ? () => services.resolveRecord!(ref) : null)
152
- const summary = props.node.attrs.view === "summary"
153
- return (
154
- <NodeViewWrapper {...blockAttrs(props, "record")}>
155
- <div className="wy-embed">
156
- <div className="wy-embed-head">
157
- <Icon name="invoice" size={14} />
158
- {data?.typeLabel ?? labels.blockRecord}
159
- <span className="flex-1" />
160
- {state === "ready" && (
161
- <>
162
- <Icon name="sync" size={12} />
163
- {labels.live}
164
- </>
165
- )}
166
- </div>
167
- {!ref ? (
168
- <div className="wy-placeholder">{labels.recordEmpty}</div>
169
- ) : state === "loading" ? (
170
- <div className="wy-skeleton" aria-busy="true" />
171
- ) : state === "hidden" || state === "none" ? (
172
- <div className="wy-placeholder">{state === "hidden" ? labels.recordHidden : labels.recordUnavailable}</div>
173
- ) : state === "error" ? (
174
- <div className="wy-placeholder">{labels.recordFailed}</div>
175
- ) : data ? (
176
- <div className="wy-embed-row">
177
- {!summary && (
178
- <span className="wy-tile is-tint" aria-hidden="true">
179
- <Icon name="invoice" />
180
- </span>
181
- )}
182
- <span className="wy-embed-main">
183
- <span className="font-semibold" dir="auto">
184
- {data.title}
185
- </span>
186
- {data.subtitle && <span className="text-[0.8125rem] text-muted-foreground">{data.subtitle}</span>}
187
- </span>
188
- {data.amount && <span className="text-base font-semibold tabular-nums" dir="ltr">{data.amount}</span>}
189
- {data.status && <Badge variant={data.status.tone}>{data.status.label}</Badge>}
190
- </div>
191
- ) : null}
192
- </div>
193
- </NodeViewWrapper>
194
- )
195
- }
196
-
197
- /** 4860 → "4.9K": the one labelled bar (BarChart card). Western digits in every language. */
198
- function compact(value: number): string {
199
- if (Math.abs(value) >= 1_000_000) return `${(value / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`
200
- if (Math.abs(value) >= 1_000) return `${(value / 1_000).toFixed(1).replace(/\.0$/, "")}K`
201
- return String(value)
202
- }
203
-
204
- function ChartView(props: ReactNodeViewProps) {
205
- const { services, labels } = useEditorServices()
206
- const source = props.node.attrs.source as { query: string; params?: Record<string, string> } | null
207
- const chart = (props.node.attrs.chart ?? "bar") as "bar" | "line" | "donut"
208
- const title = props.node.attrs.title as string | null
209
- const key = source ? JSON.stringify(source) : ""
210
- const { state, data } = useLive<ChartData>(key, source && services.loadChart ? () => services.loadChart!(source) : null)
211
- const [asTable, setAsTable] = React.useState(false)
212
- const rows = (data?.points ?? []).map((p) => ({ label: p.label, value: p.value }))
213
- const valueLabel = data?.valueLabel ?? title ?? labels.blockChart
214
- const highest = rows.reduce((best, r, i) => (r.value > (rows[best]?.value ?? -Infinity) ? i : best), 0)
215
- const ready = state === "ready" && rows.length > 0
216
- return (
217
- <NodeViewWrapper {...blockAttrs(props, "chart")}>
218
- <div className="wy-embed">
219
- {/* The BarChart card's header: the title, what it covers and that it is live, and a table view. */}
220
- <div className="wy-chart-head">
221
- <div className="grid min-w-0">
222
- <span className="wy-chart-title" dir="auto">
223
- {title || (source ? services.chartSources?.find((s) => s.key === source.query)?.label : null) || labels.blockChart}
224
- </span>
225
- {state === "ready" && (
226
- <span className="wy-chart-sub" dir="auto">
227
- {data?.subtitle ? fill(labels.chartLiveRange, { range: data.subtitle }) : labels.live}
228
- </span>
229
- )}
230
- </div>
231
- {ready && (
232
- <button
233
- type="button"
234
- className="wy-chart-toggle"
235
- contentEditable={false}
236
- aria-pressed={asTable}
237
- onMouseDown={(e) => e.preventDefault()}
238
- onClick={() => setAsTable((v) => !v)}
239
- >
240
- <Icon name={asTable ? "chart" : "table"} size={14} />
241
- {asTable ? labels.chartShowChart : labels.chartShowTable}
242
- </button>
243
- )}
244
- </div>
245
- {!source ? (
246
- <div className="wy-placeholder">{labels.chartEmpty}</div>
247
- ) : state === "loading" ? (
248
- <div className="wy-skeleton is-tall" aria-busy="true" />
249
- ) : ready && asTable ? (
250
- <table className="wy-chart-table">
251
- <thead>
252
- <tr>
253
- <th scope="col">{labels.chartCategory}</th>
254
- <th scope="col">{valueLabel}</th>
255
- </tr>
256
- </thead>
257
- <tbody>
258
- {rows.map((r) => (
259
- <tr key={r.label}>
260
- <td dir="auto">{r.label}</td>
261
- <td dir="ltr">{r.value.toLocaleString("en")}</td>
262
- </tr>
263
- ))}
264
- </tbody>
265
- </table>
266
- ) : ready ? (
267
- <Chart
268
- type={chart === "donut" ? "pie" : chart}
269
- data={rows}
270
- xKey="label"
271
- series={[{ key: "value", label: valueLabel, color: 1 }]}
272
- height={170}
273
- // One series needs no legend; a ring needs one to name its slices.
274
- legend={chart === "donut"}
275
- highlight={chart === "bar" ? highest : undefined}
276
- formatValue={compact}
277
- donut={chart === "donut"}
278
- />
279
- ) : (
280
- <div className="wy-placeholder">
281
- {state === "hidden" ? labels.chartHidden : state === "error" ? labels.chartFailed : state === "none" ? labels.chartUnavailable : labels.chartNoData}
282
- </div>
283
- )}
284
- </div>
285
- </NodeViewWrapper>
286
- )
287
- }
288
-
289
- function SignatureView(props: ReactNodeViewProps) {
290
- const { labels } = useEditorServices()
291
- const signers = (props.node.attrs.signers ?? []) as { name: Inline[]; role: Inline[] }[]
292
- const date = (props.node.attrs.date ?? []) as Inline[]
293
- return (
294
- <NodeViewWrapper {...blockAttrs(props, "signature")}>
295
- <div className="wy-sign">
296
- {signers.map((s, i) => (
297
- <div key={i} className="wy-sign-line">
298
- <b dir="auto">{inlineText(s.name) || labels.signatureName}</b>
299
- <span dir="auto">{inlineText(s.role)}</span>
300
- </div>
301
- ))}
302
- <div className="wy-sign-line">
303
- {/* @date-display-ignore: the signature date is text the author typed or a merge field (Inline[]), not a record timestamp; this is its label. */}
304
- <b>{labels.signatureDate}</b>
305
- <span dir="auto">{inlineText(date)}</span>
306
- </div>
307
- {props.node.attrs.stamp && <div className="wy-sign-stamp">{labels.signatureStamp}</div>}
308
- </div>
309
- </NodeViewWrapper>
310
- )
311
- }
312
-
313
- /** A barcode or QR code; a merge field can't be drawn until there's a value, so it shows what fills it. */
314
- function CodeView(props: ReactNodeViewProps & { qr: boolean }) {
315
- const { labels } = useEditorServices()
316
- const value = (props.node.attrs.value ?? []) as Inline[]
317
- const hasField = value.some((r) => r.type === "field")
318
- const text = inlineText(value)
319
- const symbology = props.qr ? "qrcode" : String(props.node.attrs.symbology ?? "code128")
320
- const caption = props.qr ? inlineText(props.node.attrs.caption as Inline[] | null) : ""
321
- return (
322
- <NodeViewWrapper {...blockAttrs(props, props.qr ? "qr" : "barcode")}>
323
- <figure className={cn("wy-code", props.qr ? "is-qr" : "is-bar")}>
324
- {!text ? (
325
- <div className="wy-placeholder">{props.qr ? labels.qrEmpty : labels.barcodeEmpty}</div>
326
- ) : hasField ? (
327
- <div className="wy-placeholder is-field" dir="auto">
328
- {fill(labels.codeFromField, { value: text })}
329
- </div>
330
- ) : (
331
- <div className="wy-code-art" dir="ltr">
332
- <BarcodeSvg symbology={symbology} value={text} showText={!props.qr && Boolean(props.node.attrs.showText)} />
333
- </div>
334
- )}
335
- {caption && <figcaption dir="auto">{caption}</figcaption>}
336
- </figure>
337
- </NodeViewWrapper>
338
- )
339
- }
340
-
341
- function MathView(props: ReactNodeViewProps) {
342
- const { labels } = useEditorServices()
343
- const tex = String(props.node.attrs.tex ?? "")
344
- return (
345
- <NodeViewWrapper {...blockAttrs(props, "math")}>
346
- <div className="wy-math-block" dir="ltr">
347
- {tex ? <MathText expression={tex} block /> : <div className="wy-placeholder">{labels.mathEmpty}</div>}
348
- </div>
349
- </NodeViewWrapper>
350
- )
351
- }
352
-
353
- /** An inline formula (E.42), rendered with KaTeX; its source is the tooltip. */
354
- function MathInlineView(props: ReactNodeViewProps) {
355
- const tex = String(props.node.attrs.tex ?? "")
356
- return (
357
- <NodeViewWrapper as="span" data-math="" dir="ltr" title={tex} className={cn("wy-math", props.selected && "is-selected")}>
358
- {tex ? <MathText expression={tex} /> : "…"}
359
- </NodeViewWrapper>
360
- )
361
- }
362
-
363
- /**
364
- * E.26: a merge field is a chip with its plain name, its path on hover. While
365
- * previewing with a record it shows the value with a dotted underline, or the
366
- * name on the error tint when the record has none (E.27).
367
- */
368
- function FieldView(props: ReactNodeViewProps) {
369
- const { labels, preview } = useEditorServices()
370
- const path = String(props.node.attrs.path ?? "")
371
- const label = String(props.node.attrs.label || path)
372
- if (preview) {
373
- const value = preview.values[path]
374
- if (value === null || value === undefined || value === "") {
375
- return (
376
- <NodeViewWrapper as="span" data-field={path} title={labels.fieldMissing} className="wy-mf is-missing">
377
- <Icon name="alert" size={12} />
378
- {label}
379
- </NodeViewWrapper>
380
- )
381
- }
382
- return (
383
- <NodeViewWrapper as="span" data-field={path} title={label} className="wy-mf is-preview">
384
- {value}
385
- </NodeViewWrapper>
386
- )
387
- }
388
- return (
389
- <NodeViewWrapper as="span" data-field={path} title={path} className={cn("wy-mf", props.selected && "is-selected")}>
390
- {label}
391
- </NodeViewWrapper>
392
- )
393
- }
394
-
395
- /** Table cells take their column's alignment (E.15: end-aligned numbers). */
396
- const TableWithAlign = Table.extend({
397
- addNodeView() {
398
- return ({ node: initial }) => {
399
- let node = initial
400
- const dom = document.createElement("table")
401
- const contentDOM = document.createElement("tbody")
402
- dom.append(contentDOM)
403
- const paint = () => {
404
- dom.dataset.type = "table"
405
- dom.dataset.headerRow = node.attrs.headerRow ? "true" : "false"
406
- if (node.attrs.id) dom.dataset.blockId = node.attrs.id
407
- const columns = (node.attrs.columns ?? []) as { align?: string }[]
408
- dom.style.setProperty("--wy-columns", String(columns.length))
409
- requestAnimationFrame(() => {
410
- contentDOM.querySelectorAll("tr").forEach((tr) =>
411
- Array.from(tr.children).forEach((cell, i) => {
412
- ;(cell as HTMLElement).dataset.align = columns[i]?.align === "end" ? "end" : "start"
413
- })
414
- )
415
- })
416
- }
417
- paint()
418
- return {
419
- dom,
420
- contentDOM,
421
- update(next) {
422
- if (next.type !== node.type) return false
423
- node = next
424
- paint()
425
- return true
426
- },
427
- }
428
- }
429
- },
430
- })
431
-
432
- const react = (component: React.ComponentType<ReactNodeViewProps>) => () => ReactNodeViewRenderer(component)
433
-
434
- /** The M3 node types with their editor views. */
435
- export const richNodeViews = [
436
- Callout.extend({ addNodeView: react(CalloutView) }),
437
- Toggle.extend({ addNodeView: react(ToggleView) }),
438
- TableWithAlign,
439
- PageBreak.extend({ addNodeView: react(PageBreakView) }),
440
- Image.extend({ addNodeView: react(ImageView) }),
441
- FileBlock.extend({ addNodeView: react(FileView) }),
442
- RecordBlock.extend({ addNodeView: react(RecordView_) }),
443
- ChartBlock.extend({ addNodeView: react(ChartView) }),
444
- SignatureBlock.extend({ addNodeView: react(SignatureView) }),
445
- BarcodeBlock.extend({ addNodeView: react((p) => <CodeView {...p} qr={false} />) }),
446
- QrBlock.extend({ addNodeView: react((p) => <CodeView {...p} qr />) }),
447
- MathBlock.extend({ addNodeView: react(MathView) }),
448
- MathInline.extend({ addNodeView: react(MathInlineView) }),
449
- Field.extend({ addNodeView: react(FieldView) }),
450
- ]
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import { NodeViewContent, NodeViewWrapper, ReactNodeViewRenderer, type ReactNodeViewProps } from "@tiptap/react"
5
+
6
+ import { Badge } from "../badge"
7
+ import { Chart } from "../chart"
8
+ import { BarcodeSvg } from "../documents/barcode-svg"
9
+ import { Icon } from "../icon"
10
+ import { MathText } from "../math-text"
11
+ import { cn } from "../utils"
12
+ import type { Inline } from "./contract"
13
+ import { fill } from "./labels"
14
+ import {
15
+ BarcodeBlock,
16
+ Callout,
17
+ ChartBlock,
18
+ FileBlock,
19
+ Image,
20
+ MathBlock,
21
+ PageBreak,
22
+ QrBlock,
23
+ RecordBlock,
24
+ PluginBlockNode,
25
+ SavedBlockNode,
26
+ SignatureBlock,
27
+ Table,
28
+ Toggle,
29
+ } from "./schema-rich"
30
+ import { PluginBlockView } from "./plugin-block-view"
31
+ import { SavedBlockView } from "./saved-block-view"
32
+ import { Field, MathInline } from "./schema"
33
+ import { formatSize, inlineText, useEditorServices, type ChartData, type RecordView } from "./services"
34
+
35
+ /**
36
+ * How the rich blocks look in the editor (EditorBlocks, bundle.css
37
+ * `.wy-blk--*`). Atoms are read-only here; their block toolbar edits them
38
+ * (block-toolbar.tsx). Containers (callout, toggle) keep their text editable.
39
+ */
40
+
41
+ const blockAttrs = (props: ReactNodeViewProps, type: string) => ({
42
+ "data-type": type,
43
+ "data-block-id": props.node.attrs.id ?? undefined,
44
+ className: cn("wy-rich", props.selected && "is-selected"),
45
+ })
46
+
47
+ function CalloutView(props: ReactNodeViewProps) {
48
+ const important = props.node.attrs.kind === "important"
49
+ return (
50
+ <NodeViewWrapper {...blockAttrs(props, "callout")} data-kind={important ? "important" : "note"}>
51
+ <span contentEditable={false} className="wy-callout-icon">
52
+ <Icon name={important ? "alert" : "info"} />
53
+ </span>
54
+ <NodeViewContent as="div" className="wy-callout-text" />
55
+ </NodeViewWrapper>
56
+ )
57
+ }
58
+
59
+ function ToggleView(props: ReactNodeViewProps) {
60
+ const { labels } = useEditorServices()
61
+ const open = Boolean(props.node.attrs.open)
62
+ return (
63
+ <NodeViewWrapper {...blockAttrs(props, "toggle")} data-open={open ? "true" : "false"}>
64
+ <button
65
+ type="button"
66
+ contentEditable={false}
67
+ className="wy-toggle-button"
68
+ aria-expanded={open}
69
+ aria-label={open ? labels.toggleClose : labels.toggleOpen}
70
+ onMouseDown={(e) => e.preventDefault()}
71
+ onClick={() => props.updateAttributes({ open: !open })}
72
+ >
73
+ <Icon name="chevron-right" />
74
+ </button>
75
+ <NodeViewContent as="div" className="wy-toggle-content" />
76
+ </NodeViewWrapper>
77
+ )
78
+ }
79
+
80
+ function PageBreakView(props: ReactNodeViewProps) {
81
+ const { labels } = useEditorServices()
82
+ return (
83
+ <NodeViewWrapper {...blockAttrs(props, "pageBreak")}>
84
+ <div className="wy-pagebreak">{labels.blockPageBreak}</div>
85
+ </NodeViewWrapper>
86
+ )
87
+ }
88
+
89
+ function ImageView(props: ReactNodeViewProps) {
90
+ const { labels } = useEditorServices()
91
+ const { src, alt, width, caption } = props.node.attrs as { src: string; alt: string; width: string; caption: Inline[] | null }
92
+ return (
93
+ <NodeViewWrapper {...blockAttrs(props, "image")} data-width={width}>
94
+ <figure className="wy-figure">
95
+ {src ? <img src={src} alt={alt} draggable={false} /> : <div className="wy-placeholder">{labels.imageEmpty}</div>}
96
+ {caption && caption.length > 0 && <figcaption dir="auto">{inlineText(caption)}</figcaption>}
97
+ </figure>
98
+ </NodeViewWrapper>
99
+ )
100
+ }
101
+
102
+ function FileView(props: ReactNodeViewProps) {
103
+ const { services, labels } = useEditorServices()
104
+ const { fileId, name, size, mime } = props.node.attrs as { fileId: string; name: string; size: number; mime: string }
105
+ const kind = (mime.split("/")[1] ?? "").toUpperCase()
106
+ const href = fileId && services.fileUrl ? services.fileUrl(fileId) : undefined
107
+ return (
108
+ <NodeViewWrapper {...blockAttrs(props, "file")}>
109
+ <div className="wy-file">
110
+ <span className="wy-tile" aria-hidden="true">
111
+ <Icon name="document" />
112
+ </span>
113
+ <span className="wy-file-meta">
114
+ <span className="font-medium" dir="auto">
115
+ {name || labels.fileEmpty}
116
+ </span>
117
+ {fileId && <span className="text-xs text-muted-foreground">{[formatSize(size), kind].filter(Boolean).join(" · ")}</span>}
118
+ </span>
119
+ {href && (
120
+ <a href={href} download={name} className="wy-file-download" aria-label={fill(labels.fileDownload, { name })} contentEditable={false}>
121
+ <Icon name="download" />
122
+ </a>
123
+ )}
124
+ </div>
125
+ </NodeViewWrapper>
126
+ )
127
+ }
128
+
129
+ /** Loads live data for a block, again whenever the block's source changes. */
130
+ function useLive<T>(key: string, load: (() => Promise<T | null>) | null): { state: "loading" | "ready" | "hidden" | "error" | "none"; data: T | null } {
131
+ const [result, setResult] = React.useState<{ key: string; state: "loading" | "ready" | "hidden" | "error" | "none"; data: T | null }>({ key: "", state: "loading", data: null })
132
+ React.useEffect(() => {
133
+ if (!load) {
134
+ setResult({ key, state: "none", data: null })
135
+ return
136
+ }
137
+ let live = true
138
+ setResult({ key, state: "loading", data: null })
139
+ load()
140
+ .then((data) => live && setResult({ key, state: data === null ? "hidden" : "ready", data }))
141
+ .catch(() => live && setResult({ key, state: "error", data: null }))
142
+ return () => {
143
+ live = false
144
+ }
145
+ // `load` is rebuilt every render; the key says when the source really changed.
146
+ // eslint-disable-next-line react-hooks/exhaustive-deps
147
+ }, [key])
148
+ return result.key === key ? result : { state: "loading", data: null }
149
+ }
150
+
151
+ function RecordView_(props: ReactNodeViewProps) {
152
+ const { services, labels } = useEditorServices()
153
+ const ref = props.node.attrs.ref as { type: string; id: string } | null
154
+ const key = ref ? `${ref.type}:${ref.id}` : ""
155
+ const { state, data } = useLive<RecordView>(key, ref && services.resolveRecord ? () => services.resolveRecord!(ref) : null)
156
+ const summary = props.node.attrs.view === "summary"
157
+ return (
158
+ <NodeViewWrapper {...blockAttrs(props, "record")}>
159
+ <div className="wy-embed">
160
+ <div className="wy-embed-head">
161
+ <Icon name="invoice" size={14} />
162
+ {data?.typeLabel ?? labels.blockRecord}
163
+ <span className="flex-1" />
164
+ {state === "ready" && (
165
+ <>
166
+ <Icon name="sync" size={12} />
167
+ {labels.live}
168
+ </>
169
+ )}
170
+ </div>
171
+ {!ref ? (
172
+ <div className="wy-placeholder">{labels.recordEmpty}</div>
173
+ ) : state === "loading" ? (
174
+ <div className="wy-skeleton" aria-busy="true" />
175
+ ) : state === "hidden" || state === "none" ? (
176
+ <div className="wy-placeholder">{state === "hidden" ? labels.recordHidden : labels.recordUnavailable}</div>
177
+ ) : state === "error" ? (
178
+ <div className="wy-placeholder">{labels.recordFailed}</div>
179
+ ) : data ? (
180
+ <div className="wy-embed-row">
181
+ {!summary && (
182
+ <span className="wy-tile is-tint" aria-hidden="true">
183
+ <Icon name="invoice" />
184
+ </span>
185
+ )}
186
+ <span className="wy-embed-main">
187
+ <span className="font-semibold" dir="auto">
188
+ {data.title}
189
+ </span>
190
+ {data.subtitle && <span className="text-[0.8125rem] text-muted-foreground">{data.subtitle}</span>}
191
+ </span>
192
+ {data.amount && <span className="text-base font-semibold tabular-nums" dir="ltr">{data.amount}</span>}
193
+ {data.status && <Badge variant={data.status.tone}>{data.status.label}</Badge>}
194
+ </div>
195
+ ) : null}
196
+ </div>
197
+ </NodeViewWrapper>
198
+ )
199
+ }
200
+
201
+ /** 4860 → "4.9K": the one labelled bar (BarChart card). Western digits in every language. */
202
+ function compact(value: number): string {
203
+ if (Math.abs(value) >= 1_000_000) return `${(value / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`
204
+ if (Math.abs(value) >= 1_000) return `${(value / 1_000).toFixed(1).replace(/\.0$/, "")}K`
205
+ return String(value)
206
+ }
207
+
208
+ function ChartView(props: ReactNodeViewProps) {
209
+ const { services, labels } = useEditorServices()
210
+ const source = props.node.attrs.source as { query: string; params?: Record<string, string> } | null
211
+ const chart = (props.node.attrs.chart ?? "bar") as "bar" | "line" | "donut"
212
+ const title = props.node.attrs.title as string | null
213
+ const key = source ? JSON.stringify(source) : ""
214
+ const { state, data } = useLive<ChartData>(key, source && services.loadChart ? () => services.loadChart!(source) : null)
215
+ const [asTable, setAsTable] = React.useState(false)
216
+ const rows = (data?.points ?? []).map((p) => ({ label: p.label, value: p.value }))
217
+ const valueLabel = data?.valueLabel ?? title ?? labels.blockChart
218
+ const highest = rows.reduce((best, r, i) => (r.value > (rows[best]?.value ?? -Infinity) ? i : best), 0)
219
+ const ready = state === "ready" && rows.length > 0
220
+ return (
221
+ <NodeViewWrapper {...blockAttrs(props, "chart")}>
222
+ <div className="wy-embed">
223
+ {/* The BarChart card's header: the title, what it covers and that it is live, and a table view. */}
224
+ <div className="wy-chart-head">
225
+ <div className="grid min-w-0">
226
+ <span className="wy-chart-title" dir="auto">
227
+ {title || (source ? services.chartSources?.find((s) => s.key === source.query)?.label : null) || labels.blockChart}
228
+ </span>
229
+ {state === "ready" && (
230
+ <span className="wy-chart-sub" dir="auto">
231
+ {data?.subtitle ? fill(labels.chartLiveRange, { range: data.subtitle }) : labels.live}
232
+ </span>
233
+ )}
234
+ </div>
235
+ {ready && (
236
+ <button
237
+ type="button"
238
+ className="wy-chart-toggle"
239
+ contentEditable={false}
240
+ aria-pressed={asTable}
241
+ onMouseDown={(e) => e.preventDefault()}
242
+ onClick={() => setAsTable((v) => !v)}
243
+ >
244
+ <Icon name={asTable ? "chart" : "table"} size={14} />
245
+ {asTable ? labels.chartShowChart : labels.chartShowTable}
246
+ </button>
247
+ )}
248
+ </div>
249
+ {!source ? (
250
+ <div className="wy-placeholder">{labels.chartEmpty}</div>
251
+ ) : state === "loading" ? (
252
+ <div className="wy-skeleton is-tall" aria-busy="true" />
253
+ ) : ready && asTable ? (
254
+ <table className="wy-chart-table">
255
+ <thead>
256
+ <tr>
257
+ <th scope="col">{labels.chartCategory}</th>
258
+ <th scope="col">{valueLabel}</th>
259
+ </tr>
260
+ </thead>
261
+ <tbody>
262
+ {rows.map((r) => (
263
+ <tr key={r.label}>
264
+ <td dir="auto">{r.label}</td>
265
+ <td dir="ltr">{r.value.toLocaleString("en")}</td>
266
+ </tr>
267
+ ))}
268
+ </tbody>
269
+ </table>
270
+ ) : ready ? (
271
+ <Chart
272
+ type={chart === "donut" ? "pie" : chart}
273
+ data={rows}
274
+ xKey="label"
275
+ series={[{ key: "value", label: valueLabel, color: 1 }]}
276
+ height={170}
277
+ // One series needs no legend; a ring needs one to name its slices.
278
+ legend={chart === "donut"}
279
+ highlight={chart === "bar" ? highest : undefined}
280
+ formatValue={compact}
281
+ donut={chart === "donut"}
282
+ />
283
+ ) : (
284
+ <div className="wy-placeholder">
285
+ {state === "hidden" ? labels.chartHidden : state === "error" ? labels.chartFailed : state === "none" ? labels.chartUnavailable : labels.chartNoData}
286
+ </div>
287
+ )}
288
+ </div>
289
+ </NodeViewWrapper>
290
+ )
291
+ }
292
+
293
+ function SignatureView(props: ReactNodeViewProps) {
294
+ const { labels } = useEditorServices()
295
+ const signers = (props.node.attrs.signers ?? []) as { name: Inline[]; role: Inline[] }[]
296
+ const date = (props.node.attrs.date ?? []) as Inline[]
297
+ return (
298
+ <NodeViewWrapper {...blockAttrs(props, "signature")}>
299
+ <div className="wy-sign">
300
+ {signers.map((s, i) => (
301
+ <div key={i} className="wy-sign-line">
302
+ <b dir="auto">{inlineText(s.name) || labels.signatureName}</b>
303
+ <span dir="auto">{inlineText(s.role)}</span>
304
+ </div>
305
+ ))}
306
+ <div className="wy-sign-line">
307
+ {/* @date-display-ignore: the signature date is text the author typed or a merge field (Inline[]), not a record timestamp; this is its label. */}
308
+ <b>{labels.signatureDate}</b>
309
+ <span dir="auto">{inlineText(date)}</span>
310
+ </div>
311
+ {props.node.attrs.stamp && <div className="wy-sign-stamp">{labels.signatureStamp}</div>}
312
+ </div>
313
+ </NodeViewWrapper>
314
+ )
315
+ }
316
+
317
+ /** A barcode or QR code; a merge field can't be drawn until there's a value, so it shows what fills it. */
318
+ function CodeView(props: ReactNodeViewProps & { qr: boolean }) {
319
+ const { labels } = useEditorServices()
320
+ const value = (props.node.attrs.value ?? []) as Inline[]
321
+ const hasField = value.some((r) => r.type === "field")
322
+ const text = inlineText(value)
323
+ const symbology = props.qr ? "qrcode" : String(props.node.attrs.symbology ?? "code128")
324
+ const caption = props.qr ? inlineText(props.node.attrs.caption as Inline[] | null) : ""
325
+ return (
326
+ <NodeViewWrapper {...blockAttrs(props, props.qr ? "qr" : "barcode")}>
327
+ <figure className={cn("wy-code", props.qr ? "is-qr" : "is-bar")}>
328
+ {!text ? (
329
+ <div className="wy-placeholder">{props.qr ? labels.qrEmpty : labels.barcodeEmpty}</div>
330
+ ) : hasField ? (
331
+ <div className="wy-placeholder is-field" dir="auto">
332
+ {fill(labels.codeFromField, { value: text })}
333
+ </div>
334
+ ) : (
335
+ <div className="wy-code-art" dir="ltr">
336
+ <BarcodeSvg symbology={symbology} value={text} showText={!props.qr && Boolean(props.node.attrs.showText)} />
337
+ </div>
338
+ )}
339
+ {caption && <figcaption dir="auto">{caption}</figcaption>}
340
+ </figure>
341
+ </NodeViewWrapper>
342
+ )
343
+ }
344
+
345
+ function MathView(props: ReactNodeViewProps) {
346
+ const { labels } = useEditorServices()
347
+ const tex = String(props.node.attrs.tex ?? "")
348
+ return (
349
+ <NodeViewWrapper {...blockAttrs(props, "math")}>
350
+ <div className="wy-math-block" dir="ltr">
351
+ {tex ? <MathText expression={tex} block /> : <div className="wy-placeholder">{labels.mathEmpty}</div>}
352
+ </div>
353
+ </NodeViewWrapper>
354
+ )
355
+ }
356
+
357
+ /** An inline formula (E.42), rendered with KaTeX; its source is the tooltip. */
358
+ function MathInlineView(props: ReactNodeViewProps) {
359
+ const tex = String(props.node.attrs.tex ?? "")
360
+ return (
361
+ <NodeViewWrapper as="span" data-math="" dir="ltr" title={tex} className={cn("wy-math", props.selected && "is-selected")}>
362
+ {tex ? <MathText expression={tex} /> : "…"}
363
+ </NodeViewWrapper>
364
+ )
365
+ }
366
+
367
+ /**
368
+ * E.26: a merge field is a chip with its plain name, its path on hover. While
369
+ * previewing with a record it shows the value with a dotted underline, or the
370
+ * name on the error tint when the record has none (E.27).
371
+ */
372
+ function FieldView(props: ReactNodeViewProps) {
373
+ const { labels, preview } = useEditorServices()
374
+ const path = String(props.node.attrs.path ?? "")
375
+ const label = String(props.node.attrs.label || path)
376
+ if (preview) {
377
+ const value = preview.values[path]
378
+ if (value === null || value === undefined || value === "") {
379
+ return (
380
+ <NodeViewWrapper as="span" data-field={path} title={labels.fieldMissing} className="wy-mf is-missing">
381
+ <Icon name="alert" size={12} />
382
+ {label}
383
+ </NodeViewWrapper>
384
+ )
385
+ }
386
+ return (
387
+ <NodeViewWrapper as="span" data-field={path} title={label} className="wy-mf is-preview">
388
+ {value}
389
+ </NodeViewWrapper>
390
+ )
391
+ }
392
+ return (
393
+ <NodeViewWrapper as="span" data-field={path} title={path} className={cn("wy-mf", props.selected && "is-selected")}>
394
+ {label}
395
+ </NodeViewWrapper>
396
+ )
397
+ }
398
+
399
+ /** Table cells take their column's alignment (E.15: end-aligned numbers). */
400
+ const TableWithAlign = Table.extend({
401
+ addNodeView() {
402
+ return ({ node: initial }) => {
403
+ let node = initial
404
+ const dom = document.createElement("table")
405
+ const contentDOM = document.createElement("tbody")
406
+ dom.append(contentDOM)
407
+ const paint = () => {
408
+ dom.dataset.type = "table"
409
+ dom.dataset.headerRow = node.attrs.headerRow ? "true" : "false"
410
+ if (node.attrs.id) dom.dataset.blockId = node.attrs.id
411
+ const columns = (node.attrs.columns ?? []) as { align?: string }[]
412
+ dom.style.setProperty("--wy-columns", String(columns.length))
413
+ // End-aligned columns are named on the table itself ("2 4") and the
414
+ // stylesheet aligns those cells. Writing data-align onto each cell
415
+ // after ProseMirror drew it read as an edit: ProseMirror redrew the
416
+ // cells, which were written again, thousands of times a second, and a
417
+ // click on a field chip in a table landed on a node already gone.
418
+ const ends = columns.flatMap((c, i) => (c?.align === "end" ? [String(i + 1)] : [])).join(" ")
419
+ if (ends) dom.dataset.endColumns = ends
420
+ else delete dom.dataset.endColumns
421
+ }
422
+ paint()
423
+ return {
424
+ dom,
425
+ contentDOM,
426
+ update(next) {
427
+ if (next.type !== node.type) return false
428
+ node = next
429
+ paint()
430
+ return true
431
+ },
432
+ // Its own attributes are painted here, not edits to read back.
433
+ ignoreMutation(mutation) {
434
+ return mutation.type === "attributes" && mutation.target === dom
435
+ },
436
+ }
437
+ }
438
+ },
439
+ })
440
+
441
+ const react = (component: React.ComponentType<ReactNodeViewProps>) => () => ReactNodeViewRenderer(component)
442
+
443
+ /** The M3 node types with their editor views. */
444
+ export const richNodeViews = [
445
+ Callout.extend({ addNodeView: react(CalloutView) }),
446
+ Toggle.extend({ addNodeView: react(ToggleView) }),
447
+ TableWithAlign,
448
+ PageBreak.extend({ addNodeView: react(PageBreakView) }),
449
+ Image.extend({ addNodeView: react(ImageView) }),
450
+ FileBlock.extend({ addNodeView: react(FileView) }),
451
+ RecordBlock.extend({ addNodeView: react(RecordView_) }),
452
+ ChartBlock.extend({ addNodeView: react(ChartView) }),
453
+ SignatureBlock.extend({ addNodeView: react(SignatureView) }),
454
+ BarcodeBlock.extend({ addNodeView: react((p) => <CodeView {...p} qr={false} />) }),
455
+ QrBlock.extend({ addNodeView: react((p) => <CodeView {...p} qr />) }),
456
+ MathBlock.extend({ addNodeView: react(MathView) }),
457
+ MathInline.extend({ addNodeView: react(MathInlineView) }),
458
+ SavedBlockNode.extend({ addNodeView: react(SavedBlockView) }),
459
+ PluginBlockNode.extend({ addNodeView: react(PluginBlockView) }),
460
+ Field.extend({ addNodeView: react(FieldView) }),
461
+ ]