@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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { NodeViewWrapper, type ReactNodeViewProps } from "@tiptap/react"
|
|
5
|
+
|
|
6
|
+
import { Button } from "../button"
|
|
7
|
+
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "../dialog"
|
|
8
|
+
import { Icon } from "../icon"
|
|
9
|
+
import { Input } from "../input"
|
|
10
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../select"
|
|
11
|
+
import { Textarea } from "../textarea"
|
|
12
|
+
import { cn } from "../utils"
|
|
13
|
+
import { fill, type EditorLabels } from "./labels"
|
|
14
|
+
import { fieldValues, fillView, findPluginBlock } from "./plugin-blocks"
|
|
15
|
+
import { useEditorServices, type PluginBlockDefinition } from "./services"
|
|
16
|
+
import { localizedText } from "../localized-text"
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A plugin's block in a document (EditorCustomBlocks; E.17–E.20). Whity's
|
|
20
|
+
* renderer draws its declared view with Whity's components; nothing the
|
|
21
|
+
* plugin ships runs here. Selected or hovered, its source label shows the
|
|
22
|
+
* plugin, Verified or Not verified (Whity's mark only), and Prints or Notes
|
|
23
|
+
* only, with Edit opening a form built from its fields.
|
|
24
|
+
*
|
|
25
|
+
* When its plugin is off or gone, a placeholder says which plugin it needs,
|
|
26
|
+
* with Enable for people who may switch workspace plugins on; the block's
|
|
27
|
+
* data stays in the document (E.19).
|
|
28
|
+
*/
|
|
29
|
+
export function PluginBlockView(props: ReactNodeViewProps) {
|
|
30
|
+
const { services, labels } = useEditorServices()
|
|
31
|
+
const lang = services.language ?? "en"
|
|
32
|
+
const plugin = String(props.node.attrs.plugin ?? "")
|
|
33
|
+
const blockType = String(props.node.attrs.blockType ?? "")
|
|
34
|
+
const data = (props.node.attrs.data ?? {}) as Record<string, unknown>
|
|
35
|
+
const found = findPluginBlock(services.pluginBlocks, plugin, blockType)
|
|
36
|
+
const editable = props.editor.isEditable
|
|
37
|
+
const [editing, setEditing] = React.useState(false)
|
|
38
|
+
|
|
39
|
+
if (!found) {
|
|
40
|
+
return (
|
|
41
|
+
<NodeViewWrapper data-type="plugin" data-block-id={props.node.attrs.id ?? undefined} className={cn("wy-rich", props.selected && "is-selected")}>
|
|
42
|
+
<PluginOff plugin={plugin} />
|
|
43
|
+
</NodeViewWrapper>
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const { source, definition } = found
|
|
48
|
+
const name = localizedText(definition.label, lang)
|
|
49
|
+
const view = fillView(definition.view, fieldValues(definition, data, lang), lang)
|
|
50
|
+
return (
|
|
51
|
+
<NodeViewWrapper
|
|
52
|
+
data-type="plugin"
|
|
53
|
+
data-block-id={props.node.attrs.id ?? undefined}
|
|
54
|
+
className={cn("wy-rich wy-plugin", props.selected && "is-selected")}
|
|
55
|
+
role="group"
|
|
56
|
+
aria-label={fill(labels.pluginBlockLabel, { name, plugin: localizedText(source.label, lang) })}
|
|
57
|
+
>
|
|
58
|
+
<div className="wy-saved-src" contentEditable={false}>
|
|
59
|
+
<span className="wy-plugin-tile" aria-hidden="true">
|
|
60
|
+
{localizedText(source.label, lang).slice(0, 1).toUpperCase()}
|
|
61
|
+
</span>
|
|
62
|
+
<b dir="auto">{localizedText(source.label, lang)}</b>
|
|
63
|
+
{source.verified ? (
|
|
64
|
+
<span className="wy-plugin-mark">
|
|
65
|
+
<Icon name="check" size={12} /> {labels.pluginVerified}
|
|
66
|
+
</span>
|
|
67
|
+
) : (
|
|
68
|
+
<span>{labels.pluginNotVerified}</span>
|
|
69
|
+
)}
|
|
70
|
+
<span className="wy-saved-sep" aria-hidden="true" />
|
|
71
|
+
<span>{definition.print ? labels.pluginPrints : labels.pluginNotesOnly}</span>
|
|
72
|
+
{editable && definition.fields.length > 0 && (
|
|
73
|
+
<>
|
|
74
|
+
<span className="wy-saved-sep" aria-hidden="true" />
|
|
75
|
+
<button type="button" className="wy-saved-action" onMouseDown={(e) => e.preventDefault()} onClick={() => setEditing(true)}>
|
|
76
|
+
{labels.pluginEdit}
|
|
77
|
+
</button>
|
|
78
|
+
</>
|
|
79
|
+
)}
|
|
80
|
+
</div>
|
|
81
|
+
<div className="wy-plugin-body" contentEditable={false}>
|
|
82
|
+
{services.renderPluginView ? services.renderPluginView(view) : <div className="wy-placeholder">{name}</div>}
|
|
83
|
+
</div>
|
|
84
|
+
{editing && (
|
|
85
|
+
<PluginBlockForm
|
|
86
|
+
definition={definition}
|
|
87
|
+
data={data}
|
|
88
|
+
lang={lang}
|
|
89
|
+
labels={labels}
|
|
90
|
+
onClose={() => setEditing(false)}
|
|
91
|
+
onSave={(next) => {
|
|
92
|
+
props.updateAttributes({ data: next })
|
|
93
|
+
setEditing(false)
|
|
94
|
+
}}
|
|
95
|
+
/>
|
|
96
|
+
)}
|
|
97
|
+
</NodeViewWrapper>
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** E.19: the block's plugin is off or gone. The data stays; Enable for those who may. */
|
|
102
|
+
function PluginOff({ plugin }: { plugin: string }) {
|
|
103
|
+
const { services, labels } = useEditorServices()
|
|
104
|
+
const lang = services.language ?? "en"
|
|
105
|
+
const off = services.pluginBlocks?.off.find((p) => p.plugin === plugin)
|
|
106
|
+
const name = off ? localizedText(off.label, lang) : plugin
|
|
107
|
+
const canEnable = !!off && !!services.pluginBlocks?.canEnable && !!services.enablePlugin
|
|
108
|
+
const [busy, setBusy] = React.useState(false)
|
|
109
|
+
const [failed, setFailed] = React.useState(false)
|
|
110
|
+
return (
|
|
111
|
+
<div className="wy-plugin-off" contentEditable={false}>
|
|
112
|
+
<span className="wy-tile" aria-hidden="true">
|
|
113
|
+
<Icon name="plugin" />
|
|
114
|
+
</span>
|
|
115
|
+
<span className="grid min-w-0 flex-1 gap-0.5">
|
|
116
|
+
<span className="font-medium" dir="auto">
|
|
117
|
+
{fill(labels.pluginNeeds, { plugin: name })}
|
|
118
|
+
</span>
|
|
119
|
+
<span className="text-sm text-muted-foreground">{failed ? labels.pluginEnableFailed : labels.pluginNeedsHint}</span>
|
|
120
|
+
</span>
|
|
121
|
+
{canEnable && (
|
|
122
|
+
<Button
|
|
123
|
+
variant="outline"
|
|
124
|
+
size="sm"
|
|
125
|
+
loading={busy}
|
|
126
|
+
onMouseDown={(e) => e.preventDefault()}
|
|
127
|
+
onClick={async () => {
|
|
128
|
+
setBusy(true)
|
|
129
|
+
setFailed(false)
|
|
130
|
+
try {
|
|
131
|
+
await services.enablePlugin!(plugin)
|
|
132
|
+
} catch {
|
|
133
|
+
setFailed(true)
|
|
134
|
+
} finally {
|
|
135
|
+
setBusy(false)
|
|
136
|
+
}
|
|
137
|
+
}}
|
|
138
|
+
>
|
|
139
|
+
{labels.pluginEnable}
|
|
140
|
+
</Button>
|
|
141
|
+
)}
|
|
142
|
+
</div>
|
|
143
|
+
)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The block's Edit form, built from its declared fields. */
|
|
147
|
+
function PluginBlockForm({
|
|
148
|
+
definition,
|
|
149
|
+
data,
|
|
150
|
+
lang,
|
|
151
|
+
labels,
|
|
152
|
+
onClose,
|
|
153
|
+
onSave,
|
|
154
|
+
}: {
|
|
155
|
+
definition: PluginBlockDefinition
|
|
156
|
+
data: Record<string, unknown>
|
|
157
|
+
lang: "en" | "ar"
|
|
158
|
+
labels: EditorLabels
|
|
159
|
+
onClose: () => void
|
|
160
|
+
onSave: (data: Record<string, unknown>) => void
|
|
161
|
+
}) {
|
|
162
|
+
const [values, setValues] = React.useState<Record<string, string>>(() =>
|
|
163
|
+
Object.fromEntries(definition.fields.map((f) => [f.key, data[f.key] === null || data[f.key] === undefined ? "" : String(data[f.key])]))
|
|
164
|
+
)
|
|
165
|
+
const id = React.useId()
|
|
166
|
+
const missing = definition.fields.some((f) => f.required && !values[f.key]?.trim())
|
|
167
|
+
const set = (key: string, value: string) => setValues((v) => ({ ...v, [key]: value }))
|
|
168
|
+
return (
|
|
169
|
+
<Dialog open onOpenChange={(open) => !open && onClose()}>
|
|
170
|
+
<DialogContent className="max-w-[440px]" closeLabel={labels.pluginCancel}>
|
|
171
|
+
<form
|
|
172
|
+
className="grid gap-4"
|
|
173
|
+
onSubmit={(e) => {
|
|
174
|
+
e.preventDefault()
|
|
175
|
+
if (missing) return
|
|
176
|
+
onSave(Object.fromEntries(definition.fields.map((f) => [f.key, f.type === "number" && values[f.key] !== "" ? Number(values[f.key]) : values[f.key]])))
|
|
177
|
+
}}
|
|
178
|
+
>
|
|
179
|
+
<DialogHeader>
|
|
180
|
+
<DialogTitle>{fill(labels.pluginEditTitle, { name: localizedText(definition.label, lang) })}</DialogTitle>
|
|
181
|
+
</DialogHeader>
|
|
182
|
+
{definition.fields.map((field) => {
|
|
183
|
+
const fieldId = `${id}-${field.key}`
|
|
184
|
+
const label = localizedText(field.label, lang)
|
|
185
|
+
return (
|
|
186
|
+
<div key={field.key} className="grid gap-1.5">
|
|
187
|
+
<label htmlFor={fieldId} className="text-sm font-medium">
|
|
188
|
+
{label}
|
|
189
|
+
</label>
|
|
190
|
+
{field.type === "longText" ? (
|
|
191
|
+
<Textarea id={fieldId} dir="auto" value={values[field.key]} onChange={(e) => set(field.key, e.target.value)} />
|
|
192
|
+
) : field.type === "select" ? (
|
|
193
|
+
<Select value={values[field.key] || undefined} onValueChange={(v) => set(field.key, v)}>
|
|
194
|
+
<SelectTrigger id={fieldId}>
|
|
195
|
+
<SelectValue placeholder={labels.pluginChoose} />
|
|
196
|
+
</SelectTrigger>
|
|
197
|
+
<SelectContent>
|
|
198
|
+
{(field.options ?? []).map((o) => (
|
|
199
|
+
<SelectItem key={o.value} value={o.value}>
|
|
200
|
+
{localizedText(o.label, lang)}
|
|
201
|
+
</SelectItem>
|
|
202
|
+
))}
|
|
203
|
+
</SelectContent>
|
|
204
|
+
</Select>
|
|
205
|
+
) : (
|
|
206
|
+
<Input
|
|
207
|
+
id={fieldId}
|
|
208
|
+
dir={field.type === "text" ? "auto" : "ltr"}
|
|
209
|
+
type={field.type === "number" ? "number" : field.type === "date" ? "date" : "text"}
|
|
210
|
+
required={field.required}
|
|
211
|
+
value={values[field.key]}
|
|
212
|
+
onChange={(e) => set(field.key, e.target.value)}
|
|
213
|
+
/>
|
|
214
|
+
)}
|
|
215
|
+
</div>
|
|
216
|
+
)
|
|
217
|
+
})}
|
|
218
|
+
<DialogFooter>
|
|
219
|
+
<Button type="button" variant="outline" onClick={onClose}>
|
|
220
|
+
{labels.pluginCancel}
|
|
221
|
+
</Button>
|
|
222
|
+
<Button type="submit" disabled={missing}>
|
|
223
|
+
{labels.pluginSave}
|
|
224
|
+
</Button>
|
|
225
|
+
</DialogFooter>
|
|
226
|
+
</form>
|
|
227
|
+
</DialogContent>
|
|
228
|
+
</Dialog>
|
|
229
|
+
)
|
|
230
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/react-vite"
|
|
2
|
+
import * as React from "react"
|
|
3
|
+
|
|
4
|
+
import type { WhityDocument } from "./contract"
|
|
5
|
+
import { DocEditor } from "./doc-editor"
|
|
6
|
+
import { SAMPLE_DOCUMENT } from "./sample"
|
|
7
|
+
import type { EditorServices, PluginBlocks } from "./services"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Editor M7, the plugin half of the EditorCustomBlocks card: a Verified
|
|
11
|
+
* plugin's block drawn by Whity (hover or select it for its source label and
|
|
12
|
+
* Edit), and a block whose plugin is off (E.19). In the app, the view is
|
|
13
|
+
* drawn by Whity's screen renderer; here a stand-in draws its key/value rows.
|
|
14
|
+
*/
|
|
15
|
+
const meta = {
|
|
16
|
+
title: "Editor/Plugin blocks",
|
|
17
|
+
parameters: { layout: "fullscreen", fullBleed: true },
|
|
18
|
+
} satisfies Meta
|
|
19
|
+
|
|
20
|
+
export default meta
|
|
21
|
+
type Story = StoryObj<typeof meta>
|
|
22
|
+
|
|
23
|
+
const L = (en: string, ar = en) => ({ en, ar })
|
|
24
|
+
|
|
25
|
+
const BLOCKS: PluginBlocks = {
|
|
26
|
+
plugins: [
|
|
27
|
+
{
|
|
28
|
+
plugin: "DeliveryTracker",
|
|
29
|
+
label: L("Delivery Tracker", "متتبع التوصيل"),
|
|
30
|
+
verified: true,
|
|
31
|
+
firstParty: false,
|
|
32
|
+
blocks: [
|
|
33
|
+
{
|
|
34
|
+
name: "DeliveryTracker/delivery_status",
|
|
35
|
+
blockType: "delivery_status",
|
|
36
|
+
label: L("Delivery status", "حالة التوصيل"),
|
|
37
|
+
description: L("Where an order is", "أين الطلب"),
|
|
38
|
+
icon: "package",
|
|
39
|
+
keywords: [],
|
|
40
|
+
fields: [
|
|
41
|
+
{ key: "order", label: L("Order", "الطلب"), type: "text", required: true },
|
|
42
|
+
{ key: "customer", label: L("Customer", "العميل"), type: "text" },
|
|
43
|
+
{ key: "stage", label: L("Stage", "المرحلة"), type: "select", options: [{ value: "packed", label: L("Packed", "مُعبأ") }, { value: "out", label: L("Out for delivery", "في الطريق") }] },
|
|
44
|
+
],
|
|
45
|
+
view: [{ type: "keyValue", items: [{ label: "Order", value: "{{data.order}}" }, { label: "Customer", value: "{{data.customer}}" }, { label: "Stage", value: "{{data.stage}}" }] }],
|
|
46
|
+
print: [{ type: "keyValue", items: [{ label: "Order", value: "{{data.order}}" }] }],
|
|
47
|
+
allowedIn: ["note", "wiki", "template"],
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
off: [{ plugin: "RouteMaps", label: L("Route Maps", "خرائط المسار") }],
|
|
53
|
+
canEnable: true,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A stand-in for Whity's screen renderer: key/value rows. */
|
|
57
|
+
function KeyValues({ view }: { view: unknown[] }) {
|
|
58
|
+
const items = (view as { items?: { label: string; value: string }[] }[]).flatMap((n) => n.items ?? [])
|
|
59
|
+
return (
|
|
60
|
+
<dl className="grid grid-cols-[auto_1fr] gap-x-6 gap-y-1 rounded-card border border-border bg-card px-4 py-3 text-sm">
|
|
61
|
+
{items.map((i) => (
|
|
62
|
+
<React.Fragment key={i.label}>
|
|
63
|
+
<dt className="text-muted-foreground">{i.label}</dt>
|
|
64
|
+
<dd className="font-medium">{i.value}</dd>
|
|
65
|
+
</React.Fragment>
|
|
66
|
+
))}
|
|
67
|
+
</dl>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const SERVICES: EditorServices = {
|
|
72
|
+
pluginBlocks: BLOCKS,
|
|
73
|
+
renderPluginView: (view) => <KeyValues view={view} />,
|
|
74
|
+
enablePlugin: async () => undefined,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const DOC: WhityDocument = {
|
|
78
|
+
...SAMPLE_DOCUMENT,
|
|
79
|
+
title: "Order handover",
|
|
80
|
+
blocks: [
|
|
81
|
+
{ id: "p1", type: "paragraph", content: [{ type: "text", text: "Where Example Clinic's order is:" }] },
|
|
82
|
+
{ id: "d1", type: "plugin", plugin: "DeliveryTracker", blockType: "delivery_status", schemaVersion: 1, data: { order: "SO-2207", customer: "Example Clinic", stage: "out" } },
|
|
83
|
+
{ id: "r1", type: "plugin", plugin: "RouteMaps", blockType: "route", schemaVersion: 1, data: {} },
|
|
84
|
+
],
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function Editor() {
|
|
88
|
+
const [doc, setDoc] = React.useState(DOC)
|
|
89
|
+
return (
|
|
90
|
+
<div className="h-screen">
|
|
91
|
+
<DocEditor document={doc} onChange={setDoc} canEdit services={SERVICES} />
|
|
92
|
+
</div>
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const InADocument: Story = { render: () => <Editor /> }
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Localized, PluginBlockDefinition, PluginBlocks, PluginBlockSource } from "./services"
|
|
2
|
+
import { localizedText } from "../localized-text"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Plugin blocks in the editor (editor M7, E.17–E.20). A block's view is a
|
|
6
|
+
* declaration; this fills it with the block's data the way Whity's printer
|
|
7
|
+
* does (PluginBlockPrinter): `{{data.key}}` becomes the field's value, a
|
|
8
|
+
* select's value its option's label, and `{en, ar}` text the reader's
|
|
9
|
+
* language. Nothing a plugin declares is run.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Each field's shown text; a select shows its option's label. */
|
|
13
|
+
export function fieldValues(definition: PluginBlockDefinition, data: Record<string, unknown>, lang: "en" | "ar"): Record<string, string> {
|
|
14
|
+
const out: Record<string, string> = {}
|
|
15
|
+
for (const field of definition.fields) {
|
|
16
|
+
const raw = data[field.key]
|
|
17
|
+
let text = raw === null || raw === undefined ? "" : String(raw)
|
|
18
|
+
if (field.type === "select") {
|
|
19
|
+
const option = field.options?.find((o) => o.value === text)
|
|
20
|
+
if (option) text = localizedText(option.label, lang)
|
|
21
|
+
}
|
|
22
|
+
out[field.key] = text
|
|
23
|
+
}
|
|
24
|
+
return out
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const isLocalized = (v: unknown): v is Localized =>
|
|
28
|
+
!!v && typeof v === "object" && !Array.isArray(v) && Object.keys(v).length > 0 && Object.keys(v).every((k) => k === "en" || k === "ar")
|
|
29
|
+
|
|
30
|
+
/** The view with `{{data.key}}` filled and `{en, ar}` text in the reader's language. */
|
|
31
|
+
export function fillView(view: unknown[], values: Record<string, string>, lang: "en" | "ar"): unknown[] {
|
|
32
|
+
const fill = (value: unknown): unknown => {
|
|
33
|
+
if (typeof value === "string") return value.replace(/\{\{\s*data\.([a-z][a-z0-9_]{0,39})\s*\}\}/g, (_, key: string) => values[key] ?? "")
|
|
34
|
+
if (isLocalized(value)) return fill(localizedText(value, lang))
|
|
35
|
+
if (Array.isArray(value)) return value.map(fill)
|
|
36
|
+
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, fill(v)]))
|
|
37
|
+
return value
|
|
38
|
+
}
|
|
39
|
+
return view.map(fill)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A block's declaration and its plugin, or null when the plugin is off, gone or no longer declares it. */
|
|
43
|
+
export function findPluginBlock(blocks: PluginBlocks | undefined, plugin: string, blockType: string): { source: PluginBlockSource; definition: PluginBlockDefinition } | null {
|
|
44
|
+
const source = blocks?.plugins.find((p) => p.plugin === plugin)
|
|
45
|
+
const definition = source?.blocks.find((b) => b.blockType === blockType)
|
|
46
|
+
return source && definition ? { source, definition } : null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A new block's data: empty text, a select's first option. */
|
|
50
|
+
export function defaultData(definition: PluginBlockDefinition): Record<string, unknown> {
|
|
51
|
+
return Object.fromEntries(definition.fields.map((f) => [f.key, f.type === "select" ? (f.options?.[0]?.value ?? "") : ""]))
|
|
52
|
+
}
|