@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,22 +1,23 @@
1
- export * from "./contract"
2
- export { DocEditor, type DocEditorProps, type SaveState } from "./doc-editor"
3
- export { EDITOR_LABELS, type EditorLabels } from "./labels"
4
- export { fromEditorContent, normalizeBlocks, toEditorContent } from "./convert"
5
- export { documentExtensions } from "./extensions"
6
- export { EditorServicesProvider, type EditorServices, type RecordView, type ChartData, type UploadedFile } from "./services"
7
- export { type FieldDef, type FieldGroup } from "./fields"
8
- export { type Letterhead } from "./page-view"
9
- export { type FieldPreview } from "./services"
10
- export * from "./collab"
11
- export {
12
- collectPatches,
13
- renderSuggestions,
14
- stripSuggestions,
15
- suggestEdit,
16
- summarize,
17
- type FormatEdit,
18
- type LocalChange,
19
- type PatchedSuggestion,
20
- type SuggestionKind,
21
- type SuggestionPatch,
22
- } from "./suggestions"
1
+ export * from "./contract"
2
+ export { DocEditor, type DocEditorProps, type SaveState } from "./doc-editor"
3
+ export { EDITOR_LABELS, type EditorLabels } from "./labels"
4
+ export { fromEditorContent, normalizeBlocks, toEditorContent } from "./convert"
5
+ export { documentExtensions } from "./extensions"
6
+ export { EditorServicesProvider, type EditorServices, type RecordView, type ChartData, type UploadedFile } from "./services"
7
+ export { type FieldDef, type FieldGroup } from "./fields"
8
+ export { type Letterhead } from "./page-view"
9
+ export { type FieldPreview } from "./services"
10
+ export * from "./collab"
11
+ export * from "./agents"
12
+ export {
13
+ collectPatches,
14
+ renderSuggestions,
15
+ stripSuggestions,
16
+ suggestEdit,
17
+ summarize,
18
+ type FormatEdit,
19
+ type LocalChange,
20
+ type PatchedSuggestion,
21
+ type SuggestionKind,
22
+ type SuggestionPatch,
23
+ } from "./suggestions"
@@ -1,289 +1,289 @@
1
- "use client"
2
-
3
- import * as React from "react"
4
- import type { Editor } from "@tiptap/core"
5
-
6
- import { Icon } from "../icon"
7
- import { Input } from "../input"
8
- import { Popover, PopoverContent, PopoverTrigger } from "../popover"
9
- import { ToggleGroup } from "../toggle-group"
10
- import { cn } from "../utils"
11
- import type { LabelGrid, ResolvedThemeTokens } from "./contract"
12
- import { fill, type EditorLabels } from "./labels"
13
- import { themeStyle } from "./page-view"
14
-
15
- /** 96 dpi, as the page view (EditorPageView). */
16
- const PX_PER_MM = 96 / 25.4
17
-
18
- /** The sheets a label document prints on (E.43): A4 or Letter, portrait. */
19
- export const SHEETS = {
20
- A4: { widthMm: 210, heightMm: 297 },
21
- Letter: { widthMm: 215.9, heightMm: 279.4 },
22
- } as const
23
- export type SheetSize = keyof typeof SHEETS
24
-
25
- /**
26
- * Common label stock. The grid is centred on the sheet, which matches these
27
- * sheets' own margins to within half a millimetre. A certificate is a
28
- * one-label sheet.
29
- */
30
- export const LABEL_PRESETS: { id: string; name: string; sheet: SheetSize; grid: LabelGrid }[] = [
31
- { id: "avery-l7160", name: "Avery L7160", sheet: "A4", grid: { columns: 3, rows: 7, size: { widthMm: 63.5, heightMm: 38.1 }, gapMm: 2.5, gapYMm: 0 } },
32
- { id: "avery-l7159", name: "Avery L7159", sheet: "A4", grid: { columns: 3, rows: 8, size: { widthMm: 63.5, heightMm: 33.9 }, gapMm: 2.5, gapYMm: 0 } },
33
- { id: "avery-l7165", name: "Avery L7165", sheet: "A4", grid: { columns: 2, rows: 4, size: { widthMm: 99.1, heightMm: 67.7 }, gapMm: 2.5, gapYMm: 0 } },
34
- { id: "avery-l7651", name: "Avery L7651", sheet: "A4", grid: { columns: 5, rows: 13, size: { widthMm: 38.1, heightMm: 21.2 }, gapMm: 2.5, gapYMm: 0 } },
35
- { id: "letter-2x5", name: "Letter 2 × 5", sheet: "Letter", grid: { columns: 2, rows: 5, size: { widthMm: 101.6, heightMm: 50.8 }, gapMm: 4.8, gapYMm: 0 } },
36
- { id: "certificate", name: "Certificate", sheet: "A4", grid: { columns: 1, rows: 1, size: { widthMm: 190, heightMm: 277 }, gapMm: 0 } },
37
- ]
38
-
39
- export const DEFAULT_LABEL_GRID: LabelGrid = LABEL_PRESETS[0].grid
40
-
41
- /** Where each label sits on the sheet, in mm from the top start corner. */
42
- export function labelCells(grid: LabelGrid, sheet: SheetSize): { x: number; y: number }[] {
43
- const { widthMm, heightMm } = SHEETS[sheet]
44
- const gapX = grid.gapMm
45
- const gapY = grid.gapYMm ?? grid.gapMm
46
- const left = (widthMm - grid.columns * grid.size.widthMm - (grid.columns - 1) * gapX) / 2
47
- const top = (heightMm - grid.rows * grid.size.heightMm - (grid.rows - 1) * gapY) / 2
48
- const cells: { x: number; y: number }[] = []
49
- for (let r = 0; r < grid.rows; r++) {
50
- for (let c = 0; c < grid.columns; c++) {
51
- cells.push({ x: left + c * (grid.size.widthMm + gapX), y: top + r * (grid.size.heightMm + gapY) })
52
- }
53
- }
54
- return cells
55
- }
56
-
57
- /** Whether the labels fit on the sheet. */
58
- export function gridFits(grid: LabelGrid, sheet: SheetSize): boolean {
59
- const cells = labelCells(grid, sheet)
60
- const last = cells[cells.length - 1]
61
- const { widthMm, heightMm } = SHEETS[sheet]
62
- return !!last && cells[0].x >= 0 && cells[0].y >= 0 && last.x + grid.size.widthMm <= widthMm + 0.01 && last.y + grid.size.heightMm <= heightMm + 0.01
63
- }
64
-
65
- /** The inner padding of a label, in px (2 mm). */
66
- const LABEL_PAD = Math.round(2 * PX_PER_MM)
67
-
68
- /**
69
- * The page view of a label sheet (E.43): one sheet with the grid. The first
70
- * label is the one you edit; the others repeat it, as Print fills one label
71
- * per record or CSV row. Content that runs past the label is cut off on
72
- * paper, so the label shows where it ends and `onOverflow` reports it.
73
- */
74
- export function LabelSheetView({
75
- editor,
76
- grid,
77
- sheet,
78
- zoom,
79
- theme,
80
- labels,
81
- onOverflow,
82
- children,
83
- }: {
84
- editor: Editor | null
85
- grid: LabelGrid
86
- sheet: SheetSize
87
- zoom: number
88
- /** The document's resolved theme: the print colour, and the headings with `title-and-headings`. */
89
- theme?: ResolvedThemeTokens | null
90
- labels: EditorLabels
91
- onOverflow?: (overflow: boolean) => void
92
- children: React.ReactNode
93
- }) {
94
- const cells = labelCells(grid, sheet)
95
- const w = SHEETS[sheet].widthMm * PX_PER_MM
96
- const h = SHEETS[sheet].heightMm * PX_PER_MM
97
- const copies = React.useRef<(HTMLDivElement | null)[]>([])
98
- const first = React.useRef<HTMLDivElement>(null)
99
- const [overflow, setOverflow] = React.useState(false)
100
-
101
- React.useEffect(() => {
102
- if (!editor) return
103
- let frame = 0
104
- const mirror = () => {
105
- cancelAnimationFrame(frame)
106
- frame = requestAnimationFrame(() => {
107
- if (editor.isDestroyed) return
108
- const source = editor.view.dom
109
- for (const slot of copies.current) {
110
- if (!slot) continue
111
- const copy = source.cloneNode(true) as HTMLElement
112
- copy.removeAttribute("contenteditable")
113
- copy.removeAttribute("role")
114
- copy.removeAttribute("aria-label")
115
- copy.removeAttribute("aria-multiline")
116
- copy.classList.remove("ProseMirror-focused")
117
- slot.replaceChildren(copy)
118
- }
119
- const box = first.current
120
- const over = !!box && source.scrollHeight > box.clientHeight - 2 * LABEL_PAD + 1
121
- setOverflow(over)
122
- })
123
- }
124
- mirror()
125
- editor.on("update", mirror)
126
- const dom = editor.view.dom
127
- dom.addEventListener("load", mirror, true)
128
- return () => {
129
- cancelAnimationFrame(frame)
130
- editor.off("update", mirror)
131
- dom.removeEventListener("load", mirror, true)
132
- }
133
- }, [editor, grid, sheet])
134
-
135
- React.useEffect(() => onOverflow?.(overflow), [overflow, onOverflow])
136
-
137
- const place = (i: number): React.CSSProperties => ({
138
- insetInlineStart: cells[i].x * PX_PER_MM,
139
- top: cells[i].y * PX_PER_MM,
140
- width: grid.size.widthMm * PX_PER_MM,
141
- height: grid.size.heightMm * PX_PER_MM,
142
- padding: LABEL_PAD,
143
- })
144
-
145
- return (
146
- <div className="wy-pv" style={{ zoom, width: w } as React.CSSProperties}>
147
- <div className="wy-pv__stack wy-ls light" data-accent-use={theme?.accentUse} style={{ ...themeStyle(theme), width: w, height: h }}>
148
- <div className="wy-pv__page" style={{ top: 0, width: w, height: h }} aria-hidden="true" />
149
- {cells.map((_, i) =>
150
- i === 0 ? (
151
- <div key={i} ref={first} className={cn("wy-ls__label is-first", overflow && "is-over")} style={place(i)}>
152
- {children}
153
- {overflow && (
154
- <span className="wy-ls__over" role="status">
155
- <Icon name="alert" size={12} />
156
- {labels.labelOverflow}
157
- </span>
158
- )}
159
- </div>
160
- ) : (
161
- <div
162
- key={i}
163
- ref={(el) => {
164
- copies.current[i - 1] = el
165
- }}
166
- className="wy-ls__label"
167
- style={place(i)}
168
- aria-hidden="true"
169
- inert
170
- />
171
- )
172
- )}
173
- </div>
174
- </div>
175
- )
176
- }
177
-
178
- /** The sheet setup for a label document: a stock preset, or the grid by hand. */
179
- export function LabelSetup({
180
- grid,
181
- sheet,
182
- onChange,
183
- labels,
184
- disabled,
185
- }: {
186
- grid: LabelGrid
187
- sheet: SheetSize
188
- onChange: (grid: LabelGrid, sheet: SheetSize) => void
189
- labels: EditorLabels
190
- disabled?: boolean
191
- }) {
192
- const preset = LABEL_PRESETS.find((p) => p.sheet === sheet && sameGrid(p.grid, grid))
193
- const fits = gridFits(grid, sheet)
194
- const num = (value: number, set: (n: number) => LabelGrid, opts: { min: number; max: number; step: number; label: string }) => (
195
- <Input
196
- label={opts.label}
197
- type="number"
198
- inputMode="decimal"
199
- min={opts.min}
200
- max={opts.max}
201
- step={opts.step}
202
- value={value}
203
- onChange={(e) => {
204
- const n = Number(e.target.value)
205
- if (Number.isFinite(n) && n >= opts.min && n <= opts.max) onChange(set(n), sheet)
206
- }}
207
- />
208
- )
209
-
210
- return (
211
- <Popover>
212
- <PopoverTrigger asChild>
213
- <button
214
- type="button"
215
- disabled={disabled}
216
- className="inline-flex h-7 items-center gap-1 rounded-control px-2 text-xs text-foreground hover:bg-accent disabled:opacity-50"
217
- >
218
- <Icon name="table" size={14} />
219
- {fill(labels.labelGrid, { columns: grid.columns, rows: grid.rows })}
220
- <Icon name="chevron-down" size={12} />
221
- </button>
222
- </PopoverTrigger>
223
- <PopoverContent align="end" className="grid w-[300px] gap-3">
224
- <div className="grid gap-1">
225
- <span className="text-xs font-semibold text-foreground">{labels.labelStock}</span>
226
- <div role="listbox" aria-label={labels.labelStock} className="grid gap-0.5">
227
- {LABEL_PRESETS.map((p) => (
228
- <button
229
- key={p.id}
230
- type="button"
231
- role="option"
232
- aria-selected={p === preset}
233
- onClick={() => onChange(p.grid, p.sheet)}
234
- className={cn(
235
- "flex items-center justify-between rounded-control px-2 py-1.5 text-start text-sm hover:bg-accent",
236
- p === preset && "bg-primary-tint text-primary-tint-foreground"
237
- )}
238
- >
239
- <span>{p.id === "certificate" ? labels.labelCertificate : p.name}</span>
240
- <span className="text-xs text-muted-foreground" dir="ltr">
241
- {p.sheet} · {p.grid.columns} × {p.grid.rows}
242
- </span>
243
- </button>
244
- ))}
245
- </div>
246
- </div>
247
- <div className="grid grid-cols-2 gap-2">
248
- <div className="col-span-2 flex items-center justify-between gap-2 text-xs text-muted-foreground">
249
- {labels.labelSheet}
250
- <ToggleGroup
251
- size="sm"
252
- label={labels.labelSheet}
253
- required
254
- value={sheet}
255
- onChange={(v) => v && onChange(grid, v as SheetSize)}
256
- options={[
257
- { value: "A4", label: "A4" },
258
- { value: "Letter", label: "Letter" },
259
- ]}
260
- />
261
- </div>
262
- {num(grid.columns, (n) => ({ ...grid, columns: Math.round(n) }), { min: 1, max: 10, step: 1, label: labels.labelColumns })}
263
- {num(grid.rows, (n) => ({ ...grid, rows: Math.round(n) }), { min: 1, max: 30, step: 1, label: labels.labelRows })}
264
- {num(grid.size.widthMm, (n) => ({ ...grid, size: { ...grid.size, widthMm: n } }), { min: 10, max: 216, step: 0.1, label: labels.labelWidth })}
265
- {num(grid.size.heightMm, (n) => ({ ...grid, size: { ...grid.size, heightMm: n } }), { min: 10, max: 297, step: 0.1, label: labels.labelHeight })}
266
- {num(grid.gapMm, (n) => ({ ...grid, gapMm: n }), { min: 0, max: 20, step: 0.1, label: labels.labelGapX })}
267
- {num(grid.gapYMm ?? grid.gapMm, (n) => ({ ...grid, gapYMm: n }), { min: 0, max: 20, step: 0.1, label: labels.labelGapY })}
268
- </div>
269
- {!fits && (
270
- <p role="alert" className="m-0 flex items-center gap-1.5 text-xs text-error-text">
271
- <Icon name="alert" size={12} />
272
- {labels.labelTooBig}
273
- </p>
274
- )}
275
- </PopoverContent>
276
- </Popover>
277
- )
278
- }
279
-
280
- function sameGrid(a: LabelGrid, b: LabelGrid): boolean {
281
- return (
282
- a.columns === b.columns &&
283
- a.rows === b.rows &&
284
- a.size.widthMm === b.size.widthMm &&
285
- a.size.heightMm === b.size.heightMm &&
286
- a.gapMm === b.gapMm &&
287
- (a.gapYMm ?? a.gapMm) === (b.gapYMm ?? b.gapMm)
288
- )
289
- }
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import type { Editor } from "@tiptap/core"
5
+
6
+ import { Icon } from "../icon"
7
+ import { Input } from "../input"
8
+ import { Popover, PopoverContent, PopoverTrigger } from "../popover"
9
+ import { ToggleGroup } from "../toggle-group"
10
+ import { cn } from "../utils"
11
+ import type { LabelGrid, ResolvedThemeTokens } from "./contract"
12
+ import { fill, type EditorLabels } from "./labels"
13
+ import { themeStyle } from "./page-view"
14
+
15
+ /** 96 dpi, as the page view (EditorPageView). */
16
+ const PX_PER_MM = 96 / 25.4
17
+
18
+ /** The sheets a label document prints on (E.43): A4 or Letter, portrait. */
19
+ export const SHEETS = {
20
+ A4: { widthMm: 210, heightMm: 297 },
21
+ Letter: { widthMm: 215.9, heightMm: 279.4 },
22
+ } as const
23
+ export type SheetSize = keyof typeof SHEETS
24
+
25
+ /**
26
+ * Common label stock. The grid is centred on the sheet, which matches these
27
+ * sheets' own margins to within half a millimetre. A certificate is a
28
+ * one-label sheet.
29
+ */
30
+ export const LABEL_PRESETS: { id: string; name: string; sheet: SheetSize; grid: LabelGrid }[] = [
31
+ { id: "avery-l7160", name: "Avery L7160", sheet: "A4", grid: { columns: 3, rows: 7, size: { widthMm: 63.5, heightMm: 38.1 }, gapMm: 2.5, gapYMm: 0 } },
32
+ { id: "avery-l7159", name: "Avery L7159", sheet: "A4", grid: { columns: 3, rows: 8, size: { widthMm: 63.5, heightMm: 33.9 }, gapMm: 2.5, gapYMm: 0 } },
33
+ { id: "avery-l7165", name: "Avery L7165", sheet: "A4", grid: { columns: 2, rows: 4, size: { widthMm: 99.1, heightMm: 67.7 }, gapMm: 2.5, gapYMm: 0 } },
34
+ { id: "avery-l7651", name: "Avery L7651", sheet: "A4", grid: { columns: 5, rows: 13, size: { widthMm: 38.1, heightMm: 21.2 }, gapMm: 2.5, gapYMm: 0 } },
35
+ { id: "letter-2x5", name: "Letter 2 × 5", sheet: "Letter", grid: { columns: 2, rows: 5, size: { widthMm: 101.6, heightMm: 50.8 }, gapMm: 4.8, gapYMm: 0 } },
36
+ { id: "certificate", name: "Certificate", sheet: "A4", grid: { columns: 1, rows: 1, size: { widthMm: 190, heightMm: 277 }, gapMm: 0 } },
37
+ ]
38
+
39
+ export const DEFAULT_LABEL_GRID: LabelGrid = LABEL_PRESETS[0].grid
40
+
41
+ /** Where each label sits on the sheet, in mm from the top start corner. */
42
+ export function labelCells(grid: LabelGrid, sheet: SheetSize): { x: number; y: number }[] {
43
+ const { widthMm, heightMm } = SHEETS[sheet]
44
+ const gapX = grid.gapMm
45
+ const gapY = grid.gapYMm ?? grid.gapMm
46
+ const left = (widthMm - grid.columns * grid.size.widthMm - (grid.columns - 1) * gapX) / 2
47
+ const top = (heightMm - grid.rows * grid.size.heightMm - (grid.rows - 1) * gapY) / 2
48
+ const cells: { x: number; y: number }[] = []
49
+ for (let r = 0; r < grid.rows; r++) {
50
+ for (let c = 0; c < grid.columns; c++) {
51
+ cells.push({ x: left + c * (grid.size.widthMm + gapX), y: top + r * (grid.size.heightMm + gapY) })
52
+ }
53
+ }
54
+ return cells
55
+ }
56
+
57
+ /** Whether the labels fit on the sheet. */
58
+ export function gridFits(grid: LabelGrid, sheet: SheetSize): boolean {
59
+ const cells = labelCells(grid, sheet)
60
+ const last = cells[cells.length - 1]
61
+ const { widthMm, heightMm } = SHEETS[sheet]
62
+ return !!last && cells[0].x >= 0 && cells[0].y >= 0 && last.x + grid.size.widthMm <= widthMm + 0.01 && last.y + grid.size.heightMm <= heightMm + 0.01
63
+ }
64
+
65
+ /** The inner padding of a label, in px (2 mm). */
66
+ const LABEL_PAD = Math.round(2 * PX_PER_MM)
67
+
68
+ /**
69
+ * The page view of a label sheet (E.43): one sheet with the grid. The first
70
+ * label is the one you edit; the others repeat it, as Print fills one label
71
+ * per record or CSV row. Content that runs past the label is cut off on
72
+ * paper, so the label shows where it ends and `onOverflow` reports it.
73
+ */
74
+ export function LabelSheetView({
75
+ editor,
76
+ grid,
77
+ sheet,
78
+ zoom,
79
+ theme,
80
+ labels,
81
+ onOverflow,
82
+ children,
83
+ }: {
84
+ editor: Editor | null
85
+ grid: LabelGrid
86
+ sheet: SheetSize
87
+ zoom: number
88
+ /** The document's resolved theme: the print colour, and the headings with `title-and-headings`. */
89
+ theme?: ResolvedThemeTokens | null
90
+ labels: EditorLabels
91
+ onOverflow?: (overflow: boolean) => void
92
+ children: React.ReactNode
93
+ }) {
94
+ const cells = labelCells(grid, sheet)
95
+ const w = SHEETS[sheet].widthMm * PX_PER_MM
96
+ const h = SHEETS[sheet].heightMm * PX_PER_MM
97
+ const copies = React.useRef<(HTMLDivElement | null)[]>([])
98
+ const first = React.useRef<HTMLDivElement>(null)
99
+ const [overflow, setOverflow] = React.useState(false)
100
+
101
+ React.useEffect(() => {
102
+ if (!editor) return
103
+ let frame = 0
104
+ const mirror = () => {
105
+ cancelAnimationFrame(frame)
106
+ frame = requestAnimationFrame(() => {
107
+ if (editor.isDestroyed) return
108
+ const source = editor.view.dom
109
+ for (const slot of copies.current) {
110
+ if (!slot) continue
111
+ const copy = source.cloneNode(true) as HTMLElement
112
+ copy.removeAttribute("contenteditable")
113
+ copy.removeAttribute("role")
114
+ copy.removeAttribute("aria-label")
115
+ copy.removeAttribute("aria-multiline")
116
+ copy.classList.remove("ProseMirror-focused")
117
+ slot.replaceChildren(copy)
118
+ }
119
+ const box = first.current
120
+ const over = !!box && source.scrollHeight > box.clientHeight - 2 * LABEL_PAD + 1
121
+ setOverflow(over)
122
+ })
123
+ }
124
+ mirror()
125
+ editor.on("update", mirror)
126
+ const dom = editor.view.dom
127
+ dom.addEventListener("load", mirror, true)
128
+ return () => {
129
+ cancelAnimationFrame(frame)
130
+ editor.off("update", mirror)
131
+ dom.removeEventListener("load", mirror, true)
132
+ }
133
+ }, [editor, grid, sheet])
134
+
135
+ React.useEffect(() => onOverflow?.(overflow), [overflow, onOverflow])
136
+
137
+ const place = (i: number): React.CSSProperties => ({
138
+ insetInlineStart: cells[i].x * PX_PER_MM,
139
+ top: cells[i].y * PX_PER_MM,
140
+ width: grid.size.widthMm * PX_PER_MM,
141
+ height: grid.size.heightMm * PX_PER_MM,
142
+ padding: LABEL_PAD,
143
+ })
144
+
145
+ return (
146
+ <div className="wy-pv" style={{ zoom, width: w } as React.CSSProperties}>
147
+ <div className="wy-pv__stack wy-ls light" data-accent-use={theme?.accentUse} style={{ ...themeStyle(theme), width: w, height: h }}>
148
+ <div className="wy-pv__page" style={{ top: 0, width: w, height: h }} aria-hidden="true" />
149
+ {cells.map((_, i) =>
150
+ i === 0 ? (
151
+ <div key={i} ref={first} className={cn("wy-ls__label is-first", overflow && "is-over")} style={place(i)}>
152
+ {children}
153
+ {overflow && (
154
+ <span className="wy-ls__over" role="status">
155
+ <Icon name="alert" size={12} />
156
+ {labels.labelOverflow}
157
+ </span>
158
+ )}
159
+ </div>
160
+ ) : (
161
+ <div
162
+ key={i}
163
+ ref={(el) => {
164
+ copies.current[i - 1] = el
165
+ }}
166
+ className="wy-ls__label"
167
+ style={place(i)}
168
+ aria-hidden="true"
169
+ inert
170
+ />
171
+ )
172
+ )}
173
+ </div>
174
+ </div>
175
+ )
176
+ }
177
+
178
+ /** The sheet setup for a label document: a stock preset, or the grid by hand. */
179
+ export function LabelSetup({
180
+ grid,
181
+ sheet,
182
+ onChange,
183
+ labels,
184
+ disabled,
185
+ }: {
186
+ grid: LabelGrid
187
+ sheet: SheetSize
188
+ onChange: (grid: LabelGrid, sheet: SheetSize) => void
189
+ labels: EditorLabels
190
+ disabled?: boolean
191
+ }) {
192
+ const preset = LABEL_PRESETS.find((p) => p.sheet === sheet && sameGrid(p.grid, grid))
193
+ const fits = gridFits(grid, sheet)
194
+ const num = (value: number, set: (n: number) => LabelGrid, opts: { min: number; max: number; step: number; label: string }) => (
195
+ <Input
196
+ label={opts.label}
197
+ type="number"
198
+ inputMode="decimal"
199
+ min={opts.min}
200
+ max={opts.max}
201
+ step={opts.step}
202
+ value={value}
203
+ onChange={(e) => {
204
+ const n = Number(e.target.value)
205
+ if (Number.isFinite(n) && n >= opts.min && n <= opts.max) onChange(set(n), sheet)
206
+ }}
207
+ />
208
+ )
209
+
210
+ return (
211
+ <Popover>
212
+ <PopoverTrigger asChild>
213
+ <button
214
+ type="button"
215
+ disabled={disabled}
216
+ className="inline-flex h-7 items-center gap-1 rounded-control px-2 text-xs text-foreground hover:bg-accent disabled:opacity-50"
217
+ >
218
+ <Icon name="table" size={14} />
219
+ {fill(labels.labelGrid, { columns: grid.columns, rows: grid.rows })}
220
+ <Icon name="chevron-down" size={12} />
221
+ </button>
222
+ </PopoverTrigger>
223
+ <PopoverContent align="end" className="grid w-[300px] gap-3">
224
+ <div className="grid gap-1">
225
+ <span className="text-xs font-semibold text-foreground">{labels.labelStock}</span>
226
+ <div role="listbox" aria-label={labels.labelStock} className="grid gap-0.5">
227
+ {LABEL_PRESETS.map((p) => (
228
+ <button
229
+ key={p.id}
230
+ type="button"
231
+ role="option"
232
+ aria-selected={p === preset}
233
+ onClick={() => onChange(p.grid, p.sheet)}
234
+ className={cn(
235
+ "flex items-center justify-between rounded-control px-2 py-1.5 text-start text-sm hover:bg-accent",
236
+ p === preset && "bg-primary-tint text-primary-tint-foreground"
237
+ )}
238
+ >
239
+ <span>{p.id === "certificate" ? labels.labelCertificate : p.name}</span>
240
+ <span className="text-xs text-muted-foreground" dir="ltr">
241
+ {p.sheet} · {p.grid.columns} × {p.grid.rows}
242
+ </span>
243
+ </button>
244
+ ))}
245
+ </div>
246
+ </div>
247
+ <div className="grid grid-cols-2 gap-2">
248
+ <div className="col-span-2 flex items-center justify-between gap-2 text-xs text-muted-foreground">
249
+ {labels.labelSheet}
250
+ <ToggleGroup
251
+ size="sm"
252
+ label={labels.labelSheet}
253
+ required
254
+ value={sheet}
255
+ onChange={(v) => v && onChange(grid, v as SheetSize)}
256
+ options={[
257
+ { value: "A4", label: "A4" },
258
+ { value: "Letter", label: "Letter" },
259
+ ]}
260
+ />
261
+ </div>
262
+ {num(grid.columns, (n) => ({ ...grid, columns: Math.round(n) }), { min: 1, max: 10, step: 1, label: labels.labelColumns })}
263
+ {num(grid.rows, (n) => ({ ...grid, rows: Math.round(n) }), { min: 1, max: 30, step: 1, label: labels.labelRows })}
264
+ {num(grid.size.widthMm, (n) => ({ ...grid, size: { ...grid.size, widthMm: n } }), { min: 10, max: 216, step: 0.1, label: labels.labelWidth })}
265
+ {num(grid.size.heightMm, (n) => ({ ...grid, size: { ...grid.size, heightMm: n } }), { min: 10, max: 297, step: 0.1, label: labels.labelHeight })}
266
+ {num(grid.gapMm, (n) => ({ ...grid, gapMm: n }), { min: 0, max: 20, step: 0.1, label: labels.labelGapX })}
267
+ {num(grid.gapYMm ?? grid.gapMm, (n) => ({ ...grid, gapYMm: n }), { min: 0, max: 20, step: 0.1, label: labels.labelGapY })}
268
+ </div>
269
+ {!fits && (
270
+ <p role="alert" className="m-0 flex items-center gap-1.5 text-xs text-error-text">
271
+ <Icon name="alert" size={12} />
272
+ {labels.labelTooBig}
273
+ </p>
274
+ )}
275
+ </PopoverContent>
276
+ </Popover>
277
+ )
278
+ }
279
+
280
+ function sameGrid(a: LabelGrid, b: LabelGrid): boolean {
281
+ return (
282
+ a.columns === b.columns &&
283
+ a.rows === b.rows &&
284
+ a.size.widthMm === b.size.widthMm &&
285
+ a.size.heightMm === b.size.heightMm &&
286
+ a.gapMm === b.gapMm &&
287
+ (a.gapYMm ?? a.gapMm) === (b.gapYMm ?? b.gapMm)
288
+ )
289
+ }