@nocode-factory/sanity-kit 2.32.1 → 2.33.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/dist/index.d.ts +14 -2
- package/dist/index.js +102 -8
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/tools/presentation/PagesNavigator.tsx +11 -3
- package/src/tools/presentation/PresentationHeader.tsx +116 -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
|
|
@@ -125,13 +126,17 @@ function PagesNavigator() {
|
|
|
125
126
|
|
|
126
127
|
/**
|
|
127
128
|
* Composants à passer à `presentationTool` : liste des pages à gauche de l'aperçu
|
|
128
|
-
* (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 ».
|
|
129
131
|
*
|
|
130
132
|
* ```ts
|
|
131
133
|
* presentationTool({components: ncfPresentationComponents(ncfOptions), ...})
|
|
132
134
|
* ```
|
|
133
135
|
*/
|
|
134
|
-
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
|
+
} {
|
|
135
140
|
const config = resolveConfig(options)
|
|
136
141
|
const issues = validateConfig(config)
|
|
137
142
|
function NcfPagesNavigator() {
|
|
@@ -141,5 +146,8 @@ export function ncfPresentationComponents(options: NcfKitConfig): {unstable_navi
|
|
|
141
146
|
</KitProvider>
|
|
142
147
|
)
|
|
143
148
|
}
|
|
144
|
-
return {
|
|
149
|
+
return {
|
|
150
|
+
unstable_navigator: {component: NcfPagesNavigator, minWidth: 220, maxWidth: 420},
|
|
151
|
+
unstable_header: {component: NcfPresentationHeader},
|
|
152
|
+
}
|
|
145
153
|
}
|
|
@@ -0,0 +1,116 @@
|
|
|
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
|
+
// Panneaux de Presentation : identifiants HTML stables posés par Sanity
|
|
27
|
+
// (getPresentationPanelHtmlId : `presentation-narrow-panel-<onglet>`).
|
|
28
|
+
const previewPanel = () => document.getElementById('presentation-narrow-panel-preview')
|
|
29
|
+
const contentPanel = () => document.getElementById('presentation-narrow-panel-content')
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* En-tête de l'aperçu (Presentation) : la barre d'adresse native, plus
|
|
33
|
+
* - « Tablet » : aperçu à 768 px, centré (Presentation n'a qu'ordinateur et mobile) ;
|
|
34
|
+
* - « Hide edit panel » : masque le formulaire à droite, l'aperçu prend la place
|
|
35
|
+
* (la liste des pages à gauche reste).
|
|
36
|
+
* Choix mémorisés dans le navigateur. Aucune donnée touchée : seulement l'affichage.
|
|
37
|
+
*/
|
|
38
|
+
export function NcfPresentationHeader(props: PreviewHeaderProps) {
|
|
39
|
+
const ref = useRef<HTMLDivElement>(null)
|
|
40
|
+
const [tablet, setTablet] = useState(() => load(KEY_TABLET))
|
|
41
|
+
const [hideForm, setHideForm] = useState(() => load(KEY_HIDE))
|
|
42
|
+
|
|
43
|
+
// Tablette : largeur maximale de l'iframe de l'aperçu, centrée.
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
const iframe = previewPanel()?.querySelector('iframe') ?? ref.current?.closest('div[style]')?.parentElement?.querySelector('iframe') ?? null
|
|
46
|
+
if (!iframe) return
|
|
47
|
+
if (tablet) {
|
|
48
|
+
iframe.style.setProperty('max-width', `${TABLET_WIDTH}px`, 'important')
|
|
49
|
+
iframe.style.setProperty('margin-left', 'auto', 'important')
|
|
50
|
+
iframe.style.setProperty('margin-right', 'auto', 'important')
|
|
51
|
+
iframe.style.setProperty('display', 'block', 'important')
|
|
52
|
+
}
|
|
53
|
+
return () => {
|
|
54
|
+
for (const p of ['max-width', 'margin-left', 'margin-right', 'display']) iframe.style.removeProperty(p)
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
// Panneau d'édition masqué : panneaux à droite de l'aperçu cachés, aperçu élargi.
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
const panel = previewPanel()
|
|
61
|
+
const content = contentPanel()
|
|
62
|
+
if (!panel || !content || !hideForm) return
|
|
63
|
+
// Le panneau d'édition et sa poignée de redimensionnement (juste avant lui).
|
|
64
|
+
const hidden: HTMLElement[] = [content]
|
|
65
|
+
const handle = content.previousElementSibling as HTMLElement | null
|
|
66
|
+
if (handle && handle !== panel && !handle.id) hidden.push(handle)
|
|
67
|
+
const apply = () => {
|
|
68
|
+
for (const h of hidden) if (h.style.getPropertyValue('display') !== 'none') h.style.setProperty('display', 'none', 'important')
|
|
69
|
+
if (panel.style.getPropertyPriority('flex-grow') !== 'important') panel.style.setProperty('flex-grow', '1000', 'important')
|
|
70
|
+
}
|
|
71
|
+
apply()
|
|
72
|
+
// Presentation réécrit les largeurs de ses panneaux (redimensionnement) : on
|
|
73
|
+
// réapplique, sans boucle (rien n'est réécrit si c'est déjà en place).
|
|
74
|
+
const observer = new MutationObserver(apply)
|
|
75
|
+
for (const el of [panel, ...hidden]) observer.observe(el, {attributes: true, attributeFilter: ['style']})
|
|
76
|
+
return () => {
|
|
77
|
+
observer.disconnect()
|
|
78
|
+
for (const h of hidden) h.style.removeProperty('display')
|
|
79
|
+
panel.style.removeProperty('flex-grow')
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<Flex ref={ref} align="center" gap={1} style={{width: '100%'}}>
|
|
85
|
+
<Box flex={1} style={{minWidth: 0}}>
|
|
86
|
+
{props.renderDefault(props)}
|
|
87
|
+
</Box>
|
|
88
|
+
<Button
|
|
89
|
+
icon={TabletDeviceIcon}
|
|
90
|
+
mode={tablet ? 'default' : 'bleed'}
|
|
91
|
+
tone={tablet ? 'primary' : 'default'}
|
|
92
|
+
padding={2}
|
|
93
|
+
fontSize={1}
|
|
94
|
+
title={tablet ? 'Back to full width' : `Tablet (${TABLET_WIDTH}px)`}
|
|
95
|
+
aria-pressed={tablet}
|
|
96
|
+
onClick={() => {
|
|
97
|
+
setTablet(!tablet)
|
|
98
|
+
save(KEY_TABLET, !tablet)
|
|
99
|
+
}}
|
|
100
|
+
/>
|
|
101
|
+
<Button
|
|
102
|
+
icon={PanelRightIcon}
|
|
103
|
+
mode={hideForm ? 'default' : 'bleed'}
|
|
104
|
+
tone={hideForm ? 'primary' : 'default'}
|
|
105
|
+
padding={2}
|
|
106
|
+
fontSize={1}
|
|
107
|
+
title={hideForm ? 'Show edit panel' : 'Hide edit panel'}
|
|
108
|
+
aria-pressed={hideForm}
|
|
109
|
+
onClick={() => {
|
|
110
|
+
setHideForm(!hideForm)
|
|
111
|
+
save(KEY_HIDE, !hideForm)
|
|
112
|
+
}}
|
|
113
|
+
/>
|
|
114
|
+
</Flex>
|
|
115
|
+
)
|
|
116
|
+
}
|
package/src/version.ts
CHANGED