@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.
- package/README.md +106 -2
- package/package.json +219 -4
- package/src/access-denied.stories.tsx +179 -0
- package/src/access-denied.tsx +305 -0
- package/src/action-proposal.tsx +338 -0
- package/src/actor-chip.tsx +71 -0
- package/src/agent-activity.tsx +163 -0
- package/src/agent-collaboration.stories.tsx +156 -0
- package/src/agent-run-status.tsx +196 -0
- package/src/alert-dialog.stories.tsx +45 -0
- package/src/alert-dialog.tsx +147 -0
- package/src/alert.stories.tsx +91 -0
- package/src/alert.tsx +89 -0
- package/src/app-sidebar.stories.tsx +195 -0
- package/src/app-sidebar.tsx +604 -0
- package/src/approval-queue.tsx +109 -0
- package/src/audience-group-picker.stories.tsx +198 -0
- package/src/audience-group-picker.tsx +362 -0
- package/src/audience-people-picker.stories.tsx +102 -0
- package/src/audience-people-picker.tsx +279 -0
- package/src/avatar-pattern.ts +138 -0
- package/src/avatar.tsx +125 -0
- package/src/badge.stories.tsx +105 -0
- package/src/badge.tsx +68 -0
- package/src/beta-warning.stories.tsx +79 -0
- package/src/beta-warning.tsx +189 -0
- package/src/bilingual-input.stories.tsx +82 -0
- package/src/bilingual-input.tsx +188 -0
- package/src/board.tsx +216 -0
- package/src/breadcrumb.stories.tsx +51 -0
- package/src/breadcrumb.tsx +90 -0
- package/src/built-on-whity.tsx +52 -0
- package/src/button.stories.tsx +87 -0
- package/src/button.tsx +123 -0
- package/src/card.stories.tsx +423 -0
- package/src/card.tsx +180 -0
- package/src/chart.stories.tsx +66 -0
- package/src/chart.tsx +228 -0
- package/src/checkbox.stories.tsx +62 -0
- package/src/checkbox.tsx +54 -0
- package/src/collapsible.stories.tsx +48 -0
- package/src/collapsible.tsx +34 -0
- package/src/color-card.stories.tsx +61 -0
- package/src/color-card.tsx +270 -0
- package/src/color-picker.stories.tsx +65 -0
- package/src/color-picker.tsx +425 -0
- package/src/column-matcher.tsx +84 -0
- package/src/command-palette.tsx +398 -0
- package/src/confirm-action.tsx +98 -0
- package/src/copy-block.stories.tsx +38 -0
- package/src/copy-block.tsx +85 -0
- package/src/data-table.stories.tsx +200 -0
- package/src/data-table.tsx +1686 -0
- package/src/date-picker.stories.tsx +61 -0
- package/src/date-picker.tsx +456 -0
- package/src/design-cards.stories.tsx +551 -0
- package/src/dialog.stories.tsx +48 -0
- package/src/dialog.tsx +179 -0
- package/src/display-preferences.tsx +50 -0
- package/src/documents/barcode-svg.stories.tsx +72 -0
- package/src/documents/barcode-svg.tsx +79 -0
- package/src/dropdown-menu.stories.tsx +77 -0
- package/src/dropdown-menu.tsx +271 -0
- package/src/editor/agent-panel.tsx +222 -0
- package/src/editor/agents.stories.tsx +81 -0
- package/src/editor/agents.ts +55 -0
- package/src/editor/behaviour.ts +279 -0
- package/src/editor/block-ids.ts +44 -0
- package/src/editor/block-list.tsx +104 -0
- package/src/editor/block-toolbar.tsx +490 -0
- package/src/editor/bubble.tsx +119 -0
- package/src/editor/catalogue.ts +169 -0
- package/src/editor/collab-rail.tsx +746 -0
- package/src/editor/collab.stories.tsx +207 -0
- package/src/editor/collab.ts +87 -0
- package/src/editor/contract.ts +476 -0
- package/src/editor/convert.ts +414 -0
- package/src/editor/doc-editor.stories.tsx +98 -0
- package/src/editor/doc-editor.tsx +1492 -0
- package/src/editor/editor.css +636 -0
- package/src/editor/extensions.ts +86 -0
- package/src/editor/field-bar.tsx +154 -0
- package/src/editor/field-picker.tsx +109 -0
- package/src/editor/fields.ts +100 -0
- package/src/editor/gutter.tsx +443 -0
- package/src/editor/index.ts +23 -0
- package/src/editor/label-sheet.tsx +289 -0
- package/src/editor/labels.ts +394 -0
- package/src/editor/link-tools.tsx +141 -0
- package/src/editor/node-views.tsx +461 -0
- package/src/editor/page-view.tsx +372 -0
- package/src/editor/phone.tsx +310 -0
- package/src/editor/plugin-block-view.tsx +230 -0
- package/src/editor/plugin-blocks.stories.tsx +96 -0
- package/src/editor/plugin-blocks.ts +52 -0
- package/src/editor/publish-check.tsx +201 -0
- package/src/editor/rich-blocks.stories.tsx +146 -0
- package/src/editor/sample.ts +40 -0
- package/src/editor/save-block-dialog.tsx +141 -0
- package/src/editor/saved-block-view.tsx +205 -0
- package/src/editor/saved-blocks.stories.tsx +105 -0
- package/src/editor/saved-parts.ts +192 -0
- package/src/editor/schema-rich.ts +156 -0
- package/src/editor/schema.ts +368 -0
- package/src/editor/services.tsx +205 -0
- package/src/editor/slash.ts +64 -0
- package/src/editor/suggesting.ts +372 -0
- package/src/editor/suggestions.ts +763 -0
- package/src/editor/table-keys.ts +66 -0
- package/src/editor/templates.stories.tsx +324 -0
- package/src/editor/toolbar.tsx +364 -0
- package/src/empty-state.stories.tsx +51 -0
- package/src/empty-state.tsx +126 -0
- package/src/empty-state.usage-notes.md +30 -0
- package/src/form.stories.tsx +112 -0
- package/src/form.tsx +176 -0
- package/src/globals.css +20 -0
- package/src/icon-button.tsx +85 -0
- package/src/icon-map.ts +335 -0
- package/src/icon.tsx +41 -0
- package/src/index.ts +90 -0
- package/src/input.stories.tsx +157 -0
- package/src/input.tsx +768 -0
- package/src/kpi-strip.tsx +47 -0
- package/src/localized-text.ts +76 -0
- package/src/locked-screen.stories.tsx +29 -0
- package/src/locked-screen.tsx +75 -0
- package/src/lockup.tsx +42 -0
- package/src/lottie.tsx +441 -0
- package/src/mark.tsx +62 -0
- package/src/math-text.stories.tsx +33 -0
- package/src/math-text.tsx +43 -0
- package/src/menu-bar.stories.tsx +198 -0
- package/src/menu-bar.tsx +373 -0
- package/src/message-thread.tsx +181 -0
- package/src/money/currency.ts +244 -0
- package/src/money/money-input.tsx +131 -0
- package/src/money/money.tsx +33 -0
- package/src/open-on-computer.tsx +150 -0
- package/src/otp-input.tsx +161 -0
- package/src/page-header.stories.tsx +95 -0
- package/src/page-header.tsx +122 -0
- package/src/page-shell.stories.tsx +42 -0
- package/src/page-shell.tsx +32 -0
- package/src/page-states.tsx +277 -0
- package/src/pagination.stories.tsx +45 -0
- package/src/pagination.tsx +98 -0
- package/src/paper-sheet.stories.tsx +88 -0
- package/src/paper-sheet.tsx +85 -0
- package/src/phone-tab-bar.tsx +152 -0
- package/src/plugin-card.stories.tsx +208 -0
- package/src/plugin-card.tsx +931 -0
- package/src/popover.stories.tsx +48 -0
- package/src/popover.tsx +55 -0
- package/src/radio-group.stories.tsx +26 -0
- package/src/radio-group.tsx +44 -0
- package/src/route-flow/editor.tsx +1087 -0
- package/src/route-flow/model.ts +771 -0
- package/src/save-bar.tsx +94 -0
- package/src/scope-grant.tsx +139 -0
- package/src/screen-too-small.stories.tsx +61 -0
- package/src/screen-too-small.tsx +104 -0
- package/src/search-field.tsx +65 -0
- package/src/select.stories.tsx +44 -0
- package/src/select.tsx +197 -0
- package/src/separator.stories.tsx +34 -0
- package/src/separator.tsx +34 -0
- package/src/sheet.stories.tsx +49 -0
- package/src/sheet.tsx +168 -0
- package/src/skeleton.stories.tsx +28 -0
- package/src/skeleton.tsx +19 -0
- package/src/slider.stories.tsx +78 -0
- package/src/slider.tsx +300 -0
- package/src/spinner.stories.tsx +33 -0
- package/src/spinner.tsx +55 -0
- package/src/stepper.tsx +85 -0
- package/src/switch.stories.tsx +42 -0
- package/src/switch.tsx +41 -0
- package/src/switcher.stories.tsx +111 -0
- package/src/switcher.tsx +131 -0
- package/src/system-state.tsx +55 -0
- package/src/table.stories.tsx +48 -0
- package/src/table.tsx +121 -0
- package/src/tabs.stories.tsx +74 -0
- package/src/tabs.tsx +90 -0
- package/src/tag-input.stories.tsx +67 -0
- package/src/tag-input.tsx +117 -0
- package/src/tenant-logo.tsx +63 -0
- package/src/tenant-switcher.stories.tsx +38 -0
- package/src/tenant-switcher.tsx +164 -0
- package/src/textarea.stories.tsx +27 -0
- package/src/textarea.tsx +18 -0
- package/src/theme-mode.ts +52 -0
- package/src/time-grid.tsx +163 -0
- package/src/time-picker.stories.tsx +62 -0
- package/src/time-picker.tsx +615 -0
- package/src/toggle-group.tsx +138 -0
- package/src/tooltip.stories.tsx +43 -0
- package/src/tooltip.tsx +73 -0
- package/src/top-nav.stories.tsx +58 -0
- package/src/top-nav.tsx +324 -0
- package/src/undo-bar.tsx +107 -0
- package/src/utils.ts +45 -0
- package/src/view-toggle.stories.tsx +84 -0
- package/src/view-toggle.tsx +110 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { Extension, type Editor } from "@tiptap/core"
|
|
5
|
+
import { Plugin, PluginKey } from "@tiptap/pm/state"
|
|
6
|
+
import { Decoration, DecorationSet } from "@tiptap/pm/view"
|
|
7
|
+
|
|
8
|
+
import { TenantLogo } from "../tenant-logo"
|
|
9
|
+
import { cn } from "../utils"
|
|
10
|
+
import type { LetterheadLine, ResolvedThemeTokens } from "./contract"
|
|
11
|
+
import { fill, type EditorLabels } from "./labels"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A4 at 96 dpi (EditorPageView): 794 × 1123px, 16mm margins (stage 6), 24px
|
|
15
|
+
* between pages on screen. The running header on the pages after the first
|
|
16
|
+
* takes RUN_HEADER px of the content box.
|
|
17
|
+
*/
|
|
18
|
+
export const PAGE = { width: 794, height: 1123, margin: 60, gap: 24 } as const
|
|
19
|
+
const RUN_HEADER = 44
|
|
20
|
+
|
|
21
|
+
export interface Letterhead {
|
|
22
|
+
/** The tenant's logo, framed; without one, its initials on the print colour. */
|
|
23
|
+
logo?: string | null
|
|
24
|
+
/**
|
|
25
|
+
* E.44: how the logo shows. `square` (the default) frames it, `wide` shows a
|
|
26
|
+
* wordmark unframed, `none` prints the name without a mark.
|
|
27
|
+
*/
|
|
28
|
+
logoShape?: "square" | "wide" | "none"
|
|
29
|
+
name: string
|
|
30
|
+
/** Address, tax number, phone and email lines at the start side. */
|
|
31
|
+
lines: string[]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Every workspace line a theme can put in the letterhead, by its theme name; null when not set. */
|
|
35
|
+
export type LetterheadParts = Record<LetterheadLine, string | null>
|
|
36
|
+
|
|
37
|
+
/** The workspace's uploaded logos (branding); null when one isn't uploaded. */
|
|
38
|
+
export interface BrandLogos {
|
|
39
|
+
wide: string | null
|
|
40
|
+
square: string | null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The letterhead a theme asks for (E.44, stage 6.2): the lines its
|
|
45
|
+
* `letterhead.lines` names, in that order, from `parts`; and the logo its
|
|
46
|
+
* `letterhead.logo` chooses, falling back to the other shape when that one
|
|
47
|
+
* isn't uploaded (and to the initials when neither is). Without a theme, or
|
|
48
|
+
* without the parts, the letterhead stays as `base` has it.
|
|
49
|
+
*/
|
|
50
|
+
export function themedLetterhead(
|
|
51
|
+
base: Letterhead,
|
|
52
|
+
theme: Pick<ResolvedThemeTokens, "letterhead"> | null | undefined,
|
|
53
|
+
parts?: LetterheadParts | null,
|
|
54
|
+
logos?: BrandLogos | null
|
|
55
|
+
): Letterhead {
|
|
56
|
+
if (!theme) return base
|
|
57
|
+
const { logo, lines } = theme.letterhead
|
|
58
|
+
const picked =
|
|
59
|
+
logo === "none"
|
|
60
|
+
? { logo: null, logoShape: "none" as const }
|
|
61
|
+
: logo === "wide"
|
|
62
|
+
? logos?.wide
|
|
63
|
+
? { logo: logos.wide, logoShape: "wide" as const }
|
|
64
|
+
: { logo: logos?.square ?? null, logoShape: "square" as const }
|
|
65
|
+
: logos?.square
|
|
66
|
+
? { logo: logos.square, logoShape: "square" as const }
|
|
67
|
+
: logos?.wide
|
|
68
|
+
? { logo: logos.wide, logoShape: "wide" as const }
|
|
69
|
+
: { logo: null, logoShape: "square" as const }
|
|
70
|
+
return {
|
|
71
|
+
...base,
|
|
72
|
+
...(logos || logo === "none" ? picked : {}),
|
|
73
|
+
lines: parts ? lines.map((key) => parts[key]).filter((line): line is string => !!line && line.trim() !== "") : base.lines,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The type options a resolved theme carries beside the contract's tokens
|
|
79
|
+
* (E.46, E.47). The server sends them; the contract file follows the design
|
|
80
|
+
* handoff, which doesn't list them yet.
|
|
81
|
+
*/
|
|
82
|
+
export type ThemeTypeOptions = { titleFace?: "readex" | "noto"; largePrint?: boolean }
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The theme's colours as the `--doc-*` variables the page view and sheet read
|
|
86
|
+
* (E.11): `--doc-accent` on paper, `--doc-accent-on-dark` for the continuous
|
|
87
|
+
* sheet's title in dark mode, and `--doc-title-font` when the theme sets the
|
|
88
|
+
* title in the body face (E.46).
|
|
89
|
+
*/
|
|
90
|
+
export function themeStyle(theme: ResolvedThemeTokens | null | undefined): React.CSSProperties | undefined {
|
|
91
|
+
if (!theme) return undefined
|
|
92
|
+
const options = theme as ResolvedThemeTokens & ThemeTypeOptions
|
|
93
|
+
return {
|
|
94
|
+
"--doc-accent": theme.accent,
|
|
95
|
+
"--doc-accent-on-dark": theme.accentOnDark,
|
|
96
|
+
...(options.titleFace === "noto" ? { "--doc-title-font": "var(--font-sans)" } : {}),
|
|
97
|
+
} as React.CSSProperties
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** E.47: whether the theme prints large (the page view scales its print sizes to match). */
|
|
101
|
+
export function largePrint(theme: ResolvedThemeTokens | null | undefined): boolean {
|
|
102
|
+
return (theme as (ResolvedThemeTokens & ThemeTypeOptions) | null | undefined)?.largePrint === true
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The letterhead's mark at `size`, as the theme's logo shape asks. */
|
|
106
|
+
function LetterheadMark({ letterhead, size, showName }: { letterhead: Letterhead; size: number; showName?: boolean }) {
|
|
107
|
+
if (letterhead.logoShape === "none") return showName ? <span className="text-sm font-semibold text-foreground">{letterhead.name}</span> : <span />
|
|
108
|
+
return (
|
|
109
|
+
<TenantLogo
|
|
110
|
+
name={letterhead.name}
|
|
111
|
+
src={letterhead.logo}
|
|
112
|
+
size={size}
|
|
113
|
+
showName={showName}
|
|
114
|
+
shape={letterhead.logoShape === "wide" ? "wide" : "square"}
|
|
115
|
+
/>
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/* ------------------------------------------------------- Page breaks */
|
|
120
|
+
|
|
121
|
+
interface Spacer {
|
|
122
|
+
/** The document position the gap goes before. */
|
|
123
|
+
pos: number
|
|
124
|
+
height: number
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const paginationKey = new PluginKey<DecorationSet>("pagePagination")
|
|
128
|
+
|
|
129
|
+
function spacerWidget(height: number) {
|
|
130
|
+
return () => {
|
|
131
|
+
const el = document.createElement("div")
|
|
132
|
+
el.className = "wy-pv__gap"
|
|
133
|
+
el.setAttribute("aria-hidden", "true")
|
|
134
|
+
el.contentEditable = "false"
|
|
135
|
+
el.style.height = `${height}px`
|
|
136
|
+
return el
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Pushes a block that would cross the bottom of a page to the top of the next
|
|
142
|
+
* one, and starts a page after every Page break block. Whity Render paginates
|
|
143
|
+
* the PDF itself (it measures rows); these breaks are a close preview (E.7).
|
|
144
|
+
*/
|
|
145
|
+
export const PagePagination = Extension.create({
|
|
146
|
+
name: "pagePagination",
|
|
147
|
+
addProseMirrorPlugins() {
|
|
148
|
+
return [
|
|
149
|
+
new Plugin<DecorationSet>({
|
|
150
|
+
key: paginationKey,
|
|
151
|
+
state: {
|
|
152
|
+
init: () => DecorationSet.empty,
|
|
153
|
+
apply(tr, set) {
|
|
154
|
+
const next = tr.getMeta(paginationKey) as Spacer[] | undefined
|
|
155
|
+
if (next) {
|
|
156
|
+
return DecorationSet.create(
|
|
157
|
+
tr.doc,
|
|
158
|
+
next.map((s) => Decoration.widget(s.pos, spacerWidget(s.height), { side: -1, key: `gap-${s.pos}-${s.height}` }))
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
return set.map(tr.mapping, tr.doc)
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
props: { decorations: (state) => paginationKey.getState(state) },
|
|
165
|
+
}),
|
|
166
|
+
]
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
function sameSpacers(a: Spacer[], b: Spacer[]): boolean {
|
|
171
|
+
return a.length === b.length && a.every((s, i) => s.pos === b[i].pos && Math.abs(s.height - b[i].height) < 2)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Lays the top-level blocks out on pages. `column` is the content column,
|
|
176
|
+
* `firstTop` where the editor's content starts on page 1 (below the
|
|
177
|
+
* letterhead), both in unzoomed px. Returns the number of pages.
|
|
178
|
+
*/
|
|
179
|
+
function usePagination(editor: Editor | null, column: React.RefObject<HTMLDivElement | null>, enabled: boolean): number {
|
|
180
|
+
const [pages, setPages] = React.useState(1)
|
|
181
|
+
const applied = React.useRef<Spacer[]>([])
|
|
182
|
+
|
|
183
|
+
const layout = React.useCallback(() => {
|
|
184
|
+
if (!editor || editor.isDestroyed || !column.current) return
|
|
185
|
+
const view = editor.view
|
|
186
|
+
const box = column.current.getBoundingClientRect()
|
|
187
|
+
// CSS zoom scales what getBoundingClientRect reports; offsetWidth isn't.
|
|
188
|
+
const scale = column.current.offsetWidth ? box.width / column.current.offsetWidth : 1
|
|
189
|
+
const pageStart = (k: number) => k * (PAGE.height + PAGE.gap) + (k === 0 ? 0 : PAGE.margin + RUN_HEADER)
|
|
190
|
+
const pageEnd = (k: number) => k * (PAGE.height + PAGE.gap) + PAGE.height - PAGE.margin
|
|
191
|
+
|
|
192
|
+
const spacers: Spacer[] = []
|
|
193
|
+
let shiftBefore = 0 // the gaps applied last time above this block, to recover its natural place
|
|
194
|
+
let shift = 0 // the gaps this pass adds above it
|
|
195
|
+
let page = 0
|
|
196
|
+
let breakNext = false
|
|
197
|
+
const old = new Map(applied.current.map((s) => [s.pos, s.height]))
|
|
198
|
+
|
|
199
|
+
view.state.doc.forEach((node, offset) => {
|
|
200
|
+
const dom = view.nodeDOM(offset)
|
|
201
|
+
if (!(dom instanceof HTMLElement)) return
|
|
202
|
+
shiftBefore += old.get(offset) ?? 0
|
|
203
|
+
const r = dom.getBoundingClientRect()
|
|
204
|
+
const naturalTop = (r.top - box.top) / scale - shiftBefore
|
|
205
|
+
const height = r.height / scale
|
|
206
|
+
let top = naturalTop + shift
|
|
207
|
+
if (breakNext || (top + height > pageEnd(page) && top > pageStart(page) + 1)) {
|
|
208
|
+
const target = pageStart(page + 1)
|
|
209
|
+
const gap = Math.max(0, target - top)
|
|
210
|
+
if (gap > 0) spacers.push({ pos: offset, height: Math.round(gap) })
|
|
211
|
+
shift += gap
|
|
212
|
+
top += gap
|
|
213
|
+
page += 1
|
|
214
|
+
}
|
|
215
|
+
breakNext = node.type.name === "pageBreak"
|
|
216
|
+
// A block taller than a page runs over the next ones.
|
|
217
|
+
while (top + height > pageEnd(page) + 1) page += 1
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
setPages(page + 1)
|
|
221
|
+
if (!sameSpacers(spacers, applied.current)) {
|
|
222
|
+
applied.current = spacers
|
|
223
|
+
view.dispatch(view.state.tr.setMeta(paginationKey, spacers).setMeta("addToHistory", false))
|
|
224
|
+
}
|
|
225
|
+
}, [editor, column])
|
|
226
|
+
|
|
227
|
+
React.useEffect(() => {
|
|
228
|
+
if (!editor) return
|
|
229
|
+
if (!enabled) {
|
|
230
|
+
if (applied.current.length) {
|
|
231
|
+
applied.current = []
|
|
232
|
+
editor.view.dispatch(editor.state.tr.setMeta(paginationKey, []).setMeta("addToHistory", false))
|
|
233
|
+
}
|
|
234
|
+
setPages(1)
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
let frame = 0
|
|
238
|
+
const schedule = () => {
|
|
239
|
+
cancelAnimationFrame(frame)
|
|
240
|
+
frame = requestAnimationFrame(layout)
|
|
241
|
+
}
|
|
242
|
+
schedule()
|
|
243
|
+
const onUpdate = ({ transaction }: { transaction: { docChanged: boolean } }) => {
|
|
244
|
+
if (transaction.docChanged) schedule()
|
|
245
|
+
}
|
|
246
|
+
editor.on("update", onUpdate)
|
|
247
|
+
const ro = new ResizeObserver(schedule)
|
|
248
|
+
if (column.current) ro.observe(column.current)
|
|
249
|
+
// Images and charts change height when they load.
|
|
250
|
+
const dom = editor.view.dom
|
|
251
|
+
dom.addEventListener("load", schedule, true)
|
|
252
|
+
return () => {
|
|
253
|
+
cancelAnimationFrame(frame)
|
|
254
|
+
editor.off("update", onUpdate)
|
|
255
|
+
ro.disconnect()
|
|
256
|
+
dom.removeEventListener("load", schedule, true)
|
|
257
|
+
}
|
|
258
|
+
}, [editor, enabled, layout, column])
|
|
259
|
+
|
|
260
|
+
return pages
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/* -------------------------------------------------------- Page view */
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* The page view (E.7, EditorPageView): white A4 pages in both themes (E.11),
|
|
267
|
+
* the letterhead on page 1, a running header after it, and "Page x of y" with
|
|
268
|
+
* the reference in each footer. The title is the tenant's print colour.
|
|
269
|
+
*/
|
|
270
|
+
function PageView({
|
|
271
|
+
editor,
|
|
272
|
+
title,
|
|
273
|
+
letterhead,
|
|
274
|
+
reference,
|
|
275
|
+
date,
|
|
276
|
+
footerRef,
|
|
277
|
+
theme,
|
|
278
|
+
zoom,
|
|
279
|
+
showMargins = false,
|
|
280
|
+
labels,
|
|
281
|
+
onPages,
|
|
282
|
+
children,
|
|
283
|
+
}: {
|
|
284
|
+
editor: Editor | null
|
|
285
|
+
title: string
|
|
286
|
+
letterhead?: Letterhead | null
|
|
287
|
+
/** The end side of the letterhead: the reference ("Ref INV-1042") and the date. */
|
|
288
|
+
reference?: React.ReactNode
|
|
289
|
+
date?: React.ReactNode
|
|
290
|
+
/** Beside "Page x of y" in the footer: the document's reference. */
|
|
291
|
+
footerRef?: string
|
|
292
|
+
/** The document's resolved theme: the print colour and where it's used (E.44, E.45). */
|
|
293
|
+
theme?: ResolvedThemeTokens | null
|
|
294
|
+
/** 0.5 to 1.25. */
|
|
295
|
+
zoom: number
|
|
296
|
+
showMargins?: boolean
|
|
297
|
+
labels: EditorLabels
|
|
298
|
+
onPages?: (pages: number) => void
|
|
299
|
+
children: React.ReactNode
|
|
300
|
+
}) {
|
|
301
|
+
const column = React.useRef<HTMLDivElement>(null)
|
|
302
|
+
const pages = usePagination(editor, column, true)
|
|
303
|
+
React.useEffect(() => onPages?.(pages), [pages, onPages])
|
|
304
|
+
|
|
305
|
+
return (
|
|
306
|
+
<div className="wy-pv" style={{ zoom } as React.CSSProperties}>
|
|
307
|
+
<div
|
|
308
|
+
className="wy-pv__stack light"
|
|
309
|
+
data-accent-use={theme?.accentUse}
|
|
310
|
+
data-large-print={largePrint(theme) || undefined}
|
|
311
|
+
style={{ ...themeStyle(theme), height: pages * PAGE.height + (pages - 1) * PAGE.gap }}
|
|
312
|
+
>
|
|
313
|
+
{Array.from({ length: pages }, (_, i) => (
|
|
314
|
+
<div
|
|
315
|
+
key={i}
|
|
316
|
+
className={cn("wy-pv__page", showMargins && "show-margins")}
|
|
317
|
+
style={{ top: i * (PAGE.height + PAGE.gap) }}
|
|
318
|
+
aria-hidden="true"
|
|
319
|
+
>
|
|
320
|
+
<div className="wy-pv__margin" />
|
|
321
|
+
{i > 0 && (
|
|
322
|
+
<div className="wy-pv__run">
|
|
323
|
+
{letterhead ? <LetterheadMark letterhead={letterhead} size={20} showName={false} /> : <span />}
|
|
324
|
+
<span>{fill(labels.pageContinued, { title: title || labels.untitled })}</span>
|
|
325
|
+
</div>
|
|
326
|
+
)}
|
|
327
|
+
<div className="wy-pv__foot">
|
|
328
|
+
<span>
|
|
329
|
+
{fill(labels.pageOf, { page: i + 1, pages })}
|
|
330
|
+
{footerRef ? ` · ${footerRef}` : ""}
|
|
331
|
+
</span>
|
|
332
|
+
<span>{letterhead?.name}</span>
|
|
333
|
+
</div>
|
|
334
|
+
</div>
|
|
335
|
+
))}
|
|
336
|
+
<div ref={column} className="wy-pv__column">
|
|
337
|
+
{letterhead !== null && (
|
|
338
|
+
<div className="wy-pv__lh">
|
|
339
|
+
{letterhead ? (
|
|
340
|
+
<div className="grid content-start gap-1.5">
|
|
341
|
+
<LetterheadMark letterhead={letterhead} size={32} showName />
|
|
342
|
+
{letterhead.lines.map((line, i) => (
|
|
343
|
+
<span key={i} dir="auto">
|
|
344
|
+
{line}
|
|
345
|
+
</span>
|
|
346
|
+
))}
|
|
347
|
+
</div>
|
|
348
|
+
) : (
|
|
349
|
+
<span />
|
|
350
|
+
)}
|
|
351
|
+
<div className="wy-pv__lhend">
|
|
352
|
+
<h1 className="wy-pv__title" dir="auto">
|
|
353
|
+
{title || labels.untitled}
|
|
354
|
+
</h1>
|
|
355
|
+
{reference && <b>{reference}</b>}
|
|
356
|
+
{date && <span>{date}</span>}
|
|
357
|
+
</div>
|
|
358
|
+
</div>
|
|
359
|
+
)}
|
|
360
|
+
{children}
|
|
361
|
+
</div>
|
|
362
|
+
</div>
|
|
363
|
+
</div>
|
|
364
|
+
)
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** The zoom that fits a page to `width` px of canvas, no larger than 100%. */
|
|
368
|
+
export function fitZoom(width: number): number {
|
|
369
|
+
return Math.max(0.3, Math.min(1, (width - 48) / PAGE.width))
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export { PageView }
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import type { Editor } from "@tiptap/core"
|
|
5
|
+
import { useEditorState } from "@tiptap/react"
|
|
6
|
+
|
|
7
|
+
import { Icon, type IconName } from "../icon"
|
|
8
|
+
import { IconButton } from "../icon-button"
|
|
9
|
+
import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle } from "../sheet"
|
|
10
|
+
import { cn } from "../utils"
|
|
11
|
+
import { CATALOGUE, applyItem, currentItem } from "./catalogue"
|
|
12
|
+
import { turnInto } from "./behaviour"
|
|
13
|
+
import type { EditorMode } from "./contract"
|
|
14
|
+
import type { EditorLabels } from "./labels"
|
|
15
|
+
|
|
16
|
+
/** The web app on a phone (PW.6): under 640px, the same break as the shell's tab bar. */
|
|
17
|
+
const PHONE_QUERY = "(max-width: 639.98px)"
|
|
18
|
+
|
|
19
|
+
export function useEditorPhone(): boolean {
|
|
20
|
+
const [phone, setPhone] = React.useState(false)
|
|
21
|
+
React.useEffect(() => {
|
|
22
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function") return
|
|
23
|
+
const mq = window.matchMedia(PHONE_QUERY)
|
|
24
|
+
const on = () => setPhone(mq.matches)
|
|
25
|
+
on()
|
|
26
|
+
mq.addEventListener("change", on)
|
|
27
|
+
return () => mq.removeEventListener("change", on)
|
|
28
|
+
}, [])
|
|
29
|
+
return phone
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* How far the on-screen keyboard covers the bottom of the layout viewport, in
|
|
34
|
+
* px (0 when it is closed or the browser has no visual viewport). The docked
|
|
35
|
+
* toolbar rides this far up so it sits on top of the keyboard.
|
|
36
|
+
*/
|
|
37
|
+
export function useKeyboardInset(active: boolean): number {
|
|
38
|
+
const [inset, setInset] = React.useState(0)
|
|
39
|
+
React.useEffect(() => {
|
|
40
|
+
const vv = typeof window === "undefined" ? undefined : window.visualViewport
|
|
41
|
+
if (!active || !vv) {
|
|
42
|
+
setInset(0)
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
const update = () => {
|
|
46
|
+
const covered = window.innerHeight - vv.height - vv.offsetTop
|
|
47
|
+
// Under ~80px is browser chrome moving, not a keyboard.
|
|
48
|
+
setInset(covered > 80 ? Math.round(covered) : 0)
|
|
49
|
+
}
|
|
50
|
+
update()
|
|
51
|
+
vv.addEventListener("resize", update)
|
|
52
|
+
vv.addEventListener("scroll", update)
|
|
53
|
+
return () => {
|
|
54
|
+
vv.removeEventListener("resize", update)
|
|
55
|
+
vv.removeEventListener("scroll", update)
|
|
56
|
+
}
|
|
57
|
+
}, [active])
|
|
58
|
+
return inset
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const MODE_ICON: Record<EditorMode, IconName> = { editing: "edit", suggesting: "suggest", viewing: "view" }
|
|
62
|
+
|
|
63
|
+
export interface ModeChoice {
|
|
64
|
+
mode: EditorMode
|
|
65
|
+
icon: IconName
|
|
66
|
+
title: string
|
|
67
|
+
hint: string
|
|
68
|
+
disabled?: boolean
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The mode switch as a bottom sheet (PW.6): three rows, the current one ticked. */
|
|
72
|
+
export function ModeSheet({
|
|
73
|
+
open,
|
|
74
|
+
onOpenChange,
|
|
75
|
+
labels,
|
|
76
|
+
mode,
|
|
77
|
+
choices,
|
|
78
|
+
onPick,
|
|
79
|
+
}: {
|
|
80
|
+
open: boolean
|
|
81
|
+
onOpenChange: (open: boolean) => void
|
|
82
|
+
labels: EditorLabels
|
|
83
|
+
mode: EditorMode
|
|
84
|
+
choices: ModeChoice[]
|
|
85
|
+
onPick: (mode: EditorMode) => void
|
|
86
|
+
}) {
|
|
87
|
+
return (
|
|
88
|
+
<Sheet open={open} onOpenChange={onOpenChange}>
|
|
89
|
+
<SheetContent side="bottom" showCloseButton={false} className="gap-3 rounded-t-dialog pb-[calc(1.25rem+env(safe-area-inset-bottom))]">
|
|
90
|
+
<SheetHeader>
|
|
91
|
+
<SheetTitle className="text-base">{labels.modeMenu}</SheetTitle>
|
|
92
|
+
<SheetDescription className="sr-only">{labels.modeMenu}</SheetDescription>
|
|
93
|
+
</SheetHeader>
|
|
94
|
+
<div role="radiogroup" aria-label={labels.modeMenu} className="grid overflow-hidden rounded-control-lg border border-border">
|
|
95
|
+
{choices.map((c) => (
|
|
96
|
+
<button
|
|
97
|
+
key={c.mode}
|
|
98
|
+
type="button"
|
|
99
|
+
role="radio"
|
|
100
|
+
aria-checked={mode === c.mode}
|
|
101
|
+
disabled={c.disabled}
|
|
102
|
+
onClick={() => {
|
|
103
|
+
onPick(c.mode)
|
|
104
|
+
onOpenChange(false)
|
|
105
|
+
}}
|
|
106
|
+
className="flex min-h-12 items-center gap-3 border-0 border-b border-border bg-transparent px-3 py-2 text-start last:border-b-0 hover:bg-muted disabled:opacity-50"
|
|
107
|
+
>
|
|
108
|
+
<Icon name={c.icon} />
|
|
109
|
+
<span className="grid min-w-0 flex-1 gap-0.5">
|
|
110
|
+
<span className="text-sm font-medium text-foreground">{c.title}</span>
|
|
111
|
+
<span className="text-[0.8125rem] text-muted-foreground">{c.hint}</span>
|
|
112
|
+
</span>
|
|
113
|
+
{mode === c.mode && <Icon name="check" className="text-primary" />}
|
|
114
|
+
</button>
|
|
115
|
+
))}
|
|
116
|
+
</div>
|
|
117
|
+
</SheetContent>
|
|
118
|
+
</Sheet>
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The six tiles More tools leads with; the rest stay in the / menu and Insert on desktop. */
|
|
123
|
+
const PHONE_TILES = ["h2", "table", "image", "signature", "pageBreak", "callout"]
|
|
124
|
+
|
|
125
|
+
export interface MoreAction {
|
|
126
|
+
id: string
|
|
127
|
+
icon: IconName
|
|
128
|
+
label: string
|
|
129
|
+
/** A toggle that is on shows a tick in place of the chevron. */
|
|
130
|
+
on?: boolean
|
|
131
|
+
onSelect: () => void
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** More tools as a bottom sheet (PW.6): the actions, then the Insert tiles. */
|
|
135
|
+
export function MoreToolsSheet({
|
|
136
|
+
open,
|
|
137
|
+
onOpenChange,
|
|
138
|
+
labels,
|
|
139
|
+
editor,
|
|
140
|
+
editable,
|
|
141
|
+
actions,
|
|
142
|
+
extra,
|
|
143
|
+
}: {
|
|
144
|
+
open: boolean
|
|
145
|
+
onOpenChange: (open: boolean) => void
|
|
146
|
+
labels: EditorLabels
|
|
147
|
+
/** The host's own header controls (the theme picker), shown under the actions. */
|
|
148
|
+
extra?: React.ReactNode
|
|
149
|
+
editor: Editor | null
|
|
150
|
+
editable: boolean
|
|
151
|
+
actions: MoreAction[]
|
|
152
|
+
}) {
|
|
153
|
+
return (
|
|
154
|
+
<Sheet open={open} onOpenChange={onOpenChange}>
|
|
155
|
+
<SheetContent side="bottom" showCloseButton={false} className="max-h-[85%] gap-3 rounded-t-dialog pb-[calc(1.25rem+env(safe-area-inset-bottom))]">
|
|
156
|
+
<SheetHeader>
|
|
157
|
+
<SheetTitle className="text-base">{labels.moreTools}</SheetTitle>
|
|
158
|
+
<SheetDescription className="sr-only">{labels.moreTools}</SheetDescription>
|
|
159
|
+
</SheetHeader>
|
|
160
|
+
{actions.length > 0 && (
|
|
161
|
+
<div className="grid overflow-hidden rounded-control-lg border border-border">
|
|
162
|
+
{actions.map((a) => (
|
|
163
|
+
<button
|
|
164
|
+
key={a.id}
|
|
165
|
+
type="button"
|
|
166
|
+
onClick={() => {
|
|
167
|
+
onOpenChange(false)
|
|
168
|
+
a.onSelect()
|
|
169
|
+
}}
|
|
170
|
+
className="flex min-h-12 w-full items-center gap-3 border-0 border-b border-border bg-transparent px-3 text-start text-sm text-foreground last:border-b-0 hover:bg-muted"
|
|
171
|
+
>
|
|
172
|
+
<Icon name={a.icon} className="text-muted-foreground" />
|
|
173
|
+
<span className="min-w-0 flex-1">{a.label}</span>
|
|
174
|
+
{a.on ? <Icon name="check" size={16} className="text-primary" /> : <Icon name="chevron-right" size={16} className="text-muted-foreground" />}
|
|
175
|
+
</button>
|
|
176
|
+
))}
|
|
177
|
+
</div>
|
|
178
|
+
)}
|
|
179
|
+
{extra && <div className="flex flex-wrap items-center gap-2">{extra}</div>}
|
|
180
|
+
{editor && editable && (
|
|
181
|
+
<>
|
|
182
|
+
<h3 className="m-0 text-[0.8125rem] font-medium text-muted-foreground">{labels.insert}</h3>
|
|
183
|
+
<div className="grid grid-cols-3 gap-2">
|
|
184
|
+
{PHONE_TILES.map((id) => CATALOGUE.find((i) => i.id === id)!).map((item) => (
|
|
185
|
+
<button
|
|
186
|
+
key={item.id}
|
|
187
|
+
type="button"
|
|
188
|
+
onClick={() => {
|
|
189
|
+
onOpenChange(false)
|
|
190
|
+
applyItem(editor, item)
|
|
191
|
+
}}
|
|
192
|
+
className="grid min-h-20 content-center place-items-center gap-1.5 rounded-control-lg border-0 bg-muted px-1 py-2 text-center text-[0.8125rem] text-foreground hover:bg-accent"
|
|
193
|
+
>
|
|
194
|
+
<Icon name={item.icon} size={22} className="text-primary" />
|
|
195
|
+
<span className="max-w-full min-w-0 truncate">{labels[item.label]}</span>
|
|
196
|
+
</button>
|
|
197
|
+
))}
|
|
198
|
+
</div>
|
|
199
|
+
</>
|
|
200
|
+
)}
|
|
201
|
+
</SheetContent>
|
|
202
|
+
</Sheet>
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The formatting toolbar docked at the bottom on a phone (PW.6): text style,
|
|
208
|
+
* bold, italic, list, insert, comment, then More tools and Hide keyboard. It
|
|
209
|
+
* rides on top of the on-screen keyboard by `inset` px. Suggesting tints it
|
|
210
|
+
* amber, as the mode button.
|
|
211
|
+
*/
|
|
212
|
+
export function PhoneToolbar({
|
|
213
|
+
editor,
|
|
214
|
+
labels,
|
|
215
|
+
editable,
|
|
216
|
+
suggesting,
|
|
217
|
+
inset,
|
|
218
|
+
onMore,
|
|
219
|
+
onComment,
|
|
220
|
+
onInsert,
|
|
221
|
+
}: {
|
|
222
|
+
editor: Editor
|
|
223
|
+
labels: EditorLabels
|
|
224
|
+
editable: boolean
|
|
225
|
+
suggesting: boolean
|
|
226
|
+
inset: number
|
|
227
|
+
onMore: () => void
|
|
228
|
+
onComment?: () => void
|
|
229
|
+
onInsert: () => void
|
|
230
|
+
}) {
|
|
231
|
+
const s = useEditorState({
|
|
232
|
+
editor,
|
|
233
|
+
selector: ({ editor: e }) => ({
|
|
234
|
+
bold: e.isActive("bold"),
|
|
235
|
+
italic: e.isActive("italic"),
|
|
236
|
+
item: currentItem(e)?.id ?? "text",
|
|
237
|
+
empty: e.state.selection.empty,
|
|
238
|
+
}),
|
|
239
|
+
})
|
|
240
|
+
const [typing, setTyping] = React.useState(false)
|
|
241
|
+
React.useEffect(() => {
|
|
242
|
+
const on = () => setTyping(document.activeElement instanceof HTMLElement && document.activeElement.isContentEditable)
|
|
243
|
+
const later = () => requestAnimationFrame(on)
|
|
244
|
+
document.addEventListener("focusin", later)
|
|
245
|
+
document.addEventListener("focusout", later)
|
|
246
|
+
on()
|
|
247
|
+
return () => {
|
|
248
|
+
document.removeEventListener("focusin", later)
|
|
249
|
+
document.removeEventListener("focusout", later)
|
|
250
|
+
}
|
|
251
|
+
}, [])
|
|
252
|
+
const heading = s.item === "h1" || s.item === "h2" || s.item === "h3"
|
|
253
|
+
const press = (e: React.MouseEvent) => e.preventDefault()
|
|
254
|
+
const btn = (icon: IconName, label: string, pressed: boolean, onClick: () => void, disabled = !editable) => (
|
|
255
|
+
<IconButton icon={icon} label={label} size="lg" tooltip={false} pressed={pressed} disabled={disabled} onMouseDown={press} onClick={onClick} />
|
|
256
|
+
)
|
|
257
|
+
return (
|
|
258
|
+
<div
|
|
259
|
+
role="toolbar"
|
|
260
|
+
aria-label={labels.formatting}
|
|
261
|
+
data-slot="editor-toolbar"
|
|
262
|
+
data-phone="true"
|
|
263
|
+
style={{ insetBlockEnd: inset }}
|
|
264
|
+
className={cn(
|
|
265
|
+
"fixed inset-x-0 z-30 flex items-center gap-1 border-t border-border bg-card px-2",
|
|
266
|
+
inset === 0 ? "h-[calc(3.5rem+env(safe-area-inset-bottom))] pb-[env(safe-area-inset-bottom)]" : "h-14",
|
|
267
|
+
suggesting && "bg-warning-tint"
|
|
268
|
+
)}
|
|
269
|
+
>
|
|
270
|
+
<button
|
|
271
|
+
type="button"
|
|
272
|
+
aria-label={labels.styleMenu}
|
|
273
|
+
aria-pressed={heading}
|
|
274
|
+
disabled={!editable}
|
|
275
|
+
onMouseDown={press}
|
|
276
|
+
onClick={() => {
|
|
277
|
+
if (heading) turnInto(editor, "paragraph")
|
|
278
|
+
else turnInto(editor, "heading", { level: 2 })
|
|
279
|
+
}}
|
|
280
|
+
className={cn(
|
|
281
|
+
"inline-flex size-11 shrink-0 cursor-pointer items-center justify-center rounded-control-lg border-0 bg-transparent text-sm font-semibold text-foreground disabled:opacity-40",
|
|
282
|
+
heading && "bg-primary-tint text-primary-tint-foreground"
|
|
283
|
+
)}
|
|
284
|
+
>
|
|
285
|
+
Aa
|
|
286
|
+
</button>
|
|
287
|
+
{btn("bold", labels.bold, s.bold, () => editor.chain().focus().toggleMark("bold").run())}
|
|
288
|
+
{btn("italic", labels.italic, s.italic, () => editor.chain().focus().toggleMark("italic").run())}
|
|
289
|
+
{btn("list-bullet", labels.blockBullet, s.item === "bullet", () => {
|
|
290
|
+
const item = CATALOGUE.find((i) => i.id === "bullet")!
|
|
291
|
+
if (s.item === "bullet") turnInto(editor, "paragraph")
|
|
292
|
+
else turnInto(editor, item.node.type, item.node.attrs)
|
|
293
|
+
})}
|
|
294
|
+
{btn("plus", labels.insert, false, onInsert)}
|
|
295
|
+
{onComment && btn("comment-add", labels.comment, false, onComment, s.empty)}
|
|
296
|
+
<span className="min-w-1 flex-1" />
|
|
297
|
+
<IconButton icon="more" label={labels.moreTools} size="lg" tooltip={false} onMouseDown={press} onClick={onMore} />
|
|
298
|
+
{typing && (
|
|
299
|
+
<IconButton
|
|
300
|
+
icon="keyboard"
|
|
301
|
+
label={labels.hideKeyboard}
|
|
302
|
+
size="lg"
|
|
303
|
+
tooltip={false}
|
|
304
|
+
onMouseDown={press}
|
|
305
|
+
onClick={() => (document.activeElement as HTMLElement | null)?.blur()}
|
|
306
|
+
/>
|
|
307
|
+
)}
|
|
308
|
+
</div>
|
|
309
|
+
)
|
|
310
|
+
}
|