@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.
Files changed (106) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +365 -297
  3. package/dist/index.d.ts +240 -0
  4. package/dist/index.esm.js +18 -0
  5. package/dist/index.esm.js.map +1 -0
  6. package/dist/index.js +18 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +94 -55
  9. package/sanity.json +8 -12
  10. package/src/TreeDeskStructure.tsx +80 -0
  11. package/src/TreeInputComponent.tsx +41 -0
  12. package/src/components/DeskWarning.tsx +40 -0
  13. package/src/components/DocumentInNode.tsx +133 -0
  14. package/src/components/DocumentPreviewStatus.tsx +70 -0
  15. package/src/components/NodeActions.tsx +85 -0
  16. package/src/components/NodeContentRenderer.tsx +141 -0
  17. package/src/components/PlaceholderDropzone.tsx +45 -0
  18. package/src/components/TreeEditor.tsx +167 -0
  19. package/src/components/TreeEditorErrorBoundary.tsx +14 -0
  20. package/src/components/TreeNodeRenderer.tsx +37 -0
  21. package/src/components/TreeNodeRendererScaffold.tsx +193 -0
  22. package/src/createDeskHierarchy.tsx +110 -0
  23. package/src/createHierarchicalSchemas.tsx +151 -0
  24. package/src/hooks/useAllItems.ts +119 -0
  25. package/src/hooks/useLocalTree.ts +40 -0
  26. package/src/hooks/useTreeOperations.ts +25 -0
  27. package/src/hooks/useTreeOperationsProvider.ts +86 -0
  28. package/src/index.ts +25 -0
  29. package/src/schemas/hierarchy.tree.ts +19 -0
  30. package/src/types.ts +148 -0
  31. package/src/utils/flatDataToTree.ts +20 -0
  32. package/src/utils/getAdjescentNodes.ts +30 -0
  33. package/src/utils/getCommonTreeProps.tsx +28 -0
  34. package/src/utils/getTreeHeight.ts +10 -0
  35. package/src/utils/gradientPatchAdapter.ts +43 -0
  36. package/src/utils/idUtils.ts +7 -0
  37. package/src/utils/injectNodeTypeInPatches.ts +60 -0
  38. package/src/utils/moveItemInArray.ts +26 -0
  39. package/src/utils/throwError.ts +9 -0
  40. package/src/utils/treeData.tsx +119 -0
  41. package/src/utils/treePatches.ts +171 -0
  42. package/v2-incompatible.js +11 -0
  43. package/lib/TreeDeskStructure.d.ts +0 -8
  44. package/lib/TreeDeskStructure.js +0 -96
  45. package/lib/TreeInputComponent.d.ts +0 -19
  46. package/lib/TreeInputComponent.js +0 -52
  47. package/lib/components/DeskWarning.d.ts +0 -6
  48. package/lib/components/DeskWarning.js +0 -46
  49. package/lib/components/DocumentInNode.d.ts +0 -11
  50. package/lib/components/DocumentInNode.js +0 -82
  51. package/lib/components/DocumentPreviewStatus.d.ts +0 -7
  52. package/lib/components/DocumentPreviewStatus.js +0 -39
  53. package/lib/components/NodeActions.d.ts +0 -10
  54. package/lib/components/NodeActions.js +0 -61
  55. package/lib/components/NodeContentRenderer.d.ts +0 -8
  56. package/lib/components/NodeContentRenderer.js +0 -105
  57. package/lib/components/PlaceholderDropzone.d.ts +0 -9
  58. package/lib/components/PlaceholderDropzone.js +0 -30
  59. package/lib/components/TreeEditor.d.ts +0 -12
  60. package/lib/components/TreeEditor.js +0 -59
  61. package/lib/components/TreeEditorErrorBoundary.d.ts +0 -3
  62. package/lib/components/TreeEditorErrorBoundary.js +0 -59
  63. package/lib/components/TreeNodeRenderer.d.ts +0 -3
  64. package/lib/components/TreeNodeRenderer.js +0 -59
  65. package/lib/components/TreeNodeRendererScaffold.d.ts +0 -4
  66. package/lib/components/TreeNodeRendererScaffold.js +0 -44
  67. package/lib/createDeskHierarchy.d.ts +0 -14
  68. package/lib/createDeskHierarchy.js +0 -85
  69. package/lib/createHierarchicalSchemas.d.ts +0 -78
  70. package/lib/createHierarchicalSchemas.js +0 -138
  71. package/lib/hooks/useAllItems.d.ts +0 -7
  72. package/lib/hooks/useAllItems.js +0 -119
  73. package/lib/hooks/useLocalTree.d.ts +0 -17
  74. package/lib/hooks/useLocalTree.js +0 -59
  75. package/lib/hooks/useTreeOperations.d.ts +0 -9
  76. package/lib/hooks/useTreeOperations.js +0 -39
  77. package/lib/hooks/useTreeOperationsProvider.d.ts +0 -14
  78. package/lib/hooks/useTreeOperationsProvider.js +0 -85
  79. package/lib/index.d.ts +0 -3
  80. package/lib/index.js +0 -12
  81. package/lib/schemas/hierarchy.tree.d.ts +0 -13
  82. package/lib/schemas/hierarchy.tree.js +0 -19
  83. package/lib/utils/flatDataToTree.d.ts +0 -6
  84. package/lib/utils/flatDataToTree.js +0 -26
  85. package/lib/utils/getAdjescentNodes.d.ts +0 -12
  86. package/lib/utils/getAdjescentNodes.js +0 -19
  87. package/lib/utils/getCommonTreeProps.d.ts +0 -7
  88. package/lib/utils/getCommonTreeProps.js +0 -33
  89. package/lib/utils/getTreeHeight.d.ts +0 -3
  90. package/lib/utils/getTreeHeight.js +0 -11
  91. package/lib/utils/gradientPatchAdapter.d.ts +0 -4
  92. package/lib/utils/gradientPatchAdapter.js +0 -42
  93. package/lib/utils/idUtils.d.ts +0 -2
  94. package/lib/utils/idUtils.js +0 -13
  95. package/lib/utils/injectNodeTypeInPatches.d.ts +0 -12
  96. package/lib/utils/injectNodeTypeInPatches.js +0 -58
  97. package/lib/utils/moveItemInArray.d.ts +0 -5
  98. package/lib/utils/moveItemInArray.js +0 -26
  99. package/lib/utils/throwError.d.ts +0 -7
  100. package/lib/utils/throwError.js +0 -12
  101. package/lib/utils/treeData.d.ts +0 -18
  102. package/lib/utils/treeData.js +0 -118
  103. package/lib/utils/treePatches.d.ts +0 -15
  104. package/lib/utils/treePatches.js +0 -171
  105. package/screenshot-1.jpg +0 -0
  106. package/tsconfig.json +0 -20
package/README.md CHANGED
@@ -1,297 +1,365 @@
1
- # sanity-plugin-hierarchical-document-list
2
-
3
- Plugin for visually organizing documents as hierarchies in the [Sanity studio](https://www.sanity.io/docs/sanity-studio). Applications include:
4
-
5
- - Tables of content - such as a book's sections and chapters
6
- - Navigational structure & menus - a website mega-menu with multiple levels, for example
7
- - Taxonomy inheritance - "_Carbs_ is a parent of _Legumes_ which is a parent of _Beans_"
8
-
9
- ![Screenshot of the plugin](/screenshot-1.jpg)
10
-
11
- ⚠️ **Compatibility:** This plugin requires Sanity Studio [version 2.25.0](https://github.com/sanity-io/sanity/releases/tag/v2.25.0) or higher.
12
-
13
- 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).
14
-
15
- ## Getting started
16
-
17
- ```bash
18
- # From the root of your sanity project
19
- sanity install @sanity/hierarchical-document-list
20
- ```
21
-
22
- Once you've installed the plugin the next step is to add one or more hierarchy documents to your Structure Builder.
23
-
24
- 💡 _To learn about custom desk structures, refer to the [Structure Builder docs](https://www.sanity.io/docs/overview-structure-builder)._
25
-
26
- ```js
27
- // deskStructure.js
28
- import S from '@sanity/desk-tool/structure-builder'
29
- import {createDeskHierarchy} from '@sanity/hierarchical-document-list'
30
-
31
- export default () => {
32
- return S.list()
33
- .title('Content')
34
- .items([
35
- ...S.documentTypeListItems(), // or whatever other structure you have
36
- createDeskHierarchy({
37
- title: 'Main table of contents',
38
-
39
- // The hierarchy will be stored in this document ID 👇
40
- documentId: 'main-table-of-contents',
41
-
42
- // Document types editors should be able to include in the hierarchy
43
- referenceTo: ['site.page', 'site.post', 'docs.article', 'social.youtubeVideo'],
44
-
45
- // ❓ Optional: provide filters and/or parameters for narrowing which documents can be added
46
- referenceOptions: {
47
- filter: 'status in $acceptedStatuses',
48
- filterParams: {
49
- acceptedStatuses: ['published', 'approved']
50
- }
51
- },
52
-
53
- // ❓ Optional: limit the depth of your hierarachies
54
- maxDept: 3
55
- })
56
- ])
57
- }
58
- ```
59
-
60
- ## How it works
61
-
62
- 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.
63
-
64
- This approach also simplifies querying the full structure - as you'll see in [querying data](#querying-data) below.
65
-
66
- 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.
67
-
68
- 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.
69
-
70
- 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.
71
-
72
- ## Querying data
73
-
74
- 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:
75
-
76
- ```json
77
- [
78
- {
79
- "_key": "741b9edde2ba",
80
- "_type": "hierarchy.node",
81
- "value": {
82
- "reference": {
83
- "_ref": "75c47994-e6bb-487a-b8c9-b283f2436031",
84
- "_type": "reference",
85
- "_weak": true // This plugin includes weak references by default
86
- },
87
- "docType": "docs.article"
88
- }
89
- // no `parent`, this item is top-level
90
- },
91
- {
92
- "_key": "f92eaeec96f7",
93
- "_type": "hierarchy.node",
94
- "value": {
95
- "reference": {
96
- "_ref": "7ad60a02-5d6e-47d8-92e2-6724cc130058",
97
- "_type": "reference",
98
- "_weak": true
99
- },
100
- "docType": "site.post"
101
- },
102
- // The `parent` property points to the _key of the parent node where this one is nested
103
- "parent": "741b9edde2ba"
104
- }
105
- ]
106
- ```
107
-
108
- 📌 If using GraphQL, refer to [Usage with GraphQL](#usage-with-graphql).
109
-
110
- From the the above, we know how to expand referenced documents in GROQ:
111
-
112
- ```groq
113
- *[_id == "main-table-of-contents"][0]{
114
- tree[] {
115
- // Make sure you include each item's _key and parent
116
- _key,
117
- parent,
118
-
119
- // "Expand" the reference to the node
120
- value {
121
- reference->{
122
- // Get whatever property you need from your documents
123
- title,
124
- slug,
125
- }
126
- }
127
- }
128
- }
129
- ```
130
-
131
- The query above will then need to be converted from flat data to a tree. Refer to [Using the data](#using-the-data).
132
-
133
- <!-- ### Other query scenarios
134
-
135
- Find a given document in a hierarchy and get its parent - useful for rendering breadcrumbs:
136
-
137
- ```groq
138
- // Works starting from Content Lake V2021-03-25
139
- *[_id == "main-table-of-contents"][0]{
140
- // From the tree, get the 1st node that references a given document _id
141
- tree[node._ref == "my-book-section"][0] {
142
- _key,
143
- "section": node.reference->{
144
- title,
145
- },
146
- // Then, from the tree get the element matching the `parent` _key of the found node
147
- "parentChapter": ^.tree[_key == ^.parent][0]{
148
- _key,
149
- "chapter": node.reference->{
150
- title,
151
- contributors,
152
- }
153
- },
154
- }
155
- }
156
- ```
157
-
158
- ---- -->
159
-
160
- ## Using the data
161
-
162
- From the flat data queried, you'll need to convert it to a nested tree with `flatDataToTree`:
163
-
164
- ```js
165
- import {flatDataToTree} from '@sanity/hierarchical-document-list'
166
-
167
- const hierarchyDocument = await client.fetch(`*[_id == "book-v3-review-a"][0]{
168
- tree[] {
169
- // Make sure you include each item's _key and parent
170
- _key,
171
- parent,
172
- value {
173
- reference->{
174
- title,
175
- slug,
176
- content,
177
- }
178
- }
179
- }
180
- }`)
181
- const tree = flatDataToTree(data.tree)
182
-
183
- /* Results in a recursively nested structure. Using the example data above:
184
- {
185
- "_key": "741b9edde2ba",
186
- "_type": "hierarchy.node",
187
- "value": {
188
- "reference": {
189
- "_ref": "75c47994-e6bb-487a-b8c9-b283f2436031",
190
- "_type": "reference",
191
- "_weak": true
192
- },
193
- "docType": "docs.article"
194
- },
195
- "parent": null,
196
- "children": [
197
- {
198
- "_key": "f92eaeec96f7",
199
- "_type": "hierarchy.node",
200
- "value": {
201
- "reference": {
202
- "_ref": "7ad60a02-5d6e-47d8-92e2-6724cc130058",
203
- "_type": "reference",
204
- "_weak": true
205
- },
206
- "docType": "site.post"
207
- },
208
- "parent": "741b9edde2ba"
209
- }
210
- ]
211
- }
212
- */
213
- ```
214
-
215
- After the transformation above, nodes with nested entries will include a `children` array. This data structure is recursive.
216
-
217
- ## Usage with GraphQL
218
-
219
- 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.
220
-
221
- To add this document type, create a set of schemas with the `createHierarchicalSchemas`:
222
-
223
- ```js
224
- // hierarchicalSchemas.js
225
- import {createHierarchicalSchemas} from '@sanity/hierarchical-document-list'
226
-
227
- export const hierarchicalOptions = {
228
- // choose the document type name that suits you best
229
- documentType: 'myCustomHierarchicalType',
230
-
231
- // key for the tree field in the document - "tree" by default
232
- fieldKeyInDocument: 'customTreeDataKey',
233
-
234
- // Document types editors should be able to include in the hierarchy
235
- referenceTo: ['site.page', 'site.post', 'docs.article', 'social.youtubeVideo'],
236
-
237
- // ❓ Optional: provide filters and/or parameters for narrowing which documents can be added
238
- referenceOptions: {
239
- filter: 'status in $acceptedStatuses',
240
- filterParams: {
241
- acceptedStatuses: ['published', 'approved']
242
- }
243
- },
244
-
245
- // ❓ Optional: limit the depth of your hierarachies
246
- maxDept: 3
247
- }
248
-
249
- export default createHierarchicalSchemas(hierarchicalOptions)
250
- ```
251
-
252
- And add these schemas to your studio:
253
-
254
- ```js
255
- import createSchema from 'part:@sanity/base/schema-creator'
256
- import schemaTypes from 'all:part:@sanity/base/schema-type'
257
- import hierarchicalSchemas from './hierarchicalSchemas'
258
-
259
- export default createSchema({
260
- name: 'default',
261
- types: schemaTypes.concat([
262
- // ...Other schemas
263
- ...hierarchicalSchemas // add all items in the array of hierarchical schemas
264
- ])
265
- })
266
- ```
267
-
268
- Then, in your desk structure where you added the hierarchical document(s), include the right `documentType` and `fieldKeyInDocument` properties:
269
-
270
- ```js
271
- createDeskHierarchy({
272
- // Include whatever values you defined in your schema in the step above
273
- documentType: 'myCustomHierarchicalType', // the name of your document type
274
- fieldKeyInDocument: 'customTreeDataKey' // the name of the hierarchical field
275
- // ...
276
- })
277
-
278
- // Ideally, use the same configuration object you defined in your schemas:
279
- import {hierarchicalOptions} from './hierarchicalSchemas'
280
-
281
- createDeskHierarchy({
282
- ...hierarchicalOptions
283
- // ...
284
- })
285
- ```
286
-
287
- ---
288
-
289
- 📌 **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.
290
-
291
- 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.
292
-
293
- ---
294
-
295
- ## License
296
-
297
- MIT-licensed. See LICENSE.
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
+ ![Screenshot of the plugin](/screenshot-1.jpg)
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.