@whity/ui 0.0.0-stage → 0.5.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 (169) hide show
  1. package/README.md +107 -3
  2. package/package.json +192 -6
  3. package/src/access-denied.stories.tsx +179 -0
  4. package/src/access-denied.tsx +305 -0
  5. package/src/action-proposal.tsx +302 -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 +500 -0
  16. package/src/approval-queue.tsx +108 -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.tsx +83 -0
  22. package/src/badge.stories.tsx +105 -0
  23. package/src/badge.tsx +68 -0
  24. package/src/beta-warning.stories.tsx +79 -0
  25. package/src/beta-warning.tsx +189 -0
  26. package/src/bilingual-input.stories.tsx +82 -0
  27. package/src/bilingual-input.tsx +183 -0
  28. package/src/breadcrumb.stories.tsx +51 -0
  29. package/src/breadcrumb.tsx +90 -0
  30. package/src/button.stories.tsx +87 -0
  31. package/src/button.tsx +118 -0
  32. package/src/card.stories.tsx +423 -0
  33. package/src/card.tsx +180 -0
  34. package/src/chart.stories.tsx +66 -0
  35. package/src/chart.tsx +228 -0
  36. package/src/checkbox.stories.tsx +62 -0
  37. package/src/checkbox.tsx +54 -0
  38. package/src/collapsible.stories.tsx +48 -0
  39. package/src/collapsible.tsx +34 -0
  40. package/src/color-card.stories.tsx +61 -0
  41. package/src/color-card.tsx +270 -0
  42. package/src/color-picker.stories.tsx +65 -0
  43. package/src/color-picker.tsx +425 -0
  44. package/src/command-palette.tsx +306 -0
  45. package/src/copy-block.stories.tsx +38 -0
  46. package/src/copy-block.tsx +85 -0
  47. package/src/data-table.stories.tsx +200 -0
  48. package/src/data-table.tsx +1106 -0
  49. package/src/date-picker.stories.tsx +61 -0
  50. package/src/date-picker.tsx +422 -0
  51. package/src/design-cards.stories.tsx +458 -0
  52. package/src/dialog.stories.tsx +48 -0
  53. package/src/dialog.tsx +166 -0
  54. package/src/documents/barcode-svg.stories.tsx +72 -0
  55. package/src/documents/barcode-svg.tsx +79 -0
  56. package/src/dropdown-menu.stories.tsx +77 -0
  57. package/src/dropdown-menu.tsx +271 -0
  58. package/src/editor/behaviour.ts +279 -0
  59. package/src/editor/block-ids.ts +44 -0
  60. package/src/editor/block-list.tsx +101 -0
  61. package/src/editor/block-toolbar.tsx +477 -0
  62. package/src/editor/bubble.tsx +93 -0
  63. package/src/editor/catalogue.ts +103 -0
  64. package/src/editor/collab-rail.tsx +746 -0
  65. package/src/editor/collab.stories.tsx +207 -0
  66. package/src/editor/collab.ts +87 -0
  67. package/src/editor/contract.ts +408 -0
  68. package/src/editor/convert.ts +362 -0
  69. package/src/editor/doc-editor.stories.tsx +98 -0
  70. package/src/editor/doc-editor.tsx +1287 -0
  71. package/src/editor/editor.css +536 -0
  72. package/src/editor/extensions.ts +86 -0
  73. package/src/editor/field-picker.tsx +107 -0
  74. package/src/editor/fields.ts +100 -0
  75. package/src/editor/gutter.tsx +367 -0
  76. package/src/editor/index.ts +22 -0
  77. package/src/editor/label-sheet.tsx +289 -0
  78. package/src/editor/labels.ts +310 -0
  79. package/src/editor/link-tools.tsx +141 -0
  80. package/src/editor/node-views.tsx +450 -0
  81. package/src/editor/page-view.tsx +353 -0
  82. package/src/editor/publish-check.tsx +182 -0
  83. package/src/editor/rich-blocks.stories.tsx +146 -0
  84. package/src/editor/sample.ts +40 -0
  85. package/src/editor/schema-rich.ts +150 -0
  86. package/src/editor/schema.ts +315 -0
  87. package/src/editor/services.tsx +127 -0
  88. package/src/editor/slash.ts +64 -0
  89. package/src/editor/suggesting.ts +372 -0
  90. package/src/editor/suggestions.ts +763 -0
  91. package/src/editor/table-keys.ts +66 -0
  92. package/src/editor/templates.stories.tsx +324 -0
  93. package/src/editor/toolbar.tsx +358 -0
  94. package/src/empty-state.stories.tsx +51 -0
  95. package/src/empty-state.tsx +100 -0
  96. package/src/empty-state.usage-notes.md +30 -0
  97. package/src/form.stories.tsx +112 -0
  98. package/src/form.tsx +176 -0
  99. package/src/globals.css +20 -0
  100. package/src/icon-button.tsx +85 -0
  101. package/src/icon-map.ts +244 -0
  102. package/src/icon.tsx +41 -0
  103. package/src/index.ts +67 -0
  104. package/src/input.stories.tsx +157 -0
  105. package/src/input.tsx +759 -0
  106. package/src/locked-screen.stories.tsx +29 -0
  107. package/src/locked-screen.tsx +61 -0
  108. package/src/mark.tsx +62 -0
  109. package/src/math-text.stories.tsx +33 -0
  110. package/src/math-text.tsx +43 -0
  111. package/src/menu-bar.stories.tsx +198 -0
  112. package/src/menu-bar.tsx +373 -0
  113. package/src/money/currency.ts +230 -0
  114. package/src/page-header.stories.tsx +95 -0
  115. package/src/page-header.tsx +122 -0
  116. package/src/page-shell.stories.tsx +42 -0
  117. package/src/page-shell.tsx +32 -0
  118. package/src/pagination.stories.tsx +45 -0
  119. package/src/pagination.tsx +98 -0
  120. package/src/plugin-card.stories.tsx +180 -0
  121. package/src/plugin-card.tsx +848 -0
  122. package/src/popover.stories.tsx +48 -0
  123. package/src/popover.tsx +55 -0
  124. package/src/radio-group.stories.tsx +26 -0
  125. package/src/radio-group.tsx +44 -0
  126. package/src/route-flow/editor.tsx +1014 -0
  127. package/src/route-flow/model.ts +716 -0
  128. package/src/scope-grant.tsx +139 -0
  129. package/src/screen-too-small.stories.tsx +61 -0
  130. package/src/screen-too-small.tsx +104 -0
  131. package/src/select.stories.tsx +44 -0
  132. package/src/select.tsx +197 -0
  133. package/src/separator.stories.tsx +34 -0
  134. package/src/separator.tsx +34 -0
  135. package/src/sheet.stories.tsx +49 -0
  136. package/src/sheet.tsx +168 -0
  137. package/src/skeleton.stories.tsx +28 -0
  138. package/src/skeleton.tsx +19 -0
  139. package/src/slider.stories.tsx +78 -0
  140. package/src/slider.tsx +300 -0
  141. package/src/spinner.stories.tsx +33 -0
  142. package/src/spinner.tsx +55 -0
  143. package/src/switch.stories.tsx +42 -0
  144. package/src/switch.tsx +41 -0
  145. package/src/switcher.stories.tsx +111 -0
  146. package/src/switcher.tsx +131 -0
  147. package/src/table.stories.tsx +48 -0
  148. package/src/table.tsx +121 -0
  149. package/src/tabs.stories.tsx +74 -0
  150. package/src/tabs.tsx +90 -0
  151. package/src/tag-input.stories.tsx +67 -0
  152. package/src/tag-input.tsx +117 -0
  153. package/src/tenant-logo.tsx +63 -0
  154. package/src/tenant-switcher.stories.tsx +38 -0
  155. package/src/tenant-switcher.tsx +164 -0
  156. package/src/textarea.stories.tsx +27 -0
  157. package/src/textarea.tsx +18 -0
  158. package/src/theme-mode.ts +52 -0
  159. package/src/time-picker.stories.tsx +62 -0
  160. package/src/time-picker.tsx +615 -0
  161. package/src/toggle-group.tsx +138 -0
  162. package/src/tooltip.stories.tsx +43 -0
  163. package/src/tooltip.tsx +73 -0
  164. package/src/top-nav.stories.tsx +58 -0
  165. package/src/top-nav.tsx +324 -0
  166. package/src/undo-bar.tsx +107 -0
  167. package/src/utils.ts +45 -0
  168. package/src/view-toggle.stories.tsx +84 -0
  169. package/src/view-toggle.tsx +110 -0
@@ -0,0 +1,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
+ 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
+ }
@@ -0,0 +1,98 @@
1
+ import type { Meta, StoryObj } from "@storybook/react-vite"
2
+ import * as React from "react"
3
+
4
+ import type { EditorMode, WhityDocument } from "./contract"
5
+ import { DocEditor } from "./doc-editor"
6
+ import { SAMPLE_DOCUMENT } from "./sample"
7
+
8
+ const meta = {
9
+ title: "Editor/DocEditor",
10
+ parameters: { layout: "fullscreen", fullBleed: true },
11
+ } satisfies Meta
12
+
13
+ export default meta
14
+ type Story = StoryObj<typeof meta>
15
+
16
+ function Live({ initial = SAMPLE_DOCUMENT, canEdit = true }: { initial?: WhityDocument; canEdit?: boolean }) {
17
+ const [doc, setDoc] = React.useState(initial)
18
+ const [mode, setMode] = React.useState<EditorMode>("editing")
19
+ return (
20
+ <div style={{ height: "100vh" }}>
21
+ <DocEditor
22
+ document={doc}
23
+ onChange={setDoc}
24
+ canEdit={canEdit}
25
+ mode={mode}
26
+ onModeChange={setMode}
27
+ back={{ label: "Back to notes", onClick: () => {} }}
28
+ path="Notes · Billing"
29
+ save={{ state: "saved", at: "14:02" }}
30
+ />
31
+ </div>
32
+ )
33
+ }
34
+
35
+ export const Editing: Story = { render: () => <Live /> }
36
+
37
+ export const ReadOnly: Story = { render: () => <Live canEdit={false} /> }
38
+
39
+ export const Empty: Story = {
40
+ render: () => <Live initial={{ ...SAMPLE_DOCUMENT, title: "", blocks: [] }} />,
41
+ }
42
+
43
+ const ARABIC_LABELS = {
44
+ back: "رجوع",
45
+ documentTitle: "عنوان المستند",
46
+ saved: "حُفظ {time}",
47
+ modeEditing: "تحرير",
48
+ modeMenu: "الوضع",
49
+ normalText: "نص عادي",
50
+ insert: "إدراج",
51
+ outline: "المخطط",
52
+ words: "الكلمات: {n}",
53
+ arabic: "العربية",
54
+ kindNote: "ملاحظة",
55
+ lastEdited: "آخر تعديل: {name} الساعة {time}",
56
+ placeholder: "اكتب / للكتل، أو {{ للحقول، أو ابدأ الكتابة",
57
+ }
58
+
59
+ const t = (text: string) => ({ type: "text" as const, text })
60
+
61
+ /** EditorArabic: the shell mirrors, each block follows its own text, numbers stay Western. */
62
+ export const Arabic: Story = {
63
+ globals: { direction: "rtl" },
64
+ render: () => {
65
+ const doc: WhityDocument = {
66
+ ...SAMPLE_DOCUMENT,
67
+ lang: "ar",
68
+ title: "تذكير بالدفع",
69
+ blocks: [
70
+ { id: "a1", type: "paragraph", content: [t("السادة عيادة المثال المحترمين،")] },
71
+ { id: "a2", type: "paragraph", content: [t("استحقت الفاتورة INV-1042 بقيمة JOD 301.600 بتاريخ 14 Oct 2026.")] },
72
+ { id: "a3", type: "heading", level: 1, content: [t("الفواتير المفتوحة")] },
73
+ { id: "a4", type: "number", level: 0, content: [t("INV-1042، بقيمة JOD 301.600")] },
74
+ { id: "a5", type: "number", level: 0, content: [t("INV-1051، بقيمة JOD 96.000")] },
75
+ { id: "a6", type: "paragraph", content: [t("Payment can also be made online by card.")] },
76
+ { id: "a7", type: "checklist", level: 0, checked: false, content: [t("مراجعة المبالغ")] },
77
+ ],
78
+ }
79
+ return <LiveWithLabels initial={doc} />
80
+ },
81
+ }
82
+
83
+ function LiveWithLabels({ initial }: { initial: WhityDocument }) {
84
+ const [doc, setDoc] = React.useState(initial)
85
+ return (
86
+ <div style={{ height: "100vh" }}>
87
+ <DocEditor
88
+ document={doc}
89
+ onChange={setDoc}
90
+ canEdit
91
+ back={{ label: "رجوع", onClick: () => {} }}
92
+ path="الملاحظات والويكي"
93
+ save={{ state: "saved", at: "14:02" }}
94
+ labels={ARABIC_LABELS}
95
+ />
96
+ </div>
97
+ )
98
+ }