@nocode-factory/sanity-kit 2.50.0 → 2.51.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 +106 -3
- package/dist/index.js +628 -52
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/context/ComponentMetaContext.tsx +23 -0
- package/src/form/ArrayNamesInput.tsx +25 -0
- package/src/form/renameArrayMembers.ts +25 -0
- package/src/hooks/useComponentMeta.ts +159 -0
- package/src/hooks/useComponentNames.ts +39 -0
- package/src/index.ts +12 -1
- package/src/lib/componentMeta.ts +70 -0
- package/src/schema/boardLayout.ts +3 -1
- package/src/schema/componentMeta.ts +54 -0
- package/src/tools/components/BlockCard.tsx +20 -12
- package/src/tools/components/ComponentEditDialog.tsx +83 -0
- package/src/tools/components/ComponentsTool.tsx +195 -84
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {createContext, useContext, type ReactNode} from 'react'
|
|
2
|
+
import {useComponentMetaState, type ComponentMetaState} from '../hooks/useComponentMeta'
|
|
3
|
+
|
|
4
|
+
const ComponentMetaContext = createContext<ComponentMetaState | null>(null)
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Une seule instance de l'organisation des composants dans l'onglet.
|
|
8
|
+
*
|
|
9
|
+
* Le document `ncfComponentMeta` est réécrit en entier à chaque enregistrement :
|
|
10
|
+
* deux instances s'écraseraient (renommer effacerait un groupe, etc.).
|
|
11
|
+
*/
|
|
12
|
+
export function ComponentMetaProvider(props: {children: ReactNode}) {
|
|
13
|
+
const value = useComponentMetaState()
|
|
14
|
+
return <ComponentMetaContext.Provider value={value}>{props.children}</ComponentMetaContext.Provider>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function useComponentMeta(): ComponentMetaState {
|
|
18
|
+
const value = useContext(ComponentMetaContext)
|
|
19
|
+
if (!value) {
|
|
20
|
+
throw new Error('useComponentMeta must be called inside ComponentMetaProvider.')
|
|
21
|
+
}
|
|
22
|
+
return value
|
|
23
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {createElement, useMemo, type ComponentType} from 'react'
|
|
2
|
+
import type {InputProps} from 'sanity'
|
|
3
|
+
import {useComponentNames} from '../hooks/useComponentNames'
|
|
4
|
+
import {renameArrayMembers} from './renameArrayMembers'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Input d'un champ tableau : si ses membres portent un nom marketing (groupes /
|
|
8
|
+
* renommage des composants), réécrit leurs titres pour que le menu d'insertion
|
|
9
|
+
* les affiche. Sinon, délègue au comportement normal du kit (`Fallback`).
|
|
10
|
+
*
|
|
11
|
+
* Cible naturellement le page builder (et tout tableau qui accepte des sections
|
|
12
|
+
* renommées) sans avoir à l'identifier : seuls les membres réellement renommés
|
|
13
|
+
* changent.
|
|
14
|
+
*/
|
|
15
|
+
export function createArrayNamesInput(Fallback: ComponentType<InputProps>): ComponentType<InputProps> {
|
|
16
|
+
return function ArrayNamesInput(props: InputProps) {
|
|
17
|
+
const names = useComponentNames()
|
|
18
|
+
const schemaType = useMemo(
|
|
19
|
+
() => renameArrayMembers(props.schemaType as never, names),
|
|
20
|
+
[props.schemaType, names],
|
|
21
|
+
)
|
|
22
|
+
if (schemaType === (props.schemaType as never)) return createElement(Fallback, props)
|
|
23
|
+
return props.renderDefault({...props, schemaType} as InputProps)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Réécrit le `title` des membres `of` d'un type tableau à partir des noms
|
|
3
|
+
* marketing (type -> nom). Sert à ce que le menu d'insertion du page builder
|
|
4
|
+
* affiche le nom donné par le marketing au lieu du titre du schéma.
|
|
5
|
+
*
|
|
6
|
+
* Renvoie le MÊME objet si rien n'est à renommer (aucun membre concerné) :
|
|
7
|
+
* l'appelant peut comparer par identité pour ne rien changer au rendu.
|
|
8
|
+
* Le clone préserve le prototype du type de schéma (l'input d'array peut en
|
|
9
|
+
* dépendre) et ne remplace que `title`.
|
|
10
|
+
*/
|
|
11
|
+
type ArrayMember = {name?: string; title?: string} & Record<string, unknown>
|
|
12
|
+
type ArrayLikeType = {of?: ArrayMember[]} & Record<string, unknown>
|
|
13
|
+
|
|
14
|
+
export function renameArrayMembers<T extends ArrayLikeType>(schemaType: T, names: ReadonlyMap<string, string>): T {
|
|
15
|
+
const members = schemaType.of
|
|
16
|
+
if (names.size === 0 || !Array.isArray(members) || !members.some((member) => member?.name && names.has(member.name))) {
|
|
17
|
+
return schemaType
|
|
18
|
+
}
|
|
19
|
+
const of = members.map((member) =>
|
|
20
|
+
member?.name && names.has(member.name)
|
|
21
|
+
? (Object.assign(Object.create(Object.getPrototypeOf(member)), member, {title: names.get(member.name)}) as ArrayMember)
|
|
22
|
+
: member,
|
|
23
|
+
)
|
|
24
|
+
return {...schemaType, of}
|
|
25
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import {useCallback, useEffect, useMemo, useRef, useState} from 'react'
|
|
2
|
+
import {useCurrentUser} from 'sanity'
|
|
3
|
+
import {useKitClient} from './useKitClient'
|
|
4
|
+
import {COMPONENT_META_ID, COMPONENT_META_TYPE, COMPONENT_GROUP_TYPE, COMPONENT_ENTRY_TYPE} from '../schema/componentMeta'
|
|
5
|
+
import {groupMap, nameMap, type ComponentEntry, type ComponentGroup} from '../lib/componentMeta'
|
|
6
|
+
|
|
7
|
+
export interface ComponentMetaState {
|
|
8
|
+
groups: ComponentGroup[]
|
|
9
|
+
entries: ComponentEntry[]
|
|
10
|
+
names: Map<string, string>
|
|
11
|
+
groupOf: Map<string, string>
|
|
12
|
+
loading: boolean
|
|
13
|
+
saveError: Error | null
|
|
14
|
+
createGroup: (name: string) => string
|
|
15
|
+
renameGroup: (id: string, name: string) => void
|
|
16
|
+
deleteGroup: (id: string) => void
|
|
17
|
+
/** Nom marketing d'un composant (`''`/null = retour au titre du schéma). */
|
|
18
|
+
renameComponent: (type: string, name: string | null) => void
|
|
19
|
+
/** Range un composant dans un groupe (`null` = sans groupe). */
|
|
20
|
+
setGroup: (type: string, group: string | null) => void
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type StoredGroup = {id?: string; name?: string}
|
|
24
|
+
type StoredEntry = {_key?: string; name?: string; group?: string}
|
|
25
|
+
|
|
26
|
+
const newId = (): string => `g${Math.random().toString(36).slice(2, 10)}`
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Lit et écrit l'organisation des composants (groupes + noms marketing) dans le
|
|
30
|
+
* singleton du kit `ncfComponentMeta`. Même principe que l'organisation des
|
|
31
|
+
* assets : document unique réécrit en entier, écriture débouncée, optimiste.
|
|
32
|
+
* À appeler UNE fois (via `ComponentMetaProvider`).
|
|
33
|
+
*/
|
|
34
|
+
export function useComponentMetaState(): ComponentMetaState {
|
|
35
|
+
const client = useKitClient()
|
|
36
|
+
const user = useCurrentUser()
|
|
37
|
+
|
|
38
|
+
const [groups, setGroups] = useState<ComponentGroup[]>([])
|
|
39
|
+
const [entries, setEntries] = useState<ComponentEntry[]>([])
|
|
40
|
+
const [loading, setLoading] = useState(true)
|
|
41
|
+
const [saveError, setSaveError] = useState<Error | null>(null)
|
|
42
|
+
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
let cancelled = false
|
|
45
|
+
client
|
|
46
|
+
.fetch<{groups?: StoredGroup[]; components?: StoredEntry[]} | null>(`*[_id == $id][0]{groups, components}`, {id: COMPONENT_META_ID})
|
|
47
|
+
.then((doc) => {
|
|
48
|
+
if (cancelled) return
|
|
49
|
+
setGroups((doc?.groups ?? []).filter((g) => g.id && g.name).map((g) => ({id: g.id as string, name: g.name as string})))
|
|
50
|
+
setEntries(
|
|
51
|
+
(doc?.components ?? [])
|
|
52
|
+
.filter((c) => Boolean(c._key))
|
|
53
|
+
.map((c) => ({type: c._key as string, ...(c.name ? {name: c.name} : {}), ...(c.group ? {group: c.group} : {})})),
|
|
54
|
+
)
|
|
55
|
+
})
|
|
56
|
+
.catch(() => {
|
|
57
|
+
if (!cancelled) {
|
|
58
|
+
setGroups([])
|
|
59
|
+
setEntries([])
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
.finally(() => {
|
|
63
|
+
if (!cancelled) setLoading(false)
|
|
64
|
+
})
|
|
65
|
+
return () => {
|
|
66
|
+
cancelled = true
|
|
67
|
+
}
|
|
68
|
+
}, [client])
|
|
69
|
+
|
|
70
|
+
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
|
|
71
|
+
useEffect(
|
|
72
|
+
() => () => {
|
|
73
|
+
if (timer.current) clearTimeout(timer.current)
|
|
74
|
+
},
|
|
75
|
+
[],
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
const groupsRef = useRef<ComponentGroup[]>([])
|
|
79
|
+
const entriesRef = useRef<ComponentEntry[]>([])
|
|
80
|
+
groupsRef.current = groups
|
|
81
|
+
entriesRef.current = entries
|
|
82
|
+
|
|
83
|
+
const write = useCallback(
|
|
84
|
+
(next?: {groups?: ComponentGroup[]; entries?: ComponentEntry[]}) => {
|
|
85
|
+
const nextGroups = next?.groups ?? groupsRef.current
|
|
86
|
+
const nextEntries = next?.entries ?? entriesRef.current
|
|
87
|
+
if (timer.current) clearTimeout(timer.current)
|
|
88
|
+
timer.current = setTimeout(() => {
|
|
89
|
+
client
|
|
90
|
+
.createOrReplace({
|
|
91
|
+
_id: COMPONENT_META_ID,
|
|
92
|
+
_type: COMPONENT_META_TYPE,
|
|
93
|
+
groups: nextGroups.map((group) => ({_key: group.id, _type: COMPONENT_GROUP_TYPE, id: group.id, name: group.name})),
|
|
94
|
+
// Entrées sans nom ni groupe : inutiles, on ne les stocke pas. `_key` = type.
|
|
95
|
+
components: nextEntries
|
|
96
|
+
.filter((entry) => entry.name || entry.group)
|
|
97
|
+
.map((entry) => ({_key: entry.type, _type: COMPONENT_ENTRY_TYPE, ...(entry.name ? {name: entry.name} : {}), ...(entry.group ? {group: entry.group} : {})})),
|
|
98
|
+
updatedBy: user?.id ?? null,
|
|
99
|
+
})
|
|
100
|
+
.then(() => setSaveError(null))
|
|
101
|
+
.catch((cause: unknown) => setSaveError(cause instanceof Error ? cause : new Error(String(cause))))
|
|
102
|
+
}, 800)
|
|
103
|
+
},
|
|
104
|
+
[client, user?.id],
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
// Modifie l'entrée d'un type (crée/supprime au besoin), puis écrit.
|
|
108
|
+
const patchEntry = useCallback(
|
|
109
|
+
(type: string, patch: Partial<ComponentEntry>) => {
|
|
110
|
+
const current = entriesRef.current.find((entry) => entry.type === type) ?? {type}
|
|
111
|
+
const merged: ComponentEntry = {...current, ...patch}
|
|
112
|
+
const cleaned: ComponentEntry = {type, ...(merged.name ? {name: merged.name} : {}), ...(merged.group ? {group: merged.group} : {})}
|
|
113
|
+
const others = entriesRef.current.filter((entry) => entry.type !== type)
|
|
114
|
+
const next = cleaned.name || cleaned.group ? [...others, cleaned] : others
|
|
115
|
+
setEntries(next)
|
|
116
|
+
write({entries: next})
|
|
117
|
+
},
|
|
118
|
+
[write],
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
const createGroup = useCallback(
|
|
122
|
+
(name: string): string => {
|
|
123
|
+
const id = newId()
|
|
124
|
+
const next = [...groupsRef.current, {id, name: name.trim() || 'Groupe'}]
|
|
125
|
+
setGroups(next)
|
|
126
|
+
write({groups: next})
|
|
127
|
+
return id
|
|
128
|
+
},
|
|
129
|
+
[write],
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
const renameGroup = useCallback(
|
|
133
|
+
(id: string, name: string) => {
|
|
134
|
+
const next = groupsRef.current.map((group) => (group.id === id ? {...group, name: name.trim() || group.name} : group))
|
|
135
|
+
setGroups(next)
|
|
136
|
+
write({groups: next})
|
|
137
|
+
},
|
|
138
|
+
[write],
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
const deleteGroup = useCallback(
|
|
142
|
+
(id: string) => {
|
|
143
|
+
const nextGroups = groupsRef.current.filter((group) => group.id !== id)
|
|
144
|
+
const nextEntries = entriesRef.current.map((entry) => (entry.group === id ? {...entry, group: undefined} : entry)).filter((entry) => entry.name || entry.group)
|
|
145
|
+
setGroups(nextGroups)
|
|
146
|
+
setEntries(nextEntries)
|
|
147
|
+
write({groups: nextGroups, entries: nextEntries})
|
|
148
|
+
},
|
|
149
|
+
[write],
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
const renameComponent = useCallback((type: string, name: string | null) => patchEntry(type, {name: name?.trim() || undefined}), [patchEntry])
|
|
153
|
+
const setGroup = useCallback((type: string, group: string | null) => patchEntry(type, {group: group ?? undefined}), [patchEntry])
|
|
154
|
+
|
|
155
|
+
const names = useMemo(() => nameMap(entries), [entries])
|
|
156
|
+
const groupOf = useMemo(() => groupMap(entries), [entries])
|
|
157
|
+
|
|
158
|
+
return {groups, entries, names, groupOf, loading, saveError, createGroup, renameGroup, deleteGroup, renameComponent, setGroup}
|
|
159
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {useEffect, useState} from 'react'
|
|
2
|
+
import {useKitClient} from './useKitClient'
|
|
3
|
+
import {COMPONENT_META_ID} from '../schema/componentMeta'
|
|
4
|
+
|
|
5
|
+
type StoredEntry = {_key?: string; name?: string}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Noms marketing des composants (type -> nom), en LECTURE SEULE et en direct.
|
|
9
|
+
* Pour les surfaces hors onglet (ex. l'input du page builder qui réécrit les
|
|
10
|
+
* libellés du menu d'insertion) : pas d'écriture, donc pas de provider unique.
|
|
11
|
+
*/
|
|
12
|
+
export function useComponentNames(): Map<string, string> {
|
|
13
|
+
const client = useKitClient()
|
|
14
|
+
const [names, setNames] = useState<Map<string, string>>(() => new Map())
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
let cancelled = false
|
|
18
|
+
const load = () =>
|
|
19
|
+
client
|
|
20
|
+
.fetch<{components?: StoredEntry[]} | null>(`*[_id == $id][0]{components}`, {id: COMPONENT_META_ID})
|
|
21
|
+
.then((doc) => {
|
|
22
|
+
if (cancelled) return
|
|
23
|
+
const map = new Map<string, string>()
|
|
24
|
+
for (const entry of doc?.components ?? []) if (entry._key && entry.name?.trim()) map.set(entry._key, entry.name.trim())
|
|
25
|
+
setNames(map)
|
|
26
|
+
})
|
|
27
|
+
.catch(() => {})
|
|
28
|
+
void load()
|
|
29
|
+
const subscription = client
|
|
30
|
+
.listen(`*[_id == $id]`, {id: COMPONENT_META_ID}, {visibility: 'query', events: ['mutation']})
|
|
31
|
+
.subscribe({next: () => void load(), error: () => {}})
|
|
32
|
+
return () => {
|
|
33
|
+
cancelled = true
|
|
34
|
+
subscription.unsubscribe()
|
|
35
|
+
}
|
|
36
|
+
}, [client])
|
|
37
|
+
|
|
38
|
+
return names
|
|
39
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -50,6 +50,7 @@ import {KitProvider} from './context/KitContext'
|
|
|
50
50
|
import type {ConfigIssue} from './config/types'
|
|
51
51
|
import {RemoveFromSiteAction, createRemoveFromSite} from './actions/RemoveFromSite'
|
|
52
52
|
import {createSlugUrlInput, isSlugInput} from './form/SlugUrlInput'
|
|
53
|
+
import {createArrayNamesInput} from './form/ArrayNamesInput'
|
|
53
54
|
import {withTranslationTodo} from './form/TranslationTodoInput'
|
|
54
55
|
import {withPublishedHint} from './form/PublishedHint'
|
|
55
56
|
import {withLanguageMismatch} from './form/LanguageMismatch'
|
|
@@ -62,11 +63,18 @@ import {withTranslationRedirects} from './actions/TranslationRedirects'
|
|
|
62
63
|
function createKitInput(config: Parameters<typeof createOverrideInput>[0], issues: ConfigIssue[]): ComponentType<InputProps> {
|
|
63
64
|
const Override = createOverrideInput(config)
|
|
64
65
|
const SlugUrl = createSlugUrlInput(config)
|
|
66
|
+
// Tableaux : réécrit les libellés du menu d'insertion avec les noms marketing
|
|
67
|
+
// des composants (sinon, comportement normal via Override).
|
|
68
|
+
const ArrayNames = createArrayNamesInput(Override)
|
|
65
69
|
function KitInput(props: InputProps) {
|
|
66
70
|
// Configuration du kit fournie au formulaire : les champs du kit (ex. JSON-LD,
|
|
67
71
|
// useKitConfig) la lisent comme les outils. Sans elle : « useKitContext must be
|
|
68
72
|
// called inside a tool ».
|
|
69
|
-
const children = isSlugInput(props)
|
|
73
|
+
const children = isSlugInput(props)
|
|
74
|
+
? createElement(SlugUrl, props)
|
|
75
|
+
: props.schemaType.jsonType === 'array'
|
|
76
|
+
? createElement(ArrayNames, props)
|
|
77
|
+
: createElement(Override, props)
|
|
70
78
|
return createElement(KitProvider, {config, issues, children})
|
|
71
79
|
}
|
|
72
80
|
// Traductions : pastille « À traduire » sur les champs modifiés dans la version principale.
|
|
@@ -318,6 +326,9 @@ export type {
|
|
|
318
326
|
AssetUsageFilter,
|
|
319
327
|
FolderSelection,
|
|
320
328
|
} from './lib/assets'
|
|
329
|
+
export {nameMap, groupMap, withCustomNames, groupComponents} from './lib/componentMeta'
|
|
330
|
+
export type {ComponentGroup, ComponentEntry, ComponentGrouping} from './lib/componentMeta'
|
|
331
|
+
export {useComponentNames} from './hooks/useComponentNames'
|
|
321
332
|
export {
|
|
322
333
|
buildPagesQuery,
|
|
323
334
|
buildContentsQuery,
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types et transformations pures de l'organisation des composants (groupes +
|
|
3
|
+
* noms marketing). Rien ici ne dépend de React ni de Sanity.
|
|
4
|
+
*/
|
|
5
|
+
import type {BlockTypeInfo} from './schemaTypes'
|
|
6
|
+
|
|
7
|
+
export interface ComponentGroup {
|
|
8
|
+
id: string
|
|
9
|
+
name: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Entrée d'organisation d'un composant, repérée par son TYPE. */
|
|
13
|
+
export interface ComponentEntry {
|
|
14
|
+
/** Type du composant (ex. `section.hero`). */
|
|
15
|
+
type: string
|
|
16
|
+
/** Nom marketing personnalisé (sinon le titre du schéma). */
|
|
17
|
+
name?: string
|
|
18
|
+
/** Id du groupe, ou absent (non groupé). */
|
|
19
|
+
group?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Map type -> nom personnalisé (ignore les vides). */
|
|
23
|
+
export function nameMap(entries: readonly ComponentEntry[]): Map<string, string> {
|
|
24
|
+
const map = new Map<string, string>()
|
|
25
|
+
for (const entry of entries) if (entry.name?.trim()) map.set(entry.type, entry.name.trim())
|
|
26
|
+
return map
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Map type -> id de groupe (ignore les vides). */
|
|
30
|
+
export function groupMap(entries: readonly ComponentEntry[]): Map<string, string> {
|
|
31
|
+
const map = new Map<string, string>()
|
|
32
|
+
for (const entry of entries) if (entry.group) map.set(entry.type, entry.group)
|
|
33
|
+
return map
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Applique le nom marketing au `title` d'un composant (garde `name`/type). */
|
|
37
|
+
export function withCustomNames(blocks: readonly BlockTypeInfo[], names: Map<string, string>): BlockTypeInfo[] {
|
|
38
|
+
return blocks.map((block) => (names.has(block.name) ? {...block, title: names.get(block.name) as string} : block))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface ComponentGrouping {
|
|
42
|
+
/** Groupes dans l'ordre déclaré, avec leurs composants (dans l'ordre d'entrée). */
|
|
43
|
+
grouped: {group: ComponentGroup; blocks: BlockTypeInfo[]}[]
|
|
44
|
+
/** Composants sans groupe. */
|
|
45
|
+
ungrouped: BlockTypeInfo[]
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Répartit les composants par groupe (ordre des groupes préservé). */
|
|
49
|
+
export function groupComponents(
|
|
50
|
+
blocks: readonly BlockTypeInfo[],
|
|
51
|
+
groupOf: Map<string, string>,
|
|
52
|
+
groups: readonly ComponentGroup[],
|
|
53
|
+
): ComponentGrouping {
|
|
54
|
+
const byGroup = new Map<string, BlockTypeInfo[]>()
|
|
55
|
+
const ungrouped: BlockTypeInfo[] = []
|
|
56
|
+
for (const block of blocks) {
|
|
57
|
+
const group = groupOf.get(block.name)
|
|
58
|
+
if (group && groups.some((g) => g.id === group)) {
|
|
59
|
+
const list = byGroup.get(group) ?? []
|
|
60
|
+
list.push(block)
|
|
61
|
+
byGroup.set(group, list)
|
|
62
|
+
} else {
|
|
63
|
+
ungrouped.push(block)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
grouped: groups.map((group) => ({group, blocks: byGroup.get(group.id) ?? []})),
|
|
68
|
+
ungrouped,
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {defineField, defineType} from 'sanity'
|
|
2
2
|
import {TRANSLATION_TODO_TYPE, translationTodoType} from './translationTodo'
|
|
3
3
|
import {ASSET_META_TYPE, assetMetaSchemaTypes} from './assetMeta'
|
|
4
|
+
import {COMPONENT_META_TYPE, componentMetaSchemaTypes} from './componentMeta'
|
|
4
5
|
|
|
5
6
|
/** Type et identifiant du singleton portant la disposition du board. */
|
|
6
7
|
export const BOARD_LAYOUT_TYPE = 'ncfBoardLayout'
|
|
@@ -13,7 +14,7 @@ export const COLUMN_OPTION_TYPE = 'ncfColumnOption'
|
|
|
13
14
|
export const ANNOTATION_TYPE = 'ncfAnnotation'
|
|
14
15
|
|
|
15
16
|
/** Types déclarés par le kit : à masquer de la structure du Studio hôte. */
|
|
16
|
-
export const NCF_HIDDEN_TYPES = [BOARD_LAYOUT_TYPE, TRANSLATION_TODO_TYPE, ASSET_META_TYPE] as const
|
|
17
|
+
export const NCF_HIDDEN_TYPES = [BOARD_LAYOUT_TYPE, TRANSLATION_TODO_TYPE, ASSET_META_TYPE, COMPONENT_META_TYPE] as const
|
|
17
18
|
|
|
18
19
|
const boardNodeType = defineType({
|
|
19
20
|
name: BOARD_NODE_TYPE,
|
|
@@ -187,6 +188,7 @@ export const kitSchemaTypes = [
|
|
|
187
188
|
columnOptionType,
|
|
188
189
|
annotationType,
|
|
189
190
|
...assetMetaSchemaTypes,
|
|
191
|
+
...componentMetaSchemaTypes,
|
|
190
192
|
]
|
|
191
193
|
|
|
192
194
|
/** Vrai pour les types techniques du kit. */
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {defineField, defineType} from 'sanity'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Organisation des composants (types de sections/blocs), propre au kit :
|
|
5
|
+
* nom marketing personnalisé et groupe de chaque composant, et liste des
|
|
6
|
+
* groupes. Document unique, écrit par l'onglet Composants ; jamais édité à la
|
|
7
|
+
* main (absent de « Créer »), comme le board et l'organisation des assets.
|
|
8
|
+
*
|
|
9
|
+
* Les composants sont désignés par leur TYPE (chaîne, ex. `section.hero`),
|
|
10
|
+
* SANS référence Sanity : le nom et le groupe sont de l'affichage, ils ne
|
|
11
|
+
* doivent jamais bloquer quoi que ce soit. Zéro schéma à déclarer côté projet.
|
|
12
|
+
*/
|
|
13
|
+
export const COMPONENT_META_TYPE = 'ncfComponentMeta'
|
|
14
|
+
export const COMPONENT_META_ID = 'ncfComponentMeta'
|
|
15
|
+
export const COMPONENT_GROUP_TYPE = 'ncfComponentGroup'
|
|
16
|
+
export const COMPONENT_ENTRY_TYPE = 'ncfComponentEntry'
|
|
17
|
+
|
|
18
|
+
const componentGroupType = defineType({
|
|
19
|
+
name: COMPONENT_GROUP_TYPE,
|
|
20
|
+
title: 'Component group',
|
|
21
|
+
type: 'object',
|
|
22
|
+
fields: [
|
|
23
|
+
defineField({name: 'id', title: 'Id', type: 'string'}),
|
|
24
|
+
defineField({name: 'name', title: 'Name', type: 'string'}),
|
|
25
|
+
],
|
|
26
|
+
preview: {select: {title: 'name', subtitle: 'id'}},
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const componentEntryType = defineType({
|
|
30
|
+
name: COMPONENT_ENTRY_TYPE,
|
|
31
|
+
title: 'Component organization',
|
|
32
|
+
type: 'object',
|
|
33
|
+
fields: [
|
|
34
|
+
// `_key` de l'entrée = TYPE du composant (ex. `section.hero`).
|
|
35
|
+
defineField({name: 'name', title: 'Custom name', type: 'string'}),
|
|
36
|
+
defineField({name: 'group', title: 'Group', type: 'string'}),
|
|
37
|
+
],
|
|
38
|
+
preview: {select: {title: 'name', subtitle: 'group'}},
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const componentMetaType = defineType({
|
|
42
|
+
name: COMPONENT_META_TYPE,
|
|
43
|
+
title: 'Component organization (NCF)',
|
|
44
|
+
type: 'document',
|
|
45
|
+
fields: [
|
|
46
|
+
defineField({name: 'groups', title: 'Groups', type: 'array', of: [{type: COMPONENT_GROUP_TYPE}]}),
|
|
47
|
+
defineField({name: 'components', title: 'Components', type: 'array', of: [{type: COMPONENT_ENTRY_TYPE}]}),
|
|
48
|
+
defineField({name: 'updatedBy', title: 'Modified by', type: 'string', readOnly: true}),
|
|
49
|
+
],
|
|
50
|
+
preview: {prepare: () => ({title: 'Component organization (NCF)'})},
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
/** Types du singleton des composants, à concaténer dans kitSchemaTypes. */
|
|
54
|
+
export const componentMetaSchemaTypes = [componentMetaType, componentGroupType, componentEntryType]
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {useState} from 'react'
|
|
2
|
-
import {Box, Card, Flex, Stack, Text} from '@sanity/ui'
|
|
2
|
+
import {Box, Button, Card, Flex, Stack, Text} from '@sanity/ui'
|
|
3
|
+
import {EditIcon} from '@sanity/icons/Edit'
|
|
3
4
|
import {blockName, humanizeBlockName} from '../../lib/blocks'
|
|
4
5
|
import {MONO, TABULAR} from '../../ui/tokens'
|
|
5
6
|
import type {BlockOccurrence} from '../../lib/occurrences'
|
|
@@ -12,6 +13,8 @@ export interface BlockCardProps {
|
|
|
12
13
|
thumbnailUrl: string | null
|
|
13
14
|
/** Au clic : rendu réel du composant et réglage de ses valeurs par défaut. */
|
|
14
15
|
onOpen?: () => void
|
|
16
|
+
/** Renommer / ranger dans un groupe. Masqué si absent. */
|
|
17
|
+
onEdit?: () => void
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
/**
|
|
@@ -23,21 +26,26 @@ export interface BlockCardProps {
|
|
|
23
26
|
* à le lire.
|
|
24
27
|
*/
|
|
25
28
|
export function BlockCard(props: BlockCardProps) {
|
|
26
|
-
const {block, occurrence, namespace, thumbnailUrl, onOpen} = props
|
|
29
|
+
const {block, occurrence, namespace, thumbnailUrl, onOpen, onEdit} = props
|
|
27
30
|
const [failed, setFailed] = useState(false)
|
|
28
31
|
const unused = occurrence.count === 0
|
|
29
32
|
|
|
30
33
|
return (
|
|
31
|
-
<Card
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
<Card radius={3} shadow={1} padding={0} style={{height: '100%', overflow: 'hidden', position: 'relative'}}>
|
|
35
|
+
{onEdit ? (
|
|
36
|
+
<Box style={{position: 'absolute', top: 6, right: 6, zIndex: 1}} onClick={(event) => event.stopPropagation()}>
|
|
37
|
+
<Card radius={2} shadow={1}>
|
|
38
|
+
<Button icon={EditIcon} mode="bleed" padding={2} fontSize={1} title="Renommer / ranger" aria-label="Renommer / ranger" onClick={onEdit} />
|
|
39
|
+
</Card>
|
|
40
|
+
</Box>
|
|
41
|
+
) : null}
|
|
42
|
+
<Stack
|
|
43
|
+
gap={0}
|
|
44
|
+
as={onOpen ? 'button' : undefined}
|
|
45
|
+
onClick={onOpen}
|
|
46
|
+
title={onOpen ? 'See the real component and set its default values' : undefined}
|
|
47
|
+
style={{height: '100%', width: '100%', textAlign: 'left', cursor: onOpen ? 'pointer' : undefined, background: 'transparent', border: 0, padding: 0}}
|
|
48
|
+
>
|
|
41
49
|
<Box
|
|
42
50
|
style={{
|
|
43
51
|
aspectRatio: '16 / 10',
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {useState} from 'react'
|
|
2
|
+
import {Box, Button, Dialog, Flex, Label, Select, Stack, Text, TextInput} from '@sanity/ui'
|
|
3
|
+
import {blockName, humanizeBlockName} from '../../lib/blocks'
|
|
4
|
+
import type {BlockTypeInfo} from '../../lib/schemaTypes'
|
|
5
|
+
import type {ComponentGroup} from '../../lib/componentMeta'
|
|
6
|
+
import {MONO} from '../../ui/tokens'
|
|
7
|
+
|
|
8
|
+
export interface ComponentEditDialogProps {
|
|
9
|
+
block: BlockTypeInfo
|
|
10
|
+
namespace: string
|
|
11
|
+
currentName: string
|
|
12
|
+
currentGroup: string | null
|
|
13
|
+
groups: ComponentGroup[]
|
|
14
|
+
onSave: (name: string | null, group: string | null) => void
|
|
15
|
+
onClose: () => void
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Renommer un composant (nom marketing) et le ranger dans un groupe. */
|
|
19
|
+
export function ComponentEditDialog(props: ComponentEditDialogProps) {
|
|
20
|
+
const {block, namespace, currentName, currentGroup, groups, onSave, onClose} = props
|
|
21
|
+
const [name, setName] = useState(currentName)
|
|
22
|
+
const [group, setGroup] = useState(currentGroup ?? '')
|
|
23
|
+
|
|
24
|
+
const save = () => {
|
|
25
|
+
onSave(name.trim() || null, group || null)
|
|
26
|
+
onClose()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<Dialog
|
|
31
|
+
id="component-edit"
|
|
32
|
+
header="Renommer / ranger le composant"
|
|
33
|
+
onClose={onClose}
|
|
34
|
+
width={0}
|
|
35
|
+
footer={
|
|
36
|
+
<Flex justify="flex-end" gap={2} padding={2}>
|
|
37
|
+
<Button text="Annuler" mode="bleed" onClick={onClose} />
|
|
38
|
+
<Button text="Enregistrer" tone="primary" onClick={save} />
|
|
39
|
+
</Flex>
|
|
40
|
+
}
|
|
41
|
+
>
|
|
42
|
+
<Box padding={4}>
|
|
43
|
+
<Stack gap={4}>
|
|
44
|
+
<Text size={0} muted style={MONO}>
|
|
45
|
+
{blockName(block.name, namespace)}
|
|
46
|
+
</Text>
|
|
47
|
+
|
|
48
|
+
<Stack gap={2}>
|
|
49
|
+
<Label size={0} muted>
|
|
50
|
+
Nom affiché
|
|
51
|
+
</Label>
|
|
52
|
+
<TextInput
|
|
53
|
+
value={name}
|
|
54
|
+
autoFocus
|
|
55
|
+
placeholder={humanizeBlockName(block.name, namespace)}
|
|
56
|
+
onChange={(event) => setName(event.currentTarget.value)}
|
|
57
|
+
onKeyDown={(event) => {
|
|
58
|
+
if (event.key === 'Enter') save()
|
|
59
|
+
}}
|
|
60
|
+
/>
|
|
61
|
+
<Text size={0} muted>
|
|
62
|
+
Ce nom remplace le titre partout (onglet, board, menu d'insertion). Vide = titre d'origine.
|
|
63
|
+
</Text>
|
|
64
|
+
</Stack>
|
|
65
|
+
|
|
66
|
+
<Stack gap={2}>
|
|
67
|
+
<Label size={0} muted>
|
|
68
|
+
Groupe
|
|
69
|
+
</Label>
|
|
70
|
+
<Select value={group} onChange={(event) => setGroup(event.currentTarget.value)}>
|
|
71
|
+
<option value="">Non groupé</option>
|
|
72
|
+
{groups.map((g) => (
|
|
73
|
+
<option key={g.id} value={g.id}>
|
|
74
|
+
{g.name}
|
|
75
|
+
</option>
|
|
76
|
+
))}
|
|
77
|
+
</Select>
|
|
78
|
+
</Stack>
|
|
79
|
+
</Stack>
|
|
80
|
+
</Box>
|
|
81
|
+
</Dialog>
|
|
82
|
+
)
|
|
83
|
+
}
|