@nocode-factory/sanity-kit 2.18.0 → 2.20.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 -0
- package/bin/ncf-kit.mjs +15 -0
- package/dist/index.d.ts +38 -2
- package/dist/index.js +555 -24
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/actions/PropagatePublish.tsx +218 -0
- package/src/actions/RemoveFromSite.tsx +110 -0
- package/src/form/TranslationTodoInput.tsx +44 -0
- package/src/hooks/useTranslationTodo.ts +80 -0
- package/src/index.ts +15 -1
- package/src/lib/propagate.ts +211 -0
- package/src/lib/status.ts +1 -1
- package/src/schema/boardLayout.ts +3 -1
- package/src/schema/translationTodo.ts +23 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Propagation d'une modification de la version PRINCIPALE d'un document vers ses
|
|
3
|
+
* traductions (fusion à trois états : avant, après, traduction actuelle).
|
|
4
|
+
*
|
|
5
|
+
* Règles (aucun texte traduit n'est jamais écrasé) :
|
|
6
|
+
* - STRUCTURE : un élément de liste (section, bouton, carte…) ajouté dans la
|
|
7
|
+
* version principale est ajouté à la traduction (avec le texte d'origine, marqué
|
|
8
|
+
* « à traduire ») ; supprimé, il est retiré ; l'ordre suit la version principale.
|
|
9
|
+
* Un élément qui n'existe que dans la traduction est conservé à sa place.
|
|
10
|
+
* - VALEURS non textuelles (image, lien, référence, option de style, nombre, case,
|
|
11
|
+
* date…) : recopiées. Un lien vers une page reste juste : le site le rend vers la
|
|
12
|
+
* traduction de la page cible dans la langue affichée.
|
|
13
|
+
* - TEXTES (chaîne libre, texte, texte riche) modifiés : la traduction est gardée et
|
|
14
|
+
* le champ est marqué « à traduire ». Un texte jamais traduit (identique à
|
|
15
|
+
* l'ancienne version principale, ou vide) prend le nouveau texte, marqué aussi.
|
|
16
|
+
* - Jamais touchés : champs système (`_…`), langue, slug.
|
|
17
|
+
* Ce qui n'a pas changé dans la version principale n'est pas touché.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export type FieldKind = 'text' | 'value' | 'object' | 'list'
|
|
21
|
+
|
|
22
|
+
/** Type de schéma Sanity (forme minimale lue ici). */
|
|
23
|
+
export interface SchemaTypeLike {
|
|
24
|
+
name?: string
|
|
25
|
+
jsonType?: string
|
|
26
|
+
type?: SchemaTypeLike
|
|
27
|
+
fields?: {name: string; type: SchemaTypeLike}[]
|
|
28
|
+
of?: SchemaTypeLike[]
|
|
29
|
+
to?: unknown
|
|
30
|
+
options?: {list?: unknown[]}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface PropagationResult {
|
|
34
|
+
document: Record<string, unknown>
|
|
35
|
+
/** Chemins (lisibles) des textes à traduire. */
|
|
36
|
+
toTranslate: string[]
|
|
37
|
+
/** Chemins des valeurs recopiées. */
|
|
38
|
+
copied: string[]
|
|
39
|
+
added: number
|
|
40
|
+
removed: number
|
|
41
|
+
moved: boolean
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const VALUE_TYPE_NAMES = new Set(['image', 'file', 'reference', 'crossDatasetReference', 'slug', 'geopoint', 'url', 'email', 'date', 'datetime', 'number', 'boolean'])
|
|
45
|
+
const SKIP_KEYS = new Set(['language', 'slug'])
|
|
46
|
+
|
|
47
|
+
const isObj = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null && !Array.isArray(v)
|
|
48
|
+
const same = (a: unknown, b: unknown) => JSON.stringify(a ?? null) === JSON.stringify(b ?? null)
|
|
49
|
+
const keyed = (v: unknown): v is Record<string, unknown>[] => Array.isArray(v) && v.length > 0 && v.every((i) => isObj(i) && typeof i._key === 'string')
|
|
50
|
+
|
|
51
|
+
function typeNames(t: SchemaTypeLike | undefined): string[] {
|
|
52
|
+
const out: string[] = []
|
|
53
|
+
for (let c = t; c; c = c.type) if (c.name) out.push(c.name)
|
|
54
|
+
return out
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Nature d'un champ d'après son schéma (et sa valeur, faute de schéma). */
|
|
58
|
+
export function kindOf(t: SchemaTypeLike | undefined, value: unknown): FieldKind {
|
|
59
|
+
if (t) {
|
|
60
|
+
const names = typeNames(t)
|
|
61
|
+
if (names.some((n) => VALUE_TYPE_NAMES.has(n)) || t.to) return 'value'
|
|
62
|
+
if (t.jsonType === 'array') {
|
|
63
|
+
const of = t.of ?? []
|
|
64
|
+
if (of.some((m) => typeNames(m).includes('block'))) return 'text'
|
|
65
|
+
if (of.some((m) => m.jsonType === 'object' || typeNames(m).includes('object'))) return 'list'
|
|
66
|
+
const prim = of[0]
|
|
67
|
+
return prim && prim.jsonType === 'string' && !prim.options?.list?.length ? 'text' : 'value'
|
|
68
|
+
}
|
|
69
|
+
if (t.jsonType === 'object') return 'object'
|
|
70
|
+
if (t.jsonType === 'string') return t.options?.list?.length ? 'value' : 'text'
|
|
71
|
+
return 'value'
|
|
72
|
+
}
|
|
73
|
+
if (keyed(value)) return 'list'
|
|
74
|
+
if (isObj(value)) return 'object'
|
|
75
|
+
return 'value'
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function fieldType(t: SchemaTypeLike | undefined, name: string): SchemaTypeLike | undefined {
|
|
79
|
+
for (let c = t; c; c = c.type) {
|
|
80
|
+
const f = c.fields?.find((x) => x.name === name)
|
|
81
|
+
if (f) return f.type
|
|
82
|
+
}
|
|
83
|
+
return undefined
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function itemType(t: SchemaTypeLike | undefined, item: Record<string, unknown>): SchemaTypeLike | undefined {
|
|
87
|
+
let of: SchemaTypeLike[] | undefined
|
|
88
|
+
for (let c = t; c && !of; c = c.type) of = c.of
|
|
89
|
+
if (!of?.length) return undefined
|
|
90
|
+
return of.find((m) => typeNames(m).includes(item._type as string)) ?? (of.length === 1 ? of[0] : undefined)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface Acc {
|
|
94
|
+
toTranslate: string[]
|
|
95
|
+
copied: string[]
|
|
96
|
+
added: number
|
|
97
|
+
removed: number
|
|
98
|
+
moved: boolean
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Tous les textes d'un élément ajouté : à traduire. */
|
|
102
|
+
function collectTexts(value: unknown, t: SchemaTypeLike | undefined, path: string, acc: Acc) {
|
|
103
|
+
const kind = kindOf(t, value)
|
|
104
|
+
if (kind === 'text') {
|
|
105
|
+
if (value !== undefined && value !== null && value !== '') acc.toTranslate.push(path)
|
|
106
|
+
} else if (kind === 'object' && isObj(value)) {
|
|
107
|
+
for (const [k, v] of Object.entries(value)) if (!k.startsWith('_')) collectTexts(v, fieldType(t, k), `${path}.${k}`, acc)
|
|
108
|
+
} else if (kind === 'list' && Array.isArray(value)) {
|
|
109
|
+
for (const item of value) if (isObj(item)) collectTexts(item, itemType(t, item), `${path}[${String(item._key)}]`, acc)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const ABSENT = Symbol('absent')
|
|
114
|
+
|
|
115
|
+
function merge(target: unknown, before: unknown, after: unknown, t: SchemaTypeLike | undefined, path: string, acc: Acc): unknown {
|
|
116
|
+
if (same(before, after)) return target === undefined ? ABSENT : target
|
|
117
|
+
const kind = kindOf(t, after ?? before)
|
|
118
|
+
|
|
119
|
+
if (kind === 'text') {
|
|
120
|
+
if (after === undefined || after === null || after === '') {
|
|
121
|
+
// Texte vidé dans la version principale : la traduction le garde (à revoir).
|
|
122
|
+
if (target !== undefined && target !== null && target !== '') acc.toTranslate.push(path)
|
|
123
|
+
return target === undefined ? ABSENT : target
|
|
124
|
+
}
|
|
125
|
+
acc.toTranslate.push(path)
|
|
126
|
+
// Jamais traduit (vide, ou encore le texte d'origine) : prend le nouveau texte.
|
|
127
|
+
const untranslated = target === undefined || target === null || target === '' || same(target, before)
|
|
128
|
+
return untranslated ? after : target
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (kind === 'value') {
|
|
132
|
+
acc.copied.push(path)
|
|
133
|
+
return after === undefined ? ABSENT : after
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (kind === 'object') {
|
|
137
|
+
if (!isObj(after)) {
|
|
138
|
+
acc.copied.push(path)
|
|
139
|
+
return after === undefined ? ABSENT : after
|
|
140
|
+
}
|
|
141
|
+
const base: Record<string, unknown> = isObj(target) ? {...target} : {_type: after._type}
|
|
142
|
+
const b = isObj(before) ? before : {}
|
|
143
|
+
for (const k of new Set([...Object.keys(b), ...Object.keys(after)])) {
|
|
144
|
+
if (k.startsWith('_') || SKIP_KEYS.has(k)) continue
|
|
145
|
+
const v = merge(base[k], b[k], after[k], fieldType(t, k), path ? `${path}.${k}` : k, acc)
|
|
146
|
+
if (v === ABSENT) delete base[k]
|
|
147
|
+
else base[k] = v
|
|
148
|
+
}
|
|
149
|
+
return base
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Liste d'éléments (sections, boutons, cartes…) : identifiés par _key.
|
|
153
|
+
const tgt = Array.isArray(target) ? (target as Record<string, unknown>[]).filter(isObj) : []
|
|
154
|
+
const bef = Array.isArray(before) ? (before as Record<string, unknown>[]).filter(isObj) : []
|
|
155
|
+
const aft = Array.isArray(after) ? (after as Record<string, unknown>[]).filter(isObj) : []
|
|
156
|
+
const byKey = (list: Record<string, unknown>[]) => new Map(list.map((i) => [i._key as string, i]))
|
|
157
|
+
const tMap = byKey(tgt)
|
|
158
|
+
const bMap = byKey(bef)
|
|
159
|
+
const aMap = byKey(aft)
|
|
160
|
+
|
|
161
|
+
const result: Record<string, unknown>[] = []
|
|
162
|
+
for (const item of aft) {
|
|
163
|
+
const key = item._key as string
|
|
164
|
+
const it = itemType(t, item)
|
|
165
|
+
if (tMap.has(key)) {
|
|
166
|
+
const merged = merge(tMap.get(key), bMap.get(key), item, it, `${path}[${key}]`, acc)
|
|
167
|
+
result.push(merged === ABSENT ? item : (merged as Record<string, unknown>))
|
|
168
|
+
} else {
|
|
169
|
+
// Ajouté (ou absent de la traduction) : l'élément d'origine, textes à traduire.
|
|
170
|
+
result.push(structuredClone(item))
|
|
171
|
+
acc.added++
|
|
172
|
+
collectTexts(item, it, `${path}[${key}]`, acc)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
for (const key of bMap.keys()) if (!aMap.has(key) && tMap.has(key)) acc.removed++
|
|
176
|
+
|
|
177
|
+
// Éléments propres à la traduction (ni avant ni après) : gardés à leur place.
|
|
178
|
+
tgt.forEach((item, index) => {
|
|
179
|
+
const key = item._key as string
|
|
180
|
+
if (bMap.has(key) || aMap.has(key)) return
|
|
181
|
+
const prev = tgt.slice(0, index).reverse().find((p) => result.some((r) => r._key === p._key))
|
|
182
|
+
const at = prev ? result.findIndex((r) => r._key === prev._key) + 1 : 0
|
|
183
|
+
result.splice(at, 0, item)
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
const common = (list: Record<string, unknown>[]) => list.map((i) => i._key).filter((k) => tMap.has(k as string) && aMap.has(k as string))
|
|
187
|
+
if (!same(common(tgt), common(aft))) acc.moved = true
|
|
188
|
+
return result
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Applique la modification `before` → `after` (version principale) à `target`
|
|
193
|
+
* (brouillon ou version publiée de la traduction). `documentType` : schéma du
|
|
194
|
+
* document, pour distinguer textes et valeurs.
|
|
195
|
+
*/
|
|
196
|
+
export function propagate(
|
|
197
|
+
target: Record<string, unknown> | null,
|
|
198
|
+
before: Record<string, unknown> | null,
|
|
199
|
+
after: Record<string, unknown>,
|
|
200
|
+
documentType?: SchemaTypeLike,
|
|
201
|
+
): PropagationResult {
|
|
202
|
+
const acc: Acc = {toTranslate: [], copied: [], added: 0, removed: 0, moved: false}
|
|
203
|
+
const merged = merge(target ?? {}, before ?? {}, after, documentType, '', acc)
|
|
204
|
+
const document = {...(target ?? {}), ...(isObj(merged) ? merged : {})}
|
|
205
|
+
return {document, ...acc}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** La propagation changerait-elle quelque chose ? */
|
|
209
|
+
export function hasChanges(r: PropagationResult): boolean {
|
|
210
|
+
return r.toTranslate.length > 0 || r.copied.length > 0 || r.added > 0 || r.removed > 0 || r.moved
|
|
211
|
+
}
|
package/src/lib/status.ts
CHANGED
|
@@ -118,5 +118,5 @@ export function mergeVersions<T extends VersionedDoc>(docs: readonly T[]): Merge
|
|
|
118
118
|
export const STATUS_LABELS: Record<PageStatus, {label: string; tone: 'positive' | 'caution' | 'default'; help: string}> = {
|
|
119
119
|
published: {label: 'Publiée', tone: 'positive', help: 'En ligne, à jour'},
|
|
120
120
|
modified: {label: 'Modifiée', tone: 'caution', help: 'En ligne, avec des modifications non publiées'},
|
|
121
|
-
draft: {label: '
|
|
121
|
+
draft: {label: 'Non publiée', tone: 'default', help: 'Absente du site : jamais publiée, ou retirée du site (contenu conservé en brouillon)'},
|
|
122
122
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {defineField, defineType} from 'sanity'
|
|
2
|
+
import {TRANSLATION_TODO_TYPE, translationTodoType} from './translationTodo'
|
|
2
3
|
|
|
3
4
|
/** Type et identifiant du singleton portant la disposition du board. */
|
|
4
5
|
export const BOARD_LAYOUT_TYPE = 'ncfBoardLayout'
|
|
@@ -11,7 +12,7 @@ export const COLUMN_OPTION_TYPE = 'ncfColumnOption'
|
|
|
11
12
|
export const ANNOTATION_TYPE = 'ncfAnnotation'
|
|
12
13
|
|
|
13
14
|
/** Types déclarés par le kit : à masquer de la structure du Studio hôte. */
|
|
14
|
-
export const NCF_HIDDEN_TYPES = [BOARD_LAYOUT_TYPE] as const
|
|
15
|
+
export const NCF_HIDDEN_TYPES = [BOARD_LAYOUT_TYPE, TRANSLATION_TODO_TYPE] as const
|
|
15
16
|
|
|
16
17
|
const boardNodeType = defineType({
|
|
17
18
|
name: BOARD_NODE_TYPE,
|
|
@@ -177,6 +178,7 @@ const boardLayoutType = defineType({
|
|
|
177
178
|
|
|
178
179
|
export const kitSchemaTypes = [
|
|
179
180
|
boardLayoutType,
|
|
181
|
+
translationTodoType,
|
|
180
182
|
boardNodeType,
|
|
181
183
|
sectionNodeType,
|
|
182
184
|
nodeFieldType,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {defineField, defineType} from 'sanity'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Champs « à traduire » d'une traduction, écrits par la propagation (publication de
|
|
5
|
+
* la version principale). Un document par traduction : `ncfTodo.<id de la traduction>`
|
|
6
|
+
* (point = jamais servi par l'API publique). Jamais créé ni édité à la main : la
|
|
7
|
+
* pastille du formulaire le lit et « Marquer comme traduit » retire le champ.
|
|
8
|
+
*/
|
|
9
|
+
export const TRANSLATION_TODO_TYPE = 'ncfTranslationTodo'
|
|
10
|
+
export const translationTodoId = (documentId: string) => `ncfTodo.${documentId.replace(/^drafts\./, '')}`
|
|
11
|
+
|
|
12
|
+
export const translationTodoType = defineType({
|
|
13
|
+
name: TRANSLATION_TODO_TYPE,
|
|
14
|
+
title: 'À traduire (NCF)',
|
|
15
|
+
type: 'document',
|
|
16
|
+
fields: [
|
|
17
|
+
defineField({name: 'documentId', title: 'Traduction', type: 'string', readOnly: true}),
|
|
18
|
+
defineField({name: 'sourceId', title: 'Version principale', type: 'string', readOnly: true}),
|
|
19
|
+
defineField({name: 'sourceLanguage', title: 'Langue principale', type: 'string', readOnly: true}),
|
|
20
|
+
defineField({name: 'paths', title: 'Champs à traduire', type: 'array', of: [{type: 'string'}], readOnly: true}),
|
|
21
|
+
],
|
|
22
|
+
preview: {select: {title: 'documentId'}, prepare: ({title}) => ({title: `À traduire : ${title}`})},
|
|
23
|
+
})
|
package/src/version.ts
CHANGED