@nocode-factory/sanity-kit 2.6.2 → 2.7.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 +11 -1
- package/dist/index.js +50 -8
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/config/defaults.ts +2 -0
- package/src/config/resolve.ts +2 -0
- package/src/config/types.ts +10 -0
- package/src/lib/blocks.ts +21 -0
- package/src/tools/components/BlockCard.tsx +12 -2
- package/src/tools/components/ComponentsTool.tsx +31 -2
- package/src/version.ts +1 -1
package/package.json
CHANGED
package/src/config/defaults.ts
CHANGED
|
@@ -33,6 +33,8 @@ export const DEFAULT_COMPONENT_PREVIEW: Required<NcfComponentPreviewConfig> = {
|
|
|
33
33
|
path: '/design-system',
|
|
34
34
|
anchor: '{name}',
|
|
35
35
|
thumbnail: '/sections/{name}.png',
|
|
36
|
+
defaultsType: 'componentDefaults',
|
|
37
|
+
live: '/design-system/composant?type={name}',
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
export const DEFAULT_TOOLS: Required<NcfToolsConfig> = {
|
package/src/config/resolve.ts
CHANGED
|
@@ -98,6 +98,8 @@ export function resolveConfig(input: NcfKitConfig): ResolvedKitConfig {
|
|
|
98
98
|
path: input.componentPreview?.path ?? DEFAULT_COMPONENT_PREVIEW.path,
|
|
99
99
|
anchor: input.componentPreview?.anchor ?? DEFAULT_COMPONENT_PREVIEW.anchor,
|
|
100
100
|
thumbnail: input.componentPreview?.thumbnail ?? DEFAULT_COMPONENT_PREVIEW.thumbnail,
|
|
101
|
+
defaultsType: input.componentPreview?.defaultsType ?? DEFAULT_COMPONENT_PREVIEW.defaultsType,
|
|
102
|
+
live: input.componentPreview?.live ?? DEFAULT_COMPONENT_PREVIEW.live,
|
|
101
103
|
},
|
|
102
104
|
serviceEndpoint:
|
|
103
105
|
input.serviceEndpoint === false
|
package/src/config/types.ts
CHANGED
|
@@ -52,6 +52,14 @@ export interface NcfComponentPreviewConfig {
|
|
|
52
52
|
anchor?: string
|
|
53
53
|
/** Chemin de la capture, relatif à `previewUrl`. Gabarit : {name} / {kebab}. */
|
|
54
54
|
thumbnail?: string
|
|
55
|
+
/**
|
|
56
|
+
* Type du document des valeurs par défaut des composants (un champ par
|
|
57
|
+
* composant, nommé `{name}`). Présent dans le schéma : un clic sur un composant
|
|
58
|
+
* ouvre Presentation avec son rendu réel et ses valeurs par défaut côte à côte.
|
|
59
|
+
*/
|
|
60
|
+
defaultsType?: string
|
|
61
|
+
/** Page qui rend un composant seul, avec ses valeurs par défaut. Gabarit : {name} / {kebab}. */
|
|
62
|
+
live?: string
|
|
55
63
|
}
|
|
56
64
|
|
|
57
65
|
/** Journal des modifications (phase 2). */
|
|
@@ -232,6 +240,8 @@ export interface ResolvedComponentPreview {
|
|
|
232
240
|
path: string
|
|
233
241
|
anchor: string
|
|
234
242
|
thumbnail: string
|
|
243
|
+
defaultsType: string
|
|
244
|
+
live: string
|
|
235
245
|
}
|
|
236
246
|
|
|
237
247
|
export interface ResolvedKitConfig {
|
package/src/lib/blocks.ts
CHANGED
|
@@ -36,3 +36,24 @@ export function humanizeBlockName(type: string, namespace: string): string {
|
|
|
36
36
|
const [first = '', ...rest] = words
|
|
37
37
|
return [first.charAt(0).toUpperCase() + first.slice(1), ...rest].join(' ')
|
|
38
38
|
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Paramètres de l'intention qui ouvre un composant dans Presentation : son rendu
|
|
42
|
+
* réel (page `live`) à gauche, le champ de ses valeurs par défaut à droite.
|
|
43
|
+
* `documentId` : document des valeurs par défaut (créé à la première saisie s'il
|
|
44
|
+
* n'existe pas encore).
|
|
45
|
+
*/
|
|
46
|
+
export function componentIntentParams(
|
|
47
|
+
type: string,
|
|
48
|
+
namespace: string,
|
|
49
|
+
preview: {defaultsType: string; live: string},
|
|
50
|
+
documentId: string,
|
|
51
|
+
): Record<string, string> {
|
|
52
|
+
return {
|
|
53
|
+
id: documentId.replace(/^drafts\./, ''),
|
|
54
|
+
type: preview.defaultsType,
|
|
55
|
+
mode: 'presentation',
|
|
56
|
+
path: blockName(type, namespace),
|
|
57
|
+
preview: fillTemplate(preview.live, type, namespace),
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -10,6 +10,8 @@ export interface BlockCardProps {
|
|
|
10
10
|
occurrence: BlockOccurrence
|
|
11
11
|
namespace: string
|
|
12
12
|
thumbnailUrl: string | null
|
|
13
|
+
/** Au clic : rendu réel du composant et réglage de ses valeurs par défaut. */
|
|
14
|
+
onOpen?: () => void
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
/**
|
|
@@ -21,12 +23,20 @@ export interface BlockCardProps {
|
|
|
21
23
|
* à le lire.
|
|
22
24
|
*/
|
|
23
25
|
export function BlockCard(props: BlockCardProps) {
|
|
24
|
-
const {block, occurrence, namespace, thumbnailUrl} = props
|
|
26
|
+
const {block, occurrence, namespace, thumbnailUrl, onOpen} = props
|
|
25
27
|
const [failed, setFailed] = useState(false)
|
|
26
28
|
const unused = occurrence.count === 0
|
|
27
29
|
|
|
28
30
|
return (
|
|
29
|
-
<Card
|
|
31
|
+
<Card
|
|
32
|
+
radius={3}
|
|
33
|
+
shadow={1}
|
|
34
|
+
padding={0}
|
|
35
|
+
as={onOpen ? 'button' : undefined}
|
|
36
|
+
onClick={onOpen}
|
|
37
|
+
title={onOpen ? 'Voir le composant en vrai et régler ses valeurs par défaut' : undefined}
|
|
38
|
+
style={{height: '100%', overflow: 'hidden', width: '100%', textAlign: 'left', cursor: onOpen ? 'pointer' : undefined}}
|
|
39
|
+
>
|
|
30
40
|
<Stack gap={0} style={{height: '100%'}}>
|
|
31
41
|
<Box
|
|
32
42
|
style={{
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {useMemo, useState} from 'react'
|
|
1
|
+
import {useCallback, useMemo, useState} from 'react'
|
|
2
|
+
import {useSchema} from 'sanity'
|
|
3
|
+
import {useRouter} from 'sanity/router'
|
|
2
4
|
import {Box, Button, Card, Flex, Grid, Spinner, Stack, Text} from '@sanity/ui'
|
|
3
5
|
import {useKitConfig} from '../../context/KitContext'
|
|
4
6
|
import {useOccurrences, OCCURRENCE_SCAN_LIMIT} from '../../hooks/useOccurrences'
|
|
5
|
-
import {fillTemplate} from '../../lib/blocks'
|
|
7
|
+
import {componentIntentParams, fillTemplate} from '../../lib/blocks'
|
|
8
|
+
import {useKitClient} from '../../hooks/useKitClient'
|
|
6
9
|
import {QueryError} from '../../ui/QueryError'
|
|
7
10
|
import {BlockCard} from './BlockCard'
|
|
8
11
|
import {SectionsMap} from './map/SectionsMap'
|
|
@@ -27,6 +30,31 @@ export function ComponentsTool() {
|
|
|
27
30
|
|
|
28
31
|
const preview = config.componentPreview
|
|
29
32
|
const canPreview = preview.provider === 'designSystemPage' && Boolean(config.previewUrl)
|
|
33
|
+
const schema = useSchema()
|
|
34
|
+
const router = useRouter()
|
|
35
|
+
const client = useKitClient()
|
|
36
|
+
const hasDefaults = Boolean(schema.get(preview.defaultsType))
|
|
37
|
+
|
|
38
|
+
// Clic sur un composant : Presentation avec son rendu réel et ses valeurs par
|
|
39
|
+
// défaut côte à côte. Sans document de valeurs par défaut dans le schéma : la
|
|
40
|
+
// démo du design system dans un nouvel onglet.
|
|
41
|
+
const openComponent = useCallback(
|
|
42
|
+
async (type: string) => {
|
|
43
|
+
if (!hasDefaults) {
|
|
44
|
+
if (config.previewUrl) window.open(`${config.previewUrl}${preview.path}#${fillTemplate(preview.anchor, type, config.blockNamespace)}`, '_blank', 'noopener')
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
const lang = config.i18n?.defaultLocale ?? null
|
|
48
|
+
const id = await client
|
|
49
|
+
.fetch<string | null>(
|
|
50
|
+
`*[_type == $type] | order(select($lang != null && ${config.i18n?.field ?? 'language'} == $lang => 0, 1))[0]._id`,
|
|
51
|
+
{type: preview.defaultsType, lang},
|
|
52
|
+
)
|
|
53
|
+
.catch(() => null)
|
|
54
|
+
router.navigateIntent('edit', componentIntentParams(type, config.blockNamespace, preview, id ?? preview.defaultsType))
|
|
55
|
+
},
|
|
56
|
+
[hasDefaults, config, preview, client, router],
|
|
57
|
+
)
|
|
30
58
|
|
|
31
59
|
const cards = useMemo(() => {
|
|
32
60
|
const byName = new Map(blockTypes.map((b) => [b.name, b]))
|
|
@@ -162,6 +190,7 @@ export function ComponentsTool() {
|
|
|
162
190
|
? `${config.previewUrl}${fillTemplate(preview.thumbnail, block.name, config.blockNamespace)}`
|
|
163
191
|
: null
|
|
164
192
|
}
|
|
193
|
+
onOpen={hasDefaults || config.previewUrl ? () => void openComponent(block.name) : undefined}
|
|
165
194
|
/>
|
|
166
195
|
))}
|
|
167
196
|
</Grid>
|
package/src/version.ts
CHANGED