@whity/ui 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/README.md +106 -2
  2. package/package.json +219 -4
  3. package/src/access-denied.stories.tsx +179 -0
  4. package/src/access-denied.tsx +305 -0
  5. package/src/action-proposal.tsx +338 -0
  6. package/src/actor-chip.tsx +71 -0
  7. package/src/agent-activity.tsx +163 -0
  8. package/src/agent-collaboration.stories.tsx +156 -0
  9. package/src/agent-run-status.tsx +196 -0
  10. package/src/alert-dialog.stories.tsx +45 -0
  11. package/src/alert-dialog.tsx +147 -0
  12. package/src/alert.stories.tsx +91 -0
  13. package/src/alert.tsx +89 -0
  14. package/src/app-sidebar.stories.tsx +195 -0
  15. package/src/app-sidebar.tsx +604 -0
  16. package/src/approval-queue.tsx +109 -0
  17. package/src/audience-group-picker.stories.tsx +198 -0
  18. package/src/audience-group-picker.tsx +362 -0
  19. package/src/audience-people-picker.stories.tsx +102 -0
  20. package/src/audience-people-picker.tsx +279 -0
  21. package/src/avatar-pattern.ts +138 -0
  22. package/src/avatar.tsx +125 -0
  23. package/src/badge.stories.tsx +105 -0
  24. package/src/badge.tsx +68 -0
  25. package/src/beta-warning.stories.tsx +79 -0
  26. package/src/beta-warning.tsx +189 -0
  27. package/src/bilingual-input.stories.tsx +82 -0
  28. package/src/bilingual-input.tsx +188 -0
  29. package/src/board.tsx +216 -0
  30. package/src/breadcrumb.stories.tsx +51 -0
  31. package/src/breadcrumb.tsx +90 -0
  32. package/src/built-on-whity.tsx +52 -0
  33. package/src/button.stories.tsx +87 -0
  34. package/src/button.tsx +123 -0
  35. package/src/card.stories.tsx +423 -0
  36. package/src/card.tsx +180 -0
  37. package/src/chart.stories.tsx +66 -0
  38. package/src/chart.tsx +228 -0
  39. package/src/checkbox.stories.tsx +62 -0
  40. package/src/checkbox.tsx +54 -0
  41. package/src/collapsible.stories.tsx +48 -0
  42. package/src/collapsible.tsx +34 -0
  43. package/src/color-card.stories.tsx +61 -0
  44. package/src/color-card.tsx +270 -0
  45. package/src/color-picker.stories.tsx +65 -0
  46. package/src/color-picker.tsx +425 -0
  47. package/src/column-matcher.tsx +84 -0
  48. package/src/command-palette.tsx +398 -0
  49. package/src/confirm-action.tsx +98 -0
  50. package/src/copy-block.stories.tsx +38 -0
  51. package/src/copy-block.tsx +85 -0
  52. package/src/data-table.stories.tsx +200 -0
  53. package/src/data-table.tsx +1686 -0
  54. package/src/date-picker.stories.tsx +61 -0
  55. package/src/date-picker.tsx +456 -0
  56. package/src/design-cards.stories.tsx +551 -0
  57. package/src/dialog.stories.tsx +48 -0
  58. package/src/dialog.tsx +179 -0
  59. package/src/display-preferences.tsx +50 -0
  60. package/src/documents/barcode-svg.stories.tsx +72 -0
  61. package/src/documents/barcode-svg.tsx +79 -0
  62. package/src/dropdown-menu.stories.tsx +77 -0
  63. package/src/dropdown-menu.tsx +271 -0
  64. package/src/editor/agent-panel.tsx +222 -0
  65. package/src/editor/agents.stories.tsx +81 -0
  66. package/src/editor/agents.ts +55 -0
  67. package/src/editor/behaviour.ts +279 -0
  68. package/src/editor/block-ids.ts +44 -0
  69. package/src/editor/block-list.tsx +104 -0
  70. package/src/editor/block-toolbar.tsx +490 -0
  71. package/src/editor/bubble.tsx +119 -0
  72. package/src/editor/catalogue.ts +169 -0
  73. package/src/editor/collab-rail.tsx +746 -0
  74. package/src/editor/collab.stories.tsx +207 -0
  75. package/src/editor/collab.ts +87 -0
  76. package/src/editor/contract.ts +476 -0
  77. package/src/editor/convert.ts +414 -0
  78. package/src/editor/doc-editor.stories.tsx +98 -0
  79. package/src/editor/doc-editor.tsx +1492 -0
  80. package/src/editor/editor.css +636 -0
  81. package/src/editor/extensions.ts +86 -0
  82. package/src/editor/field-bar.tsx +154 -0
  83. package/src/editor/field-picker.tsx +109 -0
  84. package/src/editor/fields.ts +100 -0
  85. package/src/editor/gutter.tsx +443 -0
  86. package/src/editor/index.ts +23 -0
  87. package/src/editor/label-sheet.tsx +289 -0
  88. package/src/editor/labels.ts +394 -0
  89. package/src/editor/link-tools.tsx +141 -0
  90. package/src/editor/node-views.tsx +461 -0
  91. package/src/editor/page-view.tsx +372 -0
  92. package/src/editor/phone.tsx +310 -0
  93. package/src/editor/plugin-block-view.tsx +230 -0
  94. package/src/editor/plugin-blocks.stories.tsx +96 -0
  95. package/src/editor/plugin-blocks.ts +52 -0
  96. package/src/editor/publish-check.tsx +201 -0
  97. package/src/editor/rich-blocks.stories.tsx +146 -0
  98. package/src/editor/sample.ts +40 -0
  99. package/src/editor/save-block-dialog.tsx +141 -0
  100. package/src/editor/saved-block-view.tsx +205 -0
  101. package/src/editor/saved-blocks.stories.tsx +105 -0
  102. package/src/editor/saved-parts.ts +192 -0
  103. package/src/editor/schema-rich.ts +156 -0
  104. package/src/editor/schema.ts +368 -0
  105. package/src/editor/services.tsx +205 -0
  106. package/src/editor/slash.ts +64 -0
  107. package/src/editor/suggesting.ts +372 -0
  108. package/src/editor/suggestions.ts +763 -0
  109. package/src/editor/table-keys.ts +66 -0
  110. package/src/editor/templates.stories.tsx +324 -0
  111. package/src/editor/toolbar.tsx +364 -0
  112. package/src/empty-state.stories.tsx +51 -0
  113. package/src/empty-state.tsx +126 -0
  114. package/src/empty-state.usage-notes.md +30 -0
  115. package/src/form.stories.tsx +112 -0
  116. package/src/form.tsx +176 -0
  117. package/src/globals.css +20 -0
  118. package/src/icon-button.tsx +85 -0
  119. package/src/icon-map.ts +335 -0
  120. package/src/icon.tsx +41 -0
  121. package/src/index.ts +90 -0
  122. package/src/input.stories.tsx +157 -0
  123. package/src/input.tsx +768 -0
  124. package/src/kpi-strip.tsx +47 -0
  125. package/src/localized-text.ts +76 -0
  126. package/src/locked-screen.stories.tsx +29 -0
  127. package/src/locked-screen.tsx +75 -0
  128. package/src/lockup.tsx +42 -0
  129. package/src/lottie.tsx +441 -0
  130. package/src/mark.tsx +62 -0
  131. package/src/math-text.stories.tsx +33 -0
  132. package/src/math-text.tsx +43 -0
  133. package/src/menu-bar.stories.tsx +198 -0
  134. package/src/menu-bar.tsx +373 -0
  135. package/src/message-thread.tsx +181 -0
  136. package/src/money/currency.ts +244 -0
  137. package/src/money/money-input.tsx +131 -0
  138. package/src/money/money.tsx +33 -0
  139. package/src/open-on-computer.tsx +150 -0
  140. package/src/otp-input.tsx +161 -0
  141. package/src/page-header.stories.tsx +95 -0
  142. package/src/page-header.tsx +122 -0
  143. package/src/page-shell.stories.tsx +42 -0
  144. package/src/page-shell.tsx +32 -0
  145. package/src/page-states.tsx +277 -0
  146. package/src/pagination.stories.tsx +45 -0
  147. package/src/pagination.tsx +98 -0
  148. package/src/paper-sheet.stories.tsx +88 -0
  149. package/src/paper-sheet.tsx +85 -0
  150. package/src/phone-tab-bar.tsx +152 -0
  151. package/src/plugin-card.stories.tsx +208 -0
  152. package/src/plugin-card.tsx +931 -0
  153. package/src/popover.stories.tsx +48 -0
  154. package/src/popover.tsx +55 -0
  155. package/src/radio-group.stories.tsx +26 -0
  156. package/src/radio-group.tsx +44 -0
  157. package/src/route-flow/editor.tsx +1087 -0
  158. package/src/route-flow/model.ts +771 -0
  159. package/src/save-bar.tsx +94 -0
  160. package/src/scope-grant.tsx +139 -0
  161. package/src/screen-too-small.stories.tsx +61 -0
  162. package/src/screen-too-small.tsx +104 -0
  163. package/src/search-field.tsx +65 -0
  164. package/src/select.stories.tsx +44 -0
  165. package/src/select.tsx +197 -0
  166. package/src/separator.stories.tsx +34 -0
  167. package/src/separator.tsx +34 -0
  168. package/src/sheet.stories.tsx +49 -0
  169. package/src/sheet.tsx +168 -0
  170. package/src/skeleton.stories.tsx +28 -0
  171. package/src/skeleton.tsx +19 -0
  172. package/src/slider.stories.tsx +78 -0
  173. package/src/slider.tsx +300 -0
  174. package/src/spinner.stories.tsx +33 -0
  175. package/src/spinner.tsx +55 -0
  176. package/src/stepper.tsx +85 -0
  177. package/src/switch.stories.tsx +42 -0
  178. package/src/switch.tsx +41 -0
  179. package/src/switcher.stories.tsx +111 -0
  180. package/src/switcher.tsx +131 -0
  181. package/src/system-state.tsx +55 -0
  182. package/src/table.stories.tsx +48 -0
  183. package/src/table.tsx +121 -0
  184. package/src/tabs.stories.tsx +74 -0
  185. package/src/tabs.tsx +90 -0
  186. package/src/tag-input.stories.tsx +67 -0
  187. package/src/tag-input.tsx +117 -0
  188. package/src/tenant-logo.tsx +63 -0
  189. package/src/tenant-switcher.stories.tsx +38 -0
  190. package/src/tenant-switcher.tsx +164 -0
  191. package/src/textarea.stories.tsx +27 -0
  192. package/src/textarea.tsx +18 -0
  193. package/src/theme-mode.ts +52 -0
  194. package/src/time-grid.tsx +163 -0
  195. package/src/time-picker.stories.tsx +62 -0
  196. package/src/time-picker.tsx +615 -0
  197. package/src/toggle-group.tsx +138 -0
  198. package/src/tooltip.stories.tsx +43 -0
  199. package/src/tooltip.tsx +73 -0
  200. package/src/top-nav.stories.tsx +58 -0
  201. package/src/top-nav.tsx +324 -0
  202. package/src/undo-bar.tsx +107 -0
  203. package/src/utils.ts +45 -0
  204. package/src/view-toggle.stories.tsx +84 -0
  205. package/src/view-toggle.tsx +110 -0
@@ -0,0 +1,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
+ 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
+ }
@@ -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 بقيمة $301.60 بتاريخ 14 Oct 2026.")] },
72
+ { id: "a3", type: "heading", level: 1, content: [t("الفواتير المفتوحة")] },
73
+ { id: "a4", type: "number", level: 0, content: [t("INV-1042، بقيمة $301.60")] },
74
+ { id: "a5", type: "number", level: 0, content: [t("INV-1051، بقيمة $96.00")] },
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
+ }