@nocode-factory/sanity-kit 2.31.0 → 2.32.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/ncf-kit.mjs +3 -0
- package/dist/index.d.ts +17 -2
- package/dist/index.js +155 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/tools/presentation/PagesNavigator.tsx +145 -0
- package/src/version.ts +1 -1
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -259,6 +259,7 @@ export {
|
|
|
259
259
|
export type {PreviewPerspective, PresentationOptions} from './lib/studioLinks'
|
|
260
260
|
export {usePreviewTarget} from './context/PreviewTargetContext'
|
|
261
261
|
export {NcfPreviewHeader, ncfPreviewComponents} from './tools/presentation/PreviewHeader'
|
|
262
|
+
export {ncfPresentationComponents} from './tools/presentation/PagesNavigator'
|
|
262
263
|
export {NcfPreviewLauncher, createPreviewLauncher} from './tools/navbar/PreviewLauncher'
|
|
263
264
|
export {NcfKitUpdate} from './tools/navbar/KitUpdate'
|
|
264
265
|
export {useKitUpdate} from './hooks/useKitUpdate'
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {useMemo, useState, type ComponentType} from 'react'
|
|
2
|
+
import {Box, Card, Flex, Select, Spinner, Stack, Text, TextInput} from '@sanity/ui'
|
|
3
|
+
import {SearchIcon} from '@sanity/icons/Search'
|
|
4
|
+
import {usePresentationNavigate, usePresentationParams} from 'sanity/presentation'
|
|
5
|
+
import type {NcfKitConfig} from '../../config/types'
|
|
6
|
+
import {resolveConfig} from '../../config/resolve'
|
|
7
|
+
import {validateConfig} from '../../config/validate'
|
|
8
|
+
import {KitProvider, useKitConfig} from '../../context/KitContext'
|
|
9
|
+
import {usePages} from '../../hooks/usePages'
|
|
10
|
+
import {localeOfPreview} from '../../lib/previewLocale'
|
|
11
|
+
import {StatusBadge} from '../../ui/StatusBadge'
|
|
12
|
+
|
|
13
|
+
const pathOf = (preview: string | undefined | null): string | null => {
|
|
14
|
+
if (!preview) return null
|
|
15
|
+
try {
|
|
16
|
+
return new URL(preview, 'http://preview.local').pathname.replace(/\/$/, '') || '/'
|
|
17
|
+
} catch {
|
|
18
|
+
return null
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Liste des pages à gauche de l'aperçu (Presentation) : passer d'une page à l'autre
|
|
24
|
+
* sans repasser par Structure. Langue de la page affichée par défaut, recherche par
|
|
25
|
+
* titre ou adresse ; un clic ouvre la page dans l'aperçu ET son document à côté.
|
|
26
|
+
*/
|
|
27
|
+
function PagesNavigator() {
|
|
28
|
+
const config = useKitConfig()
|
|
29
|
+
const {rows, loading} = usePages()
|
|
30
|
+
const params = usePresentationParams(false)
|
|
31
|
+
const navigate = usePresentationNavigate()
|
|
32
|
+
const i18n = config.i18n
|
|
33
|
+
const shownLocale = localeOfPreview(params?.preview, i18n)
|
|
34
|
+
const [locale, setLocale] = useState<string | null>(null)
|
|
35
|
+
const [search, setSearch] = useState('')
|
|
36
|
+
// Langue choisie, sinon celle de la page affichée, sinon la langue principale :
|
|
37
|
+
// le menu montre toujours la langue réellement filtrée.
|
|
38
|
+
const activeLocale = locale ?? shownLocale ?? i18n?.defaultLocale ?? ''
|
|
39
|
+
const current = pathOf(params?.preview)
|
|
40
|
+
|
|
41
|
+
const visible = useMemo(() => {
|
|
42
|
+
const q = search.trim().toLowerCase()
|
|
43
|
+
return rows
|
|
44
|
+
.filter((r) => r.path !== null)
|
|
45
|
+
.filter((r) => !i18n || !activeLocale || !r.locale || r.locale === activeLocale)
|
|
46
|
+
.filter((r) => !q || r.title.toLowerCase().includes(q) || (r.path ?? '').toLowerCase().includes(q))
|
|
47
|
+
.sort((a, b) => (a.path ?? '').localeCompare(b.path ?? ''))
|
|
48
|
+
}, [rows, search, activeLocale, i18n])
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<Card height="fill" borderRight style={{display: 'flex', flexDirection: 'column', minHeight: 0}}>
|
|
52
|
+
<Stack gap={2} padding={2} style={{flex: 'none'}}>
|
|
53
|
+
<Text size={1} weight="semibold">
|
|
54
|
+
Pages
|
|
55
|
+
</Text>
|
|
56
|
+
<Flex gap={2}>
|
|
57
|
+
<Box flex={1}>
|
|
58
|
+
<TextInput
|
|
59
|
+
icon={SearchIcon}
|
|
60
|
+
fontSize={1}
|
|
61
|
+
padding={2}
|
|
62
|
+
placeholder="Search"
|
|
63
|
+
value={search}
|
|
64
|
+
onChange={(e) => setSearch(e.currentTarget.value)}
|
|
65
|
+
/>
|
|
66
|
+
</Box>
|
|
67
|
+
{i18n && i18n.locales.length > 1 && (
|
|
68
|
+
<Select fontSize={1} padding={2} value={activeLocale} onChange={(e) => setLocale(e.currentTarget.value)} style={{width: 88}}>
|
|
69
|
+
{i18n.locales.map((code) => (
|
|
70
|
+
<option key={code} value={code}>
|
|
71
|
+
{code.toUpperCase()}
|
|
72
|
+
</option>
|
|
73
|
+
))}
|
|
74
|
+
</Select>
|
|
75
|
+
)}
|
|
76
|
+
</Flex>
|
|
77
|
+
</Stack>
|
|
78
|
+
<Box flex={1} style={{overflowY: 'auto', minHeight: 0}}>
|
|
79
|
+
{loading && !rows.length ? (
|
|
80
|
+
<Flex justify="center" padding={4}>
|
|
81
|
+
<Spinner muted />
|
|
82
|
+
</Flex>
|
|
83
|
+
) : (
|
|
84
|
+
<Stack gap={1} padding={1}>
|
|
85
|
+
{visible.map((row) => {
|
|
86
|
+
const selected = current !== null && current === ((row.path ?? '').replace(/\/$/, '') || '/')
|
|
87
|
+
return (
|
|
88
|
+
<Card
|
|
89
|
+
key={row.id}
|
|
90
|
+
as="button"
|
|
91
|
+
padding={2}
|
|
92
|
+
radius={2}
|
|
93
|
+
tone={selected ? 'primary' : 'default'}
|
|
94
|
+
pressed={selected}
|
|
95
|
+
style={{textAlign: 'left', cursor: 'pointer', border: 0, width: '100%'}}
|
|
96
|
+
onClick={() => navigate(row.path ?? undefined, {type: row.type, id: row.id})}
|
|
97
|
+
>
|
|
98
|
+
<Flex align="center" gap={2}>
|
|
99
|
+
<Stack gap={1} flex={1} style={{minWidth: 0}}>
|
|
100
|
+
<Text size={1} weight={selected ? 'semibold' : 'regular'} textOverflow="ellipsis">
|
|
101
|
+
{row.title || row.path}
|
|
102
|
+
</Text>
|
|
103
|
+
<Text size={0} muted textOverflow="ellipsis">
|
|
104
|
+
{row.path}
|
|
105
|
+
</Text>
|
|
106
|
+
</Stack>
|
|
107
|
+
{row.status !== 'published' && <StatusBadge status={row.status} />}
|
|
108
|
+
</Flex>
|
|
109
|
+
</Card>
|
|
110
|
+
)
|
|
111
|
+
})}
|
|
112
|
+
{!visible.length && (
|
|
113
|
+
<Box padding={3}>
|
|
114
|
+
<Text size={1} muted>
|
|
115
|
+
No page.
|
|
116
|
+
</Text>
|
|
117
|
+
</Box>
|
|
118
|
+
)}
|
|
119
|
+
</Stack>
|
|
120
|
+
)}
|
|
121
|
+
</Box>
|
|
122
|
+
</Card>
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Composants à passer à `presentationTool` : liste des pages à gauche de l'aperçu
|
|
128
|
+
* (bouton intégré de Presentation pour l'afficher ou la masquer).
|
|
129
|
+
*
|
|
130
|
+
* ```ts
|
|
131
|
+
* presentationTool({components: ncfPresentationComponents(ncfOptions), ...})
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export function ncfPresentationComponents(options: NcfKitConfig): {unstable_navigator: {component: ComponentType; minWidth: number; maxWidth: number}} {
|
|
135
|
+
const config = resolveConfig(options)
|
|
136
|
+
const issues = validateConfig(config)
|
|
137
|
+
function NcfPagesNavigator() {
|
|
138
|
+
return (
|
|
139
|
+
<KitProvider config={config} issues={issues}>
|
|
140
|
+
<PagesNavigator />
|
|
141
|
+
</KitProvider>
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
return {unstable_navigator: {component: NcfPagesNavigator, minWidth: 220, maxWidth: 420}}
|
|
145
|
+
}
|
package/src/version.ts
CHANGED