@sanity/hierarchical-document-list 0.1.0-next.3 → 0.1.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/lib/TreeDeskStructure.js +46 -22
- package/lib/TreeInputComponent.js +14 -3
- package/lib/components/DeskWarning.js +18 -5
- package/lib/components/DocumentInNode.js +29 -17
- package/lib/components/DocumentPreviewStatus.js +26 -7
- package/lib/components/NodeActions.js +24 -11
- package/lib/components/NodeContentRenderer.js +42 -40
- package/lib/components/PlaceholderDropzone.js +17 -6
- package/lib/components/TreeEditor.js +29 -16
- package/lib/components/TreeEditorErrorBoundary.js +50 -21
- package/lib/components/TreeNodeRenderer.js +25 -12
- package/lib/components/TreeNodeRendererScaffold.js +12 -134
- package/lib/createDeskHierarchy.js +30 -17
- package/lib/createHierarchicalField.js +29 -14
- package/lib/schemas/hierarchy.tree.js +4 -3
- package/lib/utils/flatDataToTree.js +15 -6
- package/lib/utils/getAdjescentNodes.js +7 -6
- package/lib/utils/getCommonTreeProps.js +14 -2
- package/lib/utils/getTreeHeight.js +3 -3
- package/lib/utils/gradientPatchAdapter.js +16 -15
- package/lib/utils/idUtils.js +4 -2
- package/lib/utils/moveItemInArray.js +15 -5
- package/lib/utils/treeData.js +65 -36
- package/lib/utils/treePatches.js +57 -38
- package/lib/utils/useAllItems.js +43 -41
- package/lib/utils/useLocalTree.js +20 -10
- package/lib/utils/useTreeOperations.js +1 -1
- package/lib/utils/useTreeOperationsProvider.js +25 -17
- package/package.json +1 -1
- package/tsconfig.json +1 -1
package/lib/utils/treeData.js
CHANGED
|
@@ -1,43 +1,66 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
13
|
+
var t = {};
|
|
14
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
15
|
+
t[p] = s[p];
|
|
16
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
17
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
18
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
19
|
+
t[p[i]] = s[p[i]];
|
|
20
|
+
}
|
|
21
|
+
return t;
|
|
22
|
+
};
|
|
23
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
24
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
25
|
+
if (ar || !(i in from)) {
|
|
26
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
27
|
+
ar[i] = from[i];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
31
|
+
};
|
|
1
32
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
33
|
import { randomKey } from '@sanity/util/content';
|
|
3
34
|
import DocumentInNode from '../components/DocumentInNode';
|
|
4
35
|
import NodeActions from '../components/NodeActions';
|
|
5
36
|
import flatDataToTree from './flatDataToTree';
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
publishedId: publishedDoc?._id,
|
|
19
|
-
draftUpdatedAt: draftDoc?._updatedAt,
|
|
20
|
-
publishedUpdatedAt: publishedDoc?._updatedAt
|
|
21
|
-
};
|
|
22
|
-
return {
|
|
23
|
-
...enhancedItem,
|
|
24
|
-
title: (nodeProps) => (_jsx(DocumentInNode, { item: enhancedItem, action: _jsx(NodeActions, { nodeProps: nodeProps }, void 0) }, void 0)),
|
|
25
|
-
children: []
|
|
26
|
-
};
|
|
37
|
+
export var dataToEditorTree = function (_a) {
|
|
38
|
+
var tree = _a.tree, allItems = _a.allItems, visibilityMap = _a.visibilityMap;
|
|
39
|
+
var itemsWithTitle = tree
|
|
40
|
+
.filter(function (item) { var _a, _b; return (_b = (_a = item === null || item === void 0 ? void 0 : item.value) === null || _a === void 0 ? void 0 : _a.reference) === null || _b === void 0 ? void 0 : _b._ref; })
|
|
41
|
+
.map(function (item) {
|
|
42
|
+
var _a, _b;
|
|
43
|
+
var refId = (_b = (_a = item.value) === null || _a === void 0 ? void 0 : _a.reference) === null || _b === void 0 ? void 0 : _b._ref;
|
|
44
|
+
var docPair = refId ? allItems[refId] : undefined;
|
|
45
|
+
var draftDoc = docPair === null || docPair === void 0 ? void 0 : docPair.draft;
|
|
46
|
+
var publishedDoc = docPair === null || docPair === void 0 ? void 0 : docPair.published;
|
|
47
|
+
var enhancedItem = __assign(__assign({}, item), { expanded: visibilityMap[item._key] !== false, draftId: draftDoc === null || draftDoc === void 0 ? void 0 : draftDoc._id, publishedId: publishedDoc === null || publishedDoc === void 0 ? void 0 : publishedDoc._id, draftUpdatedAt: draftDoc === null || draftDoc === void 0 ? void 0 : draftDoc._updatedAt, publishedUpdatedAt: publishedDoc === null || publishedDoc === void 0 ? void 0 : publishedDoc._updatedAt });
|
|
48
|
+
return __assign(__assign({}, enhancedItem), { title: function (nodeProps) { return (_jsx(DocumentInNode, { item: enhancedItem, action: _jsx(NodeActions, { nodeProps: nodeProps }, void 0) }, void 0)); }, children: [] });
|
|
27
49
|
});
|
|
28
50
|
return flatDataToTree(itemsWithTitle);
|
|
29
51
|
};
|
|
30
|
-
|
|
31
|
-
|
|
52
|
+
var documentPairToNode = function (doc) {
|
|
53
|
+
var _a, _b, _c, _d;
|
|
54
|
+
if (!((_a = doc === null || doc === void 0 ? void 0 : doc.published) === null || _a === void 0 ? void 0 : _a._id)) {
|
|
32
55
|
return undefined;
|
|
33
56
|
}
|
|
34
57
|
return {
|
|
35
58
|
_key: randomKey(12),
|
|
36
59
|
_type: 'hierarchy.node',
|
|
37
|
-
draftId: doc.draft
|
|
38
|
-
draftUpdatedAt: doc.draft
|
|
60
|
+
draftId: (_b = doc.draft) === null || _b === void 0 ? void 0 : _b._id,
|
|
61
|
+
draftUpdatedAt: (_c = doc.draft) === null || _c === void 0 ? void 0 : _c._updatedAt,
|
|
39
62
|
publishedId: doc.published._id,
|
|
40
|
-
publishedUpdatedAt: doc.published
|
|
63
|
+
publishedUpdatedAt: (_d = doc.published) === null || _d === void 0 ? void 0 : _d._updatedAt,
|
|
41
64
|
value: {
|
|
42
65
|
reference: {
|
|
43
66
|
_ref: doc.published._id,
|
|
@@ -48,23 +71,29 @@ const documentPairToNode = (doc) => {
|
|
|
48
71
|
}
|
|
49
72
|
};
|
|
50
73
|
};
|
|
51
|
-
export
|
|
52
|
-
return tree.reduce((flattened, item)
|
|
53
|
-
|
|
54
|
-
return [
|
|
74
|
+
export var flatTree = function (tree) {
|
|
75
|
+
return tree.reduce(function (flattened, item) {
|
|
76
|
+
var children = item.children, node = __rest(item, ["children"]);
|
|
77
|
+
return __spreadArray(__spreadArray(__spreadArray([], flattened, true), [node], false), (Array.isArray(children) ? flatTree(children) : []), true);
|
|
55
78
|
}, []);
|
|
56
79
|
};
|
|
57
|
-
export
|
|
80
|
+
export var getUnaddedItems = function (data) {
|
|
58
81
|
if (!data.tree) {
|
|
59
82
|
return Object.entries(data.allItems)
|
|
60
|
-
.map((value)
|
|
83
|
+
.map(function (value) { return documentPairToNode(value[1]); })
|
|
61
84
|
.filter(Boolean);
|
|
62
85
|
}
|
|
63
86
|
return Object.entries(data.allItems)
|
|
64
|
-
.filter((
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
87
|
+
.filter(function (_a) {
|
|
88
|
+
var publishedId = _a[0];
|
|
89
|
+
return publishedId &&
|
|
90
|
+
// unadded items shouldn't be in the tree
|
|
91
|
+
!data.tree.some(function (treeItem) { var _a, _b; return ((_b = (_a = treeItem === null || treeItem === void 0 ? void 0 : treeItem.value) === null || _a === void 0 ? void 0 : _a.reference) === null || _b === void 0 ? void 0 : _b._ref) === publishedId; });
|
|
92
|
+
})
|
|
93
|
+
.map(function (_a) {
|
|
94
|
+
var _publishedId = _a[0], documentPair = _a[1];
|
|
95
|
+
return documentPairToNode(documentPair);
|
|
96
|
+
})
|
|
68
97
|
.filter(Boolean);
|
|
69
98
|
};
|
|
70
99
|
export function normalizeNodeForStorage(item) {
|
package/lib/utils/treePatches.js
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
13
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
14
|
+
if (ar || !(i in from)) {
|
|
15
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
16
|
+
ar[i] = from[i];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
20
|
+
};
|
|
1
21
|
import * as Patch from '@sanity/form-builder/lib/patch/patches';
|
|
2
22
|
import { randomKey } from '@sanity/util/content';
|
|
3
23
|
import { getFlatDataFromTree } from 'react-sortable-tree';
|
|
@@ -5,51 +25,48 @@ import getAdjescentNodes from './getAdjescentNodes';
|
|
|
5
25
|
import moveItemInArray from './moveItemInArray';
|
|
6
26
|
import { normalizeNodeForStorage } from './treeData';
|
|
7
27
|
export function getAddItemPatch(item) {
|
|
8
|
-
|
|
28
|
+
var normalizedNode = normalizeNodeForStorage(item);
|
|
9
29
|
return [
|
|
10
30
|
// Add the node to the end of the tree
|
|
11
31
|
Patch.insert([normalizedNode], 'after', [-1])
|
|
12
32
|
];
|
|
13
33
|
}
|
|
14
34
|
export function getDuplicateItemPatch(nodeProps) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
_key: randomKey(12)
|
|
18
|
-
};
|
|
19
|
-
const normalizedNode = normalizeNodeForStorage(newItem);
|
|
35
|
+
var newItem = __assign(__assign({}, nodeProps.node), { _key: randomKey(12) });
|
|
36
|
+
var normalizedNode = normalizeNodeForStorage(newItem);
|
|
20
37
|
return [
|
|
21
38
|
// Add duplicated node before the existing one
|
|
22
39
|
Patch.insert([normalizedNode], 'before', [{ _key: nodeProps.node._key }])
|
|
23
40
|
];
|
|
24
41
|
}
|
|
25
|
-
export function getRemoveItemPatch(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
42
|
+
export function getRemoveItemPatch(_a) {
|
|
43
|
+
var node = _a.node;
|
|
44
|
+
var keyPath = { _key: node._key };
|
|
45
|
+
var children = getChildrenPaths(node);
|
|
46
|
+
return __spreadArray([
|
|
29
47
|
// 1. Unset the removed node
|
|
30
|
-
Patch.unset([keyPath])
|
|
31
|
-
|
|
32
|
-
...children.map((path) => Patch.unset([{ _key: path }]))
|
|
33
|
-
];
|
|
48
|
+
Patch.unset([keyPath])
|
|
49
|
+
], children.map(function (path) { return Patch.unset([{ _key: path }]); }), true);
|
|
34
50
|
}
|
|
35
51
|
export function getMovedNodePatch(data) {
|
|
36
|
-
|
|
37
|
-
|
|
52
|
+
var _a, _b;
|
|
53
|
+
var nextParentNode = data.nextParentNode;
|
|
54
|
+
var keyPath = { _key: data.node._key };
|
|
38
55
|
// === REMOVING NODE FROM TREE ===
|
|
39
56
|
// `nextPath` will be null if the item is removed from tree
|
|
40
57
|
if (!Array.isArray(data.nextPath)) {
|
|
41
58
|
return getRemoveItemPatch({ node: data.node });
|
|
42
59
|
}
|
|
43
|
-
|
|
60
|
+
var nextFlatTree = getFlatDataFromTree({
|
|
44
61
|
treeData: data.treeData,
|
|
45
|
-
getNodeKey: (t)
|
|
62
|
+
getNodeKey: function (t) { return t.node._key; }
|
|
46
63
|
});
|
|
47
|
-
|
|
48
|
-
|
|
64
|
+
var normalizedNode = normalizeNodeForStorage(data.node);
|
|
65
|
+
var _c = getAdjescentNodes({
|
|
49
66
|
flatTree: nextFlatTree,
|
|
50
67
|
node: data.node,
|
|
51
68
|
treeIndex: data.nextTreeIndex
|
|
52
|
-
});
|
|
69
|
+
}), leadingNode = _c.leadingNode, followingNode = _c.followingNode;
|
|
53
70
|
return [
|
|
54
71
|
// 1. Unset the moved node
|
|
55
72
|
// (will be ignored by Content Lake on new nodes with _key not yet in tree)
|
|
@@ -57,12 +74,12 @@ export function getMovedNodePatch(data) {
|
|
|
57
74
|
// 2. SIBLING-BASED PLACEMENT
|
|
58
75
|
// If we were to place solely based on nextTreeIndex, concurrent changes from other editors could put the new node in an unexpected position.
|
|
59
76
|
// Let's instead anchor it to the _key of the sibling coming before or after it.
|
|
60
|
-
leadingNode
|
|
77
|
+
((_a = leadingNode === null || leadingNode === void 0 ? void 0 : leadingNode.node) === null || _a === void 0 ? void 0 : _a._key)
|
|
61
78
|
? // After the sibling before it
|
|
62
79
|
Patch.insert([normalizedNode], 'after', [{ _key: leadingNode.node._key }])
|
|
63
80
|
: // Or before the sibling right after it, in case there's no leading sibling node
|
|
64
81
|
// prettier-ignore
|
|
65
|
-
Patch.insert([normalizedNode], 'before', [followingNode
|
|
82
|
+
Patch.insert([normalizedNode], 'before', [((_b = followingNode === null || followingNode === void 0 ? void 0 : followingNode.node) === null || _b === void 0 ? void 0 : _b._key) ? { _key: followingNode.node._key } : data.nextTreeIndex]),
|
|
66
83
|
// 3. Patch the new node with its new `parent`
|
|
67
84
|
nextParentNode
|
|
68
85
|
? // If it has a parent node, set that parent's _key
|
|
@@ -76,41 +93,43 @@ function getChildrenPaths(node) {
|
|
|
76
93
|
return [];
|
|
77
94
|
}
|
|
78
95
|
return node.children
|
|
79
|
-
.reduce((keyPaths, child)
|
|
96
|
+
.reduce(function (keyPaths, child) { return __spreadArray(__spreadArray(__spreadArray([], keyPaths, true), [child._key], false), getChildrenPaths(child), true); }, [])
|
|
80
97
|
.filter(Boolean);
|
|
81
98
|
}
|
|
82
|
-
export function getMoveItemPatch(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
99
|
+
export function getMoveItemPatch(_a) {
|
|
100
|
+
var _b, _c;
|
|
101
|
+
var _d = _a.nodeProps, node = _d.node, treeIndex = _d.treeIndex, localTree = _a.localTree, _e = _a.direction, direction = _e === void 0 ? 'up' : _e;
|
|
102
|
+
var keyPath = { _key: node._key };
|
|
103
|
+
var nextTreeIndex = treeIndex + (direction === 'up' ? -1 : 1);
|
|
104
|
+
var flatTree = getFlatDataFromTree({
|
|
86
105
|
treeData: localTree,
|
|
87
|
-
getNodeKey: (t)
|
|
106
|
+
getNodeKey: function (t) { return t.node._key; }
|
|
88
107
|
});
|
|
89
|
-
|
|
108
|
+
var nextFlatTree = moveItemInArray({
|
|
90
109
|
array: flatTree,
|
|
91
110
|
fromIndex: treeIndex,
|
|
92
111
|
toIndex: nextTreeIndex
|
|
93
112
|
});
|
|
94
|
-
|
|
113
|
+
var _f = getAdjescentNodes({
|
|
95
114
|
flatTree: nextFlatTree,
|
|
96
|
-
node,
|
|
115
|
+
node: node,
|
|
97
116
|
treeIndex: nextTreeIndex
|
|
98
|
-
});
|
|
99
|
-
|
|
117
|
+
}), leadingNode = _f.leadingNode, followingNode = _f.followingNode;
|
|
118
|
+
var normalizedNode = normalizeNodeForStorage(node);
|
|
100
119
|
// When moving up, look at following node to figure out what is the next parent.
|
|
101
|
-
|
|
102
|
-
|
|
120
|
+
var nodeToInheritParent = direction === 'up' ? followingNode : leadingNode;
|
|
121
|
+
var nextParentNode = nodeToInheritParent === null || nodeToInheritParent === void 0 ? void 0 : nodeToInheritParent.parentNode;
|
|
103
122
|
return [
|
|
104
123
|
// 1. Unset the moved node
|
|
105
124
|
// (will be ignored by Content Lake on new nodes with _key not yet in tree)
|
|
106
125
|
Patch.unset([keyPath]),
|
|
107
126
|
// 2. SIBLING-BASED PLACEMENT
|
|
108
|
-
leadingNode
|
|
127
|
+
((_b = leadingNode === null || leadingNode === void 0 ? void 0 : leadingNode.node) === null || _b === void 0 ? void 0 : _b._key)
|
|
109
128
|
? // After the sibling before it
|
|
110
129
|
Patch.insert([normalizedNode], 'after', [{ _key: leadingNode.node._key }])
|
|
111
130
|
: // Or before the sibling right after it, in case there's no leading sibling node
|
|
112
131
|
Patch.insert([normalizedNode], 'before', [
|
|
113
|
-
followingNode
|
|
132
|
+
((_c = followingNode === null || followingNode === void 0 ? void 0 : followingNode.node) === null || _c === void 0 ? void 0 : _c._key) ? { _key: followingNode.node._key } : nextTreeIndex
|
|
114
133
|
]),
|
|
115
134
|
// 3. Patch the new node with its new `parent`
|
|
116
135
|
nextParentNode
|
package/lib/utils/useAllItems.js
CHANGED
|
@@ -1,48 +1,54 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
1
12
|
import sanityClient from 'part:@sanity/base/client';
|
|
2
13
|
import React from 'react';
|
|
3
14
|
import { isDraft, unprefixId } from './idUtils';
|
|
4
|
-
|
|
15
|
+
var client = sanityClient.withConfig({
|
|
5
16
|
apiVersion: '2021-09-01'
|
|
6
17
|
});
|
|
7
|
-
function getDeskFilter(
|
|
8
|
-
|
|
9
|
-
|
|
18
|
+
function getDeskFilter(_a) {
|
|
19
|
+
var referenceTo = _a.referenceTo, referenceOptions = _a.referenceOptions;
|
|
20
|
+
var filterParts = ['_type in $docTypes'];
|
|
21
|
+
if (referenceOptions === null || referenceOptions === void 0 ? void 0 : referenceOptions.filter) {
|
|
10
22
|
filterParts.push(referenceOptions.filter);
|
|
11
23
|
}
|
|
12
24
|
return {
|
|
13
25
|
filter: filterParts.join(' && '),
|
|
14
|
-
params: {
|
|
15
|
-
...(referenceOptions?.filterParams || {}),
|
|
16
|
-
docTypes: referenceTo.map((schemaType) => schemaType)
|
|
17
|
-
}
|
|
26
|
+
params: __assign(__assign({}, ((referenceOptions === null || referenceOptions === void 0 ? void 0 : referenceOptions.filterParams) || {})), { docTypes: referenceTo.map(function (schemaType) { return schemaType; }) })
|
|
18
27
|
};
|
|
19
28
|
}
|
|
20
29
|
function updateItemInState(state, item) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
[isDraft(item._id) ? 'draft' : 'published']: item
|
|
26
|
-
};
|
|
30
|
+
var _a;
|
|
31
|
+
var newState = __assign({}, state);
|
|
32
|
+
var publishedId = unprefixId(item._id);
|
|
33
|
+
newState[publishedId] = __assign(__assign({}, (newState[publishedId] || {})), (_a = {}, _a[isDraft(item._id) ? 'draft' : 'published'] = item, _a));
|
|
27
34
|
return newState;
|
|
28
35
|
}
|
|
29
36
|
function allItemsReducer(state, action) {
|
|
30
|
-
|
|
37
|
+
var _a;
|
|
38
|
+
var _b, _c, _d;
|
|
39
|
+
if (action.type === 'addOrEditItem' && ((_b = action.item) === null || _b === void 0 ? void 0 : _b._id)) {
|
|
31
40
|
return updateItemInState(state, action.item);
|
|
32
41
|
}
|
|
33
42
|
if (action.type === 'removeItem') {
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
{
|
|
40
|
-
published: state[publishedId]?.published
|
|
41
|
-
}
|
|
42
|
-
: {
|
|
43
|
-
draft: state[publishedId]?.draft
|
|
43
|
+
var publishedId = unprefixId(action.itemId);
|
|
44
|
+
return __assign(__assign({}, state), (_a = {}, _a[publishedId] = isDraft(action.itemId)
|
|
45
|
+
? // If a draft, keep only published
|
|
46
|
+
{
|
|
47
|
+
published: (_c = state[publishedId]) === null || _c === void 0 ? void 0 : _c.published
|
|
44
48
|
}
|
|
45
|
-
|
|
49
|
+
: {
|
|
50
|
+
draft: (_d = state[publishedId]) === null || _d === void 0 ? void 0 : _d.draft
|
|
51
|
+
}, _a));
|
|
46
52
|
}
|
|
47
53
|
if (action.type === 'setInitialData') {
|
|
48
54
|
return action.items.reduce(updateItemInState, {});
|
|
@@ -50,8 +56,8 @@ function allItemsReducer(state, action) {
|
|
|
50
56
|
return state;
|
|
51
57
|
}
|
|
52
58
|
export default function useAllItems(options) {
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
var _a = React.useState('loading'), status = _a[0], setStatus = _a[1];
|
|
60
|
+
var _b = React.useReducer(allItemsReducer, {}), allItems = _b[0], dispatch = _b[1];
|
|
55
61
|
function handleListener(event) {
|
|
56
62
|
if (event.type !== 'mutation') {
|
|
57
63
|
return;
|
|
@@ -64,29 +70,25 @@ export default function useAllItems(options) {
|
|
|
64
70
|
}
|
|
65
71
|
}
|
|
66
72
|
function handleFirstLoad(items) {
|
|
67
|
-
dispatch({ type: 'setInitialData', items });
|
|
73
|
+
dispatch({ type: 'setInitialData', items: items });
|
|
68
74
|
setStatus('success');
|
|
69
75
|
}
|
|
70
|
-
React.useEffect(()
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
_id,
|
|
74
|
-
_type,
|
|
75
|
-
_updatedAt,
|
|
76
|
-
}`;
|
|
76
|
+
React.useEffect(function () {
|
|
77
|
+
var _a = getDeskFilter(options), filter = _a.filter, params = _a.params;
|
|
78
|
+
var query = "*[".concat(filter, "] {\n _id,\n _type,\n _updatedAt,\n }");
|
|
77
79
|
client
|
|
78
80
|
.fetch(query, params)
|
|
79
81
|
.then(handleFirstLoad)
|
|
80
|
-
.catch(()
|
|
82
|
+
.catch(function () {
|
|
81
83
|
setStatus('error');
|
|
82
84
|
});
|
|
83
|
-
|
|
84
|
-
return ()
|
|
85
|
+
var listener = client.listen(query, params).subscribe(handleListener);
|
|
86
|
+
return function () {
|
|
85
87
|
listener.unsubscribe();
|
|
86
88
|
};
|
|
87
89
|
}, []);
|
|
88
90
|
return {
|
|
89
|
-
status,
|
|
90
|
-
allItems
|
|
91
|
+
status: status,
|
|
92
|
+
allItems: allItems
|
|
91
93
|
};
|
|
92
94
|
}
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
1
12
|
import React from 'react';
|
|
2
13
|
import { dataToEditorTree } from './treeData';
|
|
3
14
|
/**
|
|
@@ -8,20 +19,19 @@ import { dataToEditorTree } from './treeData';
|
|
|
8
19
|
* Doesn't modify the main tree or has side-effects on data.
|
|
9
20
|
* Has the added benefit of being local to the user, so external changes won't affect local visibility.
|
|
10
21
|
*/
|
|
11
|
-
export default function useLocalTree(
|
|
12
|
-
|
|
22
|
+
export default function useLocalTree(_a) {
|
|
23
|
+
var tree = _a.tree, allItems = _a.allItems;
|
|
24
|
+
var _b = React.useState({}), visibilityMap = _b[0], setVisibilityMap = _b[1];
|
|
13
25
|
function handleVisibilityToggle(data) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
[data.node._key]: data.expanded
|
|
17
|
-
});
|
|
26
|
+
var _a;
|
|
27
|
+
setVisibilityMap(__assign(__assign({}, visibilityMap), (_a = {}, _a[data.node._key] = data.expanded, _a)));
|
|
18
28
|
}
|
|
19
29
|
return {
|
|
20
30
|
localTree: dataToEditorTree({
|
|
21
|
-
tree,
|
|
22
|
-
allItems,
|
|
23
|
-
visibilityMap
|
|
31
|
+
tree: tree,
|
|
32
|
+
allItems: allItems,
|
|
33
|
+
visibilityMap: visibilityMap
|
|
24
34
|
}),
|
|
25
|
-
handleVisibilityToggle
|
|
35
|
+
handleVisibilityToggle: handleVisibilityToggle
|
|
26
36
|
};
|
|
27
37
|
}
|
|
@@ -2,7 +2,7 @@ import React from 'react';
|
|
|
2
2
|
function placeholder() {
|
|
3
3
|
// no-op
|
|
4
4
|
}
|
|
5
|
-
export
|
|
5
|
+
export var TreeOperationsContext = React.createContext({
|
|
6
6
|
addItem: placeholder,
|
|
7
7
|
duplicateItem: placeholder,
|
|
8
8
|
removeItem: placeholder,
|
|
@@ -1,17 +1,25 @@
|
|
|
1
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
2
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
3
|
+
if (ar || !(i in from)) {
|
|
4
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
5
|
+
ar[i] = from[i];
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
9
|
+
};
|
|
1
10
|
import * as Patch from '@sanity/form-builder/lib/patch/patches';
|
|
2
11
|
import PatchEvent from '@sanity/form-builder/PatchEvent';
|
|
3
12
|
import { getAddItemPatch, getDuplicateItemPatch, getMovedNodePatch, getMoveItemPatch, getRemoveItemPatch } from './treePatches';
|
|
4
13
|
export default function useTreeOperationsProvider(props) {
|
|
5
|
-
|
|
14
|
+
var localTree = props.localTree;
|
|
6
15
|
function runPatches(patches) {
|
|
7
|
-
|
|
16
|
+
var finalPatches = __spreadArray([
|
|
8
17
|
// Ensure tree array exists before any operation
|
|
9
|
-
Patch.setIfMissing([])
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
let patchEvent = PatchEvent.from(finalPatches);
|
|
18
|
+
Patch.setIfMissing([])
|
|
19
|
+
], (patches || []), true);
|
|
20
|
+
var patchEvent = PatchEvent.from(finalPatches);
|
|
13
21
|
if (props.patchPrefix) {
|
|
14
|
-
patchEvent = PatchEvent.from(finalPatches.map((patch)
|
|
22
|
+
patchEvent = PatchEvent.from(finalPatches.map(function (patch) { return Patch.prefixPath(patch, props.patchPrefix); }));
|
|
15
23
|
}
|
|
16
24
|
props.onChange(patchEvent);
|
|
17
25
|
}
|
|
@@ -29,24 +37,24 @@ export default function useTreeOperationsProvider(props) {
|
|
|
29
37
|
}
|
|
30
38
|
function moveItemUp(nodeProps) {
|
|
31
39
|
runPatches(getMoveItemPatch({
|
|
32
|
-
nodeProps,
|
|
33
|
-
localTree,
|
|
40
|
+
nodeProps: nodeProps,
|
|
41
|
+
localTree: localTree,
|
|
34
42
|
direction: 'up'
|
|
35
43
|
}));
|
|
36
44
|
}
|
|
37
45
|
function moveItemDown(nodeProps) {
|
|
38
46
|
runPatches(getMoveItemPatch({
|
|
39
|
-
nodeProps,
|
|
40
|
-
localTree,
|
|
47
|
+
nodeProps: nodeProps,
|
|
48
|
+
localTree: localTree,
|
|
41
49
|
direction: 'down'
|
|
42
50
|
}));
|
|
43
51
|
}
|
|
44
52
|
return {
|
|
45
|
-
handleMovedNode,
|
|
46
|
-
addItem,
|
|
47
|
-
removeItem,
|
|
48
|
-
moveItemUp,
|
|
49
|
-
moveItemDown,
|
|
50
|
-
duplicateItem
|
|
53
|
+
handleMovedNode: handleMovedNode,
|
|
54
|
+
addItem: addItem,
|
|
55
|
+
removeItem: removeItem,
|
|
56
|
+
moveItemUp: moveItemUp,
|
|
57
|
+
moveItemDown: moveItemDown,
|
|
58
|
+
duplicateItem: duplicateItem
|
|
51
59
|
};
|
|
52
60
|
}
|
package/package.json
CHANGED