@nocode-factory/sanity-kit 2.51.1 → 2.53.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 +5 -4
- package/dist/index.d.ts +35 -4
- package/dist/index.js +679 -106
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/form/AssetSourceBrowser.tsx +110 -0
- package/src/hooks/useAssetDelete.ts +60 -0
- package/src/hooks/useAssetUpload.ts +46 -0
- package/src/hooks/useBoardLayout.ts +25 -1
- package/src/index.ts +26 -1
- package/src/lib/assetRefs.ts +53 -0
- package/src/lib/i18nTemplates.ts +40 -0
- package/src/lib/pageOrder.ts +36 -0
- package/src/lib/pages.ts +11 -1
- package/src/schema/boardLayout.ts +7 -0
- package/src/tools/assets/AssetCard.tsx +8 -10
- package/src/tools/assets/AssetDetail.tsx +97 -2
- package/src/tools/assets/AssetsTool.tsx +90 -13
- package/src/tools/assets/AssetsToolbar.tsx +17 -0
- package/src/tools/pages/PagesTool.tsx +15 -9
- package/src/tools/presentation/PagesNavigator.tsx +51 -6
- package/src/version.ts +1 -1
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {useEffect, useState} from 'react'
|
|
2
|
-
import {Box, Button, Card, Flex, Grid, Label, Select, Stack, Text, TextArea, TextInput} from '@sanity/ui'
|
|
2
|
+
import {Box, Button, Card, Dialog, Flex, Grid, Label, Select, Spinner, Stack, Text, TextArea, TextInput} from '@sanity/ui'
|
|
3
3
|
import {CloseIcon} from '@sanity/icons/Close'
|
|
4
4
|
import {CopyIcon} from '@sanity/icons/Copy'
|
|
5
5
|
import {LaunchIcon} from '@sanity/icons/Launch'
|
|
6
|
+
import {TrashIcon} from '@sanity/icons/Trash'
|
|
6
7
|
import {useKitClient} from '../../hooks/useKitClient'
|
|
8
|
+
import {useAssetDelete, type AssetUsage} from '../../hooks/useAssetDelete'
|
|
7
9
|
import {assetName, formatBytes, isImageAsset, thumbnailUrl, type AssetDoc, type AssetFolder} from '../../lib/assets'
|
|
8
10
|
import {MONO} from '../../ui/tokens'
|
|
9
11
|
import {formatDate} from '../../ui/formatDate'
|
|
@@ -14,6 +16,8 @@ export interface AssetDetailProps {
|
|
|
14
16
|
folder: string | null
|
|
15
17
|
onMove: (folder: string | null) => void
|
|
16
18
|
onClose: () => void
|
|
19
|
+
/** Appelé après suppression (pour fermer le détail et rafraîchir). */
|
|
20
|
+
onDeleted?: () => void
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
function Metric(props: {label: string; value: string}) {
|
|
@@ -30,12 +34,39 @@ function Metric(props: {label: string; value: string}) {
|
|
|
30
34
|
}
|
|
31
35
|
|
|
32
36
|
export function AssetDetail(props: AssetDetailProps) {
|
|
33
|
-
const {asset, folders, folder, onMove, onClose} = props
|
|
37
|
+
const {asset, folders, folder, onMove, onClose, onDeleted} = props
|
|
34
38
|
const client = useKitClient()
|
|
39
|
+
const {usage, remove} = useAssetDelete()
|
|
35
40
|
const [title, setTitle] = useState(asset.title ?? '')
|
|
36
41
|
const [alt, setAlt] = useState(asset.altText ?? '')
|
|
37
42
|
const [description, setDescription] = useState(asset.description ?? '')
|
|
38
43
|
const [copied, setCopied] = useState(false)
|
|
44
|
+
// Suppression : null = fermé ; sinon on charge d'abord les usages, puis on confirme.
|
|
45
|
+
const [confirming, setConfirming] = useState(false)
|
|
46
|
+
const [uses, setUses] = useState<AssetUsage[] | null>(null)
|
|
47
|
+
const [busy, setBusy] = useState(false)
|
|
48
|
+
const [deleteError, setDeleteError] = useState<string | null>(null)
|
|
49
|
+
|
|
50
|
+
const openDelete = () => {
|
|
51
|
+
setConfirming(true)
|
|
52
|
+
setUses(null)
|
|
53
|
+
setDeleteError(null)
|
|
54
|
+
void usage(asset._id)
|
|
55
|
+
.then((list) => setUses(list))
|
|
56
|
+
.catch(() => setUses([]))
|
|
57
|
+
}
|
|
58
|
+
const doDelete = () => {
|
|
59
|
+
setBusy(true)
|
|
60
|
+
setDeleteError(null)
|
|
61
|
+
void remove(asset._id)
|
|
62
|
+
.then(() => {
|
|
63
|
+
setConfirming(false)
|
|
64
|
+
onDeleted?.()
|
|
65
|
+
onClose()
|
|
66
|
+
})
|
|
67
|
+
.catch((cause: unknown) => setDeleteError(cause instanceof Error ? cause.message : String(cause)))
|
|
68
|
+
.finally(() => setBusy(false))
|
|
69
|
+
}
|
|
39
70
|
|
|
40
71
|
// Repart des valeurs de l'asset sélectionné quand il change.
|
|
41
72
|
useEffect(() => {
|
|
@@ -144,7 +175,71 @@ export function AssetDetail(props: AssetDetailProps) {
|
|
|
144
175
|
<Metric label="Usage" value={asset.uses === undefined ? '—' : `${asset.uses}`} />
|
|
145
176
|
<Metric label="Ajouté" value={formatDate(asset._createdAt)} />
|
|
146
177
|
</Grid>
|
|
178
|
+
|
|
179
|
+
<Box style={{borderTop: '1px solid var(--card-border-color)'}} paddingTop={3}>
|
|
180
|
+
<Button icon={TrashIcon} text="Supprimer le média" tone="critical" mode="ghost" fontSize={1} onClick={openDelete} style={{width: '100%'}} />
|
|
181
|
+
</Box>
|
|
147
182
|
</Stack>
|
|
183
|
+
|
|
184
|
+
{confirming ? (
|
|
185
|
+
<Dialog
|
|
186
|
+
id="asset-delete"
|
|
187
|
+
header="Supprimer le média"
|
|
188
|
+
width={1}
|
|
189
|
+
onClose={() => (busy ? undefined : setConfirming(false))}
|
|
190
|
+
footer={
|
|
191
|
+
<Flex justify="flex-end" gap={2} padding={2}>
|
|
192
|
+
<Button text="Annuler" mode="bleed" disabled={busy} onClick={() => setConfirming(false)} />
|
|
193
|
+
<Button text={busy ? 'Suppression…' : 'Supprimer'} tone="critical" disabled={busy || uses === null} onClick={doDelete} />
|
|
194
|
+
</Flex>
|
|
195
|
+
}
|
|
196
|
+
>
|
|
197
|
+
<Box padding={4}>
|
|
198
|
+
<Stack gap={3}>
|
|
199
|
+
<Text size={1}>
|
|
200
|
+
Supprimer « {assetName(asset)} » définitivement ?
|
|
201
|
+
</Text>
|
|
202
|
+
{uses === null ? (
|
|
203
|
+
<Flex align="center" gap={2}>
|
|
204
|
+
<Spinner muted size={1} />
|
|
205
|
+
<Text size={1} muted>
|
|
206
|
+
Recherche des endroits où il est utilisé…
|
|
207
|
+
</Text>
|
|
208
|
+
</Flex>
|
|
209
|
+
) : uses.length === 0 ? (
|
|
210
|
+
<Text size={1} muted>
|
|
211
|
+
Il n'est utilisé nulle part : aucune page ne sera affectée.
|
|
212
|
+
</Text>
|
|
213
|
+
) : (
|
|
214
|
+
<Stack gap={2}>
|
|
215
|
+
<Text size={1} weight="medium">
|
|
216
|
+
L'image deviendra vide à {uses.length} endroit{uses.length > 1 ? 's' : ''} :
|
|
217
|
+
</Text>
|
|
218
|
+
<Card tone="caution" border radius={2} padding={3}>
|
|
219
|
+
<Stack gap={2} as="ul" style={{margin: 0, paddingLeft: 16}}>
|
|
220
|
+
{uses.slice(0, 20).map((use) => (
|
|
221
|
+
<Text key={use.id} size={1} as="li">
|
|
222
|
+
{use.title} <span style={{opacity: 0.6}}>({use.type})</span>
|
|
223
|
+
</Text>
|
|
224
|
+
))}
|
|
225
|
+
{uses.length > 20 ? (
|
|
226
|
+
<Text size={1} muted as="li">
|
|
227
|
+
… et {uses.length - 20} autre{uses.length - 20 > 1 ? 's' : ''}.
|
|
228
|
+
</Text>
|
|
229
|
+
) : null}
|
|
230
|
+
</Stack>
|
|
231
|
+
</Card>
|
|
232
|
+
</Stack>
|
|
233
|
+
)}
|
|
234
|
+
{deleteError ? (
|
|
235
|
+
<Card tone="critical" border radius={2} padding={3}>
|
|
236
|
+
<Text size={1}>Suppression échouée : {deleteError}</Text>
|
|
237
|
+
</Card>
|
|
238
|
+
) : null}
|
|
239
|
+
</Stack>
|
|
240
|
+
</Box>
|
|
241
|
+
</Dialog>
|
|
242
|
+
) : null}
|
|
148
243
|
</Card>
|
|
149
244
|
)
|
|
150
245
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {useCallback, useMemo, useState} from 'react'
|
|
2
|
-
import {Box, Button, Card, Flex,
|
|
1
|
+
import {useCallback, useMemo, useRef, useState} from 'react'
|
|
2
|
+
import {Box, Button, Card, Flex, Select, Spinner, Stack, Text} from '@sanity/ui'
|
|
3
3
|
import {QueryError} from '../../ui/QueryError'
|
|
4
4
|
import {useAssets} from '../../hooks/useAssets'
|
|
5
5
|
import {useAssetMeta} from '../../hooks/useAssetMeta'
|
|
6
|
+
import {useAssetUpload} from '../../hooks/useAssetUpload'
|
|
6
7
|
import {
|
|
7
8
|
assignmentMap,
|
|
8
9
|
assetsInFolder,
|
|
@@ -25,6 +26,9 @@ import {AssetDetail} from './AssetDetail'
|
|
|
25
26
|
export function AssetsTool() {
|
|
26
27
|
const {data, loading, error, refresh} = useAssets()
|
|
27
28
|
const meta = useAssetMeta()
|
|
29
|
+
const {upload, uploading, error: uploadError} = useAssetUpload()
|
|
30
|
+
const fileInputRef = useRef<HTMLInputElement>(null)
|
|
31
|
+
const [dragging, setDragging] = useState(false)
|
|
28
32
|
const assets = useMemo(() => data ?? [], [data])
|
|
29
33
|
|
|
30
34
|
const [search, setSearch] = useState('')
|
|
@@ -80,6 +84,16 @@ export function AssetsTool() {
|
|
|
80
84
|
[meta, selectedIds, clearSelection],
|
|
81
85
|
)
|
|
82
86
|
|
|
87
|
+
// Import : téléverse, et range dans le dossier courant (si un vrai dossier est ouvert).
|
|
88
|
+
const handleFiles = useCallback(
|
|
89
|
+
(files: FileList | null) => {
|
|
90
|
+
if (!files || !files.length) return
|
|
91
|
+
const folder = typeof selection === 'string' && selection !== '' ? selection : null
|
|
92
|
+
void upload(files, folder ? (assetId) => meta.moveAsset(assetId, folder) : undefined).then(() => refresh())
|
|
93
|
+
},
|
|
94
|
+
[upload, selection, meta, refresh],
|
|
95
|
+
)
|
|
96
|
+
|
|
83
97
|
if (loading && assets.length === 0) {
|
|
84
98
|
return (
|
|
85
99
|
<Flex align="center" justify="center" padding={6}>
|
|
@@ -90,7 +104,45 @@ export function AssetsTool() {
|
|
|
90
104
|
if (error) return <QueryError error={error} onRetry={refresh} />
|
|
91
105
|
|
|
92
106
|
return (
|
|
93
|
-
<Flex
|
|
107
|
+
<Flex
|
|
108
|
+
gap={4}
|
|
109
|
+
align="stretch"
|
|
110
|
+
style={{minHeight: 420, position: 'relative'}}
|
|
111
|
+
onDragOver={(event) => {
|
|
112
|
+
event.preventDefault()
|
|
113
|
+
if (!dragging) setDragging(true)
|
|
114
|
+
}}
|
|
115
|
+
onDragLeave={(event) => {
|
|
116
|
+
if (event.currentTarget === event.target) setDragging(false)
|
|
117
|
+
}}
|
|
118
|
+
onDrop={(event) => {
|
|
119
|
+
event.preventDefault()
|
|
120
|
+
setDragging(false)
|
|
121
|
+
handleFiles(event.dataTransfer.files)
|
|
122
|
+
}}
|
|
123
|
+
>
|
|
124
|
+
<input
|
|
125
|
+
ref={fileInputRef}
|
|
126
|
+
type="file"
|
|
127
|
+
multiple
|
|
128
|
+
hidden
|
|
129
|
+
onChange={(event) => {
|
|
130
|
+
handleFiles(event.currentTarget.files)
|
|
131
|
+
event.currentTarget.value = ''
|
|
132
|
+
}}
|
|
133
|
+
/>
|
|
134
|
+
{dragging ? (
|
|
135
|
+
<Flex
|
|
136
|
+
align="center"
|
|
137
|
+
justify="center"
|
|
138
|
+
style={{position: 'absolute', inset: 0, zIndex: 5, borderRadius: 6, border: '2px dashed var(--card-focus-ring-color, #2276fc)', background: 'var(--card-bg-color, rgba(255,255,255,0.85))', pointerEvents: 'none'}}
|
|
139
|
+
>
|
|
140
|
+
<Text size={2} weight="medium">
|
|
141
|
+
Déposer pour importer
|
|
142
|
+
</Text>
|
|
143
|
+
</Flex>
|
|
144
|
+
) : null}
|
|
145
|
+
|
|
94
146
|
<Box style={{width: 220, flex: 'none'}}>
|
|
95
147
|
<FolderSidebar
|
|
96
148
|
folders={meta.folders}
|
|
@@ -125,8 +177,16 @@ export function AssetsTool() {
|
|
|
125
177
|
onView={setView}
|
|
126
178
|
onRefresh={refresh}
|
|
127
179
|
busy={loading}
|
|
180
|
+
onImport={() => fileInputRef.current?.click()}
|
|
181
|
+
uploading={uploading}
|
|
128
182
|
/>
|
|
129
183
|
|
|
184
|
+
{uploadError ? (
|
|
185
|
+
<Card tone="critical" border radius={2} padding={3}>
|
|
186
|
+
<Text size={1}>Import échoué : {uploadError.message}</Text>
|
|
187
|
+
</Card>
|
|
188
|
+
) : null}
|
|
189
|
+
|
|
130
190
|
{selectedIds.size > 0 ? (
|
|
131
191
|
<Card tone="primary" border radius={2} padding={2}>
|
|
132
192
|
<Flex align="center" gap={3} wrap="wrap">
|
|
@@ -174,17 +234,19 @@ export function AssetsTool() {
|
|
|
174
234
|
</Text>
|
|
175
235
|
</Card>
|
|
176
236
|
) : view === 'grid' ? (
|
|
177
|
-
|
|
237
|
+
// Masonry (colonnes CSS) : chaque vignette garde son vrai ratio.
|
|
238
|
+
<Box style={{columnWidth: 200, columnGap: 12}}>
|
|
178
239
|
{visible.map((asset) => (
|
|
179
|
-
<
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
240
|
+
<Box key={asset._id} style={{breakInside: 'avoid', marginBottom: 12}}>
|
|
241
|
+
<AssetCard
|
|
242
|
+
asset={asset}
|
|
243
|
+
selected={selectedIds.has(asset._id)}
|
|
244
|
+
onToggleSelect={() => toggleSelect(asset._id)}
|
|
245
|
+
onOpen={() => setOpenedId(asset._id)}
|
|
246
|
+
/>
|
|
247
|
+
</Box>
|
|
186
248
|
))}
|
|
187
|
-
</
|
|
249
|
+
</Box>
|
|
188
250
|
) : (
|
|
189
251
|
<AssetTable
|
|
190
252
|
assets={visible}
|
|
@@ -205,7 +267,22 @@ export function AssetsTool() {
|
|
|
205
267
|
</Stack>
|
|
206
268
|
|
|
207
269
|
{opened ? (
|
|
208
|
-
<AssetDetail
|
|
270
|
+
<AssetDetail
|
|
271
|
+
asset={opened}
|
|
272
|
+
folders={meta.folders}
|
|
273
|
+
folder={folderOf(opened._id)}
|
|
274
|
+
onMove={(folder) => meta.moveAsset(opened._id, folder)}
|
|
275
|
+
onClose={() => setOpenedId(null)}
|
|
276
|
+
onDeleted={() => {
|
|
277
|
+
setSelectedIds((prev) => {
|
|
278
|
+
const next = new Set(prev)
|
|
279
|
+
next.delete(opened._id)
|
|
280
|
+
return next
|
|
281
|
+
})
|
|
282
|
+
setOpenedId(null)
|
|
283
|
+
refresh()
|
|
284
|
+
}}
|
|
285
|
+
/>
|
|
209
286
|
) : null}
|
|
210
287
|
</Flex>
|
|
211
288
|
)
|
|
@@ -3,6 +3,7 @@ import {SearchIcon} from '@sanity/icons/Search'
|
|
|
3
3
|
import {SyncIcon} from '@sanity/icons/Sync'
|
|
4
4
|
import {ThLargeIcon} from '@sanity/icons/ThLarge'
|
|
5
5
|
import {ThListIcon} from '@sanity/icons/ThList'
|
|
6
|
+
import {UploadIcon} from '@sanity/icons/Upload'
|
|
6
7
|
import type {AssetSortKey, AssetTypeFilter, AssetUsageFilter, SortDirection} from '../../lib/assets'
|
|
7
8
|
|
|
8
9
|
export type AssetView = 'grid' | 'table'
|
|
@@ -21,11 +22,27 @@ export interface AssetsToolbarProps {
|
|
|
21
22
|
onView: (view: AssetView) => void
|
|
22
23
|
onRefresh: () => void
|
|
23
24
|
busy: boolean
|
|
25
|
+
/** Ouvre le sélecteur de fichiers pour importer. */
|
|
26
|
+
onImport?: () => void
|
|
27
|
+
/** Nombre de fichiers en cours d'import. */
|
|
28
|
+
uploading?: number
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
export function AssetsToolbar(props: AssetsToolbarProps) {
|
|
27
32
|
return (
|
|
28
33
|
<Flex gap={2} align="center" wrap="wrap">
|
|
34
|
+
{props.onImport ? (
|
|
35
|
+
<Button
|
|
36
|
+
icon={UploadIcon}
|
|
37
|
+
text={props.uploading ? `Import… (${props.uploading})` : 'Importer'}
|
|
38
|
+
tone="primary"
|
|
39
|
+
fontSize={1}
|
|
40
|
+
padding={2}
|
|
41
|
+
disabled={Boolean(props.uploading)}
|
|
42
|
+
onClick={props.onImport}
|
|
43
|
+
style={{flex: 'none'}}
|
|
44
|
+
/>
|
|
45
|
+
) : null}
|
|
29
46
|
<Box flex={1} style={{minWidth: 180}}>
|
|
30
47
|
<TextInput
|
|
31
48
|
value={props.search}
|
|
@@ -31,7 +31,7 @@ export function PagesTool() {
|
|
|
31
31
|
const config = useKitConfig()
|
|
32
32
|
const router = useRouter()
|
|
33
33
|
|
|
34
|
-
const {byId, saveColumns, patchNode, patchField} = useBoardLayout()
|
|
34
|
+
const {byId, saveColumns, patchNode, patchField, pageOrder} = useBoardLayout()
|
|
35
35
|
const columnOptions = useColumnOptions()
|
|
36
36
|
const preview = usePreviewTarget()
|
|
37
37
|
const columns = useInternalColumns()
|
|
@@ -45,9 +45,11 @@ export function PagesTool() {
|
|
|
45
45
|
const [onlyTodo, setOnlyTodo] = useState(false)
|
|
46
46
|
const [groupBy, setGroupBy] = useState<GroupBy>('none')
|
|
47
47
|
const [rawColorBy, setColorBy] = useState<ColorBy>('tag')
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
|
|
48
|
+
// `null` = tri automatique : l'ordre manuel (réordonné dans Presentation)
|
|
49
|
+
// s'il existe, sinon l'ordre du Studio. Un clic sur une colonne fige un tri
|
|
50
|
+
// explicite. Aucune colonne n'est marquée triée tant que l'éditeur n'a pas
|
|
51
|
+
// choisi.
|
|
52
|
+
const [sortKey, setSortKey] = useState<SortKey | null>(null)
|
|
51
53
|
const [sortDirection, setSortDirection] = useState<SortDirection>('asc')
|
|
52
54
|
const [page, setPage] = useState(0)
|
|
53
55
|
const [hidden, setHidden] = useState<ReadonlySet<string>>(new Set())
|
|
@@ -79,15 +81,19 @@ export function PagesTool() {
|
|
|
79
81
|
[config.i18n, rows],
|
|
80
82
|
)
|
|
81
83
|
|
|
84
|
+
// Tri effectif : l'ordre manuel partagé avec Presentation par défaut, sinon
|
|
85
|
+
// l'ordre du Studio ; un clic sur une colonne prend le dessus.
|
|
86
|
+
const effectiveSortKey: SortKey = sortKey ?? (pageOrder.length > 0 ? 'manual' : 'structure')
|
|
87
|
+
|
|
82
88
|
const visible = useMemo(
|
|
83
89
|
() =>
|
|
84
90
|
sortRows(
|
|
85
91
|
filterRows(rows, {search, locales: locale ? [locale] : undefined}).filter((row) => !onlyTodo || todos.has(row.id)),
|
|
86
|
-
|
|
92
|
+
effectiveSortKey,
|
|
87
93
|
sortDirection,
|
|
88
|
-
{typeOrder: config.pageTypeNames},
|
|
94
|
+
{typeOrder: config.pageTypeNames, pageOrder},
|
|
89
95
|
),
|
|
90
|
-
[rows, search, locale, onlyTodo, todos,
|
|
96
|
+
[rows, search, locale, onlyTodo, todos, effectiveSortKey, sortDirection, config.pageTypeNames, pageOrder],
|
|
91
97
|
)
|
|
92
98
|
|
|
93
99
|
// Un filtre qui réduit la liste ne doit pas laisser l'utilisateur sur une
|
|
@@ -134,7 +140,7 @@ export function PagesTool() {
|
|
|
134
140
|
})
|
|
135
141
|
|
|
136
142
|
const onSort = (key: SortKey) => {
|
|
137
|
-
if (key ===
|
|
143
|
+
if (key === effectiveSortKey) setSortDirection((d) => (d === 'asc' ? 'desc' : 'asc'))
|
|
138
144
|
else {
|
|
139
145
|
setSortKey(key)
|
|
140
146
|
setSortDirection('asc')
|
|
@@ -330,7 +336,7 @@ export function PagesTool() {
|
|
|
330
336
|
) : (
|
|
331
337
|
<PagesTable
|
|
332
338
|
groups={groups}
|
|
333
|
-
sortKey={
|
|
339
|
+
sortKey={effectiveSortKey}
|
|
334
340
|
sortDirection={sortDirection}
|
|
335
341
|
onSort={onSort}
|
|
336
342
|
columns={columns}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import {useMemo, useState, type ComponentType} from 'react'
|
|
2
2
|
import {Box, Button, Card, Flex, Spinner, Stack, Text, TextInput} from '@sanity/ui'
|
|
3
3
|
import {SearchIcon} from '@sanity/icons/Search'
|
|
4
|
+
import {DragHandleIcon} from '@sanity/icons/DragHandle'
|
|
4
5
|
import {usePresentationNavigate, usePresentationParams} from 'sanity/presentation'
|
|
5
6
|
import type {NcfKitConfig} from '../../config/types'
|
|
6
7
|
import {resolveConfig} from '../../config/resolve'
|
|
7
8
|
import {validateConfig} from '../../config/validate'
|
|
8
9
|
import {KitProvider, useKitConfig} from '../../context/KitContext'
|
|
10
|
+
import {BoardLayoutProvider, useBoardLayout} from '../../context/BoardLayoutContext'
|
|
9
11
|
import {usePages} from '../../hooks/usePages'
|
|
10
12
|
import {localeOfPreview} from '../../lib/previewLocale'
|
|
13
|
+
import {orderByIds, reorderBefore} from '../../lib/pageOrder'
|
|
11
14
|
import {StatusBadge} from '../../ui/StatusBadge'
|
|
12
15
|
import {NcfPresentationHeader} from './PresentationHeader'
|
|
13
16
|
|
|
@@ -28,25 +31,38 @@ const pathOf = (preview: string | undefined | null): string | null => {
|
|
|
28
31
|
function PagesNavigator() {
|
|
29
32
|
const config = useKitConfig()
|
|
30
33
|
const {rows, loading} = usePages()
|
|
34
|
+
const {pageOrder, savePageOrder} = useBoardLayout()
|
|
31
35
|
const params = usePresentationParams(false)
|
|
32
36
|
const navigate = usePresentationNavigate()
|
|
33
37
|
const i18n = config.i18n
|
|
34
38
|
const shownLocale = localeOfPreview(params?.preview, i18n)
|
|
35
39
|
const [locale, setLocale] = useState<string | null>(null)
|
|
36
40
|
const [search, setSearch] = useState('')
|
|
41
|
+
const [dragId, setDragId] = useState<string | null>(null)
|
|
37
42
|
// Langue choisie, sinon celle de la page affichée, sinon la langue principale :
|
|
38
43
|
// le menu montre toujours la langue réellement filtrée.
|
|
39
44
|
const activeLocale = locale ?? shownLocale ?? i18n?.defaultLocale ?? ''
|
|
40
45
|
const current = pathOf(params?.preview)
|
|
41
46
|
|
|
47
|
+
// Ordre de base : alphabétique par adresse, puis l'ordre manuel passe devant.
|
|
48
|
+
const withPath = useMemo(() => rows.filter((r) => r.path !== null).sort((a, b) => (a.path ?? '').localeCompare(b.path ?? '')), [rows])
|
|
49
|
+
|
|
42
50
|
const visible = useMemo(() => {
|
|
43
51
|
const q = search.trim().toLowerCase()
|
|
44
|
-
|
|
45
|
-
.filter((r) => r.path !== null)
|
|
52
|
+
const filtered = withPath
|
|
46
53
|
.filter((r) => !i18n || !activeLocale || !r.locale || r.locale === activeLocale)
|
|
47
54
|
.filter((r) => !q || r.title.toLowerCase().includes(q) || (r.path ?? '').toLowerCase().includes(q))
|
|
48
|
-
|
|
49
|
-
}, [
|
|
55
|
+
return orderByIds(filtered, pageOrder)
|
|
56
|
+
}, [withPath, search, activeLocale, i18n, pageOrder])
|
|
57
|
+
|
|
58
|
+
// Glisser une page la place avant celle survolée ; on réordonne l'ordre
|
|
59
|
+
// manuel COMPLET (ids), pas la vue filtrée, pour ne pas perdre les autres.
|
|
60
|
+
const onReorder = (targetId: string | null) => {
|
|
61
|
+
if (!dragId) return
|
|
62
|
+
const fullIds = orderByIds(withPath, pageOrder).map((r) => r.id)
|
|
63
|
+
savePageOrder(reorderBefore(fullIds, dragId, targetId))
|
|
64
|
+
setDragId(null)
|
|
65
|
+
}
|
|
50
66
|
|
|
51
67
|
return (
|
|
52
68
|
<Card height="fill" borderRight style={{display: 'flex', flexDirection: 'column', minHeight: 0}}>
|
|
@@ -101,10 +117,26 @@ function PagesNavigator() {
|
|
|
101
117
|
radius={2}
|
|
102
118
|
tone={selected ? 'primary' : 'default'}
|
|
103
119
|
pressed={selected}
|
|
104
|
-
|
|
120
|
+
draggable
|
|
121
|
+
onDragStart={(event) => {
|
|
122
|
+
setDragId(row.id)
|
|
123
|
+
event.dataTransfer.effectAllowed = 'move'
|
|
124
|
+
}}
|
|
125
|
+
onDragOver={(event) => {
|
|
126
|
+
if (dragId && dragId !== row.id) event.preventDefault()
|
|
127
|
+
}}
|
|
128
|
+
onDrop={(event) => {
|
|
129
|
+
event.preventDefault()
|
|
130
|
+
onReorder(row.id)
|
|
131
|
+
}}
|
|
132
|
+
onDragEnd={() => setDragId(null)}
|
|
133
|
+
style={{textAlign: 'left', cursor: 'pointer', border: 0, width: '100%', opacity: dragId === row.id ? 0.4 : 1}}
|
|
105
134
|
onClick={() => navigate(row.path ?? undefined, {type: row.type, id: row.id})}
|
|
106
135
|
>
|
|
107
136
|
<Flex align="center" gap={2}>
|
|
137
|
+
<Text size={1} muted style={{flex: 'none', cursor: 'grab'}} title="Glisser pour réordonner">
|
|
138
|
+
<DragHandleIcon />
|
|
139
|
+
</Text>
|
|
108
140
|
<Stack gap={1} flex={1} style={{minWidth: 0}}>
|
|
109
141
|
<Text size={1} weight={selected ? 'semibold' : 'regular'} textOverflow="ellipsis">
|
|
110
142
|
{row.title || row.path}
|
|
@@ -118,6 +150,17 @@ function PagesNavigator() {
|
|
|
118
150
|
</Card>
|
|
119
151
|
)
|
|
120
152
|
})}
|
|
153
|
+
{dragId ? (
|
|
154
|
+
<Box
|
|
155
|
+
padding={2}
|
|
156
|
+
onDragOver={(event) => event.preventDefault()}
|
|
157
|
+
onDrop={(event) => {
|
|
158
|
+
event.preventDefault()
|
|
159
|
+
onReorder(null)
|
|
160
|
+
}}
|
|
161
|
+
style={{height: 24}}
|
|
162
|
+
/>
|
|
163
|
+
) : null}
|
|
121
164
|
{!visible.length && (
|
|
122
165
|
<Box padding={3}>
|
|
123
166
|
<Text size={1} muted>
|
|
@@ -150,7 +193,9 @@ export function ncfPresentationComponents(options: NcfKitConfig): {
|
|
|
150
193
|
function NcfPagesNavigator() {
|
|
151
194
|
return (
|
|
152
195
|
<KitProvider config={config} issues={issues}>
|
|
153
|
-
<
|
|
196
|
+
<BoardLayoutProvider>
|
|
197
|
+
<PagesNavigator />
|
|
198
|
+
</BoardLayoutProvider>
|
|
154
199
|
</KitProvider>
|
|
155
200
|
)
|
|
156
201
|
}
|
package/src/version.ts
CHANGED