@nocode-factory/sanity-kit 2.33.3 → 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 +7 -6
- package/dist/index.js +179 -48
- package/dist/index.js.map +1 -1
- package/package.json +5 -3
- 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/tools/presentation/PresentationHeader.tsx +101 -45
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocode-factory/sanity-kit",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.35.0",
|
|
4
4
|
"description": "Outils Studio de NoCode Factory : Pages (liste + board), Composants, Historique",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"private": false,
|
|
@@ -61,7 +61,8 @@
|
|
|
61
61
|
"peerDependencies": {
|
|
62
62
|
"next": ">=15",
|
|
63
63
|
"react": "^19.2",
|
|
64
|
-
"sanity": "^6.0.0"
|
|
64
|
+
"sanity": "^6.0.0",
|
|
65
|
+
"react-dom": "^19.2"
|
|
65
66
|
},
|
|
66
67
|
"devDependencies": {
|
|
67
68
|
"@sanity/pkg-utils": "13.0.1",
|
|
@@ -73,7 +74,8 @@
|
|
|
73
74
|
"sanity": "6.15.0",
|
|
74
75
|
"styled-components": "^6.1.18",
|
|
75
76
|
"typescript": "^5.9.3",
|
|
76
|
-
"vitest": "^4.1.11"
|
|
77
|
+
"vitest": "^4.1.11",
|
|
78
|
+
"@types/react-dom": "^19.2.3"
|
|
77
79
|
},
|
|
78
80
|
"publishConfig": {
|
|
79
81
|
"registry": "https://registry.npmjs.org",
|
|
@@ -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)
|
|
@@ -1,23 +1,39 @@
|
|
|
1
|
-
import {useEffect, useRef, useState} from 'react'
|
|
2
|
-
import {
|
|
1
|
+
import {useEffect, useLayoutEffect, useRef, useState} from 'react'
|
|
2
|
+
import {createPortal} from 'react-dom'
|
|
3
|
+
import {Box, Button, Flex} from '@sanity/ui'
|
|
4
|
+
import {Menu, MenuButton, MenuDivider, MenuItem} from '@sanity/ui/menu'
|
|
3
5
|
import {TabletDeviceIcon} from '@sanity/icons/TabletDevice'
|
|
4
6
|
import {PanelRightIcon} from '@sanity/icons/PanelRight'
|
|
5
7
|
import type {PreviewHeaderProps} from 'sanity/presentation'
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
+
/** Appareils proposés (dimensions CSS de l'écran, portrait). */
|
|
10
|
+
export const PREVIEW_DEVICES = [
|
|
11
|
+
{id: 'iphone-se', label: 'iPhone SE', width: 375, height: 667},
|
|
12
|
+
{id: 'iphone-12-pro', label: 'iPhone 12 Pro', width: 390, height: 844},
|
|
13
|
+
{id: 'iphone-15-pro-max', label: 'iPhone 15 Pro Max', width: 430, height: 932},
|
|
14
|
+
{id: 'pixel-7', label: 'Pixel 7', width: 412, height: 915},
|
|
15
|
+
{id: 'galaxy-s20', label: 'Galaxy S20', width: 360, height: 800},
|
|
16
|
+
{id: 'ipad-mini', label: 'iPad Mini', width: 768, height: 1024},
|
|
17
|
+
{id: 'ipad-air', label: 'iPad Air', width: 820, height: 1180},
|
|
18
|
+
{id: 'ipad-pro-11', label: 'iPad Pro 11"', width: 834, height: 1194},
|
|
19
|
+
{id: 'ipad-pro-13', label: 'iPad Pro 12.9"', width: 1024, height: 1366},
|
|
20
|
+
] as const
|
|
21
|
+
type DeviceId = (typeof PREVIEW_DEVICES)[number]['id']
|
|
22
|
+
|
|
23
|
+
const KEY_DEVICE = 'ncf.presentation.device'
|
|
9
24
|
const KEY_HIDE = 'ncf.presentation.hideForm'
|
|
10
25
|
|
|
11
|
-
const
|
|
26
|
+
const read = (key: string): string | null => {
|
|
12
27
|
try {
|
|
13
|
-
return window.localStorage.getItem(key)
|
|
28
|
+
return window.localStorage.getItem(key)
|
|
14
29
|
} catch {
|
|
15
|
-
return
|
|
30
|
+
return null
|
|
16
31
|
}
|
|
17
32
|
}
|
|
18
|
-
const
|
|
33
|
+
const write = (key: string, value: string | null) => {
|
|
19
34
|
try {
|
|
20
|
-
window.localStorage.
|
|
35
|
+
if (value === null) window.localStorage.removeItem(key)
|
|
36
|
+
else window.localStorage.setItem(key, value)
|
|
21
37
|
} catch {
|
|
22
38
|
/* stockage indisponible : le choix vaut pour la session */
|
|
23
39
|
}
|
|
@@ -29,29 +45,52 @@ const previewPanel = () => document.getElementById('presentation-narrow-panel-pr
|
|
|
29
45
|
const contentPanel = () => document.getElementById('presentation-narrow-panel-content')
|
|
30
46
|
|
|
31
47
|
/**
|
|
32
|
-
* En-tête de l'aperçu (Presentation) : la barre
|
|
33
|
-
* -
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
48
|
+
* En-tête de l'aperçu (Presentation) : la barre native, plus
|
|
49
|
+
* - un menu d'APPAREILS, posé juste à côté du bouton mobile natif : iPhone 12 Pro
|
|
50
|
+
* (390×844), iPad Air (820×1180)... L'aperçu prend les dimensions de l'appareil,
|
|
51
|
+
* affichées à côté de l'icône ;
|
|
52
|
+
* - « Hide edit panel » : masque le formulaire à droite (la liste des pages reste).
|
|
53
|
+
* Choix mémorisés dans le navigateur. Seulement l'affichage : aucune donnée touchée.
|
|
37
54
|
*/
|
|
38
55
|
export function NcfPresentationHeader(props: PreviewHeaderProps) {
|
|
39
56
|
const ref = useRef<HTMLDivElement>(null)
|
|
40
|
-
const [
|
|
41
|
-
const [hideForm, setHideForm] = useState(() =>
|
|
57
|
+
const [deviceId, setDeviceId] = useState<DeviceId | null>(() => (read(KEY_DEVICE) as DeviceId | null) ?? null)
|
|
58
|
+
const [hideForm, setHideForm] = useState(() => read(KEY_HIDE) === '1')
|
|
59
|
+
const [slot, setSlot] = useState<HTMLElement | null>(null)
|
|
60
|
+
const device = PREVIEW_DEVICES.find((d) => d.id === deviceId) ?? null
|
|
42
61
|
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
62
|
+
// Emplacement juste après le bouton mobile / ordinateur natif de Presentation.
|
|
63
|
+
useLayoutEffect(() => {
|
|
64
|
+
const root = ref.current
|
|
65
|
+
const icon = root?.querySelector('[data-sanity-icon="mobile-device"], [data-sanity-icon="desktop"]')
|
|
66
|
+
const button = icon?.closest('button') ?? null
|
|
67
|
+
if (!button) return
|
|
68
|
+
let target = root?.querySelector<HTMLElement>('[data-ncf-device-slot]') ?? null
|
|
69
|
+
if (!target) {
|
|
70
|
+
target = document.createElement('span')
|
|
71
|
+
target.setAttribute('data-ncf-device-slot', '')
|
|
72
|
+
target.style.display = 'inline-flex'
|
|
73
|
+
button.insertAdjacentElement('afterend', target)
|
|
52
74
|
}
|
|
75
|
+
if (target !== slot) setSlot(target)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
// Appareil : l'iframe de l'aperçu prend ses dimensions (dans la place disponible).
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
const iframe = previewPanel()?.querySelector('iframe') ?? null
|
|
81
|
+
if (!iframe || !device) return
|
|
82
|
+
const set: [string, string][] = [
|
|
83
|
+
['width', `${device.width}px`],
|
|
84
|
+
['max-width', '100%'],
|
|
85
|
+
['height', `${device.height}px`],
|
|
86
|
+
['margin-left', 'auto'],
|
|
87
|
+
['margin-right', 'auto'],
|
|
88
|
+
['display', 'block'],
|
|
89
|
+
['flex', 'none'],
|
|
90
|
+
]
|
|
91
|
+
for (const [p, v] of set) iframe.style.setProperty(p, v, 'important')
|
|
53
92
|
return () => {
|
|
54
|
-
for (const p of
|
|
93
|
+
for (const [p] of set) iframe.style.removeProperty(p)
|
|
55
94
|
}
|
|
56
95
|
})
|
|
57
96
|
|
|
@@ -60,18 +99,14 @@ export function NcfPresentationHeader(props: PreviewHeaderProps) {
|
|
|
60
99
|
const panel = previewPanel()
|
|
61
100
|
const content = contentPanel()
|
|
62
101
|
if (!content || !hideForm) return
|
|
63
|
-
// Le panneau d'édition et sa poignée de redimensionnement (juste avant lui).
|
|
64
102
|
const hidden: HTMLElement[] = [content]
|
|
65
103
|
const handle = content.previousElementSibling as HTMLElement | null
|
|
66
104
|
if (handle && handle !== panel && !handle.id) hidden.push(handle)
|
|
67
|
-
// Seulement masquer : jamais toucher aux largeurs gérées par Presentation
|
|
68
|
-
// forcer puis les rendre faussait la répartition au réaffichage).
|
|
105
|
+
// Seulement masquer : jamais toucher aux largeurs gérées par Presentation.
|
|
69
106
|
const apply = () => {
|
|
70
107
|
for (const h of hidden) if (h.style.getPropertyValue('display') !== 'none') h.style.setProperty('display', 'none', 'important')
|
|
71
108
|
}
|
|
72
109
|
apply()
|
|
73
|
-
// Presentation réécrit le style de ses panneaux (redimensionnement) : on
|
|
74
|
-
// réapplique, sans boucle (rien n'est réécrit si c'est déjà en place).
|
|
75
110
|
const observer = new MutationObserver(apply)
|
|
76
111
|
for (const el of hidden) observer.observe(el, {attributes: true, attributeFilter: ['style']})
|
|
77
112
|
return () => {
|
|
@@ -80,24 +115,45 @@ export function NcfPresentationHeader(props: PreviewHeaderProps) {
|
|
|
80
115
|
}
|
|
81
116
|
})
|
|
82
117
|
|
|
118
|
+
const choose = (id: DeviceId | null) => {
|
|
119
|
+
setDeviceId(id)
|
|
120
|
+
write(KEY_DEVICE, id)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const deviceMenu = (
|
|
124
|
+
<MenuButton
|
|
125
|
+
id="ncf-preview-device"
|
|
126
|
+
button={
|
|
127
|
+
<Button
|
|
128
|
+
icon={TabletDeviceIcon}
|
|
129
|
+
text={device ? `${device.width}×${device.height}` : undefined}
|
|
130
|
+
mode={device ? 'default' : 'bleed'}
|
|
131
|
+
tone={device ? 'primary' : 'default'}
|
|
132
|
+
padding={2}
|
|
133
|
+
fontSize={1}
|
|
134
|
+
title={device ? `${device.label} (${device.width}×${device.height})` : 'Preview on a device'}
|
|
135
|
+
/>
|
|
136
|
+
}
|
|
137
|
+
menu={
|
|
138
|
+
<Menu>
|
|
139
|
+
<MenuItem text="Full width" pressed={!device} onClick={() => choose(null)} />
|
|
140
|
+
<MenuDivider />
|
|
141
|
+
{PREVIEW_DEVICES.map((d) => (
|
|
142
|
+
<MenuItem key={d.id} text={`${d.label} ${d.width}×${d.height}`} pressed={d.id === deviceId} onClick={() => choose(d.id)} />
|
|
143
|
+
))}
|
|
144
|
+
</Menu>
|
|
145
|
+
}
|
|
146
|
+
popover={{portal: true, placement: 'bottom-end'}}
|
|
147
|
+
/>
|
|
148
|
+
)
|
|
149
|
+
|
|
83
150
|
return (
|
|
84
151
|
<Flex ref={ref} align="center" gap={1} style={{width: '100%'}}>
|
|
85
152
|
<Box flex={1} style={{minWidth: 0}}>
|
|
86
153
|
{props.renderDefault(props)}
|
|
87
154
|
</Box>
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
/>
|
|
155
|
+
{/* À côté du bouton mobile natif ; à défaut (mise en page de Sanity changée), ici. */}
|
|
156
|
+
{slot ? createPortal(deviceMenu, slot) : deviceMenu}
|
|
101
157
|
<Button
|
|
102
158
|
icon={PanelRightIcon}
|
|
103
159
|
mode={hideForm ? 'default' : 'bleed'}
|
|
@@ -108,7 +164,7 @@ export function NcfPresentationHeader(props: PreviewHeaderProps) {
|
|
|
108
164
|
aria-pressed={hideForm}
|
|
109
165
|
onClick={() => {
|
|
110
166
|
setHideForm(!hideForm)
|
|
111
|
-
|
|
167
|
+
write(KEY_HIDE, !hideForm ? '1' : '0')
|
|
112
168
|
}}
|
|
113
169
|
/>
|
|
114
170
|
</Flex>
|
package/src/version.ts
CHANGED