@sanity/hierarchical-document-list 1.1.0 → 2.0.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.
Files changed (120) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +95 -59
  3. package/dist/index.d.ts +240 -0
  4. package/dist/index.esm.js +151 -0
  5. package/dist/index.esm.js.map +1 -0
  6. package/dist/index.js +151 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +82 -51
  9. package/sanity.json +2 -6
  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/{lib/components/TreeEditorErrorBoundary.d.ts → src/components/TreeEditorErrorBoundary.tsx} +2 -4
  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/.husky/commit-msg +0 -4
  44. package/.husky/pre-commit +0 -4
  45. package/.idea/hierarchical-document-list.iml +0 -11
  46. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  47. package/.idea/misc.xml +0 -6
  48. package/.idea/modules.xml +0 -8
  49. package/.idea/prettier.xml +0 -7
  50. package/.idea/vcs.xml +0 -6
  51. package/CHANGELOG.md +0 -15
  52. package/commitlint.config.js +0 -3
  53. package/lib/TreeDeskStructure.d.ts +0 -8
  54. package/lib/TreeDeskStructure.js +0 -96
  55. package/lib/TreeInputComponent.d.ts +0 -19
  56. package/lib/TreeInputComponent.js +0 -50
  57. package/lib/components/DeskWarning.d.ts +0 -6
  58. package/lib/components/DeskWarning.js +0 -46
  59. package/lib/components/DocumentInNode.d.ts +0 -11
  60. package/lib/components/DocumentInNode.js +0 -81
  61. package/lib/components/DocumentPreviewStatus.d.ts +0 -7
  62. package/lib/components/DocumentPreviewStatus.js +0 -39
  63. package/lib/components/NodeActions.d.ts +0 -10
  64. package/lib/components/NodeActions.js +0 -61
  65. package/lib/components/NodeContentRenderer.d.ts +0 -8
  66. package/lib/components/NodeContentRenderer.js +0 -105
  67. package/lib/components/PlaceholderDropzone.d.ts +0 -9
  68. package/lib/components/PlaceholderDropzone.js +0 -30
  69. package/lib/components/SuppressedDnDManager.d.ts +0 -2
  70. package/lib/components/SuppressedDnDManager.js +0 -59
  71. package/lib/components/TreeEditor.d.ts +0 -12
  72. package/lib/components/TreeEditor.js +0 -74
  73. package/lib/components/TreeEditorErrorBoundary.js +0 -74
  74. package/lib/components/TreeNodeRenderer.d.ts +0 -3
  75. package/lib/components/TreeNodeRenderer.js +0 -59
  76. package/lib/components/TreeNodeRendererScaffold.d.ts +0 -4
  77. package/lib/components/TreeNodeRendererScaffold.js +0 -44
  78. package/lib/createDeskHierarchy.d.ts +0 -14
  79. package/lib/createDeskHierarchy.js +0 -84
  80. package/lib/createHierarchicalSchemas.d.ts +0 -98
  81. package/lib/createHierarchicalSchemas.js +0 -138
  82. package/lib/hooks/useAllItems.d.ts +0 -7
  83. package/lib/hooks/useAllItems.js +0 -119
  84. package/lib/hooks/useLocalTree.d.ts +0 -17
  85. package/lib/hooks/useLocalTree.js +0 -59
  86. package/lib/hooks/useTreeOperations.d.ts +0 -9
  87. package/lib/hooks/useTreeOperations.js +0 -39
  88. package/lib/hooks/useTreeOperationsProvider.d.ts +0 -14
  89. package/lib/hooks/useTreeOperationsProvider.js +0 -85
  90. package/lib/index.d.ts +0 -3
  91. package/lib/index.js +0 -12
  92. package/lib/schemas/hierarchy.tree.d.ts +0 -13
  93. package/lib/schemas/hierarchy.tree.js +0 -19
  94. package/lib/types.d.ts +0 -128
  95. package/lib/types.js +0 -2
  96. package/lib/utils/flatDataToTree.d.ts +0 -6
  97. package/lib/utils/flatDataToTree.js +0 -26
  98. package/lib/utils/getAdjescentNodes.d.ts +0 -12
  99. package/lib/utils/getAdjescentNodes.js +0 -19
  100. package/lib/utils/getCommonTreeProps.d.ts +0 -7
  101. package/lib/utils/getCommonTreeProps.js +0 -33
  102. package/lib/utils/getTreeHeight.d.ts +0 -3
  103. package/lib/utils/getTreeHeight.js +0 -11
  104. package/lib/utils/gradientPatchAdapter.d.ts +0 -4
  105. package/lib/utils/gradientPatchAdapter.js +0 -40
  106. package/lib/utils/idUtils.d.ts +0 -2
  107. package/lib/utils/idUtils.js +0 -13
  108. package/lib/utils/injectNodeTypeInPatches.d.ts +0 -12
  109. package/lib/utils/injectNodeTypeInPatches.js +0 -59
  110. package/lib/utils/moveItemInArray.d.ts +0 -5
  111. package/lib/utils/moveItemInArray.js +0 -26
  112. package/lib/utils/throwError.d.ts +0 -7
  113. package/lib/utils/throwError.js +0 -12
  114. package/lib/utils/treeData.d.ts +0 -18
  115. package/lib/utils/treeData.js +0 -118
  116. package/lib/utils/treePatches.d.ts +0 -15
  117. package/lib/utils/treePatches.js +0 -171
  118. package/lint-staged.config.js +0 -4
  119. package/screenshot-1.jpg +0 -0
  120. package/tsconfig.json +0 -20
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 Sanity.io
3
+ Copyright (c) 2025 Sanity.io
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # sanity-plugin-hierarchical-document-list
2
2
 
3
+ > This is a **Sanity Studio v3** plugin.
4
+
3
5
  Plugin for visually organizing documents as hierarchies in the [Sanity studio](https://www.sanity.io/docs/sanity-studio). Applications include:
4
6
 
5
7
  - Tables of content - such as a book's sections and chapters
@@ -8,53 +10,100 @@ Plugin for visually organizing documents as hierarchies in the [Sanity studio](h
8
10
 
9
11
  ![Screenshot of the plugin](/screenshot-1.jpg)
10
12
 
11
- ⚠️ **Compatibility:** This plugin requires Sanity Studio [version 2.25.0](https://github.com/sanity-io/sanity/releases/tag/v2.25.0) or higher.
13
+ ⚠️ **Compatibility:** This plugin requires Sanity Studio [version 3.3.0](https://github.com/sanity-io/sanity/releases/tag/v3.3.0) or higher.
12
14
 
13
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).
14
16
 
15
- ## Getting started
17
+
18
+ ## Installation
19
+
16
20
 
17
21
  ```bash
18
22
  # From the root of your sanity project
19
- sanity install @sanity/hierarchical-document-list
23
+ npm i @sanity/hierarchical-document-list
20
24
  ```
21
25
 
22
- Once you've installed the plugin the next step is to add one or more hierarchy documents to your Structure Builder.
26
+ ## Usage
23
27
 
24
- 💡 _To learn about custom desk structures, refer to the [Structure Builder docs](https://www.sanity.io/docs/overview-structure-builder)._
28
+ ### 1. Add the plugin and the default documentType to the sanity.config.ts
25
29
 
26
30
  ```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
- },
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
+ }
52
44
 
53
- // ❓ Optional: limit the depth of your hierarachies
54
- maxDept: 3
55
- })
56
- ])
57
- }
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
+ })
58
107
  ```
59
108
 
60
109
  ## How it works
@@ -296,34 +345,21 @@ We're considering adapting this input to support any type of nest-able data, not
296
345
 
297
346
  MIT-licensed. See LICENSE.
298
347
 
299
- ## Developing
348
+ ## License
300
349
 
301
- If you want to test a dev-build of this plugin in the studio there is a bit of a workaround.
302
- npm/yarn link does not integrate correctly with StructureBuilder and schema part registration in
303
- the Studio.
350
+ [MIT](LICENSE) © Sanity
304
351
 
305
- The most consistent workflow is:
352
+ ## Develop & test
306
353
 
307
- 1.Install [yalc](https://github.com/wclr/yalc)
308
- 2. Run `npm run build && yalc publish` in this repo
309
- 3. In Sanity Studio:
310
- 1. Run `yalc link @sanity/hierarchical-document-list`
311
- 2. Run `yarn install` (installs the dependencies for the plugin)
312
- 3. Ensure `"@sanity/hierarchical-document-list"` is present in `sanity.json` plugins array.
313
- 4. Configure the plugin for structure as documented above
354
+ This plugin uses [@sanity/plugin-kit](https://github.com/sanity-io/plugin-kit)
355
+ with default configuration for build & watch scripts.
314
356
 
315
- Rerun steps 2. and 3.1 after making edits to the plugin (or automate it).
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.
316
359
 
317
- ### Publishing
360
+ ### Release new version
318
361
 
319
- Test publish command safely:
320
- * `yalc publish`
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".
321
364
 
322
- To go live with a new version, run:
323
- * `npm run release`
324
- * runs [standard-version](https://www.npmjs.com/package/standard-version) which will bump version according to semver, commit and tag
325
- * feel free to inspect the results in package.json and git log before continuing
326
- * `npm publish`
327
- * Will clean, lint and build before finally publishing to npm.
328
- * After publishing, you should push with tags:
329
- * `git push --follow-tags`
365
+ Semantic release will only release on configured branches, so it is safe to run release on any branch.
@@ -0,0 +1,240 @@
1
+ import {ArraySchemaType} from 'sanity'
2
+ import type {ConfigContext} from 'sanity'
3
+ import {DocumentDefinition} from 'sanity'
4
+ import {Plugin as Plugin_2} from 'sanity'
5
+ import {PreviewConfig} from 'sanity'
6
+ import * as React_2 from 'react'
7
+ import {StructureBuilder} from 'sanity/desk'
8
+
9
+ export declare function createDeskHierarchy(props: TreeProps): any
10
+
11
+ export declare function createHierarchicalSchemas(options: SchemaOptions): (
12
+ | {
13
+ name: string
14
+ title: string
15
+ type: string
16
+ fields: (
17
+ | {
18
+ name: string
19
+ type: string
20
+ }
21
+ | {
22
+ name: string
23
+ type: string
24
+ title: string
25
+ fields: (
26
+ | {
27
+ name: string
28
+ type: string
29
+ weak?: undefined
30
+ to?: undefined
31
+ options?: undefined
32
+ }
33
+ | {
34
+ name: string
35
+ type: string
36
+ weak: boolean
37
+ to: {
38
+ type: string
39
+ }[]
40
+ options:
41
+ | {
42
+ filter?: string | undefined
43
+ filterParams?: Record<string, unknown> | undefined
44
+ }
45
+ | undefined
46
+ }
47
+ )[]
48
+ }
49
+ )[]
50
+ }
51
+ | {
52
+ name: string
53
+ title: string
54
+ type: string
55
+ of: (
56
+ | {
57
+ name: string
58
+ title: string
59
+ type: string
60
+ fields: (
61
+ | {
62
+ name: string
63
+ type: string
64
+ }
65
+ | {
66
+ name: string
67
+ type: string
68
+ title: string
69
+ fields: (
70
+ | {
71
+ name: string
72
+ type: string
73
+ weak?: undefined
74
+ to?: undefined
75
+ options?: undefined
76
+ }
77
+ | {
78
+ name: string
79
+ type: string
80
+ weak: boolean
81
+ to: {
82
+ type: string
83
+ }[]
84
+ options:
85
+ | {
86
+ filter?: string | undefined
87
+ filterParams?: Record<string, unknown> | undefined
88
+ }
89
+ | undefined
90
+ }
91
+ )[]
92
+ }
93
+ )[]
94
+ }
95
+ | {
96
+ type: string
97
+ }
98
+ )[]
99
+ }
100
+ | {
101
+ name: string | undefined
102
+ title: string
103
+ type: string
104
+ liveEdit: boolean
105
+ fields: (Omit<ArraySchemaType<unknown>, 'type' | 'of' | 'jsonType'> & {
106
+ type: string
107
+ inputComponent: React_2.FC<any>
108
+ of?: any[] | undefined
109
+ })[]
110
+ preview: {
111
+ select: {
112
+ id: string
113
+ tree: string
114
+ }
115
+ prepare({id, tree}: {id: string; tree: unknown[]}): Record<string, string>
116
+ }
117
+ }
118
+ )[]
119
+
120
+ export declare function flatDataToTree(data: StoredTreeItem[]): TreeItemWithChildren[]
121
+
122
+ /**
123
+ * Usage in `sanity.config.ts` (or .js)
124
+ *
125
+ * ```ts
126
+ * import {defineConfig} from 'sanity'
127
+ * import {hierarchicalDocumentList} from '@sanity/hierarchical-document-list'
128
+ *
129
+ * export default defineConfig({
130
+ * // ...
131
+ * plugins: [hierarchicalDocumentList()],
132
+ * })
133
+ * ```
134
+ */
135
+ export declare const hierarchicalDocumentList: Plugin_2<void>
136
+
137
+ export declare const hierarchyTree: {
138
+ type: 'document'
139
+ name: 'hierarchy.tree'
140
+ } & Omit<DocumentDefinition, 'preview'> & {
141
+ preview?: PreviewConfig<Record<string, string>, Record<never, any>> | undefined
142
+ }
143
+
144
+ declare const INTERNAL_NODE_TYPE: string
145
+
146
+ declare const INTERNAL_NODE_VALUE_TYPE: string
147
+
148
+ declare interface SanityReference {
149
+ _type: 'reference'
150
+ _ref: string
151
+ _weak?: boolean
152
+ }
153
+
154
+ declare type SchemaOptions = Omit<TreeDeskStructureProps, 'documentId' | 'maxDepth'>
155
+
156
+ /**
157
+ * Objects saved to tree documents in Sanity's Content Lake
158
+ */
159
+ declare interface StoredTreeItem {
160
+ _key: string
161
+ _type: typeof INTERNAL_NODE_TYPE | string
162
+ value?: {
163
+ _type: typeof INTERNAL_NODE_VALUE_TYPE | string
164
+ reference?: SanityReference
165
+ docType?: string
166
+ }
167
+ /**
168
+ * _key of parent node
169
+ */
170
+ parent?: string | null
171
+ }
172
+
173
+ declare interface TreeDeskStructureProps extends TreeInputOptions {
174
+ /**
175
+ * _id of the document that will hold the tree data.
176
+ */
177
+ documentId: string
178
+ /**
179
+ * (Optional)
180
+ * Key for the field representing the hierarchical tree inside the document.
181
+ * `tree` by default.
182
+ */
183
+ fieldKeyInDocument?: string
184
+ }
185
+
186
+ declare interface TreeInputOptions {
187
+ /**
188
+ * What document types this hierarchy can refer to.
189
+ * Similar to the `to` property of the [reference field](https://www.sanity.io/docs/reference-type).
190
+ */
191
+ referenceTo: string[]
192
+ /**
193
+ * Used to provide fine-grained filtering for documents.
194
+ */
195
+ referenceOptions?: {
196
+ /**
197
+ * Static filter to apply to tree document queries.
198
+ */
199
+ filter?: string
200
+ /**
201
+ * Parameters / variables to pass to the GROQ query ran to fetch documents.
202
+ */
203
+ filterParams?: Record<string, unknown>
204
+ }
205
+ /**
206
+ * How deep should editors be allowed to nest items.
207
+ */
208
+ maxDepth?: number
209
+ /**
210
+ * Schema type for your hierarchical documents.
211
+ * Refer to documentation on how to provide these schemas in your studio.
212
+ *
213
+ * Defautlt: 'hierarchy.tree' - this schema is bundled with the plugin
214
+ */
215
+ documentType?: string
216
+ }
217
+
218
+ declare interface TreeItemWithChildren extends StoredTreeItem {
219
+ children?: TreeItemWithChildren[]
220
+ }
221
+
222
+ export declare interface TreeProps extends TreeDeskStructureProps {
223
+ /**
224
+ * Visible title above the tree.
225
+ * Also used as the label in the desk list item.
226
+ */
227
+ title: string
228
+ /**
229
+ * Optional icon for rendering the item in the desk structure.
230
+ */
231
+ icon?: any
232
+ context?: ConfigContext | any
233
+ S?: StructureBuilder | any
234
+ /**
235
+ * Restrict document types that can be created.
236
+ */
237
+ creatableTypes?: string[]
238
+ }
239
+
240
+ export {}