@sanity/hierarchical-document-list 2.1.2 → 3.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 (44) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +30 -30
  3. package/dist/index.d.ts +170 -195
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +835 -6031
  6. package/dist/index.js.map +1 -1
  7. package/package.json +38 -77
  8. package/dist/index.d.mts +0 -240
  9. package/dist/index.mjs +0 -6433
  10. package/dist/index.mjs.map +0 -1
  11. package/sanity.json +0 -8
  12. package/src/TreeDeskStructure.tsx +0 -80
  13. package/src/TreeInputComponent.tsx +0 -41
  14. package/src/components/DeskWarning.tsx +0 -40
  15. package/src/components/DocumentInNode.tsx +0 -133
  16. package/src/components/DocumentPreviewStatus.tsx +0 -70
  17. package/src/components/NodeActions.tsx +0 -85
  18. package/src/components/NodeContentRenderer.tsx +0 -141
  19. package/src/components/PlaceholderDropzone.tsx +0 -45
  20. package/src/components/TreeEditor.tsx +0 -184
  21. package/src/components/TreeEditorErrorBoundary.tsx +0 -14
  22. package/src/components/TreeNodeRenderer.tsx +0 -37
  23. package/src/components/TreeNodeRendererScaffold.tsx +0 -193
  24. package/src/createDeskHierarchy.tsx +0 -110
  25. package/src/createHierarchicalSchemas.tsx +0 -151
  26. package/src/hooks/useAllItems.ts +0 -119
  27. package/src/hooks/useLocalTree.ts +0 -40
  28. package/src/hooks/useTreeOperations.ts +0 -25
  29. package/src/hooks/useTreeOperationsProvider.ts +0 -86
  30. package/src/index.ts +0 -25
  31. package/src/schemas/hierarchy.tree.ts +0 -19
  32. package/src/types.ts +0 -148
  33. package/src/utils/flatDataToTree.ts +0 -20
  34. package/src/utils/getAdjescentNodes.ts +0 -30
  35. package/src/utils/getCommonTreeProps.tsx +0 -28
  36. package/src/utils/getTreeHeight.ts +0 -8
  37. package/src/utils/gradientPatchAdapter.ts +0 -43
  38. package/src/utils/idUtils.ts +0 -7
  39. package/src/utils/injectNodeTypeInPatches.ts +0 -60
  40. package/src/utils/moveItemInArray.ts +0 -26
  41. package/src/utils/throwError.ts +0 -9
  42. package/src/utils/treeData.tsx +0 -119
  43. package/src/utils/treePatches.ts +0 -171
  44. package/v2-incompatible.js +0 -11
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Sanity.io
3
+ Copyright (c) 2026 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,7 +1,5 @@
1
1
  # sanity-plugin-hierarchical-document-list
2
2
 
3
- > This is a **Sanity Studio v3** plugin.
4
-
5
3
  Plugin for visually organizing documents as hierarchies in the [Sanity studio](https://www.sanity.io/docs/sanity-studio). Applications include:
6
4
 
7
5
  - Tables of content - such as a book's sections and chapters
@@ -14,10 +12,8 @@ Plugin for visually organizing documents as hierarchies in the [Sanity studio](h
14
12
 
15
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).
16
14
 
17
-
18
15
  ## Installation
19
16
 
20
-
21
17
  ```bash
22
18
  # From the root of your sanity project
23
19
  npm i @sanity/hierarchical-document-list
@@ -31,17 +27,16 @@ npm i @sanity/hierarchical-document-list
31
27
  // sanity.config.js
32
28
  import {defineConfig} from 'sanity'
33
29
  import {hierarchicalDocumentList, hierarchyTree} from '@sanity/hierarchical-document-list'
34
-
30
+
35
31
  export default defineConfig({
36
- // ...
37
- plugins: [hierarchicalDocumentList()],
38
- schema: {
32
+ // ...
33
+ plugins: [hierarchicalDocumentList()],
34
+ schema: {
39
35
  types: [
40
36
  //...,
41
- hierarchyTree
42
- ]
43
- }
44
-
37
+ hierarchyTree,
38
+ ],
39
+ },
45
40
  })
46
41
  ```
47
42
 
@@ -53,14 +48,19 @@ export default defineConfig({
53
48
  // sanity.config.ts
54
49
  import {defineConfig} from 'sanity'
55
50
  import {deskTool} from 'sanity/desk'
56
- import {createDeskHierarchy, hierarchicalDocumentList, hierarchyTree} from '@sanity/hierarchical-document-list'
51
+ import {
52
+ createDeskHierarchy,
53
+ hierarchicalDocumentList,
54
+ hierarchyTree,
55
+ } from '@sanity/hierarchical-document-list'
57
56
 
58
57
  export default defineConfig({
59
58
  // ...
60
59
  plugins: [
61
60
  deskTool({
62
61
  // NOTE: I'n V3 you MUST pass S and Context along to createDeskHierarchy as props
63
- structure: (S, context) => S.list()
62
+ structure: (S, context) =>
63
+ S.list()
64
64
  .title('Content')
65
65
  .items([
66
66
  ...S.documentTypeListItems(), // or whatever other structure you have
@@ -83,26 +83,26 @@ export default defineConfig({
83
83
  referenceOptions: {
84
84
  filter: 'status in $acceptedStatuses',
85
85
  filterParams: {
86
- acceptedStatuses: ['published', 'approved']
87
- }
86
+ acceptedStatuses: ['published', 'approved'],
87
+ },
88
88
  },
89
89
 
90
90
  // ❓ Optional: limit the depth of your hierarachies
91
91
  maxDepth: 3,
92
92
 
93
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
- ])
94
+ creatableTypes: ['site.page'],
95
+ }),
96
+ ]),
97
97
  }),
98
- hierarchicalDocumentList()
98
+ hierarchicalDocumentList(),
99
99
  ],
100
100
  schema: {
101
101
  types: [
102
102
  //...,
103
- hierarchyTree
104
- ]
105
- }
103
+ hierarchyTree,
104
+ ],
105
+ },
106
106
  })
107
107
  ```
108
108
 
@@ -287,12 +287,12 @@ export const hierarchicalOptions = {
287
287
  referenceOptions: {
288
288
  filter: 'status in $acceptedStatuses',
289
289
  filterParams: {
290
- acceptedStatuses: ['published', 'approved']
291
- }
290
+ acceptedStatuses: ['published', 'approved'],
291
+ },
292
292
  },
293
293
 
294
294
  // ❓ Optional: limit the depth of your hierarachies
295
- maxDept: 3
295
+ maxDept: 3,
296
296
  }
297
297
 
298
298
  export default createHierarchicalSchemas(hierarchicalOptions)
@@ -309,8 +309,8 @@ export default createSchema({
309
309
  name: 'default',
310
310
  types: schemaTypes.concat([
311
311
  // ...Other schemas
312
- ...hierarchicalSchemas // add all items in the array of hierarchical schemas
313
- ])
312
+ ...hierarchicalSchemas, // add all items in the array of hierarchical schemas
313
+ ]),
314
314
  })
315
315
  ```
316
316
 
@@ -320,7 +320,7 @@ Then, in your desk structure where you added the hierarchical document(s), inclu
320
320
  createDeskHierarchy({
321
321
  // Include whatever values you defined in your schema in the step above
322
322
  documentType: 'myCustomHierarchicalType', // the name of your document type
323
- fieldKeyInDocument: 'customTreeDataKey' // the name of the hierarchical field
323
+ fieldKeyInDocument: 'customTreeDataKey', // the name of the hierarchical field
324
324
  // ...
325
325
  })
326
326
 
@@ -328,7 +328,7 @@ createDeskHierarchy({
328
328
  import {hierarchicalOptions} from './hierarchicalSchemas'
329
329
 
330
330
  createDeskHierarchy({
331
- ...hierarchicalOptions
331
+ ...hierarchicalOptions,
332
332
  // ...
333
333
  })
334
334
  ```
package/dist/index.d.ts CHANGED
@@ -1,194 +1,34 @@
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
1
+ import { ArraySchemaType } from "sanity";
2
+ import { FC } from "react";
3
+ declare const INTERNAL_NODE_TYPE: string;
4
+ declare const INTERNAL_NODE_VALUE_TYPE: string;
5
+ interface SanityReference {
6
+ _type: 'reference';
7
+ _ref: string;
8
+ _weak?: boolean;
152
9
  }
153
-
154
- declare type SchemaOptions = Omit<TreeDeskStructureProps, 'documentId' | 'maxDepth'>
155
-
156
10
  /**
157
11
  * Objects saved to tree documents in Sanity's Content Lake
158
12
  */
159
- declare interface StoredTreeItem {
160
- _key: string
161
- _type: typeof INTERNAL_NODE_TYPE | string
13
+ interface StoredTreeItem {
14
+ _key: string;
15
+ _type: typeof INTERNAL_NODE_TYPE;
162
16
  value?: {
163
- _type: typeof INTERNAL_NODE_VALUE_TYPE | string
164
- reference?: SanityReference
165
- docType?: string
166
- }
17
+ _type: typeof INTERNAL_NODE_VALUE_TYPE;
18
+ reference?: SanityReference;
19
+ docType?: string;
20
+ };
167
21
  /**
168
22
  * _key of parent node
169
23
  */
170
- parent?: string | null
24
+ parent?: string | null;
171
25
  }
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 {
26
+ interface TreeInputOptions {
187
27
  /**
188
28
  * What document types this hierarchy can refer to.
189
29
  * Similar to the `to` property of the [reference field](https://www.sanity.io/docs/reference-type).
190
30
  */
191
- referenceTo: string[]
31
+ referenceTo: string[];
192
32
  /**
193
33
  * Used to provide fine-grained filtering for documents.
194
34
  */
@@ -196,45 +36,180 @@ declare interface TreeInputOptions {
196
36
  /**
197
37
  * Static filter to apply to tree document queries.
198
38
  */
199
- filter?: string
39
+ filter?: string;
200
40
  /**
201
41
  * Parameters / variables to pass to the GROQ query ran to fetch documents.
202
42
  */
203
- filterParams?: Record<string, unknown>
204
- }
43
+ filterParams?: Record<string, unknown>;
44
+ };
205
45
  /**
206
46
  * How deep should editors be allowed to nest items.
207
47
  */
208
- maxDepth?: number
48
+ maxDepth?: number;
209
49
  /**
210
50
  * Schema type for your hierarchical documents.
211
51
  * Refer to documentation on how to provide these schemas in your studio.
212
52
  *
213
53
  * Defautlt: 'hierarchy.tree' - this schema is bundled with the plugin
214
54
  */
215
- documentType?: string
55
+ documentType?: string;
216
56
  }
217
-
218
- declare interface TreeItemWithChildren extends StoredTreeItem {
219
- children?: TreeItemWithChildren[]
57
+ interface TreeDeskStructureProps extends TreeInputOptions {
58
+ /**
59
+ * _id of the document that will hold the tree data.
60
+ */
61
+ documentId: string;
62
+ /**
63
+ * (Optional)
64
+ * Key for the field representing the hierarchical tree inside the document.
65
+ * `tree` by default.
66
+ */
67
+ fieldKeyInDocument?: string;
220
68
  }
221
-
222
- export declare interface TreeProps extends TreeDeskStructureProps {
69
+ interface TreeProps extends TreeDeskStructureProps {
223
70
  /**
224
71
  * Visible title above the tree.
225
72
  * Also used as the label in the desk list item.
226
73
  */
227
- title: string
74
+ title: string;
228
75
  /**
229
76
  * Optional icon for rendering the item in the desk structure.
230
77
  */
231
- icon?: any
232
- context?: ConfigContext | any
233
- S?: StructureBuilder | any
78
+ icon?: any;
79
+ /**
80
+ * The `ConfigContext` available as the second parameter of the structure resolver.
81
+ */
82
+ context?: any;
83
+ /**
84
+ * The `StructureBuilder` (`S`) available as the first parameter of the structure resolver.
85
+ */
86
+ S?: any;
234
87
  /**
235
88
  * Restrict document types that can be created.
236
89
  */
237
- creatableTypes?: string[]
90
+ creatableTypes?: string[];
91
+ }
92
+ declare function createDeskHierarchy(props: TreeProps): any;
93
+ type SchemaOptions = Omit<TreeDeskStructureProps, 'documentId' | 'maxDepth'>;
94
+ declare function createHierarchicalSchemas(options: SchemaOptions): ({
95
+ name: string | undefined;
96
+ title: string;
97
+ type: string;
98
+ liveEdit: boolean;
99
+ fields: (Omit<ArraySchemaType<unknown>, "type" | "of" | "jsonType"> & {
100
+ type: string;
101
+ inputComponent: FC<any>;
102
+ of?: any[];
103
+ })[];
104
+ preview: {
105
+ select: {
106
+ id: string;
107
+ tree: string;
108
+ };
109
+ prepare({
110
+ id,
111
+ tree
112
+ }: {
113
+ id: string;
114
+ tree: unknown[];
115
+ }): Record<string, string>;
116
+ };
117
+ } | {
118
+ name: string;
119
+ title: string;
120
+ type: string;
121
+ fields: ({
122
+ name: string;
123
+ type: string;
124
+ } | {
125
+ name: string;
126
+ type: string;
127
+ title: string;
128
+ fields: ({
129
+ name: string;
130
+ type: string;
131
+ weak?: undefined;
132
+ to?: undefined;
133
+ options?: undefined;
134
+ } | {
135
+ name: string;
136
+ type: string;
137
+ weak: boolean;
138
+ to: {
139
+ type: string;
140
+ }[];
141
+ options: {
142
+ filter?: string;
143
+ filterParams?: Record<string, unknown>;
144
+ } | undefined;
145
+ })[];
146
+ })[];
147
+ } | {
148
+ name: string;
149
+ title: string;
150
+ type: string;
151
+ of: ({
152
+ name: string;
153
+ title: string;
154
+ type: string;
155
+ fields: ({
156
+ name: string;
157
+ type: string;
158
+ } | {
159
+ name: string;
160
+ type: string;
161
+ title: string;
162
+ fields: ({
163
+ name: string;
164
+ type: string;
165
+ weak?: undefined;
166
+ to?: undefined;
167
+ options?: undefined;
168
+ } | {
169
+ name: string;
170
+ type: string;
171
+ weak: boolean;
172
+ to: {
173
+ type: string;
174
+ }[];
175
+ options: {
176
+ filter?: string;
177
+ filterParams?: Record<string, unknown>;
178
+ } | undefined;
179
+ })[];
180
+ })[];
181
+ } | {
182
+ type: string;
183
+ })[];
184
+ })[];
185
+ declare const _default: {
186
+ type: "document";
187
+ name: "hierarchy.tree";
188
+ } & Omit<import("sanity").DocumentDefinition, "preview"> & {
189
+ preview?: import("sanity").PreviewConfig<Record<string, string>, Record<never, any>> | undefined;
190
+ };
191
+ interface TreeItemWithChildren extends StoredTreeItem {
192
+ [key: string]: unknown;
193
+ children?: TreeItemWithChildren[];
238
194
  }
239
-
240
- export {}
195
+ /**
196
+ * Converts the flat array stored in the Sanity document into a nested tree,
197
+ * nesting items under their `parent` _key. Items without a `parent` are roots.
198
+ */
199
+ declare function flatDataToTree(data: StoredTreeItem[]): TreeItemWithChildren[];
200
+ /**
201
+ * Usage in `sanity.config.ts` (or .js)
202
+ *
203
+ * ```ts
204
+ * import {defineConfig} from 'sanity'
205
+ * import {hierarchicalDocumentList} from '@sanity/hierarchical-document-list'
206
+ *
207
+ * export default defineConfig({
208
+ * // ...
209
+ * plugins: [hierarchicalDocumentList()],
210
+ * })
211
+ * ```
212
+ */
213
+ declare const hierarchicalDocumentList: import("sanity").Plugin<void>;
214
+ export { type TreeProps, createDeskHierarchy, createHierarchicalSchemas, flatDataToTree, hierarchicalDocumentList, _default as hierarchyTree };
215
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/utils/injectNodeTypeInPatches.ts","../src/types.ts","../src/createDeskHierarchy.tsx","../src/createHierarchicalSchemas.tsx","../src/schemas/hierarchy.tree.ts","../src/utils/flatDataToTree.ts","../src/index.ts"],"mappings":";;cAsBa,kBAAA;AAAA,cACA,wBAAA;AAAA,UClBH,eAAA;EACR,KAAA;EACA,IAAA;EACA,KAAA;AAAA;ADeF;;;AAAA,UCTiB,cAAA;EACf,IAAA;EACA,KAAA,SAAc,kBAAA;EACd,KAAA;IACE,KAAA,SAAc,wBAAA;IACd,SAAA,GAAY,eAAA;IACZ,OAAA;EAAA;EAbF;;;EAkBA,MAAA;AAAA;AAAA,UAgCe,gBAAA;EAkBE;;;;EAbjB,WAAA;EAqCe;;;EAhCf,gBAAA;IAgC8C;;;IA5B5C,MAAA;IAuCgB;;ACvGpB;IDoEI,YAAA,GAAe,MAAM;EAAA;ECpEgC;;;ED0EvD,QAAA;EC3DA;;;;AAQc;AACf;ED0DC,YAAA;AAAA;AAAA,UAUe,sBAAA,SAA+B,gBAAgB;EErF9C;;;EFyFhB,UAAA;EEkCsB;;;;;EF3BtB,kBAAA;AAAA;AAAA,UCvGe,SAAA,SAAkB,sBAAsB;;AFezD;;;EEVE,KAAA;EFU2E;AAC7E;;EENE,IAAA;EFMsF;AAAA;;EEDtF,OAAA;EDjBuB;;;ECqBvB,CAAA;EDnBA;;;ECuBA,cAAA;AAAA;AAAA,iBAesB,mBAAA,CAAoB,KAAgB,EAAT,SAAS;AAAA,KC/BvD,aAAA,GAAgB,IAAI,CAAC,sBAAA;AAAA,iBA2HF,yBAAA,CAA0B,OAAA,EAAS,aAAA;;;;;;;oBApDzC,EAAA;;;;;;;;;;;;MA0CS,EAAA;MAAY,IAAA;IAAA,IAAmB,MAAA;EAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UE7HhD,oBAAA,SAA6B,cAAc;EAAA,CAElD,GAAA;EACD,QAAA,GAAW,oBAAA;AAAA;;;ALiBgE;AAC7E;iBKXwB,cAAA,CAAe,IAAA,EAAM,cAAA,KAAmB,oBAAoB;;ALUP;AAC7E;;;;AAAwF;;ACpBS;;;;;cKqBpF,wBAAA,mBAAwB,MAAA"}