@nocode-factory/sanity-kit 2.34.0 → 2.35.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 +46 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/actions/PropagatePublish.tsx +4 -1
- package/src/index.ts +14 -1
- package/src/lib/localizeReferences.ts +48 -0
- package/src/lib/translateDocument.ts +3 -0
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -10,6 +10,7 @@ import {collectTexts, setAtKey, valueAtKey} from '../lib/aiTexts'
|
|
|
10
10
|
import {translateValues} from '../lib/aiTranslate'
|
|
11
11
|
import {createTranslatedVersion, translationsOf} from '../lib/translateDocument'
|
|
12
12
|
import {isReplaceable, readTrace, recordTrace} from '../lib/translationTrace'
|
|
13
|
+
import {localizeReferences} from '../lib/localizeReferences'
|
|
13
14
|
|
|
14
15
|
type Doc = Record<string, unknown>
|
|
15
16
|
type Client = ReturnType<typeof useKitClient>
|
|
@@ -184,7 +185,9 @@ function PropagateDialog(props: {
|
|
|
184
185
|
;(created.published ? published : drafted).push(t.language.toUpperCase())
|
|
185
186
|
continue
|
|
186
187
|
}
|
|
187
|
-
const {_id, _rev, _createdAt, _updatedAt, ...
|
|
188
|
+
const {_id, _rev, _createdAt, _updatedAt, ...raw} = structuredClone(t.result.document) as Doc
|
|
189
|
+
// Références vers la version de la même langue (type, catégorie, pages liées).
|
|
190
|
+
const doc = await localizeReferences(client, raw, t.language)
|
|
188
191
|
const targetId = t.id as string
|
|
189
192
|
// Textes modifiés à la main dans cette langue : gardés (et marqués « To
|
|
190
193
|
// translate », texte source affiché), sauf case « Overwrite » cochée.
|
package/src/index.ts
CHANGED
|
@@ -156,7 +156,14 @@ export const ncfKit = definePlugin<NcfKitConfig>((options) => {
|
|
|
156
156
|
// (anglais en général) propose de retraduire celles des autres langues.
|
|
157
157
|
context.schemaType === config.componentPreview.defaultsType && config.i18n
|
|
158
158
|
? prev.map((action) => (action.action === 'publish' ? withPropagation(action, config, config.i18n?.defaultsSourceLocale) : action))
|
|
159
|
-
:
|
|
159
|
+
: // Tout autre type traduit document par document (collections, réglages
|
|
160
|
+
// par langue) : même proposition que les pages à la publication de la
|
|
161
|
+
// version principale (mettre à jour ou créer les autres langues).
|
|
162
|
+
// Détecté par le champ de langue du schéma : une nouvelle collection
|
|
163
|
+
// traduite en profite sans réglage.
|
|
164
|
+
config.i18n && !isKitType(context.schemaType) && hasLanguageField(context.schema.get(context.schemaType), config.i18n.field)
|
|
165
|
+
? prev.map((action) => (action.action === 'publish' ? withPropagation(action, config, undefined, {allowCreate: true}) : action))
|
|
166
|
+
: prev,
|
|
160
167
|
},
|
|
161
168
|
|
|
162
169
|
// Le lanceur d'aperçu s'ajoute à droite des outils, dans la barre du haut
|
|
@@ -258,6 +265,12 @@ export {
|
|
|
258
265
|
} from './lib/studioLinks'
|
|
259
266
|
export type {PreviewPerspective, PresentationOptions} from './lib/studioLinks'
|
|
260
267
|
export {usePreviewTarget} from './context/PreviewTargetContext'
|
|
268
|
+
/** Le type de document porte-t-il le champ de langue (traduit document par document) ? */
|
|
269
|
+
function hasLanguageField(schemaType: unknown, field: string): boolean {
|
|
270
|
+
const fields = (schemaType as {fields?: {name?: string}[]} | undefined)?.fields
|
|
271
|
+
return Array.isArray(fields) && fields.some((f) => f?.name === field)
|
|
272
|
+
}
|
|
273
|
+
|
|
261
274
|
export {NcfPreviewHeader, ncfPreviewComponents} from './tools/presentation/PreviewHeader'
|
|
262
275
|
export {ncfPresentationComponents} from './tools/presentation/PagesNavigator'
|
|
263
276
|
export {NcfPreviewLauncher, createPreviewLauncher} from './tools/navbar/PreviewLauncher'
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type {SanityClient} from 'sanity'
|
|
2
|
+
|
|
3
|
+
type Doc = Record<string, unknown>
|
|
4
|
+
|
|
5
|
+
/** Références de contenu d'une valeur (hors fichiers et images : leurs assets n'ont pas de langue). */
|
|
6
|
+
export function contentRefs(value: unknown, out = new Set<string>()): Set<string> {
|
|
7
|
+
if (Array.isArray(value)) for (const item of value) contentRefs(item, out)
|
|
8
|
+
else if (value && typeof value === 'object') {
|
|
9
|
+
const ref = (value as {_ref?: unknown})._ref
|
|
10
|
+
if (typeof ref === 'string' && !/^(image|file)-/.test(ref)) out.add(ref.replace(/^drafts\./, ''))
|
|
11
|
+
for (const [k, v] of Object.entries(value)) if (k !== '_ref') contentRefs(v, out)
|
|
12
|
+
}
|
|
13
|
+
return out
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Remplace chaque référence par celle de `map` (sans toucher au reste). Rend une copie. */
|
|
17
|
+
export function replaceRefs<T>(value: T, map: Map<string, string>): T {
|
|
18
|
+
if (Array.isArray(value)) return value.map((item) => replaceRefs(item, map)) as T
|
|
19
|
+
if (value && typeof value === 'object') {
|
|
20
|
+
const out: Doc = {}
|
|
21
|
+
for (const [k, v] of Object.entries(value as Doc)) {
|
|
22
|
+
if (k === '_ref' && typeof v === 'string' && map.has(v.replace(/^drafts\./, ''))) out[k] = map.get(v.replace(/^drafts\./, ''))
|
|
23
|
+
else out[k] = replaceRefs(v, map)
|
|
24
|
+
}
|
|
25
|
+
return out as T
|
|
26
|
+
}
|
|
27
|
+
return value
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Version d'une autre langue : chaque référence vers un document traduit pointe la
|
|
32
|
+
* traduction de CETTE langue (type de publication, catégorie, pages liées...). Une
|
|
33
|
+
* référence sans traduction dans la langue reste telle quelle (document commun à
|
|
34
|
+
* toutes les langues, ou traduction pas encore créée).
|
|
35
|
+
*/
|
|
36
|
+
export async function localizeReferences<T>(client: SanityClient, value: T, language: string): Promise<T> {
|
|
37
|
+
const refs = [...contentRefs(value)]
|
|
38
|
+
if (!refs.length) return value
|
|
39
|
+
const rows = await client.fetch<{from: string; to: string | null}[]>(
|
|
40
|
+
`*[_type == "translation.metadata" && count(translations[value._ref in $refs]) > 0]{
|
|
41
|
+
"pairs": translations[value._ref in $refs]{"from": value._ref, "to": ^.translations[_key == $language || language == $language][0].value._ref}
|
|
42
|
+
}.pairs[]`,
|
|
43
|
+
{refs, language},
|
|
44
|
+
)
|
|
45
|
+
const map = new Map<string, string>()
|
|
46
|
+
for (const row of rows ?? []) if (row?.from && row.to && row.to !== row.from) map.set(row.from, row.to)
|
|
47
|
+
return map.size ? replaceRefs(value, map) : value
|
|
48
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {localizeReferences} from './localizeReferences'
|
|
1
2
|
import type {SanityClient} from 'sanity'
|
|
2
3
|
import type {ResolvedKitConfig} from '../config/types'
|
|
3
4
|
import {TRANSLATION_TODO_TYPE, translationTodoId} from '../schema/translationTodo'
|
|
@@ -117,6 +118,8 @@ export async function createTranslatedVersion(opts: {
|
|
|
117
118
|
}
|
|
118
119
|
}
|
|
119
120
|
doc[field] = language
|
|
121
|
+
// Références vers la version de la même langue (type, catégorie, pages liées).
|
|
122
|
+
Object.assign(doc, await localizeReferences(client, doc, language))
|
|
120
123
|
// Slug : traduit depuis le titre, unique dans la langue (-2, -3...).
|
|
121
124
|
const pageType = config.pageTypes.find((t) => t.type === type)
|
|
122
125
|
const slugField = pageType?.slugField ?? ((source.slug as {current?: string} | undefined)?.current ? 'slug' : null)
|
package/src/version.ts
CHANGED