@sanity/hierarchical-document-list 1.0.0 → 2.0.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/LICENSE +1 -1
- package/README.md +365 -297
- package/dist/index.d.ts +240 -0
- package/dist/index.esm.js +18 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/package.json +94 -55
- package/sanity.json +8 -12
- package/src/TreeDeskStructure.tsx +80 -0
- package/src/TreeInputComponent.tsx +41 -0
- package/src/components/DeskWarning.tsx +40 -0
- package/src/components/DocumentInNode.tsx +133 -0
- package/src/components/DocumentPreviewStatus.tsx +70 -0
- package/src/components/NodeActions.tsx +85 -0
- package/src/components/NodeContentRenderer.tsx +141 -0
- package/src/components/PlaceholderDropzone.tsx +45 -0
- package/src/components/TreeEditor.tsx +167 -0
- package/src/components/TreeEditorErrorBoundary.tsx +14 -0
- package/src/components/TreeNodeRenderer.tsx +37 -0
- package/src/components/TreeNodeRendererScaffold.tsx +193 -0
- package/src/createDeskHierarchy.tsx +110 -0
- package/src/createHierarchicalSchemas.tsx +151 -0
- package/src/hooks/useAllItems.ts +119 -0
- package/src/hooks/useLocalTree.ts +40 -0
- package/src/hooks/useTreeOperations.ts +25 -0
- package/src/hooks/useTreeOperationsProvider.ts +86 -0
- package/src/index.ts +25 -0
- package/src/schemas/hierarchy.tree.ts +19 -0
- package/src/types.ts +148 -0
- package/src/utils/flatDataToTree.ts +20 -0
- package/src/utils/getAdjescentNodes.ts +30 -0
- package/src/utils/getCommonTreeProps.tsx +28 -0
- package/src/utils/getTreeHeight.ts +10 -0
- package/src/utils/gradientPatchAdapter.ts +43 -0
- package/src/utils/idUtils.ts +7 -0
- package/src/utils/injectNodeTypeInPatches.ts +60 -0
- package/src/utils/moveItemInArray.ts +26 -0
- package/src/utils/throwError.ts +9 -0
- package/src/utils/treeData.tsx +119 -0
- package/src/utils/treePatches.ts +171 -0
- package/v2-incompatible.js +11 -0
- package/lib/TreeDeskStructure.d.ts +0 -8
- package/lib/TreeDeskStructure.js +0 -96
- package/lib/TreeInputComponent.d.ts +0 -19
- package/lib/TreeInputComponent.js +0 -52
- package/lib/components/DeskWarning.d.ts +0 -6
- package/lib/components/DeskWarning.js +0 -46
- package/lib/components/DocumentInNode.d.ts +0 -11
- package/lib/components/DocumentInNode.js +0 -82
- package/lib/components/DocumentPreviewStatus.d.ts +0 -7
- package/lib/components/DocumentPreviewStatus.js +0 -39
- package/lib/components/NodeActions.d.ts +0 -10
- package/lib/components/NodeActions.js +0 -61
- package/lib/components/NodeContentRenderer.d.ts +0 -8
- package/lib/components/NodeContentRenderer.js +0 -105
- package/lib/components/PlaceholderDropzone.d.ts +0 -9
- package/lib/components/PlaceholderDropzone.js +0 -30
- package/lib/components/TreeEditor.d.ts +0 -12
- package/lib/components/TreeEditor.js +0 -59
- package/lib/components/TreeEditorErrorBoundary.d.ts +0 -3
- package/lib/components/TreeEditorErrorBoundary.js +0 -59
- package/lib/components/TreeNodeRenderer.d.ts +0 -3
- package/lib/components/TreeNodeRenderer.js +0 -59
- package/lib/components/TreeNodeRendererScaffold.d.ts +0 -4
- package/lib/components/TreeNodeRendererScaffold.js +0 -44
- package/lib/createDeskHierarchy.d.ts +0 -14
- package/lib/createDeskHierarchy.js +0 -85
- package/lib/createHierarchicalSchemas.d.ts +0 -78
- package/lib/createHierarchicalSchemas.js +0 -138
- package/lib/hooks/useAllItems.d.ts +0 -7
- package/lib/hooks/useAllItems.js +0 -119
- package/lib/hooks/useLocalTree.d.ts +0 -17
- package/lib/hooks/useLocalTree.js +0 -59
- package/lib/hooks/useTreeOperations.d.ts +0 -9
- package/lib/hooks/useTreeOperations.js +0 -39
- package/lib/hooks/useTreeOperationsProvider.d.ts +0 -14
- package/lib/hooks/useTreeOperationsProvider.js +0 -85
- package/lib/index.d.ts +0 -3
- package/lib/index.js +0 -12
- package/lib/schemas/hierarchy.tree.d.ts +0 -13
- package/lib/schemas/hierarchy.tree.js +0 -19
- package/lib/utils/flatDataToTree.d.ts +0 -6
- package/lib/utils/flatDataToTree.js +0 -26
- package/lib/utils/getAdjescentNodes.d.ts +0 -12
- package/lib/utils/getAdjescentNodes.js +0 -19
- package/lib/utils/getCommonTreeProps.d.ts +0 -7
- package/lib/utils/getCommonTreeProps.js +0 -33
- package/lib/utils/getTreeHeight.d.ts +0 -3
- package/lib/utils/getTreeHeight.js +0 -11
- package/lib/utils/gradientPatchAdapter.d.ts +0 -4
- package/lib/utils/gradientPatchAdapter.js +0 -42
- package/lib/utils/idUtils.d.ts +0 -2
- package/lib/utils/idUtils.js +0 -13
- package/lib/utils/injectNodeTypeInPatches.d.ts +0 -12
- package/lib/utils/injectNodeTypeInPatches.js +0 -58
- package/lib/utils/moveItemInArray.d.ts +0 -5
- package/lib/utils/moveItemInArray.js +0 -26
- package/lib/utils/throwError.d.ts +0 -7
- package/lib/utils/throwError.js +0 -12
- package/lib/utils/treeData.d.ts +0 -18
- package/lib/utils/treeData.js +0 -118
- package/lib/utils/treePatches.d.ts +0 -15
- package/lib/utils/treePatches.js +0 -171
- package/screenshot-1.jpg +0 -0
- package/tsconfig.json +0 -20
package/README.md
CHANGED
|
@@ -1,297 +1,365 @@
|
|
|
1
|
-
# sanity-plugin-hierarchical-document-list
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
//
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
"
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
"
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
"
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
},
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
//
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
1
|
+
# sanity-plugin-hierarchical-document-list
|
|
2
|
+
|
|
3
|
+
> This is a **Sanity Studio v3** plugin.
|
|
4
|
+
|
|
5
|
+
Plugin for visually organizing documents as hierarchies in the [Sanity studio](https://www.sanity.io/docs/sanity-studio). Applications include:
|
|
6
|
+
|
|
7
|
+
- Tables of content - such as a book's sections and chapters
|
|
8
|
+
- Navigational structure & menus - a website mega-menu with multiple levels, for example
|
|
9
|
+
- Taxonomy inheritance - "_Carbs_ is a parent of _Legumes_ which is a parent of _Beans_"
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
⚠️ **Compatibility:** This plugin requires Sanity Studio [version 3.3.0](https://github.com/sanity-io/sanity/releases/tag/v3.3.0) or higher.
|
|
14
|
+
|
|
15
|
+
If you're looking for a way to order documents on a flat list, refer to [@sanity/orderable-document-list](https://github.com/sanity-io/orderable-document-list).
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# From the root of your sanity project
|
|
23
|
+
npm i @sanity/hierarchical-document-list
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### 1. Add the plugin and the default documentType to the sanity.config.ts
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
// sanity.config.js
|
|
32
|
+
import {defineConfig} from 'sanity'
|
|
33
|
+
import {hierarchicalDocumentList, hierarchyTree} from '@sanity/hierarchical-document-list'
|
|
34
|
+
|
|
35
|
+
export default defineConfig({
|
|
36
|
+
// ...
|
|
37
|
+
plugins: [hierarchicalDocumentList()],
|
|
38
|
+
schema: {
|
|
39
|
+
types: [
|
|
40
|
+
//...,
|
|
41
|
+
hierarchyTree
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 2. Add one or more hierarchy documents to your Structure Builder.
|
|
49
|
+
|
|
50
|
+
💡 _To learn about custom desk structures, refer to the [Structure Builder docs](https://www.sanity.io/docs/overview-structure-builder)._
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// sanity.config.ts
|
|
54
|
+
import {defineConfig} from 'sanity'
|
|
55
|
+
import {deskTool} from 'sanity/desk'
|
|
56
|
+
import {createDeskHierarchy, hierarchicalDocumentList, hierarchyTree} from '@sanity/hierarchical-document-list'
|
|
57
|
+
|
|
58
|
+
export default defineConfig({
|
|
59
|
+
// ...
|
|
60
|
+
plugins: [
|
|
61
|
+
deskTool({
|
|
62
|
+
// NOTE: I'n V3 you MUST pass S and Context along to createDeskHierarchy as props
|
|
63
|
+
structure: (S, context) => S.list()
|
|
64
|
+
.title('Content')
|
|
65
|
+
.items([
|
|
66
|
+
...S.documentTypeListItems(), // or whatever other structure you have
|
|
67
|
+
createDeskHierarchy({
|
|
68
|
+
//prop drill S and context:
|
|
69
|
+
S,
|
|
70
|
+
context,
|
|
71
|
+
|
|
72
|
+
//configure plugin
|
|
73
|
+
|
|
74
|
+
title: 'Main table of contents',
|
|
75
|
+
|
|
76
|
+
// The hierarchy will be stored in this document ID 👇
|
|
77
|
+
documentId: 'main-table-of-contents',
|
|
78
|
+
|
|
79
|
+
// Document types editors should be able to include in the hierarchy
|
|
80
|
+
referenceTo: ['site.page', 'site.post', 'docs.article', 'social.youtubeVideo'],
|
|
81
|
+
|
|
82
|
+
// ❓ Optional: provide filters and/or parameters for narrowing which documents can be added
|
|
83
|
+
referenceOptions: {
|
|
84
|
+
filter: 'status in $acceptedStatuses',
|
|
85
|
+
filterParams: {
|
|
86
|
+
acceptedStatuses: ['published', 'approved']
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
// ❓ Optional: limit the depth of your hierarachies
|
|
91
|
+
maxDepth: 3,
|
|
92
|
+
|
|
93
|
+
// ❓ Optional: subarray of referenceTo, when it should not be possible to create new types from all referenceTo types
|
|
94
|
+
creatableTypes: ['site.page']
|
|
95
|
+
})
|
|
96
|
+
])
|
|
97
|
+
}),
|
|
98
|
+
hierarchicalDocumentList()
|
|
99
|
+
],
|
|
100
|
+
schema: {
|
|
101
|
+
types: [
|
|
102
|
+
//...,
|
|
103
|
+
hierarchyTree
|
|
104
|
+
]
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## How it works
|
|
110
|
+
|
|
111
|
+
The hierarchical data is stored in a centralized document with the `documentId` of your choosing. As compared to storing parent/child relationships in each individual document in the hierarchy, this makes it easier to implement different hierarchies for the same content according to the context.
|
|
112
|
+
|
|
113
|
+
This approach also simplifies querying the full structure - as you'll see in [querying data](#querying-data) below.
|
|
114
|
+
|
|
115
|
+
Keep in mind that this specified **document is live-edited**, meaning it has no draft and every change by editors will directly affect its published version.
|
|
116
|
+
|
|
117
|
+
Instead of requiring editors to manually add items one-by-one, the plugin will create a [GROQ](https://www.sanity.io/docs/overview-groq) query that matches all documents with a `_type` in the `referenceTo` option you specify, that also match the optional `referenceOptions.filter`. From these documents, editors are able to drag, nest and re-order them at will from the "Add more items" list.
|
|
118
|
+
|
|
119
|
+
If a document in the tree doesn't match the filters set, it'll keep existing in the data. This can happen if the document has a new, unfitting value, the configuration changed or it was deleted. Although the tree will still be publishable, editors will get a warning and won't be able to drag these faulty entries around.
|
|
120
|
+
|
|
121
|
+
## Querying data
|
|
122
|
+
|
|
123
|
+
The plugin stores flat arrays which represent your hierarchical data through `parent` keys. Here's an example of one top-level item with one child:
|
|
124
|
+
|
|
125
|
+
```json
|
|
126
|
+
[
|
|
127
|
+
{
|
|
128
|
+
"_key": "741b9edde2ba",
|
|
129
|
+
"_type": "hierarchy.node",
|
|
130
|
+
"value": {
|
|
131
|
+
"reference": {
|
|
132
|
+
"_ref": "75c47994-e6bb-487a-b8c9-b283f2436031",
|
|
133
|
+
"_type": "reference",
|
|
134
|
+
"_weak": true // This plugin includes weak references by default
|
|
135
|
+
},
|
|
136
|
+
"docType": "docs.article"
|
|
137
|
+
}
|
|
138
|
+
// no `parent`, this item is top-level
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
"_key": "f92eaeec96f7",
|
|
142
|
+
"_type": "hierarchy.node",
|
|
143
|
+
"value": {
|
|
144
|
+
"reference": {
|
|
145
|
+
"_ref": "7ad60a02-5d6e-47d8-92e2-6724cc130058",
|
|
146
|
+
"_type": "reference",
|
|
147
|
+
"_weak": true
|
|
148
|
+
},
|
|
149
|
+
"docType": "site.post"
|
|
150
|
+
},
|
|
151
|
+
// The `parent` property points to the _key of the parent node where this one is nested
|
|
152
|
+
"parent": "741b9edde2ba"
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
📌 If using GraphQL, refer to [Usage with GraphQL](#usage-with-graphql).
|
|
158
|
+
|
|
159
|
+
From the the above, we know how to expand referenced documents in GROQ:
|
|
160
|
+
|
|
161
|
+
```groq
|
|
162
|
+
*[_id == "main-table-of-contents"][0]{
|
|
163
|
+
tree[] {
|
|
164
|
+
// Make sure you include each item's _key and parent
|
|
165
|
+
_key,
|
|
166
|
+
parent,
|
|
167
|
+
|
|
168
|
+
// "Expand" the reference to the node
|
|
169
|
+
value {
|
|
170
|
+
reference->{
|
|
171
|
+
// Get whatever property you need from your documents
|
|
172
|
+
title,
|
|
173
|
+
slug,
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
The query above will then need to be converted from flat data to a tree. Refer to [Using the data](#using-the-data).
|
|
181
|
+
|
|
182
|
+
<!-- ### Other query scenarios
|
|
183
|
+
|
|
184
|
+
Find a given document in a hierarchy and get its parent - useful for rendering breadcrumbs:
|
|
185
|
+
|
|
186
|
+
```groq
|
|
187
|
+
// Works starting from Content Lake V2021-03-25
|
|
188
|
+
*[_id == "main-table-of-contents"][0]{
|
|
189
|
+
// From the tree, get the 1st node that references a given document _id
|
|
190
|
+
tree[node._ref == "my-book-section"][0] {
|
|
191
|
+
_key,
|
|
192
|
+
"section": node.reference->{
|
|
193
|
+
title,
|
|
194
|
+
},
|
|
195
|
+
// Then, from the tree get the element matching the `parent` _key of the found node
|
|
196
|
+
"parentChapter": ^.tree[_key == ^.parent][0]{
|
|
197
|
+
_key,
|
|
198
|
+
"chapter": node.reference->{
|
|
199
|
+
title,
|
|
200
|
+
contributors,
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
---- -->
|
|
208
|
+
|
|
209
|
+
## Using the data
|
|
210
|
+
|
|
211
|
+
From the flat data queried, you'll need to convert it to a nested tree with `flatDataToTree`:
|
|
212
|
+
|
|
213
|
+
```js
|
|
214
|
+
import {flatDataToTree} from '@sanity/hierarchical-document-list'
|
|
215
|
+
|
|
216
|
+
const hierarchyDocument = await client.fetch(`*[_id == "book-v3-review-a"][0]{
|
|
217
|
+
tree[] {
|
|
218
|
+
// Make sure you include each item's _key and parent
|
|
219
|
+
_key,
|
|
220
|
+
parent,
|
|
221
|
+
value {
|
|
222
|
+
reference->{
|
|
223
|
+
title,
|
|
224
|
+
slug,
|
|
225
|
+
content,
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}`)
|
|
230
|
+
const tree = flatDataToTree(data.tree)
|
|
231
|
+
|
|
232
|
+
/* Results in a recursively nested structure. Using the example data above:
|
|
233
|
+
{
|
|
234
|
+
"_key": "741b9edde2ba",
|
|
235
|
+
"_type": "hierarchy.node",
|
|
236
|
+
"value": {
|
|
237
|
+
"reference": {
|
|
238
|
+
"_ref": "75c47994-e6bb-487a-b8c9-b283f2436031",
|
|
239
|
+
"_type": "reference",
|
|
240
|
+
"_weak": true
|
|
241
|
+
},
|
|
242
|
+
"docType": "docs.article"
|
|
243
|
+
},
|
|
244
|
+
"parent": null,
|
|
245
|
+
"children": [
|
|
246
|
+
{
|
|
247
|
+
"_key": "f92eaeec96f7",
|
|
248
|
+
"_type": "hierarchy.node",
|
|
249
|
+
"value": {
|
|
250
|
+
"reference": {
|
|
251
|
+
"_ref": "7ad60a02-5d6e-47d8-92e2-6724cc130058",
|
|
252
|
+
"_type": "reference",
|
|
253
|
+
"_weak": true
|
|
254
|
+
},
|
|
255
|
+
"docType": "site.post"
|
|
256
|
+
},
|
|
257
|
+
"parent": "741b9edde2ba"
|
|
258
|
+
}
|
|
259
|
+
]
|
|
260
|
+
}
|
|
261
|
+
*/
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
After the transformation above, nodes with nested entries will include a `children` array. This data structure is recursive.
|
|
265
|
+
|
|
266
|
+
## Usage with GraphQL
|
|
267
|
+
|
|
268
|
+
By default, this plugin will create and update documents of `_type: hierarchy.tree`, with a `tree` field holding the hierarchical data. When deploying a [GraphQL Sanity endpoint](https://www.sanity.io/docs/graphql), however, you'll need an explicit document type in your schema so that you get the proper types for querying.
|
|
269
|
+
|
|
270
|
+
To add this document type, create a set of schemas with the `createHierarchicalSchemas`:
|
|
271
|
+
|
|
272
|
+
```js
|
|
273
|
+
// hierarchicalSchemas.js
|
|
274
|
+
import {createHierarchicalSchemas} from '@sanity/hierarchical-document-list'
|
|
275
|
+
|
|
276
|
+
export const hierarchicalOptions = {
|
|
277
|
+
// choose the document type name that suits you best
|
|
278
|
+
documentType: 'myCustomHierarchicalType',
|
|
279
|
+
|
|
280
|
+
// key for the tree field in the document - "tree" by default
|
|
281
|
+
fieldKeyInDocument: 'customTreeDataKey',
|
|
282
|
+
|
|
283
|
+
// Document types editors should be able to include in the hierarchy
|
|
284
|
+
referenceTo: ['site.page', 'site.post', 'docs.article', 'social.youtubeVideo'],
|
|
285
|
+
|
|
286
|
+
// ❓ Optional: provide filters and/or parameters for narrowing which documents can be added
|
|
287
|
+
referenceOptions: {
|
|
288
|
+
filter: 'status in $acceptedStatuses',
|
|
289
|
+
filterParams: {
|
|
290
|
+
acceptedStatuses: ['published', 'approved']
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
|
|
294
|
+
// ❓ Optional: limit the depth of your hierarachies
|
|
295
|
+
maxDept: 3
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export default createHierarchicalSchemas(hierarchicalOptions)
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
And add these schemas to your studio:
|
|
302
|
+
|
|
303
|
+
```js
|
|
304
|
+
import createSchema from 'part:@sanity/base/schema-creator'
|
|
305
|
+
import schemaTypes from 'all:part:@sanity/base/schema-type'
|
|
306
|
+
import hierarchicalSchemas from './hierarchicalSchemas'
|
|
307
|
+
|
|
308
|
+
export default createSchema({
|
|
309
|
+
name: 'default',
|
|
310
|
+
types: schemaTypes.concat([
|
|
311
|
+
// ...Other schemas
|
|
312
|
+
...hierarchicalSchemas // add all items in the array of hierarchical schemas
|
|
313
|
+
])
|
|
314
|
+
})
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
Then, in your desk structure where you added the hierarchical document(s), include the right `documentType` and `fieldKeyInDocument` properties:
|
|
318
|
+
|
|
319
|
+
```js
|
|
320
|
+
createDeskHierarchy({
|
|
321
|
+
// Include whatever values you defined in your schema in the step above
|
|
322
|
+
documentType: 'myCustomHierarchicalType', // the name of your document type
|
|
323
|
+
fieldKeyInDocument: 'customTreeDataKey' // the name of the hierarchical field
|
|
324
|
+
// ...
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
// Ideally, use the same configuration object you defined in your schemas:
|
|
328
|
+
import {hierarchicalOptions} from './hierarchicalSchemas'
|
|
329
|
+
|
|
330
|
+
createDeskHierarchy({
|
|
331
|
+
...hierarchicalOptions
|
|
332
|
+
// ...
|
|
333
|
+
})
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
---
|
|
337
|
+
|
|
338
|
+
📌 **Note:** you can also use the method above to add hierarchies inside the schema of documents and objects, which would be editable outside the desk structure.
|
|
339
|
+
|
|
340
|
+
We're considering adapting this input to support any type of nest-able data, not only references. Until then, avoid using the generated schemas in nested schemas as, in these contexts, it lacks the necessary affordances for a good editing experience.
|
|
341
|
+
|
|
342
|
+
---
|
|
343
|
+
|
|
344
|
+
## License
|
|
345
|
+
|
|
346
|
+
MIT-licensed. See LICENSE.
|
|
347
|
+
|
|
348
|
+
## License
|
|
349
|
+
|
|
350
|
+
[MIT](LICENSE) © Sanity
|
|
351
|
+
|
|
352
|
+
## Develop & test
|
|
353
|
+
|
|
354
|
+
This plugin uses [@sanity/plugin-kit](https://github.com/sanity-io/plugin-kit)
|
|
355
|
+
with default configuration for build & watch scripts.
|
|
356
|
+
|
|
357
|
+
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
|
|
358
|
+
on how to run this plugin with hotreload in the studio.
|
|
359
|
+
|
|
360
|
+
### Release new version
|
|
361
|
+
|
|
362
|
+
Run ["CI & Release" workflow](https://github.com/sanity-io/hierarchical-document-list/actions/workflows/main.yml).
|
|
363
|
+
Make sure to select the main branch and check "Release new version".
|
|
364
|
+
|
|
365
|
+
Semantic release will only release on configured branches, so it is safe to run release on any branch.
|