@whity/ui 0.5.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/README.md +107 -107
  2. package/package.json +221 -192
  3. package/src/access-denied.stories.tsx +179 -179
  4. package/src/access-denied.tsx +305 -305
  5. package/src/action-proposal.tsx +338 -302
  6. package/src/actor-chip.tsx +71 -71
  7. package/src/agent-activity.tsx +163 -163
  8. package/src/agent-collaboration.stories.tsx +156 -156
  9. package/src/agent-run-status.tsx +196 -196
  10. package/src/alert-dialog.stories.tsx +45 -45
  11. package/src/alert-dialog.tsx +147 -147
  12. package/src/alert.stories.tsx +91 -91
  13. package/src/alert.tsx +89 -89
  14. package/src/app-sidebar.stories.tsx +195 -195
  15. package/src/app-sidebar.tsx +604 -500
  16. package/src/approval-queue.tsx +109 -108
  17. package/src/audience-group-picker.stories.tsx +198 -198
  18. package/src/audience-group-picker.tsx +362 -362
  19. package/src/audience-people-picker.stories.tsx +102 -102
  20. package/src/audience-people-picker.tsx +279 -279
  21. package/src/avatar-pattern.ts +138 -0
  22. package/src/avatar.tsx +125 -83
  23. package/src/badge.stories.tsx +105 -105
  24. package/src/badge.tsx +68 -68
  25. package/src/beta-warning.stories.tsx +79 -79
  26. package/src/beta-warning.tsx +189 -189
  27. package/src/bilingual-input.stories.tsx +82 -82
  28. package/src/bilingual-input.tsx +188 -183
  29. package/src/board.tsx +216 -0
  30. package/src/breadcrumb.stories.tsx +51 -51
  31. package/src/breadcrumb.tsx +90 -90
  32. package/src/built-on-whity.tsx +52 -0
  33. package/src/button.stories.tsx +87 -87
  34. package/src/button.tsx +123 -118
  35. package/src/card.stories.tsx +423 -423
  36. package/src/card.tsx +180 -180
  37. package/src/chart.stories.tsx +66 -66
  38. package/src/chart.tsx +228 -228
  39. package/src/checkbox.stories.tsx +62 -62
  40. package/src/checkbox.tsx +54 -54
  41. package/src/collapsible.stories.tsx +48 -48
  42. package/src/collapsible.tsx +34 -34
  43. package/src/color-card.stories.tsx +61 -61
  44. package/src/color-card.tsx +270 -270
  45. package/src/color-picker.stories.tsx +65 -65
  46. package/src/color-picker.tsx +425 -425
  47. package/src/column-matcher.tsx +84 -0
  48. package/src/command-palette.tsx +398 -306
  49. package/src/confirm-action.tsx +98 -0
  50. package/src/copy-block.stories.tsx +38 -38
  51. package/src/copy-block.tsx +85 -85
  52. package/src/data-table.stories.tsx +200 -200
  53. package/src/data-table.tsx +1686 -1106
  54. package/src/date-picker.stories.tsx +61 -61
  55. package/src/date-picker.tsx +456 -422
  56. package/src/design-cards.stories.tsx +551 -458
  57. package/src/dialog.stories.tsx +48 -48
  58. package/src/dialog.tsx +179 -166
  59. package/src/display-preferences.tsx +50 -0
  60. package/src/documents/barcode-svg.stories.tsx +72 -72
  61. package/src/documents/barcode-svg.tsx +79 -79
  62. package/src/dropdown-menu.stories.tsx +77 -77
  63. package/src/dropdown-menu.tsx +271 -271
  64. package/src/editor/agent-panel.tsx +222 -0
  65. package/src/editor/agents.stories.tsx +81 -0
  66. package/src/editor/agents.ts +55 -0
  67. package/src/editor/behaviour.ts +279 -279
  68. package/src/editor/block-ids.ts +44 -44
  69. package/src/editor/block-list.tsx +104 -101
  70. package/src/editor/block-toolbar.tsx +490 -477
  71. package/src/editor/bubble.tsx +119 -93
  72. package/src/editor/catalogue.ts +169 -103
  73. package/src/editor/collab-rail.tsx +746 -746
  74. package/src/editor/collab.stories.tsx +207 -207
  75. package/src/editor/collab.ts +87 -87
  76. package/src/editor/contract.ts +476 -408
  77. package/src/editor/convert.ts +414 -362
  78. package/src/editor/doc-editor.stories.tsx +98 -98
  79. package/src/editor/doc-editor.tsx +1492 -1287
  80. package/src/editor/editor.css +636 -536
  81. package/src/editor/extensions.ts +86 -86
  82. package/src/editor/field-bar.tsx +154 -0
  83. package/src/editor/field-picker.tsx +109 -107
  84. package/src/editor/fields.ts +100 -100
  85. package/src/editor/gutter.tsx +443 -367
  86. package/src/editor/index.ts +23 -22
  87. package/src/editor/label-sheet.tsx +289 -289
  88. package/src/editor/labels.ts +394 -310
  89. package/src/editor/link-tools.tsx +141 -141
  90. package/src/editor/node-views.tsx +461 -450
  91. package/src/editor/page-view.tsx +372 -353
  92. package/src/editor/phone.tsx +310 -0
  93. package/src/editor/plugin-block-view.tsx +230 -0
  94. package/src/editor/plugin-blocks.stories.tsx +96 -0
  95. package/src/editor/plugin-blocks.ts +52 -0
  96. package/src/editor/publish-check.tsx +201 -182
  97. package/src/editor/rich-blocks.stories.tsx +146 -146
  98. package/src/editor/sample.ts +40 -40
  99. package/src/editor/save-block-dialog.tsx +141 -0
  100. package/src/editor/saved-block-view.tsx +205 -0
  101. package/src/editor/saved-blocks.stories.tsx +105 -0
  102. package/src/editor/saved-parts.ts +192 -0
  103. package/src/editor/schema-rich.ts +156 -150
  104. package/src/editor/schema.ts +368 -315
  105. package/src/editor/services.tsx +205 -127
  106. package/src/editor/slash.ts +64 -64
  107. package/src/editor/suggesting.ts +372 -372
  108. package/src/editor/suggestions.ts +763 -763
  109. package/src/editor/table-keys.ts +66 -66
  110. package/src/editor/templates.stories.tsx +324 -324
  111. package/src/editor/toolbar.tsx +364 -358
  112. package/src/empty-state.stories.tsx +51 -51
  113. package/src/empty-state.tsx +126 -100
  114. package/src/empty-state.usage-notes.md +30 -30
  115. package/src/form.stories.tsx +112 -112
  116. package/src/form.tsx +176 -176
  117. package/src/globals.css +20 -20
  118. package/src/icon-button.tsx +85 -85
  119. package/src/icon-map.ts +335 -244
  120. package/src/icon.tsx +41 -41
  121. package/src/index.ts +90 -67
  122. package/src/input.stories.tsx +157 -157
  123. package/src/input.tsx +768 -759
  124. package/src/kpi-strip.tsx +47 -0
  125. package/src/localized-text.ts +76 -0
  126. package/src/locked-screen.stories.tsx +29 -29
  127. package/src/locked-screen.tsx +75 -61
  128. package/src/lockup.tsx +42 -0
  129. package/src/lottie.tsx +441 -0
  130. package/src/mark.tsx +62 -62
  131. package/src/math-text.stories.tsx +33 -33
  132. package/src/math-text.tsx +43 -43
  133. package/src/menu-bar.stories.tsx +198 -198
  134. package/src/menu-bar.tsx +373 -373
  135. package/src/message-thread.tsx +181 -0
  136. package/src/money/currency.ts +244 -230
  137. package/src/money/money-input.tsx +131 -0
  138. package/src/money/money.tsx +33 -0
  139. package/src/open-on-computer.tsx +150 -0
  140. package/src/otp-input.tsx +161 -0
  141. package/src/page-header.stories.tsx +95 -95
  142. package/src/page-header.tsx +122 -122
  143. package/src/page-shell.stories.tsx +42 -42
  144. package/src/page-shell.tsx +32 -32
  145. package/src/page-states.tsx +277 -0
  146. package/src/pagination.stories.tsx +45 -45
  147. package/src/pagination.tsx +98 -98
  148. package/src/paper-sheet.stories.tsx +88 -0
  149. package/src/paper-sheet.tsx +85 -0
  150. package/src/phone-tab-bar.tsx +152 -0
  151. package/src/plugin-card.stories.tsx +208 -180
  152. package/src/plugin-card.tsx +931 -848
  153. package/src/popover.stories.tsx +48 -48
  154. package/src/popover.tsx +55 -55
  155. package/src/radio-group.stories.tsx +26 -26
  156. package/src/radio-group.tsx +44 -44
  157. package/src/route-flow/editor.tsx +1087 -1014
  158. package/src/route-flow/model.ts +771 -716
  159. package/src/save-bar.tsx +94 -0
  160. package/src/scope-grant.tsx +139 -139
  161. package/src/screen-too-small.stories.tsx +61 -61
  162. package/src/screen-too-small.tsx +104 -104
  163. package/src/search-field.tsx +65 -0
  164. package/src/select.stories.tsx +44 -44
  165. package/src/select.tsx +197 -197
  166. package/src/separator.stories.tsx +34 -34
  167. package/src/separator.tsx +34 -34
  168. package/src/sheet.stories.tsx +49 -49
  169. package/src/sheet.tsx +168 -168
  170. package/src/skeleton.stories.tsx +28 -28
  171. package/src/skeleton.tsx +19 -19
  172. package/src/slider.stories.tsx +78 -78
  173. package/src/slider.tsx +300 -300
  174. package/src/spinner.stories.tsx +33 -33
  175. package/src/spinner.tsx +55 -55
  176. package/src/stepper.tsx +85 -0
  177. package/src/switch.stories.tsx +42 -42
  178. package/src/switch.tsx +41 -41
  179. package/src/switcher.stories.tsx +111 -111
  180. package/src/switcher.tsx +131 -131
  181. package/src/system-state.tsx +55 -0
  182. package/src/table.stories.tsx +48 -48
  183. package/src/table.tsx +121 -121
  184. package/src/tabs.stories.tsx +74 -74
  185. package/src/tabs.tsx +90 -90
  186. package/src/tag-input.stories.tsx +67 -67
  187. package/src/tag-input.tsx +117 -117
  188. package/src/tenant-logo.tsx +63 -63
  189. package/src/tenant-switcher.stories.tsx +38 -38
  190. package/src/tenant-switcher.tsx +164 -164
  191. package/src/textarea.stories.tsx +27 -27
  192. package/src/textarea.tsx +18 -18
  193. package/src/theme-mode.ts +52 -52
  194. package/src/time-grid.tsx +163 -0
  195. package/src/time-picker.stories.tsx +62 -62
  196. package/src/time-picker.tsx +615 -615
  197. package/src/toggle-group.tsx +138 -138
  198. package/src/tooltip.stories.tsx +43 -43
  199. package/src/tooltip.tsx +73 -73
  200. package/src/top-nav.stories.tsx +58 -58
  201. package/src/top-nav.tsx +324 -324
  202. package/src/undo-bar.tsx +107 -107
  203. package/src/utils.ts +45 -45
  204. package/src/view-toggle.stories.tsx +84 -84
  205. package/src/view-toggle.tsx +110 -110
@@ -1,324 +1,324 @@
1
- import type { Meta, StoryObj } from "@storybook/react-vite"
2
- import * as React from "react"
3
-
4
- import type { Block, FieldRef, PublishBlocker, ResolvedThemeTokens, WhityDocument } from "./contract"
5
- import { DocEditor } from "./doc-editor"
6
- import type { FieldGroup } from "./fields"
7
- import { themedLetterhead, type Letterhead } from "./page-view"
8
- import { SAMPLE_DOCUMENT } from "./sample"
9
- import type { FieldPreview } from "./services"
10
-
11
- const meta = {
12
- title: "Editor/Templates",
13
- parameters: { layout: "fullscreen", fullBleed: true },
14
- } satisfies Meta
15
-
16
- export default meta
17
- type Story = StoryObj<typeof meta>
18
-
19
- const t = (text: string) => ({ type: "text" as const, text })
20
- const f = (path: string, label: string, required?: boolean): FieldRef => ({ type: "field", path, label, ...(required ? { required } : {}) })
21
- const wait = <T,>(value: T, ms = 500) => new Promise<T>((done) => setTimeout(() => done(value), ms))
22
-
23
- /** What an invoice template's record type offers (EditorInline). */
24
- const FIELDS: FieldGroup[] = [
25
- {
26
- group: "Customer",
27
- items: [
28
- { path: "customer.name", label: "Customer name", sample: "Example Clinic" },
29
- { path: "customer.tax_number", label: "Customer tax number", sample: "0987654" },
30
- ],
31
- },
32
- {
33
- group: "Invoice",
34
- items: [
35
- { path: "invoice.number", label: "Invoice number", sample: "INV-1042", required: true },
36
- { path: "invoice.total", label: "Invoice total", sample: "JOD 301.600", required: true },
37
- { path: "invoice.due_date", label: "Due date", sample: "14 Oct 2026" },
38
- { path: "invoice.days_overdue", label: "Days overdue", sample: "15" },
39
- ],
40
- },
41
- {
42
- group: "Company",
43
- items: [
44
- { path: "company.name", label: "Company name", sample: "Example Bakery" },
45
- { path: "company.iban", label: "Company IBAN", sample: "JO94 CBJO 0010 0000 0000 0131 0003 02" },
46
- { path: "sender.name", label: "Sender name", sample: "Rana Haddad" },
47
- ],
48
- },
49
- { group: "Today", items: [{ path: "today", label: "Today", sample: "29 October 2026" }] },
50
- ]
51
-
52
- const LETTERHEAD: Letterhead = {
53
- name: "Example Bakery",
54
- lines: ["Mecca Street 112, Amman · Tax no. 0123456", "+962 6 555 0100 · accounts@bakery.example.jo"],
55
- }
56
-
57
- const BLOCKS: Block[] = [
58
- { id: "t1", type: "paragraph", content: [t("Dear "), f("customer.name", "Customer name"), t(",")] },
59
- {
60
- id: "t2",
61
- type: "paragraph",
62
- content: [
63
- t("Invoice "),
64
- f("invoice.number", "Invoice number", true),
65
- t(" for "),
66
- f("invoice.total", "Invoice total", true),
67
- t(" was due on "),
68
- f("invoice.due_date", "Due date"),
69
- t(". Please pay within 14 days of this letter."),
70
- ],
71
- },
72
- {
73
- id: "t3",
74
- type: "callout",
75
- kind: "note",
76
- content: [
77
- t("Pay by bank transfer to "),
78
- { type: "text", text: "Example Bank", marks: [{ type: "bold" }] },
79
- t(", IBAN "),
80
- f("company.iban", "Company IBAN"),
81
- t(", with the invoice number as the reference."),
82
- ],
83
- },
84
- { id: "t4", type: "heading", level: 2, content: [t("Open invoices")] },
85
- {
86
- id: "t5",
87
- type: "table",
88
- headerRow: true,
89
- columns: [{ align: "start" }, { align: "start" }, { align: "end" }],
90
- rows: [
91
- [[t("Invoice")], [t("Due")], [t("Amount")]],
92
- [[t("INV-1042")], [t("14 Oct 2026")], [t("JOD 301.600")]],
93
- [[t("INV-1051")], [t("21 Oct 2026")], [t("JOD 96.000")]],
94
- ],
95
- },
96
- { id: "t6", type: "paragraph", content: [t("Customer tax number: "), f("customer.tax_number", "Customer tax number")] },
97
- { id: "t7", type: "paragraph", content: [t("Kind regards,")] },
98
- { id: "t8", type: "paragraph", content: [f("sender.name", "Sender name"), { type: "break" }, f("company.name", "Company name")] },
99
- ]
100
-
101
- /** INV-1042's values; Example Clinic has no tax number on file (E.27). */
102
- const PREVIEW: FieldPreview = {
103
- label: "INV-1042 · Example Clinic",
104
- values: {
105
- "customer.name": "Example Clinic",
106
- "customer.tax_number": null,
107
- "invoice.number": "INV-1042",
108
- "invoice.total": "JOD 301.600",
109
- "invoice.due_date": "14 Oct 2026",
110
- "invoice.days_overdue": "15",
111
- "company.name": "Example Bakery",
112
- "company.iban": "JO94 CBJO 0010 0000 0000 0131 0003 02",
113
- "sender.name": "Rana Haddad",
114
- today: "29 October 2026",
115
- },
116
- }
117
-
118
- const TEMPLATE: WhityDocument = {
119
- ...SAMPLE_DOCUMENT,
120
- id: "tpl-payment-reminder",
121
- kind: "template",
122
- title: "Payment reminder",
123
- defaultView: "page",
124
- recordType: "invoice",
125
- page: { size: "A4", marginMm: 16, letterhead: true },
126
- status: "draft",
127
- publishedVersion: 3,
128
- blocks: BLOCKS,
129
- }
130
-
131
- function Live({
132
- initial,
133
- blockers = [],
134
- startPreview = false,
135
- theme = null,
136
- letterhead = LETTERHEAD,
137
- }: {
138
- initial: WhityDocument
139
- blockers?: PublishBlocker[]
140
- startPreview?: boolean
141
- theme?: ResolvedThemeTokens | null
142
- letterhead?: Letterhead
143
- }) {
144
- const [doc, setDoc] = React.useState(initial)
145
- const [preview, setPreview] = React.useState<FieldPreview | null>(startPreview ? PREVIEW : null)
146
- const [version, setVersion] = React.useState<number | null>(initial.publishedVersion ?? null)
147
- return (
148
- <div style={{ height: "100vh" }}>
149
- <DocEditor
150
- document={doc}
151
- onChange={setDoc}
152
- canEdit
153
- back={{ label: "Back to templates", onClick: () => {} }}
154
- path="Templates · Letters"
155
- save={{ state: "saved", at: "14:02" }}
156
- fields={FIELDS}
157
- letterhead={letterhead}
158
- theme={theme}
159
- pageFields={{ reference: FIELDS[1].items[0], date: FIELDS[3].items[0] }}
160
- preview={preview}
161
- onPreview={() => setPreview(PREVIEW)}
162
- onClosePreview={() => setPreview(null)}
163
- onExportPdf={() => wait(undefined)}
164
- publish={{
165
- publishedVersion: version,
166
- recordLabel: "payment reminder",
167
- check: () => wait(blockers),
168
- publish: () => wait(undefined).then(() => setVersion((v) => (v ?? 0) + 1)),
169
- }}
170
- />
171
- </div>
172
- )
173
- }
174
-
175
- /** A33: the template in the page view, with fields as chips. Type {{ or use Field ▾. */
176
- export const Designer: Story = { render: () => <Live initial={TEMPLATE} /> }
177
-
178
- /** E.12: the pages as they will print with INV-1042, read-only, a missing value flagged. */
179
- export const PreviewWithRecord: Story = { render: () => <Live initial={TEMPLATE} startPreview /> }
180
-
181
- /** E.37: Publish opens the check; it stays disabled while anything blocks it. */
182
- export const PublishBlocked: Story = {
183
- render: () => (
184
- <Live
185
- initial={TEMPLATE}
186
- blockers={[
187
- { kind: "open-suggestions", count: 2 },
188
- { kind: "unknown-field", path: "customer.vat" },
189
- { kind: "theme-unavailable", ref: "tenant.autumn" },
190
- ]}
191
- />
192
- ),
193
- }
194
-
195
- /**
196
- * E.44 and E.45: a workspace theme as the server resolves it, with the print
197
- * colour on the title and H1-H3, the phone and address lines in that order,
198
- * and no logo (the name alone).
199
- */
200
- const THEME: ResolvedThemeTokens = {
201
- accent: "#8A3D12",
202
- accentOnDark: "#E7A27A",
203
- accentUse: "title-and-headings",
204
- letterhead: { logo: "none", lines: ["phone", "address"] },
205
- }
206
-
207
- export const Themed: Story = {
208
- render: () => (
209
- <Live
210
- initial={TEMPLATE}
211
- theme={THEME}
212
- letterhead={themedLetterhead(
213
- LETTERHEAD,
214
- THEME,
215
- { address: "Mecca Street 112, Amman", taxId: "Tax no. 0123456", phone: "+962 6 555 0101", email: null },
216
- { wide: null, square: null }
217
- )}
218
- />
219
- ),
220
- }
221
-
222
- /** E.11: the continuous sheet follows the app theme; in dark mode its title takes `accentOnDark`. */
223
- export const ThemedContinuous: Story = {
224
- render: () => <Live initial={{ ...TEMPLATE, defaultView: "continuous" }} theme={THEME} />,
225
- }
226
-
227
- /** Long enough for the running header, a page break and "Page x of y". */
228
- export const TwoPages: Story = {
229
- render: () => (
230
- <Live
231
- initial={{
232
- ...TEMPLATE,
233
- blocks: [
234
- ...BLOCKS,
235
- { id: "pb", type: "pageBreak" },
236
- { id: "n1", type: "heading", level: 2, content: [t("Terms")] },
237
- ...Array.from({ length: 6 }, (_, i): Block => ({
238
- id: `n${i + 2}`,
239
- type: "paragraph",
240
- content: [t("Payments are due within 30 days of the invoice date. Late payments may be charged a fee as agreed in the contract, and goods stay ours until paid in full.")],
241
- })),
242
- ],
243
- }}
244
- />
245
- ),
246
- }
247
-
248
- export const Arabic: Story = {
249
- globals: { direction: "rtl" },
250
- render: () => <Live initial={{ ...TEMPLATE, lang: "ar", title: "تذكير بالدفع" }} />,
251
- }
252
-
253
- const LABELS: WhityDocument = {
254
- ...SAMPLE_DOCUMENT,
255
- id: "tpl-shipping-labels",
256
- kind: "label",
257
- title: "Shipping labels",
258
- defaultView: "page",
259
- page: { size: "A4", marginMm: 16, letterhead: false },
260
- labels: { columns: 2, rows: 4, size: { widthMm: 99.1, heightMm: 67.7 }, gapMm: 2.5, gapYMm: 0 },
261
- ownFields: [
262
- { key: "ship_to", label: "Ship to", sample: "Example Clinic" },
263
- { key: "address", label: "Address", sample: "Rainbow Street 21, Amman" },
264
- { key: "tracking", label: "Tracking number", sample: "TRK000123456" },
265
- ],
266
- status: "draft",
267
- blocks: [
268
- { id: "l1", type: "paragraph", content: [t("From "), f("company.name", "Company name")] },
269
- {
270
- id: "l2",
271
- type: "paragraph",
272
- content: [{ ...f("template.ship_to", "Ship to"), marks: [{ type: "bold" }] }, { type: "break" }, f("template.address", "Address")],
273
- },
274
- { id: "l3", type: "barcode", symbology: "code128", value: [f("template.tracking", "Tracking number")], showText: true, heightMm: 12 },
275
- ],
276
- }
277
-
278
- /**
279
- * E.43: a label sheet. The first label is edited and the others repeat it;
280
- * the stock is set beside the zoom; Print labels replaces PDF. Ship to,
281
- * Address and Tracking number are the template's own fields (Field ▾ → This template).
282
- */
283
- export const LabelSheet: Story = { render: () => <LabelLive initial={LABELS} /> }
284
-
285
- function LabelLive({ initial }: { initial: WhityDocument }) {
286
- const [doc, setDoc] = React.useState(initial)
287
- return (
288
- <div style={{ height: "100vh" }}>
289
- <DocEditor
290
- document={doc}
291
- onChange={setDoc}
292
- canEdit
293
- back={{ label: "Back to templates", onClick: () => {} }}
294
- path="Templates · Labels"
295
- save={{ state: "saved", at: "14:02" }}
296
- fields={[FIELDS[2]]}
297
- onPrintLabels={() => {}}
298
- publish={{ publishedVersion: null, check: () => wait([]), publish: () => wait(undefined) }}
299
- />
300
- </div>
301
- )
302
- }
303
-
304
- /** A label whose content runs past its edge is flagged; on paper it would be cut. */
305
- export const LabelOverflow: Story = {
306
- render: () => (
307
- <div style={{ height: "100vh" }}>
308
- <DocEditor
309
- document={{
310
- ...LABELS,
311
- labels: { columns: 3, rows: 8, size: { widthMm: 63.5, heightMm: 33.9 }, gapMm: 2.5, gapYMm: 0 },
312
- blocks: [
313
- ...LABELS.blocks,
314
- { id: "l4", type: "paragraph", content: [t("Handle with care. Keep upright and away from heat.")] },
315
- ],
316
- }}
317
- onChange={() => {}}
318
- canEdit
319
- fields={[FIELDS[2]]}
320
- onPrintLabels={() => {}}
321
- />
322
- </div>
323
- ),
324
- }
1
+ import type { Meta, StoryObj } from "@storybook/react-vite"
2
+ import * as React from "react"
3
+
4
+ import type { Block, FieldRef, PublishBlocker, ResolvedThemeTokens, WhityDocument } from "./contract"
5
+ import { DocEditor } from "./doc-editor"
6
+ import type { FieldGroup } from "./fields"
7
+ import { themedLetterhead, type Letterhead } from "./page-view"
8
+ import { SAMPLE_DOCUMENT } from "./sample"
9
+ import type { FieldPreview } from "./services"
10
+
11
+ const meta = {
12
+ title: "Editor/Templates",
13
+ parameters: { layout: "fullscreen", fullBleed: true },
14
+ } satisfies Meta
15
+
16
+ export default meta
17
+ type Story = StoryObj<typeof meta>
18
+
19
+ const t = (text: string) => ({ type: "text" as const, text })
20
+ const f = (path: string, label: string, required?: boolean): FieldRef => ({ type: "field", path, label, ...(required ? { required } : {}) })
21
+ const wait = <T,>(value: T, ms = 500) => new Promise<T>((done) => setTimeout(() => done(value), ms))
22
+
23
+ /** What an invoice template's record type offers (EditorInline). */
24
+ const FIELDS: FieldGroup[] = [
25
+ {
26
+ group: "Customer",
27
+ items: [
28
+ { path: "customer.name", label: "Customer name", sample: "Example Clinic" },
29
+ { path: "customer.tax_number", label: "Customer tax number", sample: "0987654" },
30
+ ],
31
+ },
32
+ {
33
+ group: "Invoice",
34
+ items: [
35
+ { path: "invoice.number", label: "Invoice number", sample: "INV-1042", required: true },
36
+ { path: "invoice.total", label: "Invoice total", sample: "$301.60", required: true },
37
+ { path: "invoice.due_date", label: "Due date", sample: "14 Oct 2026" },
38
+ { path: "invoice.days_overdue", label: "Days overdue", sample: "15" },
39
+ ],
40
+ },
41
+ {
42
+ group: "Company",
43
+ items: [
44
+ { path: "company.name", label: "Company name", sample: "Example Bakery" },
45
+ { path: "company.iban", label: "Company IBAN", sample: "JO94 CBJO 0010 0000 0000 0131 0003 02" },
46
+ { path: "sender.name", label: "Sender name", sample: "Rana Haddad" },
47
+ ],
48
+ },
49
+ { group: "Today", items: [{ path: "today", label: "Today", sample: "29 October 2026" }] },
50
+ ]
51
+
52
+ const LETTERHEAD: Letterhead = {
53
+ name: "Example Bakery",
54
+ lines: ["Mecca Street 112, Amman · Tax no. 0123456", "+962 6 555 0100 · accounts@bakery.example.jo"],
55
+ }
56
+
57
+ const BLOCKS: Block[] = [
58
+ { id: "t1", type: "paragraph", content: [t("Dear "), f("customer.name", "Customer name"), t(",")] },
59
+ {
60
+ id: "t2",
61
+ type: "paragraph",
62
+ content: [
63
+ t("Invoice "),
64
+ f("invoice.number", "Invoice number", true),
65
+ t(" for "),
66
+ f("invoice.total", "Invoice total", true),
67
+ t(" was due on "),
68
+ f("invoice.due_date", "Due date"),
69
+ t(". Please pay within 14 days of this letter."),
70
+ ],
71
+ },
72
+ {
73
+ id: "t3",
74
+ type: "callout",
75
+ kind: "note",
76
+ content: [
77
+ t("Pay by bank transfer to "),
78
+ { type: "text", text: "Example Bank", marks: [{ type: "bold" }] },
79
+ t(", IBAN "),
80
+ f("company.iban", "Company IBAN"),
81
+ t(", with the invoice number as the reference."),
82
+ ],
83
+ },
84
+ { id: "t4", type: "heading", level: 2, content: [t("Open invoices")] },
85
+ {
86
+ id: "t5",
87
+ type: "table",
88
+ headerRow: true,
89
+ columns: [{ align: "start" }, { align: "start" }, { align: "end" }],
90
+ rows: [
91
+ [[t("Invoice")], [t("Due")], [t("Amount")]],
92
+ [[t("INV-1042")], [t("14 Oct 2026")], [t("$301.60")]],
93
+ [[t("INV-1051")], [t("21 Oct 2026")], [t("$96.00")]],
94
+ ],
95
+ },
96
+ { id: "t6", type: "paragraph", content: [t("Customer tax number: "), f("customer.tax_number", "Customer tax number")] },
97
+ { id: "t7", type: "paragraph", content: [t("Kind regards,")] },
98
+ { id: "t8", type: "paragraph", content: [f("sender.name", "Sender name"), { type: "break" }, f("company.name", "Company name")] },
99
+ ]
100
+
101
+ /** INV-1042's values; Example Clinic has no tax number on file (E.27). */
102
+ const PREVIEW: FieldPreview = {
103
+ label: "INV-1042 · Example Clinic",
104
+ values: {
105
+ "customer.name": "Example Clinic",
106
+ "customer.tax_number": null,
107
+ "invoice.number": "INV-1042",
108
+ "invoice.total": "$301.60",
109
+ "invoice.due_date": "14 Oct 2026",
110
+ "invoice.days_overdue": "15",
111
+ "company.name": "Example Bakery",
112
+ "company.iban": "JO94 CBJO 0010 0000 0000 0131 0003 02",
113
+ "sender.name": "Rana Haddad",
114
+ today: "29 October 2026",
115
+ },
116
+ }
117
+
118
+ const TEMPLATE: WhityDocument = {
119
+ ...SAMPLE_DOCUMENT,
120
+ id: "tpl-payment-reminder",
121
+ kind: "template",
122
+ title: "Payment reminder",
123
+ defaultView: "page",
124
+ recordType: "invoice",
125
+ page: { size: "A4", marginMm: 16, letterhead: true },
126
+ status: "draft",
127
+ publishedVersion: 3,
128
+ blocks: BLOCKS,
129
+ }
130
+
131
+ function Live({
132
+ initial,
133
+ blockers = [],
134
+ startPreview = false,
135
+ theme = null,
136
+ letterhead = LETTERHEAD,
137
+ }: {
138
+ initial: WhityDocument
139
+ blockers?: PublishBlocker[]
140
+ startPreview?: boolean
141
+ theme?: ResolvedThemeTokens | null
142
+ letterhead?: Letterhead
143
+ }) {
144
+ const [doc, setDoc] = React.useState(initial)
145
+ const [preview, setPreview] = React.useState<FieldPreview | null>(startPreview ? PREVIEW : null)
146
+ const [version, setVersion] = React.useState<number | null>(initial.publishedVersion ?? null)
147
+ return (
148
+ <div style={{ height: "100vh" }}>
149
+ <DocEditor
150
+ document={doc}
151
+ onChange={setDoc}
152
+ canEdit
153
+ back={{ label: "Back to templates", onClick: () => {} }}
154
+ path="Templates · Letters"
155
+ save={{ state: "saved", at: "14:02" }}
156
+ fields={FIELDS}
157
+ letterhead={letterhead}
158
+ theme={theme}
159
+ pageFields={{ reference: FIELDS[1].items[0], date: FIELDS[3].items[0] }}
160
+ preview={preview}
161
+ onPreview={() => setPreview(PREVIEW)}
162
+ onClosePreview={() => setPreview(null)}
163
+ onExportPdf={() => wait(undefined)}
164
+ publish={{
165
+ publishedVersion: version,
166
+ recordLabel: "payment reminder",
167
+ check: () => wait(blockers),
168
+ publish: () => wait(undefined).then(() => setVersion((v) => (v ?? 0) + 1)),
169
+ }}
170
+ />
171
+ </div>
172
+ )
173
+ }
174
+
175
+ /** A33: the template in the page view, with fields as chips. Type {{ or use Field ▾. */
176
+ export const Designer: Story = { render: () => <Live initial={TEMPLATE} /> }
177
+
178
+ /** E.12: the pages as they will print with INV-1042, read-only, a missing value flagged. */
179
+ export const PreviewWithRecord: Story = { render: () => <Live initial={TEMPLATE} startPreview /> }
180
+
181
+ /** E.37: Publish opens the check; it stays disabled while anything blocks it. */
182
+ export const PublishBlocked: Story = {
183
+ render: () => (
184
+ <Live
185
+ initial={TEMPLATE}
186
+ blockers={[
187
+ { kind: "open-suggestions", count: 2 },
188
+ { kind: "unknown-field", path: "customer.vat" },
189
+ { kind: "theme-unavailable", ref: "tenant.autumn" },
190
+ ]}
191
+ />
192
+ ),
193
+ }
194
+
195
+ /**
196
+ * E.44 and E.45: a workspace theme as the server resolves it, with the print
197
+ * colour on the title and H1-H3, the phone and address lines in that order,
198
+ * and no logo (the name alone).
199
+ */
200
+ const THEME: ResolvedThemeTokens = {
201
+ accent: "#8A3D12",
202
+ accentOnDark: "#E7A27A",
203
+ accentUse: "title-and-headings",
204
+ letterhead: { logo: "none", lines: ["phone", "address"] },
205
+ }
206
+
207
+ export const Themed: Story = {
208
+ render: () => (
209
+ <Live
210
+ initial={TEMPLATE}
211
+ theme={THEME}
212
+ letterhead={themedLetterhead(
213
+ LETTERHEAD,
214
+ THEME,
215
+ { address: "Mecca Street 112, Amman", taxId: "Tax no. 0123456", phone: "+962 6 555 0101", email: null },
216
+ { wide: null, square: null }
217
+ )}
218
+ />
219
+ ),
220
+ }
221
+
222
+ /** E.11: the continuous sheet follows the app theme; in dark mode its title takes `accentOnDark`. */
223
+ export const ThemedContinuous: Story = {
224
+ render: () => <Live initial={{ ...TEMPLATE, defaultView: "continuous" }} theme={THEME} />,
225
+ }
226
+
227
+ /** Long enough for the running header, a page break and "Page x of y". */
228
+ export const TwoPages: Story = {
229
+ render: () => (
230
+ <Live
231
+ initial={{
232
+ ...TEMPLATE,
233
+ blocks: [
234
+ ...BLOCKS,
235
+ { id: "pb", type: "pageBreak" },
236
+ { id: "n1", type: "heading", level: 2, content: [t("Terms")] },
237
+ ...Array.from({ length: 6 }, (_, i): Block => ({
238
+ id: `n${i + 2}`,
239
+ type: "paragraph",
240
+ content: [t("Payments are due within 30 days of the invoice date. Late payments may be charged a fee as agreed in the contract, and goods stay ours until paid in full.")],
241
+ })),
242
+ ],
243
+ }}
244
+ />
245
+ ),
246
+ }
247
+
248
+ export const Arabic: Story = {
249
+ globals: { direction: "rtl" },
250
+ render: () => <Live initial={{ ...TEMPLATE, lang: "ar", title: "تذكير بالدفع" }} />,
251
+ }
252
+
253
+ const LABELS: WhityDocument = {
254
+ ...SAMPLE_DOCUMENT,
255
+ id: "tpl-shipping-labels",
256
+ kind: "label",
257
+ title: "Shipping labels",
258
+ defaultView: "page",
259
+ page: { size: "A4", marginMm: 16, letterhead: false },
260
+ labels: { columns: 2, rows: 4, size: { widthMm: 99.1, heightMm: 67.7 }, gapMm: 2.5, gapYMm: 0 },
261
+ ownFields: [
262
+ { key: "ship_to", label: "Ship to", sample: "Example Clinic" },
263
+ { key: "address", label: "Address", sample: "Rainbow Street 21, Amman" },
264
+ { key: "tracking", label: "Tracking number", sample: "TRK000123456" },
265
+ ],
266
+ status: "draft",
267
+ blocks: [
268
+ { id: "l1", type: "paragraph", content: [t("From "), f("company.name", "Company name")] },
269
+ {
270
+ id: "l2",
271
+ type: "paragraph",
272
+ content: [{ ...f("template.ship_to", "Ship to"), marks: [{ type: "bold" }] }, { type: "break" }, f("template.address", "Address")],
273
+ },
274
+ { id: "l3", type: "barcode", symbology: "code128", value: [f("template.tracking", "Tracking number")], showText: true, heightMm: 12 },
275
+ ],
276
+ }
277
+
278
+ /**
279
+ * E.43: a label sheet. The first label is edited and the others repeat it;
280
+ * the stock is set beside the zoom; Print labels replaces PDF. Ship to,
281
+ * Address and Tracking number are the template's own fields (Field ▾ → This template).
282
+ */
283
+ export const LabelSheet: Story = { render: () => <LabelLive initial={LABELS} /> }
284
+
285
+ function LabelLive({ initial }: { initial: WhityDocument }) {
286
+ const [doc, setDoc] = React.useState(initial)
287
+ return (
288
+ <div style={{ height: "100vh" }}>
289
+ <DocEditor
290
+ document={doc}
291
+ onChange={setDoc}
292
+ canEdit
293
+ back={{ label: "Back to templates", onClick: () => {} }}
294
+ path="Templates · Labels"
295
+ save={{ state: "saved", at: "14:02" }}
296
+ fields={[FIELDS[2]]}
297
+ onPrintLabels={() => {}}
298
+ publish={{ publishedVersion: null, check: () => wait([]), publish: () => wait(undefined) }}
299
+ />
300
+ </div>
301
+ )
302
+ }
303
+
304
+ /** A label whose content runs past its edge is flagged; on paper it would be cut. */
305
+ export const LabelOverflow: Story = {
306
+ render: () => (
307
+ <div style={{ height: "100vh" }}>
308
+ <DocEditor
309
+ document={{
310
+ ...LABELS,
311
+ labels: { columns: 3, rows: 8, size: { widthMm: 63.5, heightMm: 33.9 }, gapMm: 2.5, gapYMm: 0 },
312
+ blocks: [
313
+ ...LABELS.blocks,
314
+ { id: "l4", type: "paragraph", content: [t("Handle with care. Keep upright and away from heat.")] },
315
+ ],
316
+ }}
317
+ onChange={() => {}}
318
+ canEdit
319
+ fields={[FIELDS[2]]}
320
+ onPrintLabels={() => {}}
321
+ />
322
+ </div>
323
+ ),
324
+ }