@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.
- package/README.md +107 -107
- package/package.json +221 -192
- package/src/access-denied.stories.tsx +179 -179
- package/src/access-denied.tsx +305 -305
- package/src/action-proposal.tsx +338 -302
- package/src/actor-chip.tsx +71 -71
- package/src/agent-activity.tsx +163 -163
- package/src/agent-collaboration.stories.tsx +156 -156
- package/src/agent-run-status.tsx +196 -196
- package/src/alert-dialog.stories.tsx +45 -45
- package/src/alert-dialog.tsx +147 -147
- package/src/alert.stories.tsx +91 -91
- package/src/alert.tsx +89 -89
- package/src/app-sidebar.stories.tsx +195 -195
- package/src/app-sidebar.tsx +604 -500
- package/src/approval-queue.tsx +109 -108
- package/src/audience-group-picker.stories.tsx +198 -198
- package/src/audience-group-picker.tsx +362 -362
- package/src/audience-people-picker.stories.tsx +102 -102
- package/src/audience-people-picker.tsx +279 -279
- package/src/avatar-pattern.ts +138 -0
- package/src/avatar.tsx +125 -83
- package/src/badge.stories.tsx +105 -105
- package/src/badge.tsx +68 -68
- package/src/beta-warning.stories.tsx +79 -79
- package/src/beta-warning.tsx +189 -189
- package/src/bilingual-input.stories.tsx +82 -82
- package/src/bilingual-input.tsx +188 -183
- package/src/board.tsx +216 -0
- package/src/breadcrumb.stories.tsx +51 -51
- package/src/breadcrumb.tsx +90 -90
- package/src/built-on-whity.tsx +52 -0
- package/src/button.stories.tsx +87 -87
- package/src/button.tsx +123 -118
- package/src/card.stories.tsx +423 -423
- package/src/card.tsx +180 -180
- package/src/chart.stories.tsx +66 -66
- package/src/chart.tsx +228 -228
- package/src/checkbox.stories.tsx +62 -62
- package/src/checkbox.tsx +54 -54
- package/src/collapsible.stories.tsx +48 -48
- package/src/collapsible.tsx +34 -34
- package/src/color-card.stories.tsx +61 -61
- package/src/color-card.tsx +270 -270
- package/src/color-picker.stories.tsx +65 -65
- package/src/color-picker.tsx +425 -425
- package/src/column-matcher.tsx +84 -0
- package/src/command-palette.tsx +398 -306
- package/src/confirm-action.tsx +98 -0
- package/src/copy-block.stories.tsx +38 -38
- package/src/copy-block.tsx +85 -85
- package/src/data-table.stories.tsx +200 -200
- package/src/data-table.tsx +1686 -1106
- package/src/date-picker.stories.tsx +61 -61
- package/src/date-picker.tsx +456 -422
- package/src/design-cards.stories.tsx +551 -458
- package/src/dialog.stories.tsx +48 -48
- package/src/dialog.tsx +179 -166
- package/src/display-preferences.tsx +50 -0
- package/src/documents/barcode-svg.stories.tsx +72 -72
- package/src/documents/barcode-svg.tsx +79 -79
- package/src/dropdown-menu.stories.tsx +77 -77
- package/src/dropdown-menu.tsx +271 -271
- 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 -279
- package/src/editor/block-ids.ts +44 -44
- package/src/editor/block-list.tsx +104 -101
- package/src/editor/block-toolbar.tsx +490 -477
- package/src/editor/bubble.tsx +119 -93
- package/src/editor/catalogue.ts +169 -103
- package/src/editor/collab-rail.tsx +746 -746
- package/src/editor/collab.stories.tsx +207 -207
- package/src/editor/collab.ts +87 -87
- package/src/editor/contract.ts +476 -408
- package/src/editor/convert.ts +414 -362
- package/src/editor/doc-editor.stories.tsx +98 -98
- package/src/editor/doc-editor.tsx +1492 -1287
- package/src/editor/editor.css +636 -536
- package/src/editor/extensions.ts +86 -86
- package/src/editor/field-bar.tsx +154 -0
- package/src/editor/field-picker.tsx +109 -107
- package/src/editor/fields.ts +100 -100
- package/src/editor/gutter.tsx +443 -367
- package/src/editor/index.ts +23 -22
- package/src/editor/label-sheet.tsx +289 -289
- package/src/editor/labels.ts +394 -310
- package/src/editor/link-tools.tsx +141 -141
- package/src/editor/node-views.tsx +461 -450
- package/src/editor/page-view.tsx +372 -353
- 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 -182
- package/src/editor/rich-blocks.stories.tsx +146 -146
- package/src/editor/sample.ts +40 -40
- 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 -150
- package/src/editor/schema.ts +368 -315
- package/src/editor/services.tsx +205 -127
- package/src/editor/slash.ts +64 -64
- package/src/editor/suggesting.ts +372 -372
- package/src/editor/suggestions.ts +763 -763
- package/src/editor/table-keys.ts +66 -66
- package/src/editor/templates.stories.tsx +324 -324
- package/src/editor/toolbar.tsx +364 -358
- package/src/empty-state.stories.tsx +51 -51
- package/src/empty-state.tsx +126 -100
- package/src/empty-state.usage-notes.md +30 -30
- package/src/form.stories.tsx +112 -112
- package/src/form.tsx +176 -176
- package/src/globals.css +20 -20
- package/src/icon-button.tsx +85 -85
- package/src/icon-map.ts +335 -244
- package/src/icon.tsx +41 -41
- package/src/index.ts +90 -67
- package/src/input.stories.tsx +157 -157
- package/src/input.tsx +768 -759
- package/src/kpi-strip.tsx +47 -0
- package/src/localized-text.ts +76 -0
- package/src/locked-screen.stories.tsx +29 -29
- package/src/locked-screen.tsx +75 -61
- package/src/lockup.tsx +42 -0
- package/src/lottie.tsx +441 -0
- package/src/mark.tsx +62 -62
- package/src/math-text.stories.tsx +33 -33
- package/src/math-text.tsx +43 -43
- package/src/menu-bar.stories.tsx +198 -198
- package/src/menu-bar.tsx +373 -373
- package/src/message-thread.tsx +181 -0
- package/src/money/currency.ts +244 -230
- 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 -95
- package/src/page-header.tsx +122 -122
- package/src/page-shell.stories.tsx +42 -42
- package/src/page-shell.tsx +32 -32
- package/src/page-states.tsx +277 -0
- package/src/pagination.stories.tsx +45 -45
- package/src/pagination.tsx +98 -98
- 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 -180
- package/src/plugin-card.tsx +931 -848
- package/src/popover.stories.tsx +48 -48
- package/src/popover.tsx +55 -55
- package/src/radio-group.stories.tsx +26 -26
- package/src/radio-group.tsx +44 -44
- package/src/route-flow/editor.tsx +1087 -1014
- package/src/route-flow/model.ts +771 -716
- package/src/save-bar.tsx +94 -0
- package/src/scope-grant.tsx +139 -139
- package/src/screen-too-small.stories.tsx +61 -61
- package/src/screen-too-small.tsx +104 -104
- package/src/search-field.tsx +65 -0
- package/src/select.stories.tsx +44 -44
- package/src/select.tsx +197 -197
- package/src/separator.stories.tsx +34 -34
- package/src/separator.tsx +34 -34
- package/src/sheet.stories.tsx +49 -49
- package/src/sheet.tsx +168 -168
- package/src/skeleton.stories.tsx +28 -28
- package/src/skeleton.tsx +19 -19
- package/src/slider.stories.tsx +78 -78
- package/src/slider.tsx +300 -300
- package/src/spinner.stories.tsx +33 -33
- package/src/spinner.tsx +55 -55
- package/src/stepper.tsx +85 -0
- package/src/switch.stories.tsx +42 -42
- package/src/switch.tsx +41 -41
- package/src/switcher.stories.tsx +111 -111
- package/src/switcher.tsx +131 -131
- package/src/system-state.tsx +55 -0
- package/src/table.stories.tsx +48 -48
- package/src/table.tsx +121 -121
- package/src/tabs.stories.tsx +74 -74
- package/src/tabs.tsx +90 -90
- package/src/tag-input.stories.tsx +67 -67
- package/src/tag-input.tsx +117 -117
- package/src/tenant-logo.tsx +63 -63
- package/src/tenant-switcher.stories.tsx +38 -38
- package/src/tenant-switcher.tsx +164 -164
- package/src/textarea.stories.tsx +27 -27
- package/src/textarea.tsx +18 -18
- package/src/theme-mode.ts +52 -52
- package/src/time-grid.tsx +163 -0
- package/src/time-picker.stories.tsx +62 -62
- package/src/time-picker.tsx +615 -615
- package/src/toggle-group.tsx +138 -138
- package/src/tooltip.stories.tsx +43 -43
- package/src/tooltip.tsx +73 -73
- package/src/top-nav.stories.tsx +58 -58
- package/src/top-nav.tsx +324 -324
- package/src/undo-bar.tsx +107 -107
- package/src/utils.ts +45 -45
- package/src/view-toggle.stories.tsx +84 -84
- package/src/view-toggle.tsx +110 -110
package/src/editor/toolbar.tsx
CHANGED
|
@@ -1,358 +1,364 @@
|
|
|
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 {
|
|
8
|
-
DropdownMenu,
|
|
9
|
-
DropdownMenuContent,
|
|
10
|
-
DropdownMenuItem,
|
|
11
|
-
DropdownMenuShortcut,
|
|
12
|
-
DropdownMenuTrigger,
|
|
13
|
-
} from "../dropdown-menu"
|
|
14
|
-
import { Icon } from "../icon"
|
|
15
|
-
import { IconButton } from "../icon-button"
|
|
16
|
-
import { Popover, PopoverContent, PopoverTrigger } from "../popover"
|
|
17
|
-
import { ToggleGroup } from "../toggle-group"
|
|
18
|
-
import { cn } from "../utils"
|
|
19
|
-
import { CATALOGUE, applyItem, currentItem } from "./catalogue"
|
|
20
|
-
import { turnInto } from "./behaviour"
|
|
21
|
-
import { FieldPicker, flatFields } from "./field-picker"
|
|
22
|
-
import { filterFields, type FieldDef, type FieldGroup } from "./fields"
|
|
23
|
-
import { fill, type EditorLabels } from "./labels"
|
|
24
|
-
|
|
25
|
-
/** E.7: the page view's zoom steps; "fit" fits the page to the canvas width. */
|
|
26
|
-
export type PageZoom = "fit" | 50 | 75 | 100 | 125
|
|
27
|
-
export const ZOOM_STEPS: PageZoom[] = ["fit", 50, 75, 100, 125]
|
|
28
|
-
export type EditorView = "continuous" | "page"
|
|
29
|
-
|
|
30
|
-
/** Inserts a merge field at the caret as one character, followed by a space. */
|
|
31
|
-
export function insertField(editor: Editor, field: FieldDef) {
|
|
32
|
-
editor
|
|
33
|
-
.chain()
|
|
34
|
-
.focus()
|
|
35
|
-
.insertContent([
|
|
36
|
-
{ type: "field", attrs: { path: field.path, label: field.label, required: field.required ? true : null } },
|
|
37
|
-
{ type: "text", text: " " },
|
|
38
|
-
])
|
|
39
|
-
.run()
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const STYLE_IDS = ["text", "h1", "h2", "h3"]
|
|
43
|
-
const STYLE_SHORTCUTS: Record<string, string> = { text: "⌘⌥0", h1: "⌘⌥1", h2: "⌘⌥2", h3: "⌘⌥3" }
|
|
44
|
-
|
|
45
|
-
function Sep() {
|
|
46
|
-
return <span aria-hidden="true" className="mx-1.5 h-5 w-px shrink-0 bg-border" />
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** A text dropdown in the bar: "Normal text ▾", "Insert ▾" (`.wy-edx__drop`). */
|
|
50
|
-
const BarDrop = React.forwardRef<HTMLButtonElement, React.ComponentProps<"button">>(function BarDrop(
|
|
51
|
-
{ className, children, ...props },
|
|
52
|
-
ref
|
|
53
|
-
) {
|
|
54
|
-
return (
|
|
55
|
-
<button
|
|
56
|
-
ref={ref}
|
|
57
|
-
type="button"
|
|
58
|
-
className={cn(
|
|
59
|
-
"inline-flex h-7 shrink-0 cursor-pointer items-center gap-1 rounded-control-sm border-0 bg-transparent ps-2 pe-1.5 text-[0.8125rem] whitespace-nowrap text-foreground hover:bg-muted focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring disabled:pointer-events-none disabled:opacity-40",
|
|
60
|
-
className
|
|
61
|
-
)}
|
|
62
|
-
{...props}
|
|
63
|
-
>
|
|
64
|
-
{children}
|
|
65
|
-
<Icon name="chevron-down" size={14} className="text-muted-foreground" />
|
|
66
|
-
</button>
|
|
67
|
-
)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
/** Field ▾ (EditorInline): the field picker with its own search box. */
|
|
71
|
-
function FieldMenu({
|
|
72
|
-
editor,
|
|
73
|
-
groups,
|
|
74
|
-
labels,
|
|
75
|
-
disabled,
|
|
76
|
-
onCreate,
|
|
77
|
-
}: {
|
|
78
|
-
editor: Editor
|
|
79
|
-
groups: FieldGroup[]
|
|
80
|
-
labels: EditorLabels
|
|
81
|
-
disabled: boolean
|
|
82
|
-
/** A template's own field named `label`, added to the document (editor M4b). */
|
|
83
|
-
onCreate?: (label: string) => FieldDef
|
|
84
|
-
}) {
|
|
85
|
-
const [open, setOpen] = React.useState(false)
|
|
86
|
-
const [query, setQuery] = React.useState("")
|
|
87
|
-
const [active, setActive] = React.useState(0)
|
|
88
|
-
const shown = React.useMemo(() => filterFields(groups, query), [groups, query])
|
|
89
|
-
const flat = flatFields(shown)
|
|
90
|
-
React.useEffect(() => setActive(0), [query, open])
|
|
91
|
-
const pick = (field: FieldDef) => {
|
|
92
|
-
setOpen(false)
|
|
93
|
-
setQuery("")
|
|
94
|
-
insertField(editor, field)
|
|
95
|
-
}
|
|
96
|
-
const name = query.trim()
|
|
97
|
-
const exists = flatFields(groups).some((f) => f.label.toLowerCase() === name.toLowerCase())
|
|
98
|
-
const create = onCreate && name && !exists ? () => pick(onCreate(name)) : undefined
|
|
99
|
-
return (
|
|
100
|
-
<Popover open={open} onOpenChange={setOpen}>
|
|
101
|
-
<PopoverTrigger asChild>
|
|
102
|
-
<BarDrop disabled={disabled}>
|
|
103
|
-
<Icon name="field" size={14} />
|
|
104
|
-
{labels.field}
|
|
105
|
-
</BarDrop>
|
|
106
|
-
</PopoverTrigger>
|
|
107
|
-
<PopoverContent align="start" className="w-auto border-0 bg-transparent p-0 shadow-none" onCloseAutoFocus={(e) => e.preventDefault()}>
|
|
108
|
-
<FieldPicker
|
|
109
|
-
id="wy-fieldmenu"
|
|
110
|
-
groups={shown}
|
|
111
|
-
active={Math.min(active, Math.max(0, flat.length - 1))}
|
|
112
|
-
onActive={setActive}
|
|
113
|
-
onPick={pick}
|
|
114
|
-
labels={labels}
|
|
115
|
-
search={
|
|
116
|
-
<label className="flex items-center gap-1.5 border-b border-border px-2 pb-1.5 pt-1 text-muted-foreground">
|
|
117
|
-
<Icon name="search" size={14} />
|
|
118
|
-
<input
|
|
119
|
-
autoFocus
|
|
120
|
-
value={query}
|
|
121
|
-
onChange={(e) => setQuery(e.target.value)}
|
|
122
|
-
onKeyDown={(e) => {
|
|
123
|
-
const n = flat.length || 1
|
|
124
|
-
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
125
|
-
e.preventDefault()
|
|
126
|
-
setActive((i) => (i + (e.key === "ArrowDown" ? 1 : n - 1)) % n)
|
|
127
|
-
} else if (e.key === "Enter" && flat[active]) {
|
|
128
|
-
e.preventDefault()
|
|
129
|
-
pick(flat[active])
|
|
130
|
-
} else if (e.key === "Enter" && create) {
|
|
131
|
-
e.preventDefault()
|
|
132
|
-
create()
|
|
133
|
-
}
|
|
134
|
-
}}
|
|
135
|
-
placeholder={labels.fieldSearch}
|
|
136
|
-
aria-label={labels.fieldSearch}
|
|
137
|
-
aria-controls="wy-fieldmenu"
|
|
138
|
-
aria-activedescendant={flat.length ? `wy-fieldmenu-${active}` : undefined}
|
|
139
|
-
className="h-7 min-w-0 flex-1 bg-transparent text-sm text-foreground outline-none placeholder:text-muted-foreground"
|
|
140
|
-
/>
|
|
141
|
-
</label>
|
|
142
|
-
}
|
|
143
|
-
footer={
|
|
144
|
-
onCreate && (
|
|
145
|
-
<div className="border-t border-border pt-1">
|
|
146
|
-
{create ? (
|
|
147
|
-
<button
|
|
148
|
-
type="button"
|
|
149
|
-
onClick={create}
|
|
150
|
-
className="flex w-full items-center gap-1.5 rounded-control px-2 py-1.5 text-start text-sm hover:bg-accent"
|
|
151
|
-
>
|
|
152
|
-
<Icon name="plus" size={14} />
|
|
153
|
-
<span className="min-w-0 truncate" dir="auto">
|
|
154
|
-
{fill(labels.fieldNew, { name })}
|
|
155
|
-
</span>
|
|
156
|
-
</button>
|
|
157
|
-
) : (
|
|
158
|
-
<p className="m-0 px-2 py-1.5 text-xs text-muted-foreground">{labels.fieldNewHint}</p>
|
|
159
|
-
)}
|
|
160
|
-
</div>
|
|
161
|
-
)
|
|
162
|
-
}
|
|
163
|
-
/>
|
|
164
|
-
</PopoverContent>
|
|
165
|
-
</Popover>
|
|
166
|
-
)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* The fixed formatting toolbar, in Google Docs order (E.5, EditorShell):
|
|
171
|
-
* undo and redo · style · bold, italic, underline, strikethrough · link,
|
|
172
|
-
* highlight · lists · Insert ▾ · Field ▾ · zoom, view, outline. Every icon
|
|
173
|
-
* button has a tooltip with its shortcut; pressed states use the tint. Under
|
|
174
|
-
* 768px it scrolls sideways.
|
|
175
|
-
*/
|
|
176
|
-
function EditorToolbar({
|
|
177
|
-
editor,
|
|
178
|
-
labels,
|
|
179
|
-
editable,
|
|
180
|
-
outline,
|
|
181
|
-
onOutline,
|
|
182
|
-
onLink,
|
|
183
|
-
fields,
|
|
184
|
-
view,
|
|
185
|
-
onView,
|
|
186
|
-
zoom,
|
|
187
|
-
onZoom,
|
|
188
|
-
sheet,
|
|
189
|
-
onCreateField,
|
|
190
|
-
onComment,
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
fields
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
sheet
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
<
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
{tb("
|
|
281
|
-
|
|
282
|
-
{tb("
|
|
283
|
-
{tb("
|
|
284
|
-
<Sep />
|
|
285
|
-
{tb("
|
|
286
|
-
{tb("
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
{
|
|
309
|
-
<
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
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 {
|
|
8
|
+
DropdownMenu,
|
|
9
|
+
DropdownMenuContent,
|
|
10
|
+
DropdownMenuItem,
|
|
11
|
+
DropdownMenuShortcut,
|
|
12
|
+
DropdownMenuTrigger,
|
|
13
|
+
} from "../dropdown-menu"
|
|
14
|
+
import { Icon } from "../icon"
|
|
15
|
+
import { IconButton } from "../icon-button"
|
|
16
|
+
import { Popover, PopoverContent, PopoverTrigger } from "../popover"
|
|
17
|
+
import { ToggleGroup } from "../toggle-group"
|
|
18
|
+
import { cn } from "../utils"
|
|
19
|
+
import { CATALOGUE, applyItem, currentItem } from "./catalogue"
|
|
20
|
+
import { turnInto } from "./behaviour"
|
|
21
|
+
import { FieldPicker, flatFields } from "./field-picker"
|
|
22
|
+
import { filterFields, type FieldDef, type FieldGroup } from "./fields"
|
|
23
|
+
import { fill, type EditorLabels } from "./labels"
|
|
24
|
+
|
|
25
|
+
/** E.7: the page view's zoom steps; "fit" fits the page to the canvas width. */
|
|
26
|
+
export type PageZoom = "fit" | 50 | 75 | 100 | 125
|
|
27
|
+
export const ZOOM_STEPS: PageZoom[] = ["fit", 50, 75, 100, 125]
|
|
28
|
+
export type EditorView = "continuous" | "page"
|
|
29
|
+
|
|
30
|
+
/** Inserts a merge field at the caret as one character, followed by a space. */
|
|
31
|
+
export function insertField(editor: Editor, field: FieldDef) {
|
|
32
|
+
editor
|
|
33
|
+
.chain()
|
|
34
|
+
.focus()
|
|
35
|
+
.insertContent([
|
|
36
|
+
{ type: "field", attrs: { path: field.path, label: field.label, required: field.required ? true : null } },
|
|
37
|
+
{ type: "text", text: " " },
|
|
38
|
+
])
|
|
39
|
+
.run()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const STYLE_IDS = ["text", "h1", "h2", "h3"]
|
|
43
|
+
const STYLE_SHORTCUTS: Record<string, string> = { text: "⌘⌥0", h1: "⌘⌥1", h2: "⌘⌥2", h3: "⌘⌥3" }
|
|
44
|
+
|
|
45
|
+
function Sep() {
|
|
46
|
+
return <span aria-hidden="true" className="mx-1.5 h-5 w-px shrink-0 bg-border" />
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A text dropdown in the bar: "Normal text ▾", "Insert ▾" (`.wy-edx__drop`). */
|
|
50
|
+
const BarDrop = React.forwardRef<HTMLButtonElement, React.ComponentProps<"button">>(function BarDrop(
|
|
51
|
+
{ className, children, ...props },
|
|
52
|
+
ref
|
|
53
|
+
) {
|
|
54
|
+
return (
|
|
55
|
+
<button
|
|
56
|
+
ref={ref}
|
|
57
|
+
type="button"
|
|
58
|
+
className={cn(
|
|
59
|
+
"inline-flex h-7 shrink-0 cursor-pointer items-center gap-1 rounded-control-sm border-0 bg-transparent ps-2 pe-1.5 text-[0.8125rem] whitespace-nowrap text-foreground hover:bg-muted focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring disabled:pointer-events-none disabled:opacity-40",
|
|
60
|
+
className
|
|
61
|
+
)}
|
|
62
|
+
{...props}
|
|
63
|
+
>
|
|
64
|
+
{children}
|
|
65
|
+
<Icon name="chevron-down" size={14} className="text-muted-foreground" />
|
|
66
|
+
</button>
|
|
67
|
+
)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
/** Field ▾ (EditorInline): the field picker with its own search box. */
|
|
71
|
+
function FieldMenu({
|
|
72
|
+
editor,
|
|
73
|
+
groups,
|
|
74
|
+
labels,
|
|
75
|
+
disabled,
|
|
76
|
+
onCreate,
|
|
77
|
+
}: {
|
|
78
|
+
editor: Editor
|
|
79
|
+
groups: FieldGroup[]
|
|
80
|
+
labels: EditorLabels
|
|
81
|
+
disabled: boolean
|
|
82
|
+
/** A template's own field named `label`, added to the document (editor M4b). */
|
|
83
|
+
onCreate?: (label: string) => FieldDef
|
|
84
|
+
}) {
|
|
85
|
+
const [open, setOpen] = React.useState(false)
|
|
86
|
+
const [query, setQuery] = React.useState("")
|
|
87
|
+
const [active, setActive] = React.useState(0)
|
|
88
|
+
const shown = React.useMemo(() => filterFields(groups, query), [groups, query])
|
|
89
|
+
const flat = flatFields(shown)
|
|
90
|
+
React.useEffect(() => setActive(0), [query, open])
|
|
91
|
+
const pick = (field: FieldDef) => {
|
|
92
|
+
setOpen(false)
|
|
93
|
+
setQuery("")
|
|
94
|
+
insertField(editor, field)
|
|
95
|
+
}
|
|
96
|
+
const name = query.trim()
|
|
97
|
+
const exists = flatFields(groups).some((f) => f.label.toLowerCase() === name.toLowerCase())
|
|
98
|
+
const create = onCreate && name && !exists ? () => pick(onCreate(name)) : undefined
|
|
99
|
+
return (
|
|
100
|
+
<Popover open={open} onOpenChange={setOpen}>
|
|
101
|
+
<PopoverTrigger asChild>
|
|
102
|
+
<BarDrop disabled={disabled}>
|
|
103
|
+
<Icon name="field" size={14} />
|
|
104
|
+
{labels.field}
|
|
105
|
+
</BarDrop>
|
|
106
|
+
</PopoverTrigger>
|
|
107
|
+
<PopoverContent align="start" className="w-auto border-0 bg-transparent p-0 shadow-none" onCloseAutoFocus={(e) => e.preventDefault()}>
|
|
108
|
+
<FieldPicker
|
|
109
|
+
id="wy-fieldmenu"
|
|
110
|
+
groups={shown}
|
|
111
|
+
active={Math.min(active, Math.max(0, flat.length - 1))}
|
|
112
|
+
onActive={setActive}
|
|
113
|
+
onPick={pick}
|
|
114
|
+
labels={labels}
|
|
115
|
+
search={
|
|
116
|
+
<label className="flex items-center gap-1.5 border-b border-border px-2 pb-1.5 pt-1 text-muted-foreground">
|
|
117
|
+
<Icon name="search" size={14} />
|
|
118
|
+
<input
|
|
119
|
+
autoFocus
|
|
120
|
+
value={query}
|
|
121
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
122
|
+
onKeyDown={(e) => {
|
|
123
|
+
const n = flat.length || 1
|
|
124
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
125
|
+
e.preventDefault()
|
|
126
|
+
setActive((i) => (i + (e.key === "ArrowDown" ? 1 : n - 1)) % n)
|
|
127
|
+
} else if (e.key === "Enter" && flat[active]) {
|
|
128
|
+
e.preventDefault()
|
|
129
|
+
pick(flat[active])
|
|
130
|
+
} else if (e.key === "Enter" && create) {
|
|
131
|
+
e.preventDefault()
|
|
132
|
+
create()
|
|
133
|
+
}
|
|
134
|
+
}}
|
|
135
|
+
placeholder={labels.fieldSearch}
|
|
136
|
+
aria-label={labels.fieldSearch}
|
|
137
|
+
aria-controls="wy-fieldmenu"
|
|
138
|
+
aria-activedescendant={flat.length ? `wy-fieldmenu-${active}` : undefined}
|
|
139
|
+
className="h-7 min-w-0 flex-1 bg-transparent text-sm text-foreground outline-none placeholder:text-muted-foreground"
|
|
140
|
+
/>
|
|
141
|
+
</label>
|
|
142
|
+
}
|
|
143
|
+
footer={
|
|
144
|
+
onCreate && (
|
|
145
|
+
<div className="border-t border-border pt-1">
|
|
146
|
+
{create ? (
|
|
147
|
+
<button
|
|
148
|
+
type="button"
|
|
149
|
+
onClick={create}
|
|
150
|
+
className="flex w-full items-center gap-1.5 rounded-control px-2 py-1.5 text-start text-sm hover:bg-accent"
|
|
151
|
+
>
|
|
152
|
+
<Icon name="plus" size={14} />
|
|
153
|
+
<span className="min-w-0 truncate" dir="auto">
|
|
154
|
+
{fill(labels.fieldNew, { name })}
|
|
155
|
+
</span>
|
|
156
|
+
</button>
|
|
157
|
+
) : (
|
|
158
|
+
<p className="m-0 px-2 py-1.5 text-xs text-muted-foreground">{labels.fieldNewHint}</p>
|
|
159
|
+
)}
|
|
160
|
+
</div>
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
/>
|
|
164
|
+
</PopoverContent>
|
|
165
|
+
</Popover>
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The fixed formatting toolbar, in Google Docs order (E.5, EditorShell):
|
|
171
|
+
* undo and redo · style · bold, italic, underline, strikethrough · link,
|
|
172
|
+
* highlight · lists · Insert ▾ · Field ▾ · zoom, view, outline. Every icon
|
|
173
|
+
* button has a tooltip with its shortcut; pressed states use the tint. Under
|
|
174
|
+
* 768px it scrolls sideways.
|
|
175
|
+
*/
|
|
176
|
+
function EditorToolbar({
|
|
177
|
+
editor,
|
|
178
|
+
labels,
|
|
179
|
+
editable,
|
|
180
|
+
outline,
|
|
181
|
+
onOutline,
|
|
182
|
+
onLink,
|
|
183
|
+
fields,
|
|
184
|
+
view,
|
|
185
|
+
onView,
|
|
186
|
+
zoom,
|
|
187
|
+
onZoom,
|
|
188
|
+
sheet,
|
|
189
|
+
onCreateField,
|
|
190
|
+
onComment,
|
|
191
|
+
onAskAgent,
|
|
192
|
+
}: {
|
|
193
|
+
editor: Editor
|
|
194
|
+
labels: EditorLabels
|
|
195
|
+
editable: boolean
|
|
196
|
+
outline: boolean
|
|
197
|
+
onOutline: () => void
|
|
198
|
+
onLink: () => void
|
|
199
|
+
/** The template's fields; Field ▾ shows only when there are some. */
|
|
200
|
+
fields?: FieldGroup[]
|
|
201
|
+
view?: EditorView
|
|
202
|
+
/** Omitted when the document can't switch (label sheets are page view only, E.43). */
|
|
203
|
+
onView?: (view: EditorView) => void
|
|
204
|
+
zoom?: PageZoom
|
|
205
|
+
onZoom?: (zoom: PageZoom) => void
|
|
206
|
+
/** Label sheets: the sheet setup, beside the zoom (E.43). */
|
|
207
|
+
sheet?: React.ReactNode
|
|
208
|
+
/** A template's own field, made from Field ▾. */
|
|
209
|
+
onCreateField?: (label: string) => FieldDef
|
|
210
|
+
/** M5: comment on the selection (⌥⌘M); offered with comment permission, editable or not. */
|
|
211
|
+
onComment?: () => void
|
|
212
|
+
/** M6: open the Ask an agent sheet (EditorAgents). */
|
|
213
|
+
onAskAgent?: () => void
|
|
214
|
+
}) {
|
|
215
|
+
const s = useEditorState({
|
|
216
|
+
editor,
|
|
217
|
+
selector: ({ editor: e }) => ({
|
|
218
|
+
bold: e.isActive("bold"),
|
|
219
|
+
italic: e.isActive("italic"),
|
|
220
|
+
underline: e.isActive("underline"),
|
|
221
|
+
strike: e.isActive("strike"),
|
|
222
|
+
link: e.isActive("link"),
|
|
223
|
+
highlight: e.isActive("highlight"),
|
|
224
|
+
canUndo: e.can().undo(),
|
|
225
|
+
canRedo: e.can().redo(),
|
|
226
|
+
item: currentItem(e)?.id ?? "text",
|
|
227
|
+
empty: e.state.selection.empty,
|
|
228
|
+
}),
|
|
229
|
+
})
|
|
230
|
+
const mark = (name: string) => () => editor.chain().focus().toggleMark(name).run()
|
|
231
|
+
const list = (id: string) => () => {
|
|
232
|
+
const item = CATALOGUE.find((i) => i.id === id)!
|
|
233
|
+
if (s.item === id) turnInto(editor, "paragraph")
|
|
234
|
+
else turnInto(editor, item.node.type, item.node.attrs)
|
|
235
|
+
}
|
|
236
|
+
const styleItem = CATALOGUE.find((i) => i.id === (STYLE_IDS.includes(s.item) ? s.item : "text"))!
|
|
237
|
+
const tb = (icon: React.ComponentProps<typeof IconButton>["icon"], label: string, pressed: boolean, onClick: () => void, shortcut?: string) => (
|
|
238
|
+
<IconButton
|
|
239
|
+
icon={icon}
|
|
240
|
+
label={label}
|
|
241
|
+
size="sm"
|
|
242
|
+
tooltip="bottom"
|
|
243
|
+
shortcut={shortcut}
|
|
244
|
+
pressed={pressed}
|
|
245
|
+
disabled={!editable}
|
|
246
|
+
onMouseDown={(e) => e.preventDefault()}
|
|
247
|
+
onClick={onClick}
|
|
248
|
+
/>
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
return (
|
|
252
|
+
<div
|
|
253
|
+
role="toolbar"
|
|
254
|
+
aria-label={labels.formatting}
|
|
255
|
+
data-slot="editor-toolbar"
|
|
256
|
+
className="flex h-11 items-center gap-0.5 overflow-x-auto border-b border-border bg-card px-3 [scrollbar-width:none]"
|
|
257
|
+
>
|
|
258
|
+
<IconButton icon="undo" label={labels.undo} size="sm" tooltip="bottom" shortcut="⌘Z" disabled={!editable || !s.canUndo} onMouseDown={(e) => e.preventDefault()} onClick={() => editor.chain().focus().undo().run()} />
|
|
259
|
+
<IconButton icon="redo" label={labels.redo} size="sm" tooltip="bottom" shortcut="⇧⌘Z" disabled={!editable || !s.canRedo} onMouseDown={(e) => e.preventDefault()} onClick={() => editor.chain().focus().redo().run()} />
|
|
260
|
+
<Sep />
|
|
261
|
+
<DropdownMenu modal={false}>
|
|
262
|
+
<DropdownMenuTrigger asChild>
|
|
263
|
+
<BarDrop aria-label={labels.styleMenu} disabled={!editable} className="min-w-28 justify-between">
|
|
264
|
+
{styleItem.id === "text" ? labels.normalText : labels[styleItem.label]}
|
|
265
|
+
</BarDrop>
|
|
266
|
+
</DropdownMenuTrigger>
|
|
267
|
+
<DropdownMenuContent align="start" onCloseAutoFocus={(e) => { e.preventDefault(); editor.commands.focus() }}>
|
|
268
|
+
{STYLE_IDS.map((id) => {
|
|
269
|
+
const item = CATALOGUE.find((i) => i.id === id)!
|
|
270
|
+
return (
|
|
271
|
+
<DropdownMenuItem key={id} onSelect={() => turnInto(editor, item.node.type, item.node.attrs)} className={cn(s.item === id && "bg-primary-tint text-primary-tint-foreground")}>
|
|
272
|
+
<Icon name={item.icon} /> {id === "text" ? labels.normalText : labels[item.label]}
|
|
273
|
+
<DropdownMenuShortcut>{STYLE_SHORTCUTS[id]}</DropdownMenuShortcut>
|
|
274
|
+
</DropdownMenuItem>
|
|
275
|
+
)
|
|
276
|
+
})}
|
|
277
|
+
</DropdownMenuContent>
|
|
278
|
+
</DropdownMenu>
|
|
279
|
+
<Sep />
|
|
280
|
+
{tb("bold", labels.bold, s.bold, mark("bold"), "⌘B")}
|
|
281
|
+
{tb("italic", labels.italic, s.italic, mark("italic"), "⌘I")}
|
|
282
|
+
{tb("underline", labels.underline, s.underline, mark("underline"), "⌘U")}
|
|
283
|
+
{tb("strike", labels.strike, s.strike, mark("strike"), "⇧⌘X")}
|
|
284
|
+
<Sep />
|
|
285
|
+
{tb("link", labels.link, s.link, onLink, "⌘K")}
|
|
286
|
+
{tb("highlight", labels.highlight, s.highlight, mark("highlight"))}
|
|
287
|
+
<Sep />
|
|
288
|
+
{tb("list-bullet", labels.blockBullet, s.item === "bullet", list("bullet"), "⇧⌘8")}
|
|
289
|
+
{tb("list-numbered", labels.blockNumber, s.item === "number", list("number"), "⇧⌘7")}
|
|
290
|
+
{tb("list-todo", labels.blockChecklist, s.item === "checklist", list("checklist"), "⇧⌘9")}
|
|
291
|
+
<Sep />
|
|
292
|
+
<DropdownMenu modal={false}>
|
|
293
|
+
<DropdownMenuTrigger asChild>
|
|
294
|
+
<BarDrop disabled={!editable}>
|
|
295
|
+
<Icon name="plus" size={14} />
|
|
296
|
+
{labels.insert}
|
|
297
|
+
</BarDrop>
|
|
298
|
+
</DropdownMenuTrigger>
|
|
299
|
+
<DropdownMenuContent align="start" onCloseAutoFocus={(e) => { e.preventDefault(); editor.commands.focus() }}>
|
|
300
|
+
{CATALOGUE.map((item) => (
|
|
301
|
+
<DropdownMenuItem key={item.id} onSelect={() => applyItem(editor, item)}>
|
|
302
|
+
<Icon name={item.icon} /> {labels[item.label]}
|
|
303
|
+
{item.markdown && <DropdownMenuShortcut>{item.markdown}</DropdownMenuShortcut>}
|
|
304
|
+
</DropdownMenuItem>
|
|
305
|
+
))}
|
|
306
|
+
</DropdownMenuContent>
|
|
307
|
+
</DropdownMenu>
|
|
308
|
+
{fields && (fields.length > 0 || onCreateField) && (
|
|
309
|
+
<FieldMenu editor={editor} groups={fields} labels={labels} disabled={!editable} onCreate={onCreateField} />
|
|
310
|
+
)}
|
|
311
|
+
{onComment && (
|
|
312
|
+
<IconButton
|
|
313
|
+
icon="comment-add"
|
|
314
|
+
label={labels.comment}
|
|
315
|
+
size="sm"
|
|
316
|
+
tooltip="bottom"
|
|
317
|
+
shortcut="⌥⌘M"
|
|
318
|
+
disabled={s.empty}
|
|
319
|
+
onMouseDown={(e) => e.preventDefault()}
|
|
320
|
+
onClick={onComment}
|
|
321
|
+
/>
|
|
322
|
+
)}
|
|
323
|
+
{onAskAgent && (
|
|
324
|
+
<IconButton icon="robot" label={labels.agentAsk} size="sm" tooltip="bottom" onClick={onAskAgent} />
|
|
325
|
+
)}
|
|
326
|
+
<span className="min-w-2 flex-1" />
|
|
327
|
+
{sheet}
|
|
328
|
+
{view === "page" && onZoom && (
|
|
329
|
+
<DropdownMenu modal={false}>
|
|
330
|
+
<DropdownMenuTrigger asChild>
|
|
331
|
+
<BarDrop aria-label={labels.zoom}>{zoomLabel(zoom ?? "fit", labels)}</BarDrop>
|
|
332
|
+
</DropdownMenuTrigger>
|
|
333
|
+
<DropdownMenuContent align="end">
|
|
334
|
+
{ZOOM_STEPS.map((step) => (
|
|
335
|
+
<DropdownMenuItem key={String(step)} onSelect={() => onZoom(step)} className={cn(step === (zoom ?? "fit") && "bg-primary-tint text-primary-tint-foreground")}>
|
|
336
|
+
{zoomLabel(step, labels)}
|
|
337
|
+
</DropdownMenuItem>
|
|
338
|
+
))}
|
|
339
|
+
</DropdownMenuContent>
|
|
340
|
+
</DropdownMenu>
|
|
341
|
+
)}
|
|
342
|
+
{view && onView && (
|
|
343
|
+
<ToggleGroup
|
|
344
|
+
size="sm"
|
|
345
|
+
label={labels.view}
|
|
346
|
+
required
|
|
347
|
+
value={view}
|
|
348
|
+
onChange={(v) => v && onView(v as EditorView)}
|
|
349
|
+
options={[
|
|
350
|
+
{ value: "continuous", label: labels.viewContinuous, icon: "continuous", iconOnly: true },
|
|
351
|
+
{ value: "page", label: labels.viewPage, icon: "page", iconOnly: true },
|
|
352
|
+
]}
|
|
353
|
+
/>
|
|
354
|
+
)}
|
|
355
|
+
<IconButton icon="outline" label={labels.outline} size="sm" tooltip="bottom" pressed={outline} onClick={onOutline} />
|
|
356
|
+
</div>
|
|
357
|
+
)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function zoomLabel(zoom: PageZoom, labels: EditorLabels): string {
|
|
361
|
+
return zoom === "fit" ? labels.zoomFit : fill(labels.zoomPercent, { n: zoom })
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export { EditorToolbar }
|