@nocode-factory/sanity-kit 2.9.0 → 2.9.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/README.md +2 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +55 -18
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/form/OverrideInput.tsx +40 -1
- package/src/hooks/useOpenComponent.ts +4 -23
- package/src/lib/openComponent.ts +34 -0
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import {useState, type ComponentType} from 'react'
|
|
2
2
|
import {set, unset, useFormValue, useSchema, type InputProps} from 'sanity'
|
|
3
3
|
import {useRouter} from 'sanity/router'
|
|
4
|
-
import {Box, Flex, Stack, Text} from '@sanity/ui'
|
|
4
|
+
import {Box, Button, Flex, Stack, Text} from '@sanity/ui'
|
|
5
5
|
import {Tooltip} from '@sanity/ui/tooltip'
|
|
6
6
|
import type {ResolvedKitConfig} from '../config/types'
|
|
7
7
|
import {blockName, componentIntentParams} from '../lib/blocks'
|
|
8
8
|
import {useKitClient} from '../hooks/useKitClient'
|
|
9
|
+
import {openComponentDefaults} from '../lib/openComponent'
|
|
9
10
|
import {useComponentDefaults} from '../hooks/useComponentDefaults'
|
|
10
11
|
import {
|
|
11
12
|
defaultValueAt,
|
|
@@ -46,6 +47,17 @@ export function createOverrideInput(config: ResolvedKitConfig): ComponentType<In
|
|
|
46
47
|
const parent = useFormValue((path.length > 1 ? path.slice(0, -1) : path) as never)
|
|
47
48
|
const resolved = resolveFieldPath(path, anchor, instance, namespace)
|
|
48
49
|
|
|
50
|
+
// L'instance elle-même (l'objet section) : bouton « Modifier l'instance principale ».
|
|
51
|
+
const own = props.value as {_type?: unknown} | undefined
|
|
52
|
+
if (
|
|
53
|
+
props.schemaType.jsonType === 'object' &&
|
|
54
|
+
typeof own?._type === 'string' &&
|
|
55
|
+
own._type.startsWith(namespace) &&
|
|
56
|
+
props.path.length > 0 &&
|
|
57
|
+
schema.get(defaultsType)
|
|
58
|
+
)
|
|
59
|
+
return <WithMainInstance {...props} type={own._type} />
|
|
60
|
+
|
|
49
61
|
if (!resolved || !schema.get(defaultsType) || !carriesDot(props.schemaType)) return props.renderDefault(props)
|
|
50
62
|
const shortName = blockName((instance as {_type: string})._type, namespace)
|
|
51
63
|
return <WithDot {...props} rel={resolved.path} inList={resolved.inList} shortName={shortName} parent={parent} />
|
|
@@ -113,6 +125,33 @@ export function createOverrideInput(config: ResolvedKitConfig): ComponentType<In
|
|
|
113
125
|
)
|
|
114
126
|
}
|
|
115
127
|
|
|
128
|
+
// En tête du formulaire d'une instance de composant : mène aux valeurs par
|
|
129
|
+
// défaut du composant (son « instance principale »), qui s'appliquent à toutes
|
|
130
|
+
// les instances dont le champ est vide.
|
|
131
|
+
function WithMainInstance(props: InputProps & {type: string}) {
|
|
132
|
+
const {type, ...inputProps} = props
|
|
133
|
+
const client = useKitClient()
|
|
134
|
+
const router = useRouter()
|
|
135
|
+
const open = () =>
|
|
136
|
+
void openComponentDefaults({type, config, hasDefaults: true, client, navigateIntent: (i, p) => router.navigateIntent(i, p)})
|
|
137
|
+
return (
|
|
138
|
+
<Stack gap={3}>
|
|
139
|
+
<Flex justify="flex-end">
|
|
140
|
+
<Button
|
|
141
|
+
mode="ghost"
|
|
142
|
+
tone="primary"
|
|
143
|
+
fontSize={1}
|
|
144
|
+
padding={2}
|
|
145
|
+
text="Modifier l'instance principale"
|
|
146
|
+
title="Valeurs par défaut du composant : elles s'appliquent à toutes ses instances dont le champ est vide"
|
|
147
|
+
onClick={open}
|
|
148
|
+
/>
|
|
149
|
+
</Flex>
|
|
150
|
+
{inputProps.renderDefault(inputProps)}
|
|
151
|
+
</Stack>
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
|
|
116
155
|
return OverrideInput
|
|
117
156
|
}
|
|
118
157
|
|
|
@@ -2,15 +2,10 @@ import {useCallback} from 'react'
|
|
|
2
2
|
import {useSchema} from 'sanity'
|
|
3
3
|
import {useRouter} from 'sanity/router'
|
|
4
4
|
import {useKitConfig} from '../context/KitContext'
|
|
5
|
-
import {
|
|
5
|
+
import {openComponentDefaults} from '../lib/openComponent'
|
|
6
6
|
import {useKitClient} from './useKitClient'
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* Ouvre un composant : Presentation avec son rendu réel et ses valeurs par
|
|
10
|
-
* défaut côte à côte. Sans document de valeurs par défaut dans le schéma : la
|
|
11
|
-
* démo du design system dans un nouvel onglet. `null` : rien à ouvrir.
|
|
12
|
-
* Partagé par la grille et la vue Map de l'onglet Composants.
|
|
13
|
-
*/
|
|
8
|
+
/** Ouvre un composant (voir openComponentDefaults). `null` : rien à ouvrir. */
|
|
14
9
|
export function useOpenComponent(): ((type: string) => void) | null {
|
|
15
10
|
const config = useKitConfig()
|
|
16
11
|
const schema = useSchema()
|
|
@@ -20,22 +15,8 @@ export function useOpenComponent(): ((type: string) => void) | null {
|
|
|
20
15
|
const hasDefaults = Boolean(schema.get(preview.defaultsType))
|
|
21
16
|
|
|
22
17
|
const open = useCallback(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (config.previewUrl)
|
|
26
|
-
window.open(`${config.previewUrl}${preview.path}#${fillTemplate(preview.anchor, type, config.blockNamespace)}`, '_blank', 'noopener')
|
|
27
|
-
return
|
|
28
|
-
}
|
|
29
|
-
const lang = config.i18n?.defaultLocale ?? null
|
|
30
|
-
const id = await client
|
|
31
|
-
.fetch<string | null>(
|
|
32
|
-
`*[_type == $type] | order(select($lang != null && ${config.i18n?.field ?? 'language'} == $lang => 0, 1))[0]._id`,
|
|
33
|
-
{type: preview.defaultsType, lang},
|
|
34
|
-
)
|
|
35
|
-
.catch(() => null)
|
|
36
|
-
router.navigateIntent('edit', componentIntentParams(type, config.blockNamespace, preview, id ?? preview.defaultsType))
|
|
37
|
-
},
|
|
38
|
-
[hasDefaults, config, preview, client, router],
|
|
18
|
+
(type: string) => openComponentDefaults({type, config, hasDefaults, client, navigateIntent: (i, p) => router.navigateIntent(i, p)}),
|
|
19
|
+
[hasDefaults, config, client, router],
|
|
39
20
|
)
|
|
40
21
|
|
|
41
22
|
if (!hasDefaults && !config.previewUrl) return null
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type {SanityClient} from 'sanity'
|
|
2
|
+
import type {ResolvedKitConfig} from '../config/types'
|
|
3
|
+
import {componentIntentParams, fillTemplate} from './blocks'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Ouvre un composant : Presentation avec son rendu réel et ses valeurs par
|
|
7
|
+
* défaut côte à côte (le document des valeurs par défaut, champ du composant).
|
|
8
|
+
* Sans document de valeurs par défaut dans le schéma : la démo du design system
|
|
9
|
+
* dans un nouvel onglet. Partagé par l'onglet Composants (grille, Map) et le
|
|
10
|
+
* bouton « Modifier l'instance principale » des formulaires.
|
|
11
|
+
*/
|
|
12
|
+
export async function openComponentDefaults(options: {
|
|
13
|
+
type: string
|
|
14
|
+
config: ResolvedKitConfig
|
|
15
|
+
hasDefaults: boolean
|
|
16
|
+
client: SanityClient
|
|
17
|
+
navigateIntent: (intent: string, params: Record<string, string>) => void
|
|
18
|
+
}): Promise<void> {
|
|
19
|
+
const {type, config, hasDefaults, client, navigateIntent} = options
|
|
20
|
+
const preview = config.componentPreview
|
|
21
|
+
if (!hasDefaults) {
|
|
22
|
+
if (config.previewUrl)
|
|
23
|
+
window.open(`${config.previewUrl}${preview.path}#${fillTemplate(preview.anchor, type, config.blockNamespace)}`, '_blank', 'noopener')
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
const lang = config.i18n?.defaultLocale ?? null
|
|
27
|
+
const id = await client
|
|
28
|
+
.fetch<string | null>(
|
|
29
|
+
`*[_type == $type] | order(select($lang != null && ${config.i18n?.field ?? 'language'} == $lang => 0, 1))[0]._id`,
|
|
30
|
+
{type: preview.defaultsType, lang},
|
|
31
|
+
)
|
|
32
|
+
.catch(() => null)
|
|
33
|
+
navigateIntent('edit', componentIntentParams(type, config.blockNamespace, preview, id ?? preview.defaultsType))
|
|
34
|
+
}
|
package/src/version.ts
CHANGED