@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,362 +1,414 @@
1
- import type { JSONContent } from "@tiptap/core"
2
-
3
- import type { Block, Inline, Mark as DocMark, Text } from "./contract"
4
- import { MARK_ORDER } from "./schema"
5
-
6
- /**
7
- * WhityDocument blocks ⇄ ProseMirror JSON. `toEditorContent` and
8
- * `fromEditorContent` are inverses on normalised documents
9
- * (`normalizeBlocks`): adjacent text runs with the same marks are one run,
10
- * marks are in schema order, and empty runs are dropped. Blocks the editor
11
- * doesn't edit yet travel through `unknownBlock` unchanged.
12
- */
13
-
14
- type PmMark = { type: string; attrs?: Record<string, unknown> }
15
-
16
- const TEXT_BLOCKS = new Set(["paragraph", "heading", "quote", "bullet", "number", "checklist", "code", "divider", "callout"])
17
-
18
- /* ------------------------------------------------------------- inline */
19
-
20
- function markToPm(mark: DocMark): PmMark {
21
- switch (mark.type) {
22
- case "link":
23
- return { type: "link", attrs: { href: mark.href, recordRef: mark.recordRef ?? null } }
24
- case "comment":
25
- return { type: "comment", attrs: { threadId: mark.threadId } }
26
- case "insertion":
27
- case "deletion":
28
- return { type: mark.type, attrs: { suggestionId: mark.suggestionId } }
29
- default:
30
- return { type: mark.type }
31
- }
32
- }
33
-
34
- function markFromPm(mark: PmMark): DocMark {
35
- const a = mark.attrs ?? {}
36
- switch (mark.type) {
37
- case "link":
38
- return a.recordRef
39
- ? { type: "link", href: String(a.href ?? ""), recordRef: a.recordRef as { type: string; id: string } }
40
- : { type: "link", href: String(a.href ?? "") }
41
- case "comment":
42
- return { type: "comment", threadId: String(a.threadId ?? "") }
43
- case "insertion":
44
- case "deletion":
45
- return { type: mark.type, suggestionId: String(a.suggestionId ?? "") }
46
- default:
47
- return { type: mark.type } as DocMark
48
- }
49
- }
50
-
51
- const markRank = (m: DocMark) => MARK_ORDER.indexOf(m.type as (typeof MARK_ORDER)[number])
52
-
53
- function sortMarks(marks: DocMark[] | undefined): DocMark[] | undefined {
54
- if (!marks || marks.length === 0) return undefined
55
- return [...marks].sort((a, b) => markRank(a) - markRank(b))
56
- }
57
-
58
- const sameMarks = (a?: DocMark[], b?: DocMark[]) => JSON.stringify(a ?? []) === JSON.stringify(b ?? [])
59
-
60
- function inlineToPm(content: Inline[]): JSONContent[] {
61
- const out: JSONContent[] = []
62
- for (const run of content) {
63
- if (run.type === "text") {
64
- if (!run.text) continue
65
- const node: JSONContent = { type: "text", text: run.text }
66
- if (run.marks?.length) node.marks = run.marks.map(markToPm)
67
- out.push(node)
68
- } else if (run.type === "break") {
69
- out.push({ type: "hardBreak" })
70
- } else if (run.type === "math") {
71
- out.push({ type: "mathInline", attrs: { tex: run.tex } })
72
- } else {
73
- const node: JSONContent = {
74
- type: "field",
75
- attrs: { path: run.path, label: run.label, required: run.required ?? null, perDocument: run.perDocument ?? null },
76
- }
77
- if (run.marks?.length) node.marks = run.marks.map(markToPm)
78
- out.push(node)
79
- }
80
- }
81
- return out
82
- }
83
-
84
- function inlineFromPm(content: JSONContent[] | undefined): Inline[] {
85
- const out: Inline[] = []
86
- for (const node of content ?? []) {
87
- if (node.type === "text") {
88
- const marks = sortMarks(node.marks?.map((m) => markFromPm(m as PmMark)))
89
- const last = out[out.length - 1]
90
- if (last && last.type === "text" && sameMarks(last.marks, marks)) {
91
- last.text += node.text ?? ""
92
- } else {
93
- const run: Text = { type: "text", text: node.text ?? "" }
94
- if (marks) run.marks = marks
95
- out.push(run)
96
- }
97
- } else if (node.type === "hardBreak") {
98
- out.push({ type: "break" })
99
- } else if (node.type === "mathInline") {
100
- out.push({ type: "math", tex: String(node.attrs?.tex ?? "") })
101
- } else if (node.type === "field") {
102
- const a = node.attrs ?? {}
103
- const field: Inline = { type: "field", path: String(a.path ?? ""), label: String(a.label ?? "") }
104
- if (a.required != null) field.required = Boolean(a.required)
105
- if (a.perDocument != null) field.perDocument = Boolean(a.perDocument)
106
- const marks = sortMarks(node.marks?.map((m) => markFromPm(m as PmMark)))
107
- if (marks) field.marks = marks
108
- out.push(field)
109
- }
110
- }
111
- return out
112
- }
113
-
114
- /** The canonical form of inline content: what comes back out of the editor. */
115
- export function normalizeInline(content: Inline[]): Inline[] {
116
- return inlineFromPm(inlineToPm(content))
117
- }
118
-
119
- /* ------------------------------------------------------------- blocks */
120
-
121
- function common(block: Block) {
122
- return { id: block.id, suggestion: block.suggestion ?? null }
123
- }
124
-
125
- export function blockToPm(block: Block): JSONContent {
126
- const content = (inline: Inline[]) => {
127
- const c = inlineToPm(inline)
128
- return c.length ? { content: c } : {}
129
- }
130
- switch (block.type) {
131
- case "paragraph":
132
- return { type: "paragraph", attrs: common(block), ...content(block.content) }
133
- case "heading":
134
- return { type: "heading", attrs: { ...common(block), level: block.level }, ...content(block.content) }
135
- case "quote":
136
- return { type: "quote", attrs: common(block), ...content(block.content) }
137
- case "code":
138
- return {
139
- type: "codeBlock",
140
- attrs: { ...common(block), language: block.language ?? null },
141
- ...(block.text ? { content: [{ type: "text", text: block.text }] } : {}),
142
- }
143
- case "bullet":
144
- case "number":
145
- return { type: "listItem", attrs: { ...common(block), kind: block.type, level: block.level }, ...content(block.content) }
146
- case "checklist":
147
- return {
148
- type: "checkItem",
149
- attrs: { ...common(block), level: block.level, checked: block.checked },
150
- ...content(block.content),
151
- }
152
- case "divider":
153
- return { type: "divider", attrs: common(block) }
154
- case "callout":
155
- return { type: "callout", attrs: { ...common(block), kind: block.kind }, ...content(block.content) }
156
- case "toggle":
157
- return {
158
- type: "toggle",
159
- attrs: { ...common(block), open: block.open },
160
- content: [{ type: "toggleSummary", ...content(block.summary) }, ...block.children.map(blockToPm)],
161
- }
162
- case "columns":
163
- return {
164
- type: "columns",
165
- attrs: common(block),
166
- content: block.columns.map((col) => ({ type: "column", ...(col.length ? { content: col.map(blockToPm) } : {}) })),
167
- }
168
- case "table":
169
- return {
170
- type: "table",
171
- attrs: { ...common(block), headerRow: block.headerRow, columns: block.columns },
172
- content: block.rows.map((row) => ({
173
- type: "tableRow",
174
- content: row.map((cell) => ({ type: "tableCell", ...content(cell) })),
175
- })),
176
- }
177
- case "pageBreak":
178
- return { type: "pageBreak", attrs: common(block) }
179
- case "image":
180
- return { type: "image", attrs: { ...common(block), src: block.src, alt: block.alt, width: block.width, caption: block.caption ?? null } }
181
- case "file":
182
- return { type: "file", attrs: { ...common(block), fileId: block.fileId, name: block.name, size: block.size, mime: block.mime } }
183
- case "record":
184
- return { type: "record", attrs: { ...common(block), ref: block.ref, view: block.view } }
185
- case "chart":
186
- return { type: "chart", attrs: { ...common(block), chart: block.chart, source: block.source, title: block.title ?? null } }
187
- case "signature":
188
- // @date-display-ignore: the signature date is text the author typed or a merge field (Inline[]), not a record timestamp.
189
- return { type: "signature", attrs: { ...common(block), signers: block.signers, date: block.date, stamp: block.stamp } }
190
- case "barcode":
191
- return {
192
- type: "barcode",
193
- attrs: {
194
- ...common(block),
195
- symbology: block.symbology,
196
- value: block.value,
197
- showText: block.showText,
198
- widthMm: block.widthMm ?? null,
199
- heightMm: block.heightMm ?? null,
200
- },
201
- }
202
- case "qr":
203
- return { type: "qr", attrs: { ...common(block), value: block.value, caption: block.caption ?? null, sizeMm: block.sizeMm ?? null } }
204
- case "math":
205
- return { type: "math", attrs: { ...common(block), tex: block.tex } }
206
- default:
207
- return { type: "unknownBlock", attrs: { ...common(block), data: block } }
208
- }
209
- }
210
-
211
- /** Copy an optional contract field only when it is set, so a round trip adds nothing. */
212
- function optional<T extends object>(block: T, key: string, value: unknown): T {
213
- if (value !== null && value !== undefined) (block as Record<string, unknown>)[key] = value
214
- return block
215
- }
216
-
217
- type Of<T extends Block["type"]> = Extract<Block, { type: T }>
218
-
219
- function withCommon<T extends Block>(block: T, attrs: Record<string, unknown>): T {
220
- if (attrs.suggestion) (block as Block).suggestion = attrs.suggestion as Block["suggestion"]
221
- return block
222
- }
223
-
224
- export function blockFromPm(node: JSONContent): Block | null {
225
- const a = node.attrs ?? {}
226
- const id = String(a.id ?? "")
227
- const inline = () => inlineFromPm(node.content)
228
- switch (node.type) {
229
- case "paragraph":
230
- return withCommon({ id, type: "paragraph", content: inline() }, a)
231
- case "heading":
232
- return withCommon({ id, type: "heading", level: Number(a.level) as 1 | 2 | 3, content: inline() }, a)
233
- case "quote":
234
- return withCommon({ id, type: "quote", content: inline() }, a)
235
- case "codeBlock": {
236
- const block: Block = { id, type: "code", text: (node.content ?? []).map((t) => t.text ?? "").join("") }
237
- if (a.language) block.language = String(a.language)
238
- return withCommon(block, a)
239
- }
240
- case "listItem":
241
- return withCommon(
242
- { id, type: a.kind === "number" ? "number" : "bullet", level: Number(a.level) as 0 | 1 | 2, content: inline() },
243
- a
244
- )
245
- case "checkItem":
246
- return withCommon(
247
- { id, type: "checklist", level: Number(a.level) as 0 | 1 | 2, checked: Boolean(a.checked), content: inline() },
248
- a
249
- )
250
- case "divider":
251
- return withCommon({ id, type: "divider" }, a)
252
- case "callout":
253
- return withCommon({ id, type: "callout", kind: a.kind === "important" ? "important" : "note", content: inline() }, a)
254
- case "toggle": {
255
- const [summary, ...children] = node.content ?? []
256
- return withCommon(
257
- { id, type: "toggle", open: Boolean(a.open), summary: inlineFromPm(summary?.content), children: blocksFromPm(children) },
258
- a
259
- )
260
- }
261
- case "columns": {
262
- const cols = (node.content ?? []).map((col) => blocksFromPm(col.content))
263
- return withCommon({ id, type: "columns", columns: [cols[0] ?? [], cols[1] ?? []] }, a)
264
- }
265
- case "table":
266
- return withCommon(
267
- {
268
- id,
269
- type: "table",
270
- headerRow: Boolean(a.headerRow),
271
- columns: (a.columns ?? []) as Of<"table">["columns"],
272
- rows: (node.content ?? []).map((row) => (row.content ?? []).map((cell) => inlineFromPm(cell.content))),
273
- },
274
- a
275
- )
276
- case "pageBreak":
277
- return withCommon({ id, type: "pageBreak" }, a)
278
- case "image":
279
- return withCommon(
280
- optional<Of<"image">>(
281
- { id, type: "image", src: String(a.src ?? ""), alt: String(a.alt ?? ""), width: (a.width ?? "full") as Of<"image">["width"] },
282
- "caption",
283
- a.caption
284
- ),
285
- a
286
- )
287
- case "file":
288
- return withCommon(
289
- { id, type: "file", fileId: String(a.fileId ?? ""), name: String(a.name ?? ""), size: Number(a.size ?? 0), mime: String(a.mime ?? "") },
290
- a
291
- )
292
- case "record":
293
- return withCommon({ id, type: "record", ref: a.ref as Of<"record">["ref"], view: a.view === "summary" ? "summary" : "card" }, a)
294
- case "chart":
295
- return withCommon(
296
- optional<Of<"chart">>(
297
- { id, type: "chart", chart: (a.chart ?? "bar") as Of<"chart">["chart"], source: a.source as Of<"chart">["source"] },
298
- "title",
299
- a.title
300
- ),
301
- a
302
- )
303
- case "signature":
304
- return withCommon(
305
- { id, type: "signature", signers: (a.signers ?? []) as Of<"signature">["signers"], date: (a.date ?? []) as Inline[], stamp: Boolean(a.stamp) },
306
- a
307
- )
308
- case "barcode": {
309
- const block: Of<"barcode"> = {
310
- id,
311
- type: "barcode",
312
- symbology: (a.symbology ?? "code128") as Of<"barcode">["symbology"],
313
- value: (a.value ?? []) as Of<"barcode">["value"],
314
- showText: Boolean(a.showText),
315
- }
316
- return withCommon(optional(optional(block, "widthMm", a.widthMm), "heightMm", a.heightMm), a)
317
- }
318
- case "qr": {
319
- const block: Of<"qr"> = { id, type: "qr", value: (a.value ?? []) as Of<"qr">["value"] }
320
- return withCommon(optional(optional(block, "caption", a.caption), "sizeMm", a.sizeMm), a)
321
- }
322
- case "math":
323
- return withCommon({ id, type: "math", tex: String(a.tex ?? "") }, a)
324
- case "unknownBlock":
325
- return a.data ? { ...(a.data as Block), id } : null
326
- default:
327
- return null
328
- }
329
- }
330
-
331
- function blocksFromPm(nodes: JSONContent[] | undefined): Block[] {
332
- return (nodes ?? []).map(blockFromPm).filter((b): b is Block => b !== null)
333
- }
334
-
335
- /** The editor's content for a document's blocks. An empty document gets one empty paragraph. */
336
- export function toEditorContent(blocks: Block[]): JSONContent {
337
- const content = blocks.map(blockToPm)
338
- return { type: "doc", content: content.length ? content : [{ type: "paragraph", attrs: { id: null, suggestion: null } }] }
339
- }
340
-
341
- export function fromEditorContent(doc: JSONContent): Block[] {
342
- return (doc.content ?? []).map(blockFromPm).filter((b): b is Block => b !== null)
343
- }
344
-
345
- /** The canonical form of a document's blocks: equal to what a round trip through the editor returns. */
346
- export function normalizeBlocks(blocks: Block[]): Block[] {
347
- return blocks.map((block) => {
348
- switch (block.type) {
349
- case "toggle":
350
- return { ...block, summary: normalizeInline(block.summary), children: normalizeBlocks(block.children) }
351
- case "columns":
352
- return { ...block, columns: [normalizeBlocks(block.columns[0]), normalizeBlocks(block.columns[1])] as Of<"columns">["columns"] }
353
- case "table":
354
- return { ...block, rows: block.rows.map((row) => row.map(normalizeInline)) }
355
- }
356
- if (!TEXT_BLOCKS.has(block.type)) return block
357
- if ("content" in block && Array.isArray(block.content)) {
358
- return { ...block, content: normalizeInline(block.content) } as Block
359
- }
360
- return block
361
- })
362
- }
1
+ import type { JSONContent } from "@tiptap/core"
2
+
3
+ import type { Block, Inline, Mark as DocMark, Text } from "./contract"
4
+ import { MARK_ORDER } from "./schema"
5
+
6
+ /**
7
+ * WhityDocument blocks ⇄ ProseMirror JSON. `toEditorContent` and
8
+ * `fromEditorContent` are inverses on normalised documents
9
+ * (`normalizeBlocks`): adjacent text runs with the same marks are one run,
10
+ * marks are in schema order, and empty runs are dropped. Blocks the editor
11
+ * doesn't edit yet travel through `unknownBlock` unchanged.
12
+ */
13
+
14
+ type PmMark = { type: string; attrs?: Record<string, unknown> }
15
+
16
+ const TEXT_BLOCKS = new Set(["paragraph", "heading", "quote", "bullet", "number", "checklist", "code", "divider", "callout"])
17
+
18
+ /* ------------------------------------------------------------- inline */
19
+
20
+ function markToPm(mark: DocMark): PmMark {
21
+ switch (mark.type) {
22
+ case "link":
23
+ return { type: "link", attrs: { href: mark.href, recordRef: mark.recordRef ?? null } }
24
+ case "comment":
25
+ return { type: "comment", attrs: { threadId: mark.threadId } }
26
+ case "insertion":
27
+ case "deletion":
28
+ return { type: mark.type, attrs: { suggestionId: mark.suggestionId } }
29
+ case "perDocument":
30
+ return { type: "perDocument", attrs: { key: mark.key || null } }
31
+ default:
32
+ return { type: mark.type }
33
+ }
34
+ }
35
+
36
+ function markFromPm(mark: PmMark): DocMark {
37
+ const a = mark.attrs ?? {}
38
+ switch (mark.type) {
39
+ case "link":
40
+ return a.recordRef
41
+ ? { type: "link", href: String(a.href ?? ""), recordRef: a.recordRef as { type: string; id: string } }
42
+ : { type: "link", href: String(a.href ?? "") }
43
+ case "comment":
44
+ return { type: "comment", threadId: String(a.threadId ?? "") }
45
+ case "insertion":
46
+ case "deletion":
47
+ return { type: mark.type, suggestionId: String(a.suggestionId ?? "") }
48
+ case "perDocument":
49
+ // E.23a: the part's stable key; a pre-3.4 mark has none until it is keyed.
50
+ return (typeof a.key === "string" && a.key ? { type: "perDocument", key: a.key } : { type: "perDocument" }) as DocMark
51
+ default:
52
+ return { type: mark.type } as DocMark
53
+ }
54
+ }
55
+
56
+ const markRank = (m: DocMark) => MARK_ORDER.indexOf(m.type as (typeof MARK_ORDER)[number])
57
+
58
+ function sortMarks(marks: DocMark[] | undefined): DocMark[] | undefined {
59
+ if (!marks || marks.length === 0) return undefined
60
+ return [...marks].sort((a, b) => markRank(a) - markRank(b))
61
+ }
62
+
63
+ const sameMarks = (a?: DocMark[], b?: DocMark[]) => JSON.stringify(a ?? []) === JSON.stringify(b ?? [])
64
+
65
+ function inlineToPm(content: Inline[]): JSONContent[] {
66
+ const out: JSONContent[] = []
67
+ for (const run of content) {
68
+ if (run.type === "text") {
69
+ if (!run.text) continue
70
+ const node: JSONContent = { type: "text", text: run.text }
71
+ if (run.marks?.length) node.marks = run.marks.map(markToPm)
72
+ out.push(node)
73
+ } else if (run.type === "break") {
74
+ out.push({ type: "hardBreak" })
75
+ } else if (run.type === "math") {
76
+ out.push({ type: "mathInline", attrs: { tex: run.tex } })
77
+ } else {
78
+ const node: JSONContent = {
79
+ type: "field",
80
+ attrs: { path: run.path, label: run.label, required: run.required ?? null, perDocument: run.perDocument ?? null },
81
+ }
82
+ if (run.marks?.length) node.marks = run.marks.map(markToPm)
83
+ out.push(node)
84
+ }
85
+ }
86
+ return out
87
+ }
88
+
89
+ function inlineFromPm(content: JSONContent[] | undefined): Inline[] {
90
+ const out: Inline[] = []
91
+ for (const node of content ?? []) {
92
+ if (node.type === "text") {
93
+ const marks = sortMarks(node.marks?.map((m) => markFromPm(m as PmMark)))
94
+ const last = out[out.length - 1]
95
+ if (last && last.type === "text" && sameMarks(last.marks, marks)) {
96
+ last.text += node.text ?? ""
97
+ } else {
98
+ const run: Text = { type: "text", text: node.text ?? "" }
99
+ if (marks) run.marks = marks
100
+ out.push(run)
101
+ }
102
+ } else if (node.type === "hardBreak") {
103
+ out.push({ type: "break" })
104
+ } else if (node.type === "mathInline") {
105
+ out.push({ type: "math", tex: String(node.attrs?.tex ?? "") })
106
+ } else if (node.type === "field") {
107
+ const a = node.attrs ?? {}
108
+ const field: Inline = { type: "field", path: String(a.path ?? ""), label: String(a.label ?? "") }
109
+ if (a.required != null) field.required = Boolean(a.required)
110
+ // E.23a: the part's key; `true` is a pre-3.4 field not keyed yet.
111
+ if (a.perDocument != null) field.perDocument = (typeof a.perDocument === "string" ? a.perDocument : true) as string
112
+ const marks = sortMarks(node.marks?.map((m) => markFromPm(m as PmMark)))
113
+ if (marks) field.marks = marks
114
+ out.push(field)
115
+ }
116
+ }
117
+ return out
118
+ }
119
+
120
+ /** The canonical form of inline content: what comes back out of the editor. */
121
+ export function normalizeInline(content: Inline[]): Inline[] {
122
+ return inlineFromPm(inlineToPm(content))
123
+ }
124
+
125
+ /* ------------------------------------------------------------- blocks */
126
+
127
+ function common(block: Block) {
128
+ return { id: block.id, suggestion: block.suggestion ?? null }
129
+ }
130
+
131
+ export function blockToPm(block: Block): JSONContent {
132
+ const content = (inline: Inline[]) => {
133
+ const c = inlineToPm(inline)
134
+ return c.length ? { content: c } : {}
135
+ }
136
+ switch (block.type) {
137
+ case "paragraph":
138
+ return { type: "paragraph", attrs: common(block), ...content(block.content) }
139
+ case "heading":
140
+ return { type: "heading", attrs: { ...common(block), level: block.level }, ...content(block.content) }
141
+ case "quote":
142
+ return { type: "quote", attrs: common(block), ...content(block.content) }
143
+ case "code":
144
+ return {
145
+ type: "codeBlock",
146
+ attrs: { ...common(block), language: block.language ?? null },
147
+ ...(block.text ? { content: [{ type: "text", text: block.text }] } : {}),
148
+ }
149
+ case "bullet":
150
+ case "number":
151
+ return { type: "listItem", attrs: { ...common(block), kind: block.type, level: block.level }, ...content(block.content) }
152
+ case "checklist":
153
+ return {
154
+ type: "checkItem",
155
+ attrs: { ...common(block), level: block.level, checked: block.checked },
156
+ ...content(block.content),
157
+ }
158
+ case "divider":
159
+ return { type: "divider", attrs: common(block) }
160
+ case "callout":
161
+ return { type: "callout", attrs: { ...common(block), kind: block.kind }, ...content(block.content) }
162
+ case "toggle":
163
+ return {
164
+ type: "toggle",
165
+ attrs: { ...common(block), open: block.open },
166
+ content: [{ type: "toggleSummary", ...content(block.summary) }, ...block.children.map(blockToPm)],
167
+ }
168
+ case "columns":
169
+ return {
170
+ type: "columns",
171
+ attrs: common(block),
172
+ content: block.columns.map((col) => ({ type: "column", ...(col.length ? { content: col.map(blockToPm) } : {}) })),
173
+ }
174
+ case "table":
175
+ return {
176
+ type: "table",
177
+ attrs: { ...common(block), headerRow: block.headerRow, columns: block.columns },
178
+ content: block.rows.map((row) => ({
179
+ type: "tableRow",
180
+ content: row.map((cell) => ({ type: "tableCell", ...content(cell) })),
181
+ })),
182
+ }
183
+ case "pageBreak":
184
+ return { type: "pageBreak", attrs: common(block) }
185
+ case "image":
186
+ return { type: "image", attrs: { ...common(block), src: block.src, alt: block.alt, width: block.width, caption: block.caption ?? null } }
187
+ case "file":
188
+ return { type: "file", attrs: { ...common(block), fileId: block.fileId, name: block.name, size: block.size, mime: block.mime } }
189
+ case "record":
190
+ return { type: "record", attrs: { ...common(block), ref: block.ref, view: block.view } }
191
+ case "chart":
192
+ return { type: "chart", attrs: { ...common(block), chart: block.chart, source: block.source, title: block.title ?? null } }
193
+ case "signature":
194
+ // @date-display-ignore: the signature date is text the author typed or a merge field (Inline[]), not a record timestamp.
195
+ return { type: "signature", attrs: { ...common(block), signers: block.signers, date: block.date, stamp: block.stamp } }
196
+ case "barcode":
197
+ return {
198
+ type: "barcode",
199
+ attrs: {
200
+ ...common(block),
201
+ symbology: block.symbology,
202
+ value: block.value,
203
+ showText: block.showText,
204
+ widthMm: block.widthMm ?? null,
205
+ heightMm: block.heightMm ?? null,
206
+ },
207
+ }
208
+ case "qr":
209
+ return { type: "qr", attrs: { ...common(block), value: block.value, caption: block.caption ?? null, sizeMm: block.sizeMm ?? null } }
210
+ case "math":
211
+ return { type: "math", attrs: { ...common(block), tex: block.tex } }
212
+ case "plugin":
213
+ return {
214
+ type: "plugin",
215
+ attrs: { ...common(block), plugin: block.plugin, blockType: block.blockType, schemaVersion: block.schemaVersion, data: block.data },
216
+ }
217
+ case "savedBlock":
218
+ return {
219
+ type: "savedBlock",
220
+ attrs: { ...common(block), savedBlockId: block.savedBlockId, version: block.version, overrides: block.overrides },
221
+ }
222
+ default:
223
+ return { type: "unknownBlock", attrs: { ...common(block), data: block } }
224
+ }
225
+ }
226
+
227
+ /** Copy an optional contract field only when it is set, so a round trip adds nothing. */
228
+ function optional<T extends object>(block: T, key: string, value: unknown): T {
229
+ if (value !== null && value !== undefined) (block as Record<string, unknown>)[key] = value
230
+ return block
231
+ }
232
+
233
+ type Of<T extends Block["type"]> = Extract<Block, { type: T }>
234
+
235
+ function withCommon<T extends Block>(block: T, attrs: Record<string, unknown>): T {
236
+ if (attrs.suggestion) (block as Block).suggestion = attrs.suggestion as Block["suggestion"]
237
+ return block
238
+ }
239
+
240
+ export function blockFromPm(node: JSONContent): Block | null {
241
+ const a = node.attrs ?? {}
242
+ const id = String(a.id ?? "")
243
+ const inline = () => inlineFromPm(node.content)
244
+ switch (node.type) {
245
+ case "paragraph":
246
+ return withCommon({ id, type: "paragraph", content: inline() }, a)
247
+ case "heading":
248
+ return withCommon({ id, type: "heading", level: Number(a.level) as 1 | 2 | 3, content: inline() }, a)
249
+ case "quote":
250
+ return withCommon({ id, type: "quote", content: inline() }, a)
251
+ case "codeBlock": {
252
+ const block: Block = { id, type: "code", text: (node.content ?? []).map((t) => t.text ?? "").join("") }
253
+ if (a.language) block.language = String(a.language)
254
+ return withCommon(block, a)
255
+ }
256
+ case "listItem":
257
+ return withCommon(
258
+ { id, type: a.kind === "number" ? "number" : "bullet", level: Number(a.level) as 0 | 1 | 2, content: inline() },
259
+ a
260
+ )
261
+ case "checkItem":
262
+ return withCommon(
263
+ { id, type: "checklist", level: Number(a.level) as 0 | 1 | 2, checked: Boolean(a.checked), content: inline() },
264
+ a
265
+ )
266
+ case "divider":
267
+ return withCommon({ id, type: "divider" }, a)
268
+ case "callout":
269
+ return withCommon({ id, type: "callout", kind: a.kind === "important" ? "important" : "note", content: inline() }, a)
270
+ case "toggle": {
271
+ const [summary, ...children] = node.content ?? []
272
+ return withCommon(
273
+ { id, type: "toggle", open: Boolean(a.open), summary: inlineFromPm(summary?.content), children: blocksFromPm(children) },
274
+ a
275
+ )
276
+ }
277
+ case "columns": {
278
+ const cols = (node.content ?? []).map((col) => blocksFromPm(col.content))
279
+ return withCommon({ id, type: "columns", columns: [cols[0] ?? [], cols[1] ?? []] }, a)
280
+ }
281
+ case "table":
282
+ return withCommon(
283
+ {
284
+ id,
285
+ type: "table",
286
+ headerRow: Boolean(a.headerRow),
287
+ columns: (a.columns ?? []) as Of<"table">["columns"],
288
+ rows: (node.content ?? []).map((row) => (row.content ?? []).map((cell) => inlineFromPm(cell.content))),
289
+ },
290
+ a
291
+ )
292
+ case "pageBreak":
293
+ return withCommon({ id, type: "pageBreak" }, a)
294
+ case "image":
295
+ return withCommon(
296
+ optional<Of<"image">>(
297
+ { id, type: "image", src: String(a.src ?? ""), alt: String(a.alt ?? ""), width: (a.width ?? "full") as Of<"image">["width"] },
298
+ "caption",
299
+ a.caption
300
+ ),
301
+ a
302
+ )
303
+ case "file":
304
+ return withCommon(
305
+ { id, type: "file", fileId: String(a.fileId ?? ""), name: String(a.name ?? ""), size: Number(a.size ?? 0), mime: String(a.mime ?? "") },
306
+ a
307
+ )
308
+ case "record":
309
+ return withCommon({ id, type: "record", ref: a.ref as Of<"record">["ref"], view: a.view === "summary" ? "summary" : "card" }, a)
310
+ case "chart":
311
+ return withCommon(
312
+ optional<Of<"chart">>(
313
+ { id, type: "chart", chart: (a.chart ?? "bar") as Of<"chart">["chart"], source: a.source as Of<"chart">["source"] },
314
+ "title",
315
+ a.title
316
+ ),
317
+ a
318
+ )
319
+ case "signature":
320
+ return withCommon(
321
+ { id, type: "signature", signers: (a.signers ?? []) as Of<"signature">["signers"], date: (a.date ?? []) as Inline[], stamp: Boolean(a.stamp) },
322
+ a
323
+ )
324
+ case "barcode": {
325
+ const block: Of<"barcode"> = {
326
+ id,
327
+ type: "barcode",
328
+ symbology: (a.symbology ?? "code128") as Of<"barcode">["symbology"],
329
+ value: (a.value ?? []) as Of<"barcode">["value"],
330
+ showText: Boolean(a.showText),
331
+ }
332
+ return withCommon(optional(optional(block, "widthMm", a.widthMm), "heightMm", a.heightMm), a)
333
+ }
334
+ case "qr": {
335
+ const block: Of<"qr"> = { id, type: "qr", value: (a.value ?? []) as Of<"qr">["value"] }
336
+ return withCommon(optional(optional(block, "caption", a.caption), "sizeMm", a.sizeMm), a)
337
+ }
338
+ case "math":
339
+ return withCommon({ id, type: "math", tex: String(a.tex ?? "") }, a)
340
+ case "plugin":
341
+ return withCommon(
342
+ {
343
+ id,
344
+ type: "plugin",
345
+ plugin: String(a.plugin ?? ""),
346
+ blockType: String(a.blockType ?? ""),
347
+ schemaVersion: Number(a.schemaVersion ?? 1),
348
+ data: (a.data ?? {}) as Of<"plugin">["data"],
349
+ },
350
+ a
351
+ )
352
+ case "savedBlock":
353
+ return withCommon(
354
+ {
355
+ id,
356
+ type: "savedBlock",
357
+ savedBlockId: String(a.savedBlockId ?? ""),
358
+ version: Number(a.version ?? 1),
359
+ overrides: (a.overrides ?? {}) as Of<"savedBlock">["overrides"],
360
+ },
361
+ a
362
+ )
363
+ case "unknownBlock":
364
+ return a.data ? { ...(a.data as Block), id } : null
365
+ default:
366
+ return null
367
+ }
368
+ }
369
+
370
+ function blocksFromPm(nodes: JSONContent[] | undefined): Block[] {
371
+ return (nodes ?? []).map(blockFromPm).filter((b): b is Block => b !== null)
372
+ }
373
+
374
+ /**
375
+ * Blocks as fresh editor content: the same blocks with no ids, so the editor
376
+ * gives each a new one (a saved block's copy, or a detached synced block).
377
+ */
378
+ export function freshEditorBlocks(blocks: Block[]): JSONContent[] {
379
+ const strip = (node: JSONContent): JSONContent => ({
380
+ ...node,
381
+ ...(node.attrs && "id" in node.attrs ? { attrs: { ...node.attrs, id: null } } : {}),
382
+ ...(node.content ? { content: node.content.map(strip) } : {}),
383
+ })
384
+ return blocks.map((b) => strip(blockToPm(b)))
385
+ }
386
+
387
+ /** The editor's content for a document's blocks. An empty document gets one empty paragraph. */
388
+ export function toEditorContent(blocks: Block[]): JSONContent {
389
+ const content = blocks.map(blockToPm)
390
+ return { type: "doc", content: content.length ? content : [{ type: "paragraph", attrs: { id: null, suggestion: null } }] }
391
+ }
392
+
393
+ export function fromEditorContent(doc: JSONContent): Block[] {
394
+ return (doc.content ?? []).map(blockFromPm).filter((b): b is Block => b !== null)
395
+ }
396
+
397
+ /** The canonical form of a document's blocks: equal to what a round trip through the editor returns. */
398
+ export function normalizeBlocks(blocks: Block[]): Block[] {
399
+ return blocks.map((block) => {
400
+ switch (block.type) {
401
+ case "toggle":
402
+ return { ...block, summary: normalizeInline(block.summary), children: normalizeBlocks(block.children) }
403
+ case "columns":
404
+ return { ...block, columns: [normalizeBlocks(block.columns[0]), normalizeBlocks(block.columns[1])] as Of<"columns">["columns"] }
405
+ case "table":
406
+ return { ...block, rows: block.rows.map((row) => row.map(normalizeInline)) }
407
+ }
408
+ if (!TEXT_BLOCKS.has(block.type)) return block
409
+ if ("content" in block && Array.isArray(block.content)) {
410
+ return { ...block, content: normalizeInline(block.content) } as Block
411
+ }
412
+ return block
413
+ })
414
+ }