@nocode-factory/sanity-kit 2.54.0 → 2.58.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/README.md +61 -6
- package/bin/item-types.mjs +131 -0
- package/bin/ncf-kit.mjs +97 -0
- package/dist/index.d.ts +48 -4
- package/dist/index.js +7222 -6916
- package/dist/index.js.map +1 -1
- package/dist/inherited-DZvM35P6.js +101 -0
- package/dist/inherited-DZvM35P6.js.map +1 -0
- package/dist/protocol-Bl7Yj2Ro.js +31 -0
- package/dist/protocol-Bl7Yj2Ro.js.map +1 -0
- package/dist/web-edit.d.ts +192 -0
- package/dist/web-edit.js +144 -0
- package/dist/web-edit.js.map +1 -0
- package/dist/web-editing.d.ts +51 -0
- package/dist/web-editing.js +693 -0
- package/dist/web-editing.js.map +1 -0
- package/package.json +22 -2
- package/scripts/use-client.mjs +1 -1
- package/src/form/AssetSourceBrowser.tsx +68 -39
- package/src/hooks/useCollection.ts +19 -3
- package/src/index.ts +3 -2
- package/src/lib/collectionQuery.ts +10 -2
- package/src/structure/collections.tsx +35 -5
- package/src/tools/collections/CollectionTable.tsx +295 -89
- package/src/tools/collections/CollectionToolbar.tsx +53 -4
- package/src/ui/PreviewEditBridge.tsx +74 -0
- package/src/version.ts +1 -1
- package/src/web/edit/ImageReplaceLayer.tsx +174 -0
- package/src/web/edit/InlineTextEditor.tsx +365 -0
- package/src/web/edit/InlineTextOverlay.tsx +43 -0
- package/src/web/edit/PreviewEditing.tsx +191 -0
- package/src/web/edit/index.ts +18 -0
- package/src/web/edit/inherited.ts +174 -0
- package/src/web/edit/labels.ts +37 -0
- package/src/web/edit/protocol.ts +47 -0
- package/src/web/edit/store.ts +98 -0
- package/src/web/edit/targets.ts +186 -0
- package/src/web/editing.ts +18 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import {createDataAttribute} from '@sanity/visual-editing/create-data-attribute'
|
|
2
|
+
import {resolveEditInfo, studioPath, type ContentSourceMap, type ContentSourceMapParsedPath} from '@sanity/client/csm'
|
|
3
|
+
import {vercelStegaClean} from '@vercel/stega'
|
|
4
|
+
import {retagInheritedList, type InheritedEdit} from './inherited'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* ÉDITION DANS L'APERÇU, IMAGES (mode brouillon seulement) : chaque image est
|
|
8
|
+
* cliquable dans Presentation, alt rempli ou non, et porte son document et son
|
|
9
|
+
* chemin EXACTS pour la pastille « Replace this image » (`NcfPreviewEditing`).
|
|
10
|
+
*
|
|
11
|
+
* Sanity ne repère d'ordinaire une image que par son alt (texte encodé en stega) :
|
|
12
|
+
* une image sans alt n'avait aucun overlay. Ici, la carte des sources (Content
|
|
13
|
+
* Source Map, renvoyée par `sanityFetch` en brouillon) donne pour chaque image son
|
|
14
|
+
* document et son champ ; on les attache à la valeur (`_editTarget`, `_editPath`,
|
|
15
|
+
* `_editDoc`, `_editType`), que le composant pose avec `imageEditAttributes`.
|
|
16
|
+
*
|
|
17
|
+
* Câblage d'un projet (starter) :
|
|
18
|
+
* - `annotateImageEditTargets(data, sourceMap, {studioUrl})` sur le résultat de
|
|
19
|
+
* `sanityFetch` en brouillon ;
|
|
20
|
+
* - `withPreviewEditTargets(reattributeTo(source, key), source, key, collected, {studioUrl})`
|
|
21
|
+
* comme rappel d'héritage de `applyDefaults` ;
|
|
22
|
+
* - `rekeyOwnImageTargets(section, source, key, {studioUrl})` sur chaque section ;
|
|
23
|
+
* - `imageEditAttributes(image)` / `imageAlt(image)` sur chaque image affichée.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
export type ImageEditValue = {
|
|
27
|
+
asset?: {_ref?: string}
|
|
28
|
+
alt?: string
|
|
29
|
+
_editTarget?: string
|
|
30
|
+
_editPath?: string
|
|
31
|
+
_editDoc?: string
|
|
32
|
+
_editType?: string
|
|
33
|
+
} | null | undefined
|
|
34
|
+
|
|
35
|
+
type Marked = {asset: {_ref: string}; _editTarget?: string; _editPath?: string; _editDoc?: string; _editType?: string}
|
|
36
|
+
type Segment = ContentSourceMapParsedPath[number]
|
|
37
|
+
|
|
38
|
+
function isImageValue(value: unknown): value is Marked {
|
|
39
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return false
|
|
40
|
+
const ref = (value as {asset?: {_ref?: unknown}}).asset?._ref
|
|
41
|
+
return typeof ref === 'string' && ref.startsWith('image-')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Pose le repère Sanity et le chemin exact. Image élément de liste
|
|
46
|
+
* (`…images[_key=="i3"]`) : Sanity n'active pas un repère placé dans un élément de
|
|
47
|
+
* liste d'images ; le repère vise la liste, le chemin exact reste dans `_editPath`.
|
|
48
|
+
*/
|
|
49
|
+
function mark(value: Marked, id: string, type: string, path: string, extra: Record<string, string | undefined> & {baseUrl: string}) {
|
|
50
|
+
const inList = /\[[^\]]+\]$/.test(path)
|
|
51
|
+
value._editTarget = createDataAttribute({...extra, id, type, path: inList ? path.replace(/\[[^\]]+\]$/, '') : path}).toString()
|
|
52
|
+
value._editPath = path
|
|
53
|
+
value._editDoc = id.replace(/^drafts\./, '')
|
|
54
|
+
value._editType = type
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type ImageEditOptions = {
|
|
58
|
+
/** Adresse du Studio (celle de `stega.studioUrl`). */
|
|
59
|
+
studioUrl: string
|
|
60
|
+
/** Type du document des valeurs par défaut des composants (jamais marqué). */
|
|
61
|
+
defaultsType?: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Marque chaque image du résultat d'une requête en brouillon. Une image venue du
|
|
66
|
+
* document des valeurs par défaut n'est pas marquée ici (un clic modifierait
|
|
67
|
+
* l'image de TOUTES les pages) : `withPreviewEditTargets` la fait viser l'instance.
|
|
68
|
+
*/
|
|
69
|
+
export function annotateImageEditTargets<T>(data: T, sourceMap: ContentSourceMap | null | undefined, options: ImageEditOptions): T {
|
|
70
|
+
if (!sourceMap) return data
|
|
71
|
+
const defaultsType = options.defaultsType ?? 'componentDefaults'
|
|
72
|
+
const fromSourceMap = (value: Marked, path: ContentSourceMapParsedPath) => {
|
|
73
|
+
const info = resolveEditInfo({studioUrl: options.studioUrl, resultSourceMap: sourceMap, resultPath: [...path, 'asset', '_ref']})
|
|
74
|
+
if (!info || info.type === defaultsType) return
|
|
75
|
+
const full = typeof info.path === 'string' ? info.path : studioPath.toString(info.path as never)
|
|
76
|
+
const imagePath = full.replace(/\.asset\._ref$/, '')
|
|
77
|
+
if (!imagePath || imagePath === full) return
|
|
78
|
+
mark(value, info.id, info.type, imagePath, {
|
|
79
|
+
baseUrl: info.baseUrl,
|
|
80
|
+
workspace: info.workspace,
|
|
81
|
+
tool: info.tool,
|
|
82
|
+
projectId: info.projectId,
|
|
83
|
+
dataset: info.dataset,
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
const visit = (value: unknown, path: ContentSourceMapParsedPath) => {
|
|
87
|
+
if (Array.isArray(value)) {
|
|
88
|
+
value.forEach((item, index) => {
|
|
89
|
+
const key = item && typeof item === 'object' && typeof (item as {_key?: unknown})._key === 'string' ? (item as {_key: string})._key : null
|
|
90
|
+
const segment: Segment = key ? {_key: key, _index: index} : index
|
|
91
|
+
visit(item, [...path, segment])
|
|
92
|
+
})
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
if (!value || typeof value !== 'object') return
|
|
96
|
+
if (isImageValue(value) && !value._editTarget) fromSourceMap(value, path)
|
|
97
|
+
for (const [key, child] of Object.entries(value)) {
|
|
98
|
+
if (key.startsWith('_edit')) continue
|
|
99
|
+
visit(child, [...path, key])
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
visit(data, [])
|
|
103
|
+
return data
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
type OnInherit = (value: unknown, path: string[]) => unknown
|
|
107
|
+
type Source = {id: string; type: string; path: string; single?: boolean}
|
|
108
|
+
|
|
109
|
+
const sectionBase = (source: Source, key: string | undefined) => (key && !source.single ? `${source.path}[_key=="${key}"]` : source.path)
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Rappel d'héritage pour `applyDefaults` (en aperçu), autour de `reattributeTo` :
|
|
113
|
+
* - image héritée : vise le champ image de L'INSTANCE (choisir une image crée une
|
|
114
|
+
* image propre à la page, la valeur par défaut ne change pas) ;
|
|
115
|
+
* - texte hérité : sa valeur par défaut exacte est jointe (`collected`) ;
|
|
116
|
+
* - liste simple ou texte riche hérité : modifiable ligne à ligne (voir `inherited.ts`).
|
|
117
|
+
* `collected` est à rendre par la section avec `inheritedEditScriptProps`.
|
|
118
|
+
*/
|
|
119
|
+
export function withPreviewEditTargets(onInherit: OnInherit, source: Source, key: string | undefined, collected: InheritedEdit[], options: ImageEditOptions): OnInherit {
|
|
120
|
+
const base = sectionBase(source, key)
|
|
121
|
+
const pageId = source.id.replace(/^drafts\./, '')
|
|
122
|
+
const markInherited = (value: unknown, path: string) => {
|
|
123
|
+
if (Array.isArray(value) || !value || typeof value !== 'object') return
|
|
124
|
+
if (isImageValue(value)) {
|
|
125
|
+
mark(value, pageId, source.type, path, {baseUrl: options.studioUrl})
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
for (const [k, child] of Object.entries(value)) if (!k.startsWith('_')) markInherited(child, `${path}.${k}`)
|
|
129
|
+
}
|
|
130
|
+
return (value, path) => {
|
|
131
|
+
const out = onInherit(value, path)
|
|
132
|
+
if (out && typeof out === 'object' && !Array.isArray(out)) {
|
|
133
|
+
const copy = isImageValue(out) ? {...out} : out
|
|
134
|
+
markInherited(copy, `${base}.${path.join('.')}`)
|
|
135
|
+
return copy
|
|
136
|
+
}
|
|
137
|
+
if (typeof out === 'string') collected.push({path: `${base}.${path.join('.')}`, value: vercelStegaClean(out)})
|
|
138
|
+
// Liste héritée directement par un champ de la section (le parent existe dans la page).
|
|
139
|
+
if (Array.isArray(out) && path.length === 1) return retagInheritedList(out, `${base}.${path[0]}`, source, collected)
|
|
140
|
+
return out
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Chemin EXACT des images qui appartiennent à la page, recalculé avec les clés
|
|
146
|
+
* `_key` de la section. La carte des sources donne parfois un rang au lieu de la
|
|
147
|
+
* clé (`images:0`) : Sanity ne retrouve alors pas le champ. Une image d'un AUTRE
|
|
148
|
+
* document (carte liée, témoignage...) garde son repère.
|
|
149
|
+
*/
|
|
150
|
+
export function rekeyOwnImageTargets<T>(section: T, source: Source, key: string | undefined, options: ImageEditOptions): T {
|
|
151
|
+
const pageId = source.id.replace(/^drafts\./, '')
|
|
152
|
+
const visit = (value: unknown, path: string) => {
|
|
153
|
+
if (Array.isArray(value)) {
|
|
154
|
+
value.forEach((item, index) => {
|
|
155
|
+
const k = item && typeof item === 'object' ? (item as {_key?: unknown})._key : undefined
|
|
156
|
+
visit(item, typeof k === 'string' ? `${path}[_key=="${k}"]` : `${path}[${index}]`)
|
|
157
|
+
})
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
if (!value || typeof value !== 'object') return
|
|
161
|
+
if (isImageValue(value) && value._editDoc) {
|
|
162
|
+
if (value._editDoc === pageId) mark(value, pageId, source.type, path, {baseUrl: options.studioUrl})
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
for (const [k, child] of Object.entries(value)) if (!k.startsWith('_')) visit(child, `${path}.${k}`)
|
|
166
|
+
}
|
|
167
|
+
visit(section, sectionBase(source, key))
|
|
168
|
+
return section
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export type ImageEditAttributes = {'data-sanity'?: string; 'data-edit-image'?: string; 'data-edit-doc'?: string; 'data-edit-type'?: string}
|
|
172
|
+
|
|
173
|
+
/** Attributs à poser sur le cadre d'une image marquée ; vide pour les visiteurs. */
|
|
174
|
+
export function imageEditAttributes(image: ImageEditValue): ImageEditAttributes {
|
|
175
|
+
if (!image?._editTarget || !image._editPath || !image._editDoc || !image._editType) return {}
|
|
176
|
+
return {'data-sanity': image._editTarget, 'data-edit-image': image._editPath, 'data-edit-doc': image._editDoc, 'data-edit-type': image._editType}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Alt d'une image marquée : nettoyé de ses marqueurs (sinon Sanity la prendrait
|
|
181
|
+
* pour un texte et le clic ouvrirait l'alt au lieu de l'image).
|
|
182
|
+
*/
|
|
183
|
+
export function imageAlt(image: ImageEditValue, alt?: string): string {
|
|
184
|
+
const value = alt ?? image?.alt ?? ''
|
|
185
|
+
return image?._editTarget ? vercelStegaClean(value) : value
|
|
186
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ÉDITION SUR PLACE dans l'aperçu (Presentation), point d'entrée **client** :
|
|
3
|
+
*
|
|
4
|
+
* ```tsx
|
|
5
|
+
* import {NcfPreviewEditing, ncfOverlayComponents} from '@nocode-factory/sanity-kit/web/editing'
|
|
6
|
+
*
|
|
7
|
+
* <VisualEditing refresh={useNcfPreviewRefresh()} components={ncfOverlayComponents} />
|
|
8
|
+
* <NcfPreviewEditing labels={messages.previewEditing} />
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* Séparé de `/web` : il dépend de `@sanity/visual-editing` et `@sanity/mutate`
|
|
12
|
+
* (fournis par next-sanity), qu'un projet sans édition visuelle n'a pas à installer.
|
|
13
|
+
* Côté données (requêtes, sections, images) : `@nocode-factory/sanity-kit/web/edit`.
|
|
14
|
+
*/
|
|
15
|
+
export {NcfPreviewEditing, ncfOverlayComponents} from './edit/PreviewEditing'
|
|
16
|
+
export type {NcfPreviewEditingProps} from './edit/PreviewEditing'
|
|
17
|
+
export {DEFAULT_PREVIEW_EDITING_LABELS} from './edit/labels'
|
|
18
|
+
export type {PreviewEditingLabels} from './edit/labels'
|