@sanity/hierarchical-document-list 0.1.0-next.3 → 1.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 (59) hide show
  1. package/README.md +99 -26
  2. package/lib/TreeDeskStructure.d.ts +2 -1
  3. package/lib/TreeDeskStructure.js +82 -33
  4. package/lib/TreeInputComponent.d.ts +1 -1
  5. package/lib/TreeInputComponent.js +50 -8
  6. package/lib/components/DeskWarning.d.ts +1 -1
  7. package/lib/components/DeskWarning.js +43 -9
  8. package/lib/components/DocumentInNode.d.ts +1 -1
  9. package/lib/components/DocumentInNode.js +64 -28
  10. package/lib/components/DocumentPreviewStatus.d.ts +1 -1
  11. package/lib/components/DocumentPreviewStatus.js +36 -13
  12. package/lib/components/NodeActions.d.ts +1 -1
  13. package/lib/components/NodeActions.js +55 -18
  14. package/lib/components/NodeContentRenderer.js +75 -49
  15. package/lib/components/PlaceholderDropzone.d.ts +1 -1
  16. package/lib/components/PlaceholderDropzone.js +22 -9
  17. package/lib/components/TreeEditor.d.ts +1 -1
  18. package/lib/components/TreeEditor.js +48 -30
  19. package/lib/components/TreeEditorErrorBoundary.d.ts +2 -16
  20. package/lib/components/TreeEditorErrorBoundary.js +50 -31
  21. package/lib/components/TreeNodeRenderer.js +53 -16
  22. package/lib/components/TreeNodeRendererScaffold.d.ts +1 -1
  23. package/lib/components/TreeNodeRendererScaffold.js +19 -139
  24. package/lib/createDeskHierarchy.js +58 -27
  25. package/lib/createHierarchicalSchemas.d.ts +78 -0
  26. package/lib/createHierarchicalSchemas.js +138 -0
  27. package/lib/{utils → hooks}/useAllItems.d.ts +0 -0
  28. package/lib/hooks/useAllItems.js +119 -0
  29. package/lib/{utils → hooks}/useLocalTree.d.ts +0 -0
  30. package/lib/hooks/useLocalTree.js +59 -0
  31. package/lib/{utils → hooks}/useTreeOperations.d.ts +1 -1
  32. package/lib/hooks/useTreeOperations.js +39 -0
  33. package/lib/{utils → hooks}/useTreeOperationsProvider.d.ts +1 -1
  34. package/lib/hooks/useTreeOperationsProvider.js +85 -0
  35. package/lib/index.d.ts +1 -1
  36. package/lib/index.js +12 -3
  37. package/lib/schemas/hierarchy.tree.d.ts +5 -15
  38. package/lib/schemas/hierarchy.tree.js +10 -21
  39. package/lib/utils/flatDataToTree.js +21 -9
  40. package/lib/utils/getAdjescentNodes.js +10 -6
  41. package/lib/utils/getCommonTreeProps.js +28 -10
  42. package/lib/utils/getTreeHeight.js +9 -5
  43. package/lib/utils/gradientPatchAdapter.js +26 -18
  44. package/lib/utils/idUtils.js +9 -2
  45. package/lib/utils/injectNodeTypeInPatches.d.ts +12 -0
  46. package/lib/utils/injectNodeTypeInPatches.js +58 -0
  47. package/lib/utils/moveItemInArray.js +18 -5
  48. package/lib/utils/throwError.d.ts +7 -0
  49. package/lib/utils/throwError.js +12 -0
  50. package/lib/utils/treeData.js +87 -46
  51. package/lib/utils/treePatches.js +96 -47
  52. package/package.json +1 -2
  53. package/tsconfig.json +4 -4
  54. package/lib/createHierarchicalField.d.ts +0 -8
  55. package/lib/createHierarchicalField.js +0 -36
  56. package/lib/utils/useAllItems.js +0 -92
  57. package/lib/utils/useLocalTree.js +0 -27
  58. package/lib/utils/useTreeOperations.js +0 -16
  59. package/lib/utils/useTreeOperationsProvider.js +0 -52
@@ -1,92 +0,0 @@
1
- import sanityClient from 'part:@sanity/base/client';
2
- import React from 'react';
3
- import { isDraft, unprefixId } from './idUtils';
4
- const client = sanityClient.withConfig({
5
- apiVersion: '2021-09-01'
6
- });
7
- function getDeskFilter({ referenceTo, referenceOptions }) {
8
- const filterParts = ['_type in $docTypes'];
9
- if (referenceOptions?.filter) {
10
- filterParts.push(referenceOptions.filter);
11
- }
12
- return {
13
- filter: filterParts.join(' && '),
14
- params: {
15
- ...(referenceOptions?.filterParams || {}),
16
- docTypes: referenceTo.map((schemaType) => schemaType)
17
- }
18
- };
19
- }
20
- function updateItemInState(state, item) {
21
- const newState = { ...state };
22
- const publishedId = unprefixId(item._id);
23
- newState[publishedId] = {
24
- ...(newState[publishedId] || {}),
25
- [isDraft(item._id) ? 'draft' : 'published']: item
26
- };
27
- return newState;
28
- }
29
- function allItemsReducer(state, action) {
30
- if (action.type === 'addOrEditItem' && action.item?._id) {
31
- return updateItemInState(state, action.item);
32
- }
33
- if (action.type === 'removeItem') {
34
- const publishedId = unprefixId(action.itemId);
35
- return {
36
- ...state,
37
- [publishedId]: isDraft(action.itemId)
38
- ? // If a draft, keep only published
39
- {
40
- published: state[publishedId]?.published
41
- }
42
- : {
43
- draft: state[publishedId]?.draft
44
- }
45
- };
46
- }
47
- if (action.type === 'setInitialData') {
48
- return action.items.reduce(updateItemInState, {});
49
- }
50
- return state;
51
- }
52
- export default function useAllItems(options) {
53
- const [status, setStatus] = React.useState('loading');
54
- const [allItems, dispatch] = React.useReducer(allItemsReducer, {});
55
- function handleListener(event) {
56
- if (event.type !== 'mutation') {
57
- return;
58
- }
59
- if (event.result) {
60
- dispatch({ type: 'addOrEditItem', item: event.result });
61
- }
62
- else {
63
- dispatch({ type: 'removeItem', itemId: event.documentId });
64
- }
65
- }
66
- function handleFirstLoad(items) {
67
- dispatch({ type: 'setInitialData', items });
68
- setStatus('success');
69
- }
70
- React.useEffect(() => {
71
- const { filter, params } = getDeskFilter(options);
72
- const query = `*[${filter}] {
73
- _id,
74
- _type,
75
- _updatedAt,
76
- }`;
77
- client
78
- .fetch(query, params)
79
- .then(handleFirstLoad)
80
- .catch(() => {
81
- setStatus('error');
82
- });
83
- const listener = client.listen(query, params).subscribe(handleListener);
84
- return () => {
85
- listener.unsubscribe();
86
- };
87
- }, []);
88
- return {
89
- status,
90
- allItems
91
- };
92
- }
@@ -1,27 +0,0 @@
1
- import React from 'react';
2
- import { dataToEditorTree } from './treeData';
3
- /**
4
- * Enhances tree data with information on:
5
- * - `expanded` - native property of react-sortable-tree to determine collapsing & expanding of a node's children
6
- * - `draftId` & `publishedId` - refer to LocalTreeItem's type annotations
7
- *
8
- * Doesn't modify the main tree or has side-effects on data.
9
- * Has the added benefit of being local to the user, so external changes won't affect local visibility.
10
- */
11
- export default function useLocalTree({ tree, allItems }) {
12
- const [visibilityMap, setVisibilityMap] = React.useState({});
13
- function handleVisibilityToggle(data) {
14
- setVisibilityMap({
15
- ...visibilityMap,
16
- [data.node._key]: data.expanded
17
- });
18
- }
19
- return {
20
- localTree: dataToEditorTree({
21
- tree,
22
- allItems,
23
- visibilityMap
24
- }),
25
- handleVisibilityToggle
26
- };
27
- }
@@ -1,16 +0,0 @@
1
- import React from 'react';
2
- function placeholder() {
3
- // no-op
4
- }
5
- export const TreeOperationsContext = React.createContext({
6
- addItem: placeholder,
7
- duplicateItem: placeholder,
8
- removeItem: placeholder,
9
- handleMovedNode: placeholder,
10
- moveItemDown: placeholder,
11
- moveItemUp: placeholder,
12
- allItemsStatus: 'loading'
13
- });
14
- export default function useTreeOperations() {
15
- return React.useContext(TreeOperationsContext);
16
- }
@@ -1,52 +0,0 @@
1
- import * as Patch from '@sanity/form-builder/lib/patch/patches';
2
- import PatchEvent from '@sanity/form-builder/PatchEvent';
3
- import { getAddItemPatch, getDuplicateItemPatch, getMovedNodePatch, getMoveItemPatch, getRemoveItemPatch } from './treePatches';
4
- export default function useTreeOperationsProvider(props) {
5
- const { localTree } = props;
6
- function runPatches(patches) {
7
- const finalPatches = [
8
- // Ensure tree array exists before any operation
9
- Patch.setIfMissing([]),
10
- ...(patches || [])
11
- ];
12
- let patchEvent = PatchEvent.from(finalPatches);
13
- if (props.patchPrefix) {
14
- patchEvent = PatchEvent.from(finalPatches.map((patch) => Patch.prefixPath(patch, props.patchPrefix)));
15
- }
16
- props.onChange(patchEvent);
17
- }
18
- function handleMovedNode(data) {
19
- runPatches(getMovedNodePatch(data));
20
- }
21
- function addItem(item) {
22
- runPatches(getAddItemPatch(item));
23
- }
24
- function duplicateItem(nodeProps) {
25
- runPatches(getDuplicateItemPatch(nodeProps));
26
- }
27
- function removeItem(nodeProps) {
28
- runPatches(getRemoveItemPatch(nodeProps));
29
- }
30
- function moveItemUp(nodeProps) {
31
- runPatches(getMoveItemPatch({
32
- nodeProps,
33
- localTree,
34
- direction: 'up'
35
- }));
36
- }
37
- function moveItemDown(nodeProps) {
38
- runPatches(getMoveItemPatch({
39
- nodeProps,
40
- localTree,
41
- direction: 'down'
42
- }));
43
- }
44
- return {
45
- handleMovedNode,
46
- addItem,
47
- removeItem,
48
- moveItemUp,
49
- moveItemDown,
50
- duplicateItem
51
- };
52
- }