@prunellejs/ui 0.1.1 → 0.5.3
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/llms.txt +9 -3
- package/package.json +2 -2
- package/src/CommandPalette.tsx +120 -67
- package/src/DataTable.tsx +27 -17
- package/src/Dialog.tsx +29 -13
- package/src/Menu.tsx +10 -8
- package/src/Select.tsx +16 -6
- package/src/Toaster.tsx +112 -2
- package/src/behaviors.ts +108 -6
- package/src/index.ts +1 -1
- package/src/plugin.ts +96 -32
- package/src/styles/bare.css +21 -312
- package/src/styles/base.css +55 -0
- package/src/styles/components/accordion.css +10 -0
- package/src/styles/components/badge.css +6 -0
- package/src/styles/components/button.css +11 -0
- package/src/styles/components/card.css +5 -0
- package/src/styles/components/checkbox.css +11 -0
- package/src/styles/components/command.css +25 -0
- package/src/styles/components/datatable.css +9 -0
- package/src/styles/components/dialog.css +16 -0
- package/src/styles/components/drawer.css +29 -0
- package/src/styles/components/field.css +8 -0
- package/src/styles/components/layout.css +69 -0
- package/src/styles/components/menu.css +21 -0
- package/src/styles/components/notice.css +5 -0
- package/src/styles/components/popover.css +8 -0
- package/src/styles/components/progress.css +14 -0
- package/src/styles/components/radio-group.css +11 -0
- package/src/styles/components/select.css +25 -0
- package/src/styles/components/switch.css +42 -0
- package/src/styles/components/tabs.css +4 -0
- package/src/styles/components/toast.css +28 -0
- package/src/styles/grammars/console.css +113 -256
- package/src/styles/grammars/editorial.css +99 -270
- package/src/styles/grammars/orbit.css +118 -388
- package/src/styles/grammars/prunelle.css +157 -360
- package/src/styles/grammars/soft.css +103 -237
- package/src/styles/grammars/workshop.css +104 -246
- package/src/styles/scan.ts +137 -0
- package/src/styles/split.ts +381 -0
- package/src/styles/theme.css +21 -0
- package/src/styles/themed/accordion.css +20 -0
- package/src/styles/themed/badge.css +20 -0
- package/src/styles/themed/button.css +21 -0
- package/src/styles/themed/card.css +23 -0
- package/src/styles/themed/checkbox.css +4 -0
- package/src/styles/themed/command.css +33 -0
- package/src/styles/themed/datatable.css +28 -0
- package/src/styles/themed/dialog.css +13 -0
- package/src/styles/themed/drawer.css +13 -0
- package/src/styles/themed/field.css +22 -0
- package/src/styles/themed/menu.css +21 -0
- package/src/styles/themed/notice.css +17 -0
- package/src/styles/themed/popover.css +11 -0
- package/src/styles/themed/radio-group.css +12 -0
- package/src/styles/themed/select.css +32 -0
- package/src/styles/themed/switch.css +18 -0
- package/src/styles/themed/tabs.css +17 -0
- package/src/styles/themed/toast.css +18 -0
package/src/Toaster.tsx
CHANGED
|
@@ -1,17 +1,127 @@
|
|
|
1
1
|
/** @jsxImportSource prunelle */
|
|
2
2
|
// Live-region Toast stack driven by the reactive `toast` store.
|
|
3
3
|
|
|
4
|
+
import { effect, onCleanup } from 'prunelle'
|
|
4
5
|
import { toast } from './toast'
|
|
5
6
|
|
|
6
7
|
export interface ToasterProps {
|
|
7
8
|
dismissLabel?: string | undefined
|
|
8
9
|
}
|
|
9
10
|
|
|
11
|
+
const TOAST_STORE_KEY = Symbol.for('pru.ui.toasts')
|
|
12
|
+
const RESUME_DURATION_MS = 4500
|
|
13
|
+
|
|
14
|
+
interface SharedToastTimers {
|
|
15
|
+
t: Map<string, ReturnType<typeof setTimeout>>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getTimerMap(): Map<string, ReturnType<typeof setTimeout>> | undefined {
|
|
19
|
+
if (typeof document === 'undefined')
|
|
20
|
+
return undefined
|
|
21
|
+
return (document as unknown as Record<symbol, SharedToastTimers | undefined>)[TOAST_STORE_KEY]?.t
|
|
22
|
+
}
|
|
23
|
+
|
|
10
24
|
export function Toaster({ dismissLabel = 'Dismiss' }: ToasterProps = {}) {
|
|
25
|
+
let hoveredToaster = false
|
|
26
|
+
let focusedToaster = false
|
|
27
|
+
const hoveredToasts = new Set<string>()
|
|
28
|
+
const pausedIds = new Set<string>()
|
|
29
|
+
|
|
30
|
+
const isPaused = () => hoveredToaster || focusedToaster || hoveredToasts.size > 0
|
|
31
|
+
|
|
32
|
+
const pauseTimers = () => {
|
|
33
|
+
const timers = getTimerMap()
|
|
34
|
+
if (timers === undefined)
|
|
35
|
+
return
|
|
36
|
+
for (const item of toast.items.peek()) {
|
|
37
|
+
const handle = timers.get(item.id)
|
|
38
|
+
if (handle !== undefined) {
|
|
39
|
+
clearTimeout(handle)
|
|
40
|
+
timers.delete(item.id)
|
|
41
|
+
pausedIds.add(item.id)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const resumeTimers = () => {
|
|
47
|
+
if (isPaused())
|
|
48
|
+
return
|
|
49
|
+
const timers = getTimerMap()
|
|
50
|
+
if (timers === undefined) {
|
|
51
|
+
pausedIds.clear()
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
const active = new Set(toast.items.peek().map(x => x.id))
|
|
55
|
+
for (const id of pausedIds) {
|
|
56
|
+
if (active.has(id) && !timers.has(id))
|
|
57
|
+
timers.set(id, setTimeout(() => toast.dismiss(id), RESUME_DURATION_MS))
|
|
58
|
+
}
|
|
59
|
+
pausedIds.clear()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
effect(() => {
|
|
63
|
+
const items = toast.items.value
|
|
64
|
+
if (items.length === 0)
|
|
65
|
+
return
|
|
66
|
+
if (isPaused())
|
|
67
|
+
pauseTimers()
|
|
68
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
69
|
+
if (e.key !== 'Escape' || e.defaultPrevented)
|
|
70
|
+
return
|
|
71
|
+
const current = toast.items.peek()
|
|
72
|
+
if (current.length === 0)
|
|
73
|
+
return
|
|
74
|
+
const activeEl = typeof document !== 'undefined' ? document.activeElement : null
|
|
75
|
+
const focusedId = activeEl?.closest?.('[data-ui="toast"]')?.getAttribute('data-toast-id')
|
|
76
|
+
const targetId = focusedId && current.some(x => x.id === focusedId)
|
|
77
|
+
? focusedId
|
|
78
|
+
: current[current.length - 1]!.id
|
|
79
|
+
toast.dismiss(targetId)
|
|
80
|
+
}
|
|
81
|
+
document.addEventListener('keydown', onKeyDown)
|
|
82
|
+
onCleanup(() => document.removeEventListener('keydown', onKeyDown))
|
|
83
|
+
})
|
|
84
|
+
|
|
11
85
|
return (
|
|
12
|
-
<div
|
|
86
|
+
<div
|
|
87
|
+
data-ui="toaster"
|
|
88
|
+
onMouseEnter={() => {
|
|
89
|
+
hoveredToaster = true
|
|
90
|
+
pauseTimers()
|
|
91
|
+
}}
|
|
92
|
+
onMouseLeave={() => {
|
|
93
|
+
hoveredToaster = false
|
|
94
|
+
resumeTimers()
|
|
95
|
+
}}
|
|
96
|
+
onFocusIn={() => {
|
|
97
|
+
focusedToaster = true
|
|
98
|
+
pauseTimers()
|
|
99
|
+
}}
|
|
100
|
+
onFocusOut={(e) => {
|
|
101
|
+
const next = e.relatedTarget as Node | null
|
|
102
|
+
if (next === null || !e.currentTarget.contains(next)) {
|
|
103
|
+
focusedToaster = false
|
|
104
|
+
resumeTimers()
|
|
105
|
+
}
|
|
106
|
+
}}
|
|
107
|
+
>
|
|
13
108
|
{toast.items.value.map(item => (
|
|
14
|
-
<div
|
|
109
|
+
<div
|
|
110
|
+
key={item.id}
|
|
111
|
+
data-ui="toast"
|
|
112
|
+
data-toast-id={item.id}
|
|
113
|
+
data-tone={item.tone}
|
|
114
|
+
role={item.tone === 'danger' ? 'alert' : 'status'}
|
|
115
|
+
aria-atomic="true"
|
|
116
|
+
onMouseEnter={() => {
|
|
117
|
+
hoveredToasts.add(item.id)
|
|
118
|
+
pauseTimers()
|
|
119
|
+
}}
|
|
120
|
+
onMouseLeave={() => {
|
|
121
|
+
hoveredToasts.delete(item.id)
|
|
122
|
+
resumeTimers()
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
15
125
|
<div>
|
|
16
126
|
<strong data-part="title">{item.title}</strong>
|
|
17
127
|
{item.description && <p data-part="description">{item.description}</p>}
|
package/src/behaviors.ts
CHANGED
|
@@ -13,6 +13,53 @@ export { toast } from './toast'
|
|
|
13
13
|
export type { ToastItem } from './toast'
|
|
14
14
|
export { useFieldContext }
|
|
15
15
|
|
|
16
|
+
// ---- Safe href validation & SPA-friendly navigation ----
|
|
17
|
+
|
|
18
|
+
const UNSAFE_HREF_RE = /^(?:javascript|vbscript|data):/i
|
|
19
|
+
|
|
20
|
+
/** Returns a safe navigable URL (rejecting `javascript:`, `vbscript:`, and `data:` schemes even with control-char obfuscation), or `undefined`. */
|
|
21
|
+
export function safeHref(href: string | undefined): string | undefined {
|
|
22
|
+
if (href === undefined)
|
|
23
|
+
return undefined
|
|
24
|
+
const trimmed = href.trim()
|
|
25
|
+
if (trimmed.length === 0)
|
|
26
|
+
return undefined
|
|
27
|
+
let cleaned = ''
|
|
28
|
+
for (let i = 0; i < trimmed.length; i++) {
|
|
29
|
+
const code = trimmed.charCodeAt(i)
|
|
30
|
+
if (code > 0x20 && code !== 0x7F)
|
|
31
|
+
cleaned += trimmed[i]
|
|
32
|
+
}
|
|
33
|
+
return UNSAFE_HREF_RE.test(cleaned) ? undefined : trimmed
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Navigates to a safe href via a synthetic anchor click so `@prunellejs/spa` can intercept same-origin links, falling back to `location.assign`. */
|
|
37
|
+
export function navigateSafeHref(href: string | undefined): boolean {
|
|
38
|
+
const safe = safeHref(href)
|
|
39
|
+
if (safe === undefined)
|
|
40
|
+
return false
|
|
41
|
+
if (typeof document !== 'undefined' && document.body !== null) {
|
|
42
|
+
const a = document.createElement('a')
|
|
43
|
+
a.href = safe
|
|
44
|
+
a.hidden = true
|
|
45
|
+
document.body.appendChild(a)
|
|
46
|
+
let prevented = false
|
|
47
|
+
try {
|
|
48
|
+
const ev = new MouseEvent('click', { bubbles: true, cancelable: true, button: 0 })
|
|
49
|
+
a.dispatchEvent(ev)
|
|
50
|
+
prevented = ev.defaultPrevented
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
a.remove()
|
|
54
|
+
}
|
|
55
|
+
if (prevented)
|
|
56
|
+
return true
|
|
57
|
+
}
|
|
58
|
+
if (typeof location !== 'undefined')
|
|
59
|
+
location.assign(safe)
|
|
60
|
+
return true
|
|
61
|
+
}
|
|
62
|
+
|
|
16
63
|
// ---- Stable hydration-safe unique IDs for ARIA wiring ----
|
|
17
64
|
|
|
18
65
|
export function nextUiId(prefix: string): string {
|
|
@@ -117,8 +164,14 @@ export interface ListPopupState {
|
|
|
117
164
|
|
|
118
165
|
// In the top layer no overflow ancestor clips the popup, and the trigger as its `source` is the anchor CSS places it against and a click the browser does not count as outside.
|
|
119
166
|
function showAnchored(el: HTMLElement, trigger: HTMLElement): void {
|
|
120
|
-
if (el.isConnected && !el.matches(':popover-open'))
|
|
121
|
-
|
|
167
|
+
if (el.isConnected && !el.matches(':popover-open')) {
|
|
168
|
+
try {
|
|
169
|
+
el.showPopover({ source: trigger })
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
el.showPopover()
|
|
173
|
+
}
|
|
174
|
+
}
|
|
122
175
|
}
|
|
123
176
|
|
|
124
177
|
/** Shared popup open/close and keyboard list navigation for Menu and Select; the browser dismisses the popup on an outside click. */
|
|
@@ -127,6 +180,20 @@ export function createListPopup<T>(opts: ListPopupOptions<T>): ListPopupState {
|
|
|
127
180
|
const highlighted = signal(findFirstEnabledIndex(opts.items(), opts.isDisabled))
|
|
128
181
|
let rootEl: HTMLElement | undefined
|
|
129
182
|
let lastOpenEvent: Event | undefined
|
|
183
|
+
// On browsers without `showPopover({ source })`, clicking the trigger light-dismisses the popover before `click` fires; suppress that click so it stays closed.
|
|
184
|
+
let pointerDownOnTrigger = false
|
|
185
|
+
let pointerDown = false
|
|
186
|
+
let suppressNextClick = false
|
|
187
|
+
let clearSuppressTimer: ReturnType<typeof setTimeout> | undefined
|
|
188
|
+
|
|
189
|
+
const scheduleSuppressReset = () => {
|
|
190
|
+
clearTimeout(clearSuppressTimer)
|
|
191
|
+
clearSuppressTimer = setTimeout(() => {
|
|
192
|
+
clearSuppressTimer = undefined
|
|
193
|
+
pointerDownOnTrigger = false
|
|
194
|
+
suppressNextClick = false
|
|
195
|
+
}, 50)
|
|
196
|
+
}
|
|
130
197
|
|
|
131
198
|
const activeDescendantId = (): string | undefined => {
|
|
132
199
|
const idx = highlighted.value
|
|
@@ -163,6 +230,22 @@ export function createListPopup<T>(opts: ListPopupOptions<T>): ListPopupState {
|
|
|
163
230
|
effect(() => {
|
|
164
231
|
if (!open.value)
|
|
165
232
|
return
|
|
233
|
+
const onPointerDown = (e: Event) => {
|
|
234
|
+
const trigger = rootEl?.querySelector('[data-part="trigger"]')
|
|
235
|
+
pointerDownOnTrigger = trigger !== null && trigger !== undefined && trigger.contains(e.target as Node)
|
|
236
|
+
if (!pointerDownOnTrigger)
|
|
237
|
+
return
|
|
238
|
+
pointerDown = true
|
|
239
|
+
suppressNextClick = false
|
|
240
|
+
const onUp = () => {
|
|
241
|
+
pointerDown = false
|
|
242
|
+
document.removeEventListener('pointerup', onUp, true)
|
|
243
|
+
document.removeEventListener('pointercancel', onUp, true)
|
|
244
|
+
scheduleSuppressReset()
|
|
245
|
+
}
|
|
246
|
+
document.addEventListener('pointerup', onUp, true)
|
|
247
|
+
document.addEventListener('pointercancel', onUp, true)
|
|
248
|
+
}
|
|
166
249
|
const onKey = (e: KeyboardEvent) => {
|
|
167
250
|
if (e === lastOpenEvent)
|
|
168
251
|
return
|
|
@@ -178,8 +261,12 @@ export function createListPopup<T>(opts: ListPopupOptions<T>): ListPopupState {
|
|
|
178
261
|
if (open.peek() && highlighted.peek() !== prev)
|
|
179
262
|
focusActive()
|
|
180
263
|
}
|
|
264
|
+
document.addEventListener('pointerdown', onPointerDown, true)
|
|
181
265
|
document.addEventListener('keydown', onKey)
|
|
182
|
-
onCleanup(() =>
|
|
266
|
+
onCleanup(() => {
|
|
267
|
+
document.removeEventListener('pointerdown', onPointerDown, true)
|
|
268
|
+
document.removeEventListener('keydown', onKey)
|
|
269
|
+
})
|
|
183
270
|
})
|
|
184
271
|
|
|
185
272
|
return {
|
|
@@ -189,10 +276,16 @@ export function createListPopup<T>(opts: ListPopupOptions<T>): ListPopupState {
|
|
|
189
276
|
close,
|
|
190
277
|
toggle(e) {
|
|
191
278
|
e.stopPropagation()
|
|
192
|
-
if (open.value)
|
|
279
|
+
if (open.value) {
|
|
193
280
|
close()
|
|
194
|
-
|
|
281
|
+
}
|
|
282
|
+
else if (suppressNextClick) {
|
|
283
|
+
suppressNextClick = false
|
|
284
|
+
pointerDownOnTrigger = false
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
195
287
|
openWith(false)
|
|
288
|
+
}
|
|
196
289
|
},
|
|
197
290
|
onTriggerKeyDown(e) {
|
|
198
291
|
if (!open.value && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
|
|
@@ -207,7 +300,16 @@ export function createListPopup<T>(opts: ListPopupOptions<T>): ListPopupState {
|
|
|
207
300
|
mountPopup(el) {
|
|
208
301
|
if (el === undefined)
|
|
209
302
|
return
|
|
210
|
-
el.addEventListener('toggle',
|
|
303
|
+
el.addEventListener('toggle', (e) => {
|
|
304
|
+
if ((e as ToggleEvent).newState !== 'closed')
|
|
305
|
+
return
|
|
306
|
+
if (open.peek() && pointerDownOnTrigger) {
|
|
307
|
+
suppressNextClick = true
|
|
308
|
+
if (!pointerDown)
|
|
309
|
+
scheduleSuppressReset()
|
|
310
|
+
}
|
|
311
|
+
close()
|
|
312
|
+
})
|
|
211
313
|
queueMicrotask(() => {
|
|
212
314
|
showAnchored(el, rootEl!.querySelector<HTMLElement>('[data-part="trigger"]')!)
|
|
213
315
|
focusActive()
|
package/src/index.ts
CHANGED
|
@@ -17,7 +17,7 @@ export type { DataTableOptions, DataTableState, DialogBehavior, ListNavigationOp
|
|
|
17
17
|
export { toast } from './toast'
|
|
18
18
|
export type { ToastItem } from './toast'
|
|
19
19
|
export { CommandPalette } from './CommandPalette'
|
|
20
|
-
export type { CommandItem, CommandPaletteProps } from './CommandPalette'
|
|
20
|
+
export type { CommandItem, CommandPaletteProps, CommandSource } from './CommandPalette'
|
|
21
21
|
export { DataTable } from './DataTable'
|
|
22
22
|
export type { DataTableColumn, DataTableProps } from './DataTable'
|
|
23
23
|
export { ControlledDialog, Dialog } from './Dialog'
|
package/src/plugin.ts
CHANGED
|
@@ -1,38 +1,42 @@
|
|
|
1
1
|
// @prunellejs/ui PrunellePlugin — wires a pure CSS Grammar (`editorial | workshop | console | soft | orbit | prunelle | bare`)
|
|
2
|
-
// and adds `prunelle ui-eject <grammar> [dest]` so apps can copy & own the entire look in ONE
|
|
2
|
+
// and adds `prunelle ui-eject <grammar> [dest]` so apps can copy & own the entire look in ONE CSS file.
|
|
3
3
|
|
|
4
|
-
import type { PrunellePlugin } from 'prunelle/config'
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
4
|
+
import type { BuildPluginContext, PrunellePlugin } from 'prunelle/config'
|
|
5
|
+
import type { GrammarName, UiComponentPart } from './styles/split'
|
|
6
|
+
import { createHash } from 'node:crypto'
|
|
7
|
+
import { mkdirSync, writeFileSync } from 'node:fs'
|
|
8
|
+
import { dirname, resolve } from 'node:path'
|
|
9
|
+
import { definePlugin, minifyCss, serviceKey } from 'prunelle/config'
|
|
8
10
|
import { FALLBACK_FILE, FALLBACK_GUARD, FALLBACK_JS, withPlatformFallback } from './platform-fallback'
|
|
11
|
+
import { createUiPartScanner } from './styles/scan'
|
|
12
|
+
import {
|
|
13
|
+
GRAMMAR_NAMES,
|
|
14
|
+
grammarCssPath,
|
|
15
|
+
renderGrammarBaseCss,
|
|
16
|
+
renderGrammarCss as renderFullGrammarCss,
|
|
17
|
+
renderGrammarPartsCss,
|
|
18
|
+
UI_COMPONENT_PARTS,
|
|
19
|
+
UI_EXPORT_TO_PARTS,
|
|
20
|
+
} from './styles/split'
|
|
9
21
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const BARE_IMPORT_RE = /^@import\s+['"]\.\.\/bare\.css['"];?\s*/gm
|
|
18
|
-
|
|
19
|
-
export function grammarCssPath(grammar: GrammarName): string {
|
|
20
|
-
return grammar === 'bare'
|
|
21
|
-
? join(STYLES_DIR, 'bare.css')
|
|
22
|
-
: join(STYLES_DIR, 'grammars', `${grammar}.css`)
|
|
22
|
+
export {
|
|
23
|
+
GRAMMAR_NAMES,
|
|
24
|
+
grammarCssPath,
|
|
25
|
+
renderGrammarBaseCss,
|
|
26
|
+
renderGrammarPartsCss,
|
|
27
|
+
UI_COMPONENT_PARTS,
|
|
28
|
+
UI_EXPORT_TO_PARTS,
|
|
23
29
|
}
|
|
30
|
+
export type { GrammarName, UiComponentPart }
|
|
24
31
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const rawGrammar = readFileSync(grammarCssPath(grammar), 'utf8')
|
|
31
|
-
const withoutImport = rawGrammar.replace(BARE_IMPORT_RE, '')
|
|
32
|
-
return `${bare}\n${withoutImport}`
|
|
32
|
+
const UI_KUI_LAYER_SERVICE = serviceKey<boolean>('prunelle.ui.kuiLayer')
|
|
33
|
+
|
|
34
|
+
/** Returns the self-contained CSS source for a grammar (with `base.css` and component sheets inlined). */
|
|
35
|
+
export function renderGrammarCss(grammar: GrammarName, _bareCss?: string): string {
|
|
36
|
+
return renderFullGrammarCss(grammar)
|
|
33
37
|
}
|
|
34
38
|
|
|
35
|
-
/** Ejects a complete, self-contained CSS Grammar file
|
|
39
|
+
/** Ejects a complete, self-contained CSS Grammar file into the app. */
|
|
36
40
|
export function ejectGrammar(root: string, grammar: GrammarName = 'editorial', destRel = 'src/styles/ui.css'): string {
|
|
37
41
|
const outPath = resolve(root, destRel)
|
|
38
42
|
mkdirSync(dirname(outPath), { recursive: true })
|
|
@@ -66,23 +70,83 @@ function resolveEmitGrammars(opt: UiPluginOptions['emitGrammars']): { dir: strin
|
|
|
66
70
|
return { dir, grammars: opts.grammars ?? GRAMMAR_NAMES }
|
|
67
71
|
}
|
|
68
72
|
|
|
73
|
+
function cssHash(css: string): string {
|
|
74
|
+
return createHash('sha256').update(css).digest('hex').slice(0, 8)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function emitGrammarStyles(ctx: BuildPluginContext, grammar: GrammarName): void {
|
|
78
|
+
const prepGlobal = ctx.prependStyle ?? ctx.addStyle
|
|
79
|
+
const prepRoute = ctx.prependRouteStyle ?? ctx.addRouteStyle
|
|
80
|
+
if (prepGlobal === undefined)
|
|
81
|
+
return
|
|
82
|
+
const format = (css: string): string => ctx.minify ? minifyCss(css) : css
|
|
83
|
+
|
|
84
|
+
if (ctx.routeSourceFiles === undefined) {
|
|
85
|
+
const full = format(renderGrammarCss(grammar))
|
|
86
|
+
prepGlobal(ctx.emitAsset(`${grammar}-${cssHash(full)}.css`, full))
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const scan = createUiPartScanner()
|
|
91
|
+
const globalParts = scan(ctx.globalSourceFiles ?? [])
|
|
92
|
+
const routes = ctx.routes ?? []
|
|
93
|
+
const perRoute = new Map<string, Set<UiComponentPart>>()
|
|
94
|
+
for (const route of routes)
|
|
95
|
+
perRoute.set(route.path, scan(ctx.routeSourceFiles.get(route.path) ?? []))
|
|
96
|
+
|
|
97
|
+
// Any UI component part rendered on every route belongs in the site-wide sheet so it is cached across navigations.
|
|
98
|
+
if (routes.length > 0) {
|
|
99
|
+
const firstParts = perRoute.get(routes[0]!.path)!
|
|
100
|
+
for (const part of firstParts) {
|
|
101
|
+
if (routes.every(r => perRoute.get(r.path)!.has(part)))
|
|
102
|
+
globalParts.add(part)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const baseSource = globalParts.size > 0
|
|
107
|
+
? `${renderGrammarBaseCss(grammar)}\n${renderGrammarPartsCss(grammar, globalParts)}`
|
|
108
|
+
: renderGrammarBaseCss(grammar)
|
|
109
|
+
const baseCss = format(baseSource)
|
|
110
|
+
prepGlobal(ctx.emitAsset(`${grammar}-${cssHash(baseCss)}.css`, baseCss))
|
|
111
|
+
|
|
112
|
+
if (prepRoute === undefined)
|
|
113
|
+
return
|
|
114
|
+
const routeCache = new Map<string, string>()
|
|
115
|
+
for (const route of routes) {
|
|
116
|
+
const routeParts = perRoute.get(route.path)!
|
|
117
|
+
const extra = UI_COMPONENT_PARTS.filter(p => routeParts.has(p) && !globalParts.has(p))
|
|
118
|
+
if (extra.length === 0)
|
|
119
|
+
continue
|
|
120
|
+
const key = extra.join(',')
|
|
121
|
+
let href = routeCache.get(key)
|
|
122
|
+
if (href === undefined) {
|
|
123
|
+
const partsCss = format(renderGrammarPartsCss(grammar, extra))
|
|
124
|
+
href = ctx.emitAsset(`${grammar}-parts-${cssHash(partsCss)}.css`, partsCss)
|
|
125
|
+
routeCache.set(key, href)
|
|
126
|
+
}
|
|
127
|
+
prepRoute(route.path, href)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
69
131
|
export function ui(options: UiPluginOptions = {}): PrunellePlugin {
|
|
70
132
|
const grammar = options.grammar === undefined ? 'editorial' : options.grammar
|
|
71
133
|
const emit = resolveEmitGrammars(options.emitGrammars)
|
|
72
134
|
let assetPrefix = '/_pru'
|
|
73
135
|
return definePlugin({
|
|
74
136
|
name: '@prunellejs/ui',
|
|
75
|
-
css: grammar === false ? undefined : [grammarCssPath(grammar)],
|
|
76
137
|
inlineScripts: [FALLBACK_GUARD],
|
|
77
138
|
build(ctx) {
|
|
78
139
|
ctx.emitAsset(FALLBACK_FILE, FALLBACK_JS)
|
|
79
|
-
if (grammar !== false)
|
|
140
|
+
if (grammar !== false) {
|
|
80
141
|
ctx.provide?.(UI_KUI_LAYER_SERVICE, true)
|
|
142
|
+
emitGrammarStyles(ctx, grammar)
|
|
143
|
+
}
|
|
81
144
|
if (emit !== undefined) {
|
|
82
|
-
const bare = readFileSync(join(STYLES_DIR, 'bare.css'), 'utf8')
|
|
83
145
|
const prefix = emit.dir ? `${emit.dir}/` : ''
|
|
84
|
-
for (const g of emit.grammars)
|
|
85
|
-
|
|
146
|
+
for (const g of emit.grammars) {
|
|
147
|
+
const css = renderGrammarCss(g)
|
|
148
|
+
ctx.emitPublic(`${prefix}${g}.css`, ctx.minify ? minifyCss(css) : css)
|
|
149
|
+
}
|
|
86
150
|
}
|
|
87
151
|
},
|
|
88
152
|
server(api) {
|