@nocode-factory/sanity-kit 2.32.0 → 2.33.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/dist/index.d.ts +14 -2
- package/dist/index.js +113 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/tools/presentation/PagesNavigator.tsx +16 -6
- package/src/tools/presentation/PresentationHeader.tsx +118 -0
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -9,6 +9,7 @@ import {KitProvider, useKitConfig} from '../../context/KitContext'
|
|
|
9
9
|
import {usePages} from '../../hooks/usePages'
|
|
10
10
|
import {localeOfPreview} from '../../lib/previewLocale'
|
|
11
11
|
import {StatusBadge} from '../../ui/StatusBadge'
|
|
12
|
+
import {NcfPresentationHeader} from './PresentationHeader'
|
|
12
13
|
|
|
13
14
|
const pathOf = (preview: string | undefined | null): string | null => {
|
|
14
15
|
if (!preview) return null
|
|
@@ -33,14 +34,16 @@ function PagesNavigator() {
|
|
|
33
34
|
const shownLocale = localeOfPreview(params?.preview, i18n)
|
|
34
35
|
const [locale, setLocale] = useState<string | null>(null)
|
|
35
36
|
const [search, setSearch] = useState('')
|
|
36
|
-
|
|
37
|
+
// Langue choisie, sinon celle de la page affichée, sinon la langue principale :
|
|
38
|
+
// le menu montre toujours la langue réellement filtrée.
|
|
39
|
+
const activeLocale = locale ?? shownLocale ?? i18n?.defaultLocale ?? ''
|
|
37
40
|
const current = pathOf(params?.preview)
|
|
38
41
|
|
|
39
42
|
const visible = useMemo(() => {
|
|
40
43
|
const q = search.trim().toLowerCase()
|
|
41
44
|
return rows
|
|
42
45
|
.filter((r) => r.path !== null)
|
|
43
|
-
.filter((r) => !i18n || !activeLocale || r.locale === activeLocale)
|
|
46
|
+
.filter((r) => !i18n || !activeLocale || !r.locale || r.locale === activeLocale)
|
|
44
47
|
.filter((r) => !q || r.title.toLowerCase().includes(q) || (r.path ?? '').toLowerCase().includes(q))
|
|
45
48
|
.sort((a, b) => (a.path ?? '').localeCompare(b.path ?? ''))
|
|
46
49
|
}, [rows, search, activeLocale, i18n])
|
|
@@ -63,7 +66,7 @@ function PagesNavigator() {
|
|
|
63
66
|
/>
|
|
64
67
|
</Box>
|
|
65
68
|
{i18n && i18n.locales.length > 1 && (
|
|
66
|
-
<Select fontSize={1} padding={2} value={activeLocale} onChange={(e) => setLocale(e.currentTarget.value)} style={{width:
|
|
69
|
+
<Select fontSize={1} padding={2} value={activeLocale} onChange={(e) => setLocale(e.currentTarget.value)} style={{width: 88}}>
|
|
67
70
|
{i18n.locales.map((code) => (
|
|
68
71
|
<option key={code} value={code}>
|
|
69
72
|
{code.toUpperCase()}
|
|
@@ -123,13 +126,17 @@ function PagesNavigator() {
|
|
|
123
126
|
|
|
124
127
|
/**
|
|
125
128
|
* Composants à passer à `presentationTool` : liste des pages à gauche de l'aperçu
|
|
126
|
-
* (bouton intégré de Presentation pour l'afficher ou la masquer)
|
|
129
|
+
* (bouton intégré de Presentation pour l'afficher ou la masquer), et en-tête avec
|
|
130
|
+
* le format tablette et « Hide edit panel ».
|
|
127
131
|
*
|
|
128
132
|
* ```ts
|
|
129
133
|
* presentationTool({components: ncfPresentationComponents(ncfOptions), ...})
|
|
130
134
|
* ```
|
|
131
135
|
*/
|
|
132
|
-
export function ncfPresentationComponents(options: NcfKitConfig): {
|
|
136
|
+
export function ncfPresentationComponents(options: NcfKitConfig): {
|
|
137
|
+
unstable_navigator: {component: ComponentType; minWidth: number; maxWidth: number}
|
|
138
|
+
unstable_header: {component: typeof NcfPresentationHeader}
|
|
139
|
+
} {
|
|
133
140
|
const config = resolveConfig(options)
|
|
134
141
|
const issues = validateConfig(config)
|
|
135
142
|
function NcfPagesNavigator() {
|
|
@@ -139,5 +146,8 @@ export function ncfPresentationComponents(options: NcfKitConfig): {unstable_navi
|
|
|
139
146
|
</KitProvider>
|
|
140
147
|
)
|
|
141
148
|
}
|
|
142
|
-
return {
|
|
149
|
+
return {
|
|
150
|
+
unstable_navigator: {component: NcfPagesNavigator, minWidth: 220, maxWidth: 420},
|
|
151
|
+
unstable_header: {component: NcfPresentationHeader},
|
|
152
|
+
}
|
|
143
153
|
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {useEffect, useRef, useState} from 'react'
|
|
2
|
+
import {Button, Flex, Box} from '@sanity/ui'
|
|
3
|
+
import {TabletDeviceIcon} from '@sanity/icons/TabletDevice'
|
|
4
|
+
import {PanelRightIcon} from '@sanity/icons/PanelRight'
|
|
5
|
+
import type {PreviewHeaderProps} from 'sanity/presentation'
|
|
6
|
+
|
|
7
|
+
const TABLET_WIDTH = 768
|
|
8
|
+
const KEY_TABLET = 'ncf.presentation.tablet'
|
|
9
|
+
const KEY_HIDE = 'ncf.presentation.hideForm'
|
|
10
|
+
|
|
11
|
+
const load = (key: string) => {
|
|
12
|
+
try {
|
|
13
|
+
return window.localStorage.getItem(key) === '1'
|
|
14
|
+
} catch {
|
|
15
|
+
return false
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const save = (key: string, on: boolean) => {
|
|
19
|
+
try {
|
|
20
|
+
window.localStorage.setItem(key, on ? '1' : '0')
|
|
21
|
+
} catch {
|
|
22
|
+
/* stockage indisponible : le choix vaut pour la session */
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Panneau (enfant direct du conteneur des panneaux de Presentation) qui contient `el`. */
|
|
27
|
+
function panelOf(el: HTMLElement | null): HTMLElement | null {
|
|
28
|
+
for (let node = el; node?.parentElement; node = node.parentElement) {
|
|
29
|
+
const sibling = node.nextElementSibling as HTMLElement | null
|
|
30
|
+
if (node.style.flexGrow !== '' && sibling && sibling.style.flexGrow !== '') return node
|
|
31
|
+
}
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* En-tête de l'aperçu (Presentation) : la barre d'adresse native, plus
|
|
37
|
+
* - « Tablet » : aperçu à 768 px, centré (Presentation n'a qu'ordinateur et mobile) ;
|
|
38
|
+
* - « Hide edit panel » : masque le formulaire à droite, l'aperçu prend la place
|
|
39
|
+
* (la liste des pages à gauche reste).
|
|
40
|
+
* Choix mémorisés dans le navigateur. Aucune donnée touchée : seulement l'affichage.
|
|
41
|
+
*/
|
|
42
|
+
export function NcfPresentationHeader(props: PreviewHeaderProps) {
|
|
43
|
+
const ref = useRef<HTMLDivElement>(null)
|
|
44
|
+
const [tablet, setTablet] = useState(() => load(KEY_TABLET))
|
|
45
|
+
const [hideForm, setHideForm] = useState(() => load(KEY_HIDE))
|
|
46
|
+
|
|
47
|
+
// Tablette : largeur maximale de l'iframe de l'aperçu, centrée.
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
const panel = panelOf(ref.current)
|
|
50
|
+
const iframe = panel?.querySelector('iframe') ?? null
|
|
51
|
+
if (!iframe) return
|
|
52
|
+
if (tablet) {
|
|
53
|
+
iframe.style.setProperty('max-width', `${TABLET_WIDTH}px`, 'important')
|
|
54
|
+
iframe.style.setProperty('margin-left', 'auto', 'important')
|
|
55
|
+
iframe.style.setProperty('margin-right', 'auto', 'important')
|
|
56
|
+
iframe.style.setProperty('display', 'block', 'important')
|
|
57
|
+
}
|
|
58
|
+
return () => {
|
|
59
|
+
for (const p of ['max-width', 'margin-left', 'margin-right', 'display']) iframe.style.removeProperty(p)
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
// Panneau d'édition masqué : panneaux à droite de l'aperçu cachés, aperçu élargi.
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
const panel = panelOf(ref.current)
|
|
66
|
+
if (!panel || !hideForm) return
|
|
67
|
+
const hidden: HTMLElement[] = []
|
|
68
|
+
for (let next = panel.nextElementSibling as HTMLElement | null; next; next = next.nextElementSibling as HTMLElement | null) hidden.push(next)
|
|
69
|
+
const apply = () => {
|
|
70
|
+
for (const h of hidden) if (h.style.getPropertyValue('display') !== 'none') h.style.setProperty('display', 'none', 'important')
|
|
71
|
+
if (panel.style.getPropertyPriority('flex-grow') !== 'important') panel.style.setProperty('flex-grow', '1000', 'important')
|
|
72
|
+
}
|
|
73
|
+
apply()
|
|
74
|
+
// Presentation réécrit les largeurs de ses panneaux (redimensionnement) : on
|
|
75
|
+
// réapplique, sans boucle (rien n'est réécrit si c'est déjà en place).
|
|
76
|
+
const observer = new MutationObserver(apply)
|
|
77
|
+
for (const el of [panel, ...hidden]) observer.observe(el, {attributes: true, attributeFilter: ['style']})
|
|
78
|
+
return () => {
|
|
79
|
+
observer.disconnect()
|
|
80
|
+
for (const h of hidden) h.style.removeProperty('display')
|
|
81
|
+
panel.style.removeProperty('flex-grow')
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<Flex ref={ref} align="center" gap={1} style={{width: '100%'}}>
|
|
87
|
+
<Box flex={1} style={{minWidth: 0}}>
|
|
88
|
+
{props.renderDefault(props)}
|
|
89
|
+
</Box>
|
|
90
|
+
<Button
|
|
91
|
+
icon={TabletDeviceIcon}
|
|
92
|
+
mode={tablet ? 'default' : 'bleed'}
|
|
93
|
+
tone={tablet ? 'primary' : 'default'}
|
|
94
|
+
padding={2}
|
|
95
|
+
fontSize={1}
|
|
96
|
+
title={tablet ? 'Back to full width' : `Tablet (${TABLET_WIDTH}px)`}
|
|
97
|
+
aria-pressed={tablet}
|
|
98
|
+
onClick={() => {
|
|
99
|
+
setTablet(!tablet)
|
|
100
|
+
save(KEY_TABLET, !tablet)
|
|
101
|
+
}}
|
|
102
|
+
/>
|
|
103
|
+
<Button
|
|
104
|
+
icon={PanelRightIcon}
|
|
105
|
+
mode={hideForm ? 'default' : 'bleed'}
|
|
106
|
+
tone={hideForm ? 'primary' : 'default'}
|
|
107
|
+
padding={2}
|
|
108
|
+
fontSize={1}
|
|
109
|
+
title={hideForm ? 'Show edit panel' : 'Hide edit panel'}
|
|
110
|
+
aria-pressed={hideForm}
|
|
111
|
+
onClick={() => {
|
|
112
|
+
setHideForm(!hideForm)
|
|
113
|
+
save(KEY_HIDE, !hideForm)
|
|
114
|
+
}}
|
|
115
|
+
/>
|
|
116
|
+
</Flex>
|
|
117
|
+
)
|
|
118
|
+
}
|
package/src/version.ts
CHANGED