@nocode-factory/sanity-kit 2.8.0 → 2.8.1
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 +64 -37
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/form/OverrideInput.tsx +27 -26
- package/src/hooks/useComponentDefaults.ts +5 -4
- package/src/lib/overrides.ts +58 -19
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {useState, type ComponentType} from 'react'
|
|
2
|
-
import {unset, useFormValue, useSchema, type InputProps} from 'sanity'
|
|
2
|
+
import {set, unset, useFormValue, useSchema, type InputProps} from 'sanity'
|
|
3
3
|
import {Box, Flex, Text} from '@sanity/ui'
|
|
4
4
|
import {Tooltip} from '@sanity/ui/tooltip'
|
|
5
5
|
import type {ResolvedKitConfig} from '../config/types'
|
|
@@ -11,18 +11,17 @@ import {
|
|
|
11
11
|
describeValue,
|
|
12
12
|
firstKeyedIndex,
|
|
13
13
|
isOverride,
|
|
14
|
-
|
|
15
|
-
sameValue,
|
|
14
|
+
resolveFieldPath,
|
|
16
15
|
type PathSegment,
|
|
17
16
|
} from '../lib/overrides'
|
|
18
17
|
|
|
19
18
|
/**
|
|
20
|
-
* Pastille bleue sur
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* sous-champs.
|
|
19
|
+
* Pastille bleue sur tout champ d'une instance de composant dont la valeur
|
|
20
|
+
* DIFFÈRE de la valeur par défaut du composant (valeur par défaut vide comprise),
|
|
21
|
+
* éléments de liste compris (comparés à l'élément de même rang). Au survol, elle
|
|
22
|
+
* devient « Réinitialiser » : retour à la valeur par défaut. Les champs
|
|
23
|
+
* « simples » la portent (texte, nombre, choix, case, liste, image, référence,
|
|
24
|
+
* couleur...) ; un objet composé (lien) la porte sur ses sous-champs.
|
|
26
25
|
*/
|
|
27
26
|
export function createOverrideInput(config: ResolvedKitConfig): ComponentType<InputProps> {
|
|
28
27
|
const {defaultsType} = config.componentPreview
|
|
@@ -31,20 +30,22 @@ export function createOverrideInput(config: ResolvedKitConfig): ComponentType<In
|
|
|
31
30
|
function OverrideInput(props: InputProps) {
|
|
32
31
|
const schema = useSchema()
|
|
33
32
|
const path = props.path as PathSegment[]
|
|
34
|
-
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
const
|
|
33
|
+
// L'instance de composant : le premier élément de liste du chemin (page
|
|
34
|
+
// builder, corps d'article) ou, sinon, le champ de premier niveau (composant
|
|
35
|
+
// posé seul, ex. le CTA de fin d'article des Réglages).
|
|
36
|
+
const keyed = firstKeyedIndex(path)
|
|
37
|
+
const anchor = keyed >= 0 ? keyed : 0
|
|
38
|
+
const instance = useFormValue(path.slice(0, anchor + 1) as never)
|
|
38
39
|
const parent = useFormValue((path.length > 1 ? path.slice(0, -1) : path) as never)
|
|
39
|
-
const
|
|
40
|
+
const resolved = resolveFieldPath(path, anchor, instance, namespace)
|
|
40
41
|
|
|
41
|
-
if (!
|
|
42
|
+
if (!resolved || !schema.get(defaultsType) || !carriesDot(props.schemaType)) return props.renderDefault(props)
|
|
42
43
|
const shortName = blockName((instance as {_type: string})._type, namespace)
|
|
43
|
-
return <WithDot {...props} rel={
|
|
44
|
+
return <WithDot {...props} rel={resolved.path} inList={resolved.inList} shortName={shortName} parent={parent} />
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
function WithDot(props: InputProps & {rel: string[]; shortName: string; parent: unknown}) {
|
|
47
|
-
const {rel, shortName, parent, ...inputProps} = props
|
|
47
|
+
function WithDot(props: InputProps & {rel: (string | number)[]; inList: boolean; shortName: string; parent: unknown}) {
|
|
48
|
+
const {rel, inList, shortName, parent, ...inputProps} = props
|
|
48
49
|
const client = useKitClient()
|
|
49
50
|
const languageField = config.i18n?.field ?? 'language'
|
|
50
51
|
const language = (useFormValue([languageField]) as string | undefined) ?? null
|
|
@@ -53,16 +54,17 @@ export function createOverrideInput(config: ResolvedKitConfig): ComponentType<In
|
|
|
53
54
|
const field = rel[rel.length - 1] ?? ''
|
|
54
55
|
|
|
55
56
|
if (!isOverride(inputProps.value, inherited, field, parent)) return inputProps.renderDefault(inputProps)
|
|
57
|
+
// Champ de premier niveau : vidé, il hérite à nouveau. Champ d'un élément de
|
|
58
|
+
// liste : la valeur par défaut est recopiée (une liste remplie remplace celle
|
|
59
|
+
// du défaut en entier, un champ vidé y resterait vide).
|
|
60
|
+
const reset = () =>
|
|
61
|
+
inputProps.onChange(inList && inherited !== undefined && inherited !== null ? set(structuredClone(inherited)) : unset())
|
|
56
62
|
return (
|
|
57
63
|
<Flex align="flex-start" gap={2}>
|
|
58
64
|
<Box flex={1} style={{minWidth: 0}}>
|
|
59
65
|
{inputProps.renderDefault(inputProps)}
|
|
60
66
|
</Box>
|
|
61
|
-
<OverrideDot
|
|
62
|
-
inherited={describeValue(inherited)}
|
|
63
|
-
same={sameValue(inputProps.value, inherited)}
|
|
64
|
-
onReset={() => inputProps.onChange(unset())}
|
|
65
|
-
/>
|
|
67
|
+
<OverrideDot inherited={describeValue(inherited)} onReset={reset} />
|
|
66
68
|
</Flex>
|
|
67
69
|
)
|
|
68
70
|
}
|
|
@@ -85,7 +87,7 @@ function carriesDot(schemaType: InputProps['schemaType']): boolean {
|
|
|
85
87
|
|
|
86
88
|
const BLUE = 'var(--card-badge-primary-dot-color, #2276fc)'
|
|
87
89
|
|
|
88
|
-
function OverrideDot(props: {inherited: string;
|
|
90
|
+
function OverrideDot(props: {inherited: string; onReset: () => void}) {
|
|
89
91
|
const [hover, setHover] = useState(false)
|
|
90
92
|
return (
|
|
91
93
|
<Tooltip
|
|
@@ -94,8 +96,7 @@ function OverrideDot(props: {inherited: string; same: boolean; onReset: () => vo
|
|
|
94
96
|
content={
|
|
95
97
|
<Box padding={2} style={{maxWidth: 260}}>
|
|
96
98
|
<Text size={1}>
|
|
97
|
-
Valeur
|
|
98
|
-
Défaut du composant : {props.inherited}.
|
|
99
|
+
Valeur différente du défaut du composant ({props.inherited}). Survoler puis cliquer pour revenir à la valeur par défaut.
|
|
99
100
|
</Text>
|
|
100
101
|
</Box>
|
|
101
102
|
}
|
|
@@ -2,8 +2,8 @@ import {useEffect, useSyncExternalStore} from 'react'
|
|
|
2
2
|
import type {SanityClient} from 'sanity'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Valeurs par défaut des composants (
|
|
6
|
-
* l'instance
|
|
5
|
+
* Valeurs par défaut des composants (dernière version, brouillon compris : la
|
|
6
|
+
* même que le formulaire du composant), dans la langue de l'instance, lues UNE fois et tenues à jour en direct pour tous les champs du
|
|
7
7
|
* formulaire : une seule requête et une seule écoute, quel que soit le nombre de
|
|
8
8
|
* pastilles affichées.
|
|
9
9
|
*/
|
|
@@ -25,10 +25,11 @@ function entryFor(client: SanityClient, type: string, languageField: string, lan
|
|
|
25
25
|
}
|
|
26
26
|
const e = entry
|
|
27
27
|
if (!e.stop) {
|
|
28
|
-
const filter = `_type == $type &&
|
|
28
|
+
const filter = `_type == $type && ($language == null || !defined(${languageField}) || ${languageField} == $language)`
|
|
29
29
|
const params = {type, language}
|
|
30
|
+
const drafts = client.withConfig({perspective: 'drafts'})
|
|
30
31
|
const load = () =>
|
|
31
|
-
|
|
32
|
+
drafts
|
|
32
33
|
.fetch<Defaults>(`*[${filter}][0]`, params)
|
|
33
34
|
.then((value) => {
|
|
34
35
|
e.value = value
|
package/src/lib/overrides.ts
CHANGED
|
@@ -27,49 +27,87 @@ export function firstKeyedIndex(path: readonly PathSegment[]): number {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
|
-
* Chemin du champ à l'intérieur de l'instance
|
|
31
|
-
* est
|
|
32
|
-
*
|
|
33
|
-
*
|
|
30
|
+
* Chemin du champ à l'intérieur de l'instance, listes comprises : un élément de
|
|
31
|
+
* liste est repéré par son rang (`['items', 0, 'title']`), comparé à l'élément de
|
|
32
|
+
* même rang de la valeur par défaut. `inList` : le champ est dans un élément de
|
|
33
|
+
* liste (une liste remplie remplace celle du défaut en entier : réinitialiser
|
|
34
|
+
* RECOPIE la valeur par défaut au lieu de vider le champ). `null` : pas un
|
|
35
|
+
* composant de l'espace de noms.
|
|
34
36
|
*/
|
|
35
|
-
export function
|
|
37
|
+
export function resolveFieldPath(
|
|
36
38
|
path: readonly PathSegment[],
|
|
37
39
|
index: number,
|
|
38
40
|
instance: unknown,
|
|
39
41
|
namespace: string,
|
|
40
|
-
): string[] | null {
|
|
42
|
+
): {path: (string | number)[]; inList: boolean} | null {
|
|
41
43
|
const type = (instance as {_type?: unknown} | null)?._type
|
|
42
44
|
if (index < 0 || typeof type !== 'string' || !type.startsWith(namespace)) return null
|
|
43
|
-
const
|
|
44
|
-
if (
|
|
45
|
-
|
|
45
|
+
const rest = path.slice(index + 1)
|
|
46
|
+
if (rest.length === 0) return null
|
|
47
|
+
const out: (string | number)[] = []
|
|
48
|
+
let inList = false
|
|
49
|
+
let node: unknown = instance
|
|
50
|
+
for (const seg of rest) {
|
|
51
|
+
if (typeof seg === 'string') {
|
|
52
|
+
out.push(seg)
|
|
53
|
+
node = node && typeof node === 'object' ? (node as Record<string, unknown>)[seg] : undefined
|
|
54
|
+
} else if (isKeyed(seg) || typeof seg === 'number') {
|
|
55
|
+
if (!Array.isArray(node)) return null
|
|
56
|
+
const i = typeof seg === 'number' ? seg : node.findIndex((item) => (item as {_key?: unknown})?._key === seg._key)
|
|
57
|
+
if (i < 0) return null
|
|
58
|
+
// Texte riche : comparé comme un tout (le champ), pas paragraphe par paragraphe.
|
|
59
|
+
if ((node[i] as {_type?: unknown} | null)?._type === 'block') return null
|
|
60
|
+
out.push(i)
|
|
61
|
+
inList = true
|
|
62
|
+
node = node[i]
|
|
63
|
+
} else return null
|
|
64
|
+
}
|
|
65
|
+
return {path: out, inList}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** @deprecated Utiliser resolveFieldPath (listes comprises). */
|
|
69
|
+
export function relativeFieldPath(
|
|
70
|
+
path: readonly PathSegment[],
|
|
71
|
+
index: number,
|
|
72
|
+
instance: unknown,
|
|
73
|
+
namespace: string,
|
|
74
|
+
): string[] | null {
|
|
75
|
+
const r = resolveFieldPath(path, index, instance, namespace)
|
|
76
|
+
return r && !r.inList ? (r.path as string[]) : null
|
|
46
77
|
}
|
|
47
78
|
|
|
48
79
|
/** Valeur par défaut d'un champ de composant. */
|
|
49
80
|
export function defaultValueAt(
|
|
50
81
|
defaults: Record<string, unknown> | null | undefined,
|
|
51
82
|
blockShortName: string,
|
|
52
|
-
relPath: readonly string[],
|
|
83
|
+
relPath: readonly (string | number)[],
|
|
53
84
|
): unknown {
|
|
54
85
|
let node: unknown = defaults?.[blockShortName]
|
|
55
86
|
for (const key of relPath) {
|
|
56
87
|
if (!node || typeof node !== 'object') return undefined
|
|
57
|
-
node = (node as Record<string, unknown>)[key]
|
|
88
|
+
node = (node as Record<string | number, unknown>)[key]
|
|
58
89
|
}
|
|
59
90
|
return node
|
|
60
91
|
}
|
|
61
92
|
|
|
93
|
+
const hasLinkTarget = (link: unknown) => {
|
|
94
|
+
const l = (link ?? {}) as Record<string, unknown>
|
|
95
|
+
return LINK_TARGET_FIELDS.some((k) => (k === 'file' ? Boolean((l.file as {asset?: unknown} | undefined)?.asset) : !isEmptyValue(l[k])))
|
|
96
|
+
}
|
|
97
|
+
|
|
62
98
|
/**
|
|
63
|
-
* Le champ est-il un override ? Valeur propre à l'instance
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* une
|
|
99
|
+
* Le champ est-il un override ? Valeur propre à l'instance DIFFÉRENTE de la
|
|
100
|
+
* valeur par défaut du composant (y compris quand le composant n'a pas de valeur
|
|
101
|
+
* par défaut pour ce champ). Un champ vide hérite : jamais d'override. Une case
|
|
102
|
+
* décochée vaut une case non renseignée. Le type d'un lien ne compte que si le
|
|
103
|
+
* lien a sa propre cible (il est prérempli par le Studio).
|
|
67
104
|
*/
|
|
68
|
-
export function isOverride(local: unknown, inherited: unknown, field: string, parent: unknown): boolean {
|
|
69
|
-
if (isEmptyValue(local)
|
|
105
|
+
export function isOverride(local: unknown, inherited: unknown, field: string | number, parent: unknown): boolean {
|
|
106
|
+
if (isEmptyValue(local)) return false
|
|
107
|
+
if (local === false && isEmptyValue(inherited)) return false
|
|
70
108
|
const parentType = (parent as {_type?: unknown} | null)?._type
|
|
71
|
-
if (field === 'type' && typeof parentType === 'string' && LINK_TYPES.has(parentType)) return false
|
|
72
|
-
return
|
|
109
|
+
if (field === 'type' && typeof parentType === 'string' && LINK_TYPES.has(parentType) && !hasLinkTarget(parent)) return false
|
|
110
|
+
return !sameValue(local, inherited)
|
|
73
111
|
}
|
|
74
112
|
|
|
75
113
|
/** Égalité de valeurs (champ identique au défaut : override « figé »). */
|
|
@@ -91,6 +129,7 @@ export function sameValue(a: unknown, b: unknown): boolean {
|
|
|
91
129
|
|
|
92
130
|
/** Valeur par défaut lisible, pour la bulle de la pastille. */
|
|
93
131
|
export function describeValue(value: unknown): string {
|
|
132
|
+
if (isEmptyValue(value)) return 'vide (valeur du design system)'
|
|
94
133
|
if (typeof value === 'string') return value.length > 60 ? `« ${value.slice(0, 57)}… »` : `« ${value} »`
|
|
95
134
|
if (typeof value === 'boolean') return value ? 'Oui' : 'Non'
|
|
96
135
|
if (typeof value === 'number') return String(value)
|
package/src/version.ts
CHANGED