@nocode-factory/sanity-kit 2.15.0 → 2.17.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 +1 -1
- package/dist/index.js +184 -72
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/pages.ts +2 -1
- package/src/lib/undo.ts +91 -0
- package/src/tools/history/DiffPanel.tsx +87 -37
- package/src/tools/pages/PagesTool.tsx +35 -4
- package/src/tools/pages/PagesToolbar.tsx +5 -5
- package/src/version.ts +1 -1
package/package.json
CHANGED
package/src/lib/pages.ts
CHANGED
|
@@ -184,7 +184,8 @@ export function filterRows(rows: readonly PageRow[], filters: PageFilters): Page
|
|
|
184
184
|
return rows.filter((row) => {
|
|
185
185
|
if (filters.types?.length && !filters.types.includes(row.type)) return false
|
|
186
186
|
if (filters.statuses?.length && !filters.statuses.includes(row.status)) return false
|
|
187
|
-
|
|
187
|
+
// Un document sans langue vaut pour toutes : il reste visible quel que soit le filtre.
|
|
188
|
+
if (filters.locales?.length && row.locale && !filters.locales.includes(row.locale)) return false
|
|
188
189
|
if (search) {
|
|
189
190
|
const haystack = `${normalize(row.title)} ${normalize(row.path ?? '')}`
|
|
190
191
|
if (!haystack.includes(search)) return false
|
package/src/lib/undo.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* « Annuler cette modification » de l'Historique.
|
|
3
|
+
*
|
|
4
|
+
* Remettre l'état d'avant TOUT ENTIER effacerait aussi ce qui a été fait après.
|
|
5
|
+
* On annule donc seulement ce que l'événement a changé, dans le document
|
|
6
|
+
* ACTUEL (fusion à trois états : avant, après, actuel) :
|
|
7
|
+
* - une valeur que l'événement a changée reprend sa valeur d'avant, sauf si
|
|
8
|
+
* elle a encore changé depuis (conflit : laissée telle quelle, signalée) ;
|
|
9
|
+
* - un élément de liste (section, bouton…) ajouté par l'événement est retiré,
|
|
10
|
+
* un élément supprimé est remis à sa place ;
|
|
11
|
+
* - tout le reste du document actuel est conservé.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const SYSTEM_KEYS = new Set(['_rev', '_updatedAt', '_createdAt', '_id', '_type'])
|
|
15
|
+
|
|
16
|
+
const isObject = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null && !Array.isArray(v)
|
|
17
|
+
const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b)
|
|
18
|
+
const keyed = (v: unknown): v is Array<Record<string, unknown> & {_key: string}> =>
|
|
19
|
+
Array.isArray(v) && v.every((item) => isObject(item) && typeof item._key === 'string')
|
|
20
|
+
|
|
21
|
+
export interface UndoResult {
|
|
22
|
+
/** Document actuel, modification annulée. */
|
|
23
|
+
document: Record<string, unknown>
|
|
24
|
+
/** Chemins changés depuis l'événement : non annulés. */
|
|
25
|
+
conflicts: string[]
|
|
26
|
+
/** Nombre de valeurs remises. */
|
|
27
|
+
reverted: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const ABSENT = Symbol('absent')
|
|
31
|
+
type Value = unknown | typeof ABSENT
|
|
32
|
+
|
|
33
|
+
function revert(current: Value, before: Value, after: Value, path: string, out: {conflicts: string[]; reverted: number}): Value {
|
|
34
|
+
if (same(before, after)) return current
|
|
35
|
+
// Rien n'a bougé depuis l'événement : on remet l'état d'avant tel quel.
|
|
36
|
+
if (same(current, after)) {
|
|
37
|
+
out.reverted++
|
|
38
|
+
return before
|
|
39
|
+
}
|
|
40
|
+
if (isObject(current) && isObject(before) && isObject(after)) {
|
|
41
|
+
const result: Record<string, unknown> = {...current}
|
|
42
|
+
for (const key of new Set([...Object.keys(before), ...Object.keys(after)])) {
|
|
43
|
+
if (SYSTEM_KEYS.has(key)) continue
|
|
44
|
+
const v = revert(key in current ? current[key] : ABSENT, key in before ? before[key] : ABSENT, key in after ? after[key] : ABSENT, path ? `${path}.${key}` : key, out)
|
|
45
|
+
if (v === ABSENT) delete result[key]
|
|
46
|
+
else result[key] = v
|
|
47
|
+
}
|
|
48
|
+
return result
|
|
49
|
+
}
|
|
50
|
+
if (keyed(current) && keyed(before) && keyed(after)) {
|
|
51
|
+
const result = [...current]
|
|
52
|
+
const at = (list: typeof current, key: string) => list.findIndex((item) => item._key === key)
|
|
53
|
+
for (const key of new Set([...before.map((i) => i._key), ...after.map((i) => i._key)])) {
|
|
54
|
+
const b = before[at(before, key)]
|
|
55
|
+
const a = after[at(after, key)]
|
|
56
|
+
const index = at(result, key)
|
|
57
|
+
const label = `${path}[${key}]`
|
|
58
|
+
if (index >= 0) {
|
|
59
|
+
const v = revert(result[index], b ?? ABSENT, a ?? ABSENT, label, out)
|
|
60
|
+
if (v === ABSENT) result.splice(index, 1)
|
|
61
|
+
else result[index] = v as (typeof result)[number]
|
|
62
|
+
} else if (b && !a) {
|
|
63
|
+
// Supprimé par l'événement, toujours absent : remis à sa place d'origine.
|
|
64
|
+
result.splice(Math.min(at(before, key), result.length), 0, b)
|
|
65
|
+
out.reverted++
|
|
66
|
+
} else if (b && a && !same(a, b)) {
|
|
67
|
+
// Modifié par l'événement puis supprimé depuis : on ne le ressuscite pas.
|
|
68
|
+
out.conflicts.push(label)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return result
|
|
72
|
+
}
|
|
73
|
+
out.conflicts.push(path || '(document)')
|
|
74
|
+
return current
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Annule l'événement `before` → `after` dans `current` (brouillon ou version
|
|
79
|
+
* publiée actuelle). `current` absent (document supprimé depuis) : l'état
|
|
80
|
+
* d'avant l'événement.
|
|
81
|
+
*/
|
|
82
|
+
export function undoChange(
|
|
83
|
+
current: Record<string, unknown> | null,
|
|
84
|
+
before: Record<string, unknown> | null,
|
|
85
|
+
after: Record<string, unknown> | null,
|
|
86
|
+
): UndoResult {
|
|
87
|
+
const out = {conflicts: [] as string[], reverted: 0}
|
|
88
|
+
const base = current ?? after ?? {}
|
|
89
|
+
const document = revert(base, before ?? {}, after ?? {}, '', out) as Record<string, unknown>
|
|
90
|
+
return {document: isObject(document) ? document : {}, conflicts: out.conflicts, reverted: out.reverted}
|
|
91
|
+
}
|
|
@@ -7,6 +7,8 @@ import {useKitConfig} from '../../context/KitContext'
|
|
|
7
7
|
import {useKitClient} from '../../hooks/useKitClient'
|
|
8
8
|
import {fetchDetail, type HistoryDetail, type HistoryEvent} from '../../lib/service'
|
|
9
9
|
import {diffDocuments} from '../../lib/diff'
|
|
10
|
+
import {undoChange} from '../../lib/undo'
|
|
11
|
+
import {UndoIcon} from '@sanity/icons/Undo'
|
|
10
12
|
import {ChangeRow} from './ChangeRow'
|
|
11
13
|
import {BORDER} from '../../ui/tokens'
|
|
12
14
|
import {ActorName} from '../../ui/ActorName'
|
|
@@ -33,6 +35,8 @@ export function DiffPanel(props: DiffPanelProps) {
|
|
|
33
35
|
const [error, setError] = useState<string | null>(null)
|
|
34
36
|
const [restoring, setRestoring] = useState(false)
|
|
35
37
|
const [restored, setRestored] = useState<string | null>(null)
|
|
38
|
+
/** Chemins changés depuis l'événement, laissés tels quels par l'annulation. */
|
|
39
|
+
const [conflicts, setConflicts] = useState<string[]>([])
|
|
36
40
|
|
|
37
41
|
useEffect(() => {
|
|
38
42
|
if (!config.history) return undefined
|
|
@@ -40,6 +44,7 @@ export function DiffPanel(props: DiffPanelProps) {
|
|
|
40
44
|
setLoading(true)
|
|
41
45
|
setError(null)
|
|
42
46
|
setRestored(null)
|
|
47
|
+
setConflicts([])
|
|
43
48
|
|
|
44
49
|
fetchDetail(config.history.endpoint, config.history.projectKey, event.id, controller.signal)
|
|
45
50
|
.then((result) => {
|
|
@@ -58,28 +63,23 @@ export function DiffPanel(props: DiffPanelProps) {
|
|
|
58
63
|
const snapshot = detail?.current.snapshot ?? null
|
|
59
64
|
const changes = detail ? diffDocuments(detail.previous?.snapshot ?? null, snapshot) : []
|
|
60
65
|
|
|
61
|
-
const
|
|
62
|
-
|
|
66
|
+
const base = event.document_id.replace(/^drafts\./, '')
|
|
67
|
+
|
|
68
|
+
// Écrit dans le brouillon, jamais par-dessus la version publiée : l'éditeur
|
|
69
|
+
// relit et publie. `createOrReplace` et non `patch` : un `patch` exige un
|
|
70
|
+
// document existant, et échouerait sur un document supprimé depuis.
|
|
71
|
+
const writeDraft = async (content: Record<string, unknown>) => {
|
|
72
|
+
const {_rev, _createdAt, _updatedAt, ...rest} = content
|
|
73
|
+
await client.createOrReplace({...rest, _id: `drafts.${base}`, _type: event.document_type} as {_id: string; _type: string})
|
|
74
|
+
setRestored(base)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const run = async (action: () => Promise<void>) => {
|
|
63
78
|
setRestoring(true)
|
|
64
79
|
setError(null)
|
|
80
|
+
setConflicts([])
|
|
65
81
|
try {
|
|
66
|
-
|
|
67
|
-
// existant, et échouerait donc sur une suppression — c'est-à-dire
|
|
68
|
-
// précisément quand une restauration compte le plus.
|
|
69
|
-
//
|
|
70
|
-
// On écrit dans le brouillon, jamais par-dessus la version publiée :
|
|
71
|
-
// l'éditeur relit et publie. Une restauration ne doit pas mettre en
|
|
72
|
-
// ligne un état ancien sans que personne l'ait vu.
|
|
73
|
-
const {_rev, _createdAt, _updatedAt, ...content} = snapshot as Record<string, unknown>
|
|
74
|
-
const base = event.document_id.replace(/^drafts\./, '')
|
|
75
|
-
|
|
76
|
-
await client.createOrReplace({
|
|
77
|
-
...content,
|
|
78
|
-
_id: `drafts.${base}`,
|
|
79
|
-
_type: event.document_type,
|
|
80
|
-
} as {_id: string; _type: string})
|
|
81
|
-
|
|
82
|
-
setRestored(base)
|
|
82
|
+
await action()
|
|
83
83
|
} catch (cause) {
|
|
84
84
|
setError(explainRestore(cause))
|
|
85
85
|
} finally {
|
|
@@ -87,6 +87,30 @@ export function DiffPanel(props: DiffPanelProps) {
|
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
/** Revenir à l'état d'avant CETTE modification, sans perdre ce qui a suivi. */
|
|
91
|
+
const undo = () =>
|
|
92
|
+
run(async () => {
|
|
93
|
+
if (!detail) return
|
|
94
|
+
// État actuel : le brouillon en cours, sinon la version publiée.
|
|
95
|
+
const current =
|
|
96
|
+
((await client.getDocument(`drafts.${base}`)) as Record<string, unknown> | undefined) ??
|
|
97
|
+
((await client.getDocument(base)) as Record<string, unknown> | undefined) ??
|
|
98
|
+
null
|
|
99
|
+
const result = undoChange(
|
|
100
|
+
current,
|
|
101
|
+
(detail.previous?.snapshot as Record<string, unknown> | null) ?? null,
|
|
102
|
+
(snapshot as Record<string, unknown> | null) ?? null,
|
|
103
|
+
)
|
|
104
|
+
await writeDraft(result.document)
|
|
105
|
+
setConflicts(result.conflicts)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
/** Remettre le document entier dans l'état APRÈS cette modification. */
|
|
109
|
+
const restore = () =>
|
|
110
|
+
run(async () => {
|
|
111
|
+
if (snapshot) await writeDraft(snapshot as Record<string, unknown>)
|
|
112
|
+
})
|
|
113
|
+
|
|
90
114
|
return (
|
|
91
115
|
<Card radius={3} shadow={1} padding={3} style={{width: 420, flex: 'none', overflow: 'auto'}}>
|
|
92
116
|
<Stack gap={3}>
|
|
@@ -131,9 +155,14 @@ export function DiffPanel(props: DiffPanelProps) {
|
|
|
131
155
|
<Card padding={3} radius={2} tone="positive">
|
|
132
156
|
<Stack gap={3}>
|
|
133
157
|
<Text size={1}>
|
|
134
|
-
|
|
135
|
-
|
|
158
|
+
Fait <b>en brouillon</b>. Rien n'est encore en ligne : ouvrez le document,
|
|
159
|
+
vérifiez, puis <b>publiez</b>.
|
|
136
160
|
</Text>
|
|
161
|
+
{conflicts.length > 0 && (
|
|
162
|
+
<Text size={1}>
|
|
163
|
+
Non annulé, car modifié depuis : {conflicts.join(', ')}.
|
|
164
|
+
</Text>
|
|
165
|
+
)}
|
|
137
166
|
<IntentLink
|
|
138
167
|
intent="edit"
|
|
139
168
|
params={{id: restored, type: event.document_type}}
|
|
@@ -183,22 +212,43 @@ export function DiffPanel(props: DiffPanelProps) {
|
|
|
183
212
|
|
|
184
213
|
{snapshot && (
|
|
185
214
|
<Box style={{borderTop: `1px solid ${BORDER}`, paddingTop: 12}}>
|
|
186
|
-
<Stack gap={
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
215
|
+
<Stack gap={3}>
|
|
216
|
+
{detail.previous && changes.length > 0 && (
|
|
217
|
+
<Stack gap={2}>
|
|
218
|
+
<Button
|
|
219
|
+
mode="default"
|
|
220
|
+
tone="primary"
|
|
221
|
+
fontSize={1}
|
|
222
|
+
padding={3}
|
|
223
|
+
icon={UndoIcon}
|
|
224
|
+
text={restoring ? 'Annulation…' : 'Annuler cette modification'}
|
|
225
|
+
disabled={restoring}
|
|
226
|
+
onClick={undo}
|
|
227
|
+
/>
|
|
228
|
+
<Text size={0} muted>
|
|
229
|
+
Remet les valeurs d'avant cette modification, et seulement elles : ce
|
|
230
|
+
qui a été modifié ensuite est conservé. Le résultat va dans le
|
|
231
|
+
brouillon : publiez-le pour le mettre en ligne.
|
|
232
|
+
</Text>
|
|
233
|
+
</Stack>
|
|
234
|
+
)}
|
|
235
|
+
<Stack gap={2}>
|
|
236
|
+
<Button
|
|
237
|
+
mode="ghost"
|
|
238
|
+
tone="caution"
|
|
239
|
+
fontSize={1}
|
|
240
|
+
padding={3}
|
|
241
|
+
icon={RestoreIcon}
|
|
242
|
+
text={restoring ? 'Restauration…' : 'Restaurer le document tel qu’après cette modification'}
|
|
243
|
+
disabled={restoring}
|
|
244
|
+
onClick={restore}
|
|
245
|
+
/>
|
|
246
|
+
<Text size={0} muted>
|
|
247
|
+
Remet le document ENTIER dans l'état qui a suivi cette modification
|
|
248
|
+
(les modifications plus récentes sont perdues), dans le brouillon. La
|
|
249
|
+
version en ligne n'est pas touchée tant que vous ne publiez pas.
|
|
250
|
+
</Text>
|
|
251
|
+
</Stack>
|
|
202
252
|
</Stack>
|
|
203
253
|
</Box>
|
|
204
254
|
)}
|
|
@@ -9,7 +9,7 @@ import {useDocumentColumns} from '../../hooks/useDocumentColumns'
|
|
|
9
9
|
import {usePreviewTarget} from '../../context/PreviewTargetContext'
|
|
10
10
|
import {PreviewTarget} from './PreviewTarget'
|
|
11
11
|
import {SharePreview} from './SharePreview'
|
|
12
|
-
import {filterRows, sortRows, type PageRow, type SortDirection, type SortKey} from '../../lib/pages'
|
|
12
|
+
import {filterRows, localesInRows, sortRows, type PageRow, type SortDirection, type SortKey} from '../../lib/pages'
|
|
13
13
|
import {groupPageRows, type GroupBy} from '../../lib/grouping'
|
|
14
14
|
import {resolveColorBy, type ColorBy} from '../../lib/colors'
|
|
15
15
|
import * as columnOps from '../../lib/columns'
|
|
@@ -35,6 +35,8 @@ export function PagesTool() {
|
|
|
35
35
|
|
|
36
36
|
const [view, setView] = useState<PagesView>('list')
|
|
37
37
|
const [search, setSearch] = useState('')
|
|
38
|
+
// Langue affichée : '' = toutes.
|
|
39
|
+
const [locale, setLocale] = useState('')
|
|
38
40
|
const [groupBy, setGroupBy] = useState<GroupBy>('none')
|
|
39
41
|
const [rawColorBy, setColorBy] = useState<ColorBy>('tag')
|
|
40
42
|
// L'ordre du Studio par défaut : aucune colonne n'est marquée triée tant
|
|
@@ -64,12 +66,19 @@ export function PagesTool() {
|
|
|
64
66
|
|
|
65
67
|
const colorBy = resolveColorBy(rawColorBy, columns)
|
|
66
68
|
|
|
69
|
+
// Langues proposées : celles déclarées dans la config i18n, sinon celles des
|
|
70
|
+
// documents. Le sélecteur n'apparaît qu'à partir de deux langues.
|
|
71
|
+
const locales = useMemo(
|
|
72
|
+
() => (config.i18n?.locales.length ? config.i18n.locales : localesInRows(rows)),
|
|
73
|
+
[config.i18n, rows],
|
|
74
|
+
)
|
|
75
|
+
|
|
67
76
|
const visible = useMemo(
|
|
68
77
|
() =>
|
|
69
|
-
sortRows(filterRows(rows, {search}), sortKey, sortDirection, {
|
|
78
|
+
sortRows(filterRows(rows, {search, locales: locale ? [locale] : undefined}), sortKey, sortDirection, {
|
|
70
79
|
typeOrder: config.pageTypeNames,
|
|
71
80
|
}),
|
|
72
|
-
[rows, search, sortKey, sortDirection, config.pageTypeNames],
|
|
81
|
+
[rows, search, locale, sortKey, sortDirection, config.pageTypeNames],
|
|
73
82
|
)
|
|
74
83
|
|
|
75
84
|
// Un filtre qui réduit la liste ne doit pas laisser l'utilisateur sur une
|
|
@@ -182,6 +191,28 @@ export function PagesTool() {
|
|
|
182
191
|
</Box>
|
|
183
192
|
)}
|
|
184
193
|
|
|
194
|
+
{locales.length > 1 && (
|
|
195
|
+
<Box style={{width: 120, flex: 'none'}}>
|
|
196
|
+
<FieldLabel>Langue</FieldLabel>
|
|
197
|
+
<Select
|
|
198
|
+
fontSize={1}
|
|
199
|
+
radius={2}
|
|
200
|
+
value={locale}
|
|
201
|
+
onChange={(event) => {
|
|
202
|
+
setLocale(event.currentTarget.value)
|
|
203
|
+
setPage(0)
|
|
204
|
+
}}
|
|
205
|
+
>
|
|
206
|
+
<option value="">Toutes</option>
|
|
207
|
+
{locales.map((code) => (
|
|
208
|
+
<option key={code} value={code}>
|
|
209
|
+
{code.toUpperCase()}
|
|
210
|
+
</option>
|
|
211
|
+
))}
|
|
212
|
+
</Select>
|
|
213
|
+
</Box>
|
|
214
|
+
)}
|
|
215
|
+
|
|
185
216
|
{view === 'list' && (
|
|
186
217
|
<Box style={{width: 168, flex: 'none'}}>
|
|
187
218
|
<FieldLabel>Grouper par</FieldLabel>
|
|
@@ -259,7 +290,7 @@ export function PagesTool() {
|
|
|
259
290
|
<Text size={1} muted align="center">
|
|
260
291
|
{rows.length === 0
|
|
261
292
|
? `Le kit suit les types ${config.pageTypeNames.join(', ')}.`
|
|
262
|
-
: 'Essaie un autre terme, ou vide la recherche.'}
|
|
293
|
+
: 'Essaie un autre terme ou une autre langue, ou vide la recherche.'}
|
|
263
294
|
</Text>
|
|
264
295
|
</Stack>
|
|
265
296
|
</Box>
|
|
@@ -12,12 +12,12 @@ export interface PagesToolbarProps {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* Recherche et rafraîchissement
|
|
15
|
+
* Recherche et rafraîchissement.
|
|
16
16
|
*
|
|
17
|
-
* Les filtres par type
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
17
|
+
* Les filtres par type et statut restent absents : le regroupement rend ces
|
|
18
|
+
* distinctions visibles sans rien masquer. La langue, elle, a son propre
|
|
19
|
+
* sélecteur (PagesTool) : sur un site multilingue, l'éditeur travaille une
|
|
20
|
+
* langue à la fois et le regroupement ne suffit pas à alléger la liste.
|
|
21
21
|
*/
|
|
22
22
|
export function PagesToolbar(props: PagesToolbarProps) {
|
|
23
23
|
return (
|
package/src/version.ts
CHANGED