@sanity/hierarchical-document-list 0.1.0-next.1 → 0.1.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.
- package/README.md +91 -40
- package/lib/TreeDeskStructure.js +56 -28
- package/lib/TreeInputComponent.d.ts +3 -3
- package/lib/TreeInputComponent.js +14 -3
- package/lib/components/DeskWarning.d.ts +6 -0
- package/lib/components/DeskWarning.js +25 -0
- package/lib/components/DocumentInNode.d.ts +2 -2
- package/lib/components/DocumentInNode.js +29 -17
- package/lib/components/DocumentPreviewStatus.js +26 -7
- package/lib/components/NodeActions.d.ts +2 -2
- package/lib/components/NodeActions.js +24 -10
- package/lib/components/NodeContentRenderer.js +42 -40
- package/lib/components/PlaceholderDropzone.js +19 -8
- package/lib/components/TreeEditor.d.ts +2 -2
- package/lib/components/TreeEditor.js +29 -16
- package/lib/components/TreeEditorErrorBoundary.d.ts +1 -15
- package/lib/components/TreeEditorErrorBoundary.js +26 -28
- package/lib/components/TreeNodeRenderer.js +25 -12
- package/lib/components/TreeNodeRendererScaffold.js +12 -134
- package/lib/createDeskHierarchy.d.ts +4 -0
- package/lib/createDeskHierarchy.js +34 -19
- package/lib/createHierarchicalField.js +40 -19
- package/lib/{hiearchy.tree.d.ts → schemas/hierarchy.tree.d.ts} +0 -0
- package/lib/schemas/hierarchy.tree.js +31 -0
- package/lib/utils/flatDataToTree.d.ts +3 -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.d.ts +7 -7
- package/lib/utils/treeData.js +65 -36
- package/lib/utils/treePatches.d.ts +10 -8
- package/lib/utils/treePatches.js +58 -50
- package/lib/utils/useAllItems.js +43 -41
- package/lib/utils/useLocalTree.d.ts +5 -5
- package/lib/utils/useLocalTree.js +21 -11
- package/lib/utils/useTreeOperations.js +1 -1
- package/lib/utils/useTreeOperationsProvider.d.ts +7 -8
- package/lib/utils/useTreeOperationsProvider.js +25 -17
- package/package.json +11 -9
- package/sanity.json +1 -1
- package/screenshot-1.jpg +0 -0
- package/tsconfig.json +1 -1
- package/lib/hiearchy.tree.js +0 -28
|
@@ -1,32 +1,45 @@
|
|
|
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 { jsx as _jsx } from "react/jsx-runtime";
|
|
2
13
|
import S from '@sanity/desk-tool/structure-builder';
|
|
3
14
|
import { AddIcon } from '@sanity/icons';
|
|
4
15
|
import TreeDeskStructure from './TreeDeskStructure';
|
|
5
|
-
|
|
6
|
-
|
|
16
|
+
var deskTreeValidator = function (props) {
|
|
17
|
+
var documentId = props.documentId, referenceTo = props.referenceTo;
|
|
7
18
|
if (typeof documentId !== 'string' && !documentId) {
|
|
8
19
|
throw new Error('[hierarchical input] Please add a documentId to your tree');
|
|
9
20
|
}
|
|
10
21
|
if (!Array.isArray(referenceTo)) {
|
|
11
|
-
throw new Error(
|
|
22
|
+
throw new Error("[hierarchical input] Missing valid 'referenceTo' in createDeskHierarchy (documentId \"".concat(documentId, "\")"));
|
|
12
23
|
}
|
|
13
|
-
return (deskProps)
|
|
24
|
+
return function (deskProps) { return _jsx(TreeDeskStructure, __assign({}, deskProps, { options: props }), void 0); };
|
|
14
25
|
};
|
|
15
26
|
export default function createDeskHierarchy(props) {
|
|
16
|
-
|
|
17
|
-
|
|
27
|
+
var documentId = props.documentId, referenceTo = props.referenceTo, referenceOptions = props.referenceOptions;
|
|
28
|
+
var mainList = ((referenceTo === null || referenceTo === void 0 ? void 0 : referenceTo.length) === 1
|
|
18
29
|
? S.documentTypeList(referenceTo[0]).schemaType(referenceTo[0])
|
|
19
|
-
: S.documentList())
|
|
30
|
+
: S.documentList().filter('_type in $types').params({ types: referenceTo }))
|
|
20
31
|
.id(documentId)
|
|
21
|
-
.menuItems((referenceTo || []).map((schemaType)
|
|
22
|
-
.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
.menuItems((referenceTo || []).map(function (schemaType) {
|
|
33
|
+
return S.menuItem()
|
|
34
|
+
.intent({
|
|
35
|
+
type: 'create',
|
|
36
|
+
params: { type: schemaType, template: schemaType }
|
|
37
|
+
})
|
|
38
|
+
// @TODO: get the title for each schema type
|
|
39
|
+
.title(schemaType)
|
|
40
|
+
.icon(AddIcon);
|
|
41
|
+
}))
|
|
42
|
+
.canHandleIntent(function (intent, context) {
|
|
30
43
|
if (intent === 'edit' && context.id === props.documentId) {
|
|
31
44
|
return true;
|
|
32
45
|
}
|
|
@@ -35,18 +48,20 @@ export default function createDeskHierarchy(props) {
|
|
|
35
48
|
}
|
|
36
49
|
return false;
|
|
37
50
|
});
|
|
38
|
-
if (referenceOptions
|
|
51
|
+
if (referenceOptions === null || referenceOptions === void 0 ? void 0 : referenceOptions.filter) {
|
|
39
52
|
mainList = mainList.filter(referenceOptions.filter);
|
|
40
53
|
}
|
|
41
|
-
if (referenceOptions
|
|
54
|
+
if (referenceOptions === null || referenceOptions === void 0 ? void 0 : referenceOptions.filterParams) {
|
|
42
55
|
mainList = mainList.params(referenceOptions.filterParams);
|
|
43
56
|
}
|
|
44
57
|
return S.listItem()
|
|
45
58
|
.id(documentId)
|
|
46
59
|
.title(props.title || documentId)
|
|
60
|
+
.icon(props.icon)
|
|
47
61
|
.child(Object.assign(mainList.serialize(), {
|
|
48
62
|
type: 'component',
|
|
49
63
|
component: deskTreeValidator(props),
|
|
50
|
-
options: props
|
|
64
|
+
options: props,
|
|
65
|
+
__preserveInstance: true
|
|
51
66
|
}, props.title ? { title: props.title } : {}));
|
|
52
67
|
}
|
|
@@ -1,30 +1,51 @@
|
|
|
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
|
+
};
|
|
1
23
|
import TreeInputComponent from './TreeInputComponent';
|
|
2
|
-
export default function createHierarchicalField(
|
|
3
|
-
|
|
4
|
-
|
|
24
|
+
export default function createHierarchicalField(_a) {
|
|
25
|
+
var name = _a.name, title = _a.title, options = _a.options, rest = __rest(_a, ["name", "title", "options"]);
|
|
26
|
+
if (!Array.isArray(options === null || options === void 0 ? void 0 : options.referenceTo)) {
|
|
27
|
+
throw new Error("[hierarchical input] Missing valid options.referenceTo in createHierarchicalField (field of name \"".concat(name, "\")"));
|
|
5
28
|
}
|
|
6
|
-
return {
|
|
7
|
-
...rest,
|
|
8
|
-
options,
|
|
9
|
-
name,
|
|
10
|
-
title,
|
|
11
|
-
type: 'array',
|
|
12
|
-
of: [
|
|
29
|
+
return __assign(__assign({}, rest), { options: options, name: name, title: title, type: 'array', of: [
|
|
13
30
|
{
|
|
14
31
|
type: 'object',
|
|
15
32
|
fields: [
|
|
16
33
|
{ name: 'parent', type: 'string' },
|
|
17
|
-
{ name: 'nodeDocType', type: 'string' },
|
|
18
34
|
{
|
|
19
|
-
name: '
|
|
20
|
-
type: '
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
35
|
+
name: 'value',
|
|
36
|
+
type: 'object',
|
|
37
|
+
fields: [
|
|
38
|
+
{ name: 'docType', type: 'string' },
|
|
39
|
+
{
|
|
40
|
+
name: 'reference',
|
|
41
|
+
type: 'reference',
|
|
42
|
+
weak: true,
|
|
43
|
+
to: options.referenceTo.map(function (type) { return ({ type: type }); }),
|
|
44
|
+
options: options.referenceOptions
|
|
45
|
+
}
|
|
46
|
+
]
|
|
24
47
|
}
|
|
25
48
|
]
|
|
26
49
|
}
|
|
27
|
-
],
|
|
28
|
-
inputComponent: TreeInputComponent
|
|
29
|
-
};
|
|
50
|
+
], inputComponent: TreeInputComponent });
|
|
30
51
|
}
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import createHierarchicalField from '../createHierarchicalField';
|
|
2
|
+
export default {
|
|
3
|
+
name: 'hierarchy.tree',
|
|
4
|
+
title: 'Hierarchical tree',
|
|
5
|
+
type: 'document',
|
|
6
|
+
// The plugin needs to define a `schemaType` with liveEdit enabled so that
|
|
7
|
+
// `useDocumentOperation` in TreeDeskStructure.tsx doesn't create drafts at every patch.
|
|
8
|
+
liveEdit: true,
|
|
9
|
+
fields: [
|
|
10
|
+
createHierarchicalField({
|
|
11
|
+
name: 'tree',
|
|
12
|
+
title: 'Tree',
|
|
13
|
+
options: {
|
|
14
|
+
referenceTo: ['image']
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
],
|
|
18
|
+
preview: {
|
|
19
|
+
select: {
|
|
20
|
+
id: '_id',
|
|
21
|
+
tree: 'tree'
|
|
22
|
+
},
|
|
23
|
+
prepare: function (_a) {
|
|
24
|
+
var id = _a.id, tree = _a.tree;
|
|
25
|
+
return {
|
|
26
|
+
title: "Hierarchical documents (ID: ".concat(id, ")"),
|
|
27
|
+
subtitle: "".concat((tree === null || tree === void 0 ? void 0 : tree.length) || 0, " document(s) in its list.")
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
interface TreeItemWithChildren extends
|
|
1
|
+
import { StoredTreeItem } from '../types';
|
|
2
|
+
interface TreeItemWithChildren extends StoredTreeItem {
|
|
3
3
|
children?: TreeItemWithChildren[];
|
|
4
4
|
}
|
|
5
|
-
export default function flatDataToTree(data:
|
|
5
|
+
export default function flatDataToTree(data: StoredTreeItem[]): TreeItemWithChildren[];
|
|
6
6
|
export {};
|
|
@@ -1,13 +1,22 @@
|
|
|
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 { getTreeFromFlatData } from 'react-sortable-tree';
|
|
2
13
|
export default function flatDataToTree(data) {
|
|
3
14
|
return getTreeFromFlatData({
|
|
4
|
-
flatData: data.map((item)
|
|
5
|
-
...item,
|
|
15
|
+
flatData: data.map(function (item) { return (__assign(__assign({}, item), {
|
|
6
16
|
// if parent: undefined, the tree won't be constructed
|
|
7
|
-
parent: item.parent || null
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
getParentKey: (item) => item.parent,
|
|
17
|
+
parent: item.parent || null })); }),
|
|
18
|
+
getKey: function (item) { return item._key; },
|
|
19
|
+
getParentKey: function (item) { return item.parent; },
|
|
11
20
|
// without rootKey, the tree won't be constructed
|
|
12
21
|
rootKey: null
|
|
13
22
|
});
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Gets adjescent non-children nodes of a given treeIndex.
|
|
3
3
|
*/
|
|
4
|
-
export default function getAdjescentNodes(
|
|
5
|
-
|
|
4
|
+
export default function getAdjescentNodes(_a) {
|
|
5
|
+
var flatTree = _a.flatTree, node = _a.node, treeIndex = _a.treeIndex;
|
|
6
|
+
var leadingNode = flatTree
|
|
6
7
|
.slice(0, treeIndex)
|
|
7
8
|
.reverse()
|
|
8
9
|
// Disregard children nodes - these include the current node's key in their `path` array
|
|
9
|
-
.find((item)
|
|
10
|
-
|
|
10
|
+
.find(function (item) { return !item.path.includes(node._key); });
|
|
11
|
+
var followingNode = flatTree.slice(treeIndex + 1).find(function (item) { return !item.path.includes(node._key); });
|
|
11
12
|
return {
|
|
12
|
-
leadingNode,
|
|
13
|
-
followingNode
|
|
13
|
+
leadingNode: leadingNode,
|
|
14
|
+
followingNode: followingNode
|
|
14
15
|
};
|
|
15
16
|
}
|
|
@@ -1,13 +1,25 @@
|
|
|
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 { jsx as _jsx } from "react/jsx-runtime";
|
|
2
13
|
import NodeContentRenderer from '../components/NodeContentRenderer';
|
|
3
14
|
import PlaceholderDropzone from '../components/PlaceholderDropzone';
|
|
4
15
|
import TreeNodeRenderer from '../components/TreeNodeRenderer';
|
|
5
16
|
import { ROW_HEIGHT } from './getTreeHeight';
|
|
6
|
-
export default function getCommonTreeProps(
|
|
17
|
+
export default function getCommonTreeProps(_a) {
|
|
18
|
+
var placeholder = _a.placeholder;
|
|
7
19
|
return {
|
|
8
20
|
theme: {
|
|
9
21
|
nodeContentRenderer: NodeContentRenderer,
|
|
10
|
-
placeholderRenderer: (props)
|
|
22
|
+
placeholderRenderer: function (props) { return _jsx(PlaceholderDropzone, __assign({}, placeholder, props), void 0); },
|
|
11
23
|
treeNodeRenderer: TreeNodeRenderer,
|
|
12
24
|
rowHeight: ROW_HEIGHT
|
|
13
25
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getVisibleNodeCount } from 'react-sortable-tree';
|
|
2
|
-
export
|
|
2
|
+
export var ROW_HEIGHT = 51;
|
|
3
3
|
export default function getTreeHeight(treeData) {
|
|
4
|
-
|
|
4
|
+
var visibleNodeCount = getVisibleNodeCount({ treeData: treeData });
|
|
5
5
|
// prettier-ignore
|
|
6
|
-
return
|
|
6
|
+
return "".concat(50 + (ROW_HEIGHT * visibleNodeCount), "px");
|
|
7
7
|
}
|
|
@@ -5,14 +5,15 @@ export function toGradient(patches) {
|
|
|
5
5
|
return patches.map(toGradientPatch);
|
|
6
6
|
}
|
|
7
7
|
function toGradientPatch(patch) {
|
|
8
|
-
|
|
8
|
+
var _a, _b, _c, _d;
|
|
9
|
+
var matchPath = arrayToJSONMatchPath(patch.path || []);
|
|
9
10
|
if (patch.type === 'insert') {
|
|
10
|
-
|
|
11
|
+
var position = patch.position, items = patch.items;
|
|
11
12
|
return {
|
|
12
|
-
insert: {
|
|
13
|
-
[position]
|
|
14
|
-
items
|
|
15
|
-
|
|
13
|
+
insert: (_a = {},
|
|
14
|
+
_a[position] = matchPath,
|
|
15
|
+
_a.items = items,
|
|
16
|
+
_a)
|
|
16
17
|
};
|
|
17
18
|
}
|
|
18
19
|
if (patch.type === 'unset') {
|
|
@@ -20,15 +21,15 @@ function toGradientPatch(patch) {
|
|
|
20
21
|
unset: [matchPath]
|
|
21
22
|
};
|
|
22
23
|
}
|
|
23
|
-
assert(patch.type,
|
|
24
|
+
assert(patch.type, "Missing patch type in patch ".concat(JSON.stringify(patch)));
|
|
24
25
|
if (matchPath) {
|
|
25
|
-
return {
|
|
26
|
-
[patch.type]
|
|
27
|
-
[matchPath]
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
return _b = {},
|
|
27
|
+
_b[patch.type] = (_c = {},
|
|
28
|
+
_c[matchPath] = patch.value,
|
|
29
|
+
_c),
|
|
30
|
+
_b;
|
|
30
31
|
}
|
|
31
|
-
return {
|
|
32
|
-
[patch.type]
|
|
33
|
-
|
|
32
|
+
return _d = {},
|
|
33
|
+
_d[patch.type] = patch.value,
|
|
34
|
+
_d;
|
|
34
35
|
}
|
package/lib/utils/idUtils.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
export function unprefixId(_id
|
|
1
|
+
export function unprefixId(_id) {
|
|
2
|
+
if (_id === void 0) { _id = ''; }
|
|
2
3
|
return _id.replace('drafts.', '');
|
|
3
4
|
}
|
|
4
|
-
export function isDraft(_id
|
|
5
|
+
export function isDraft(_id) {
|
|
6
|
+
if (_id === void 0) { _id = ''; }
|
|
5
7
|
return _id.startsWith('drafts.');
|
|
6
8
|
}
|
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
|
|
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
|
+
};
|
|
10
|
+
export default function moveItemInArray(_a) {
|
|
11
|
+
var array = _a.array, fromIndex = _a.fromIndex, toIndex = _a.toIndex;
|
|
2
12
|
if (fromIndex === toIndex) {
|
|
3
13
|
return array;
|
|
4
14
|
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
for (
|
|
15
|
+
var newArray = __spreadArray([], array, true);
|
|
16
|
+
var target = newArray[fromIndex];
|
|
17
|
+
var inc = toIndex < fromIndex ? -1 : 1;
|
|
18
|
+
for (var i = fromIndex; i !== toIndex; i += inc) {
|
|
9
19
|
newArray[i] = newArray[i + inc];
|
|
10
20
|
}
|
|
11
21
|
newArray[toIndex] = target;
|
package/lib/utils/treeData.d.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { SanityDocument } from '@sanity/client';
|
|
2
2
|
import { TreeItem } from 'react-sortable-tree';
|
|
3
|
-
import { AllItems,
|
|
3
|
+
import { AllItems, EnhancedTreeItem, LocalTreeItem, StoredTreeItem, VisibilityMap } from '../types';
|
|
4
4
|
export declare const dataToEditorTree: ({ tree, allItems, visibilityMap }: {
|
|
5
|
-
tree:
|
|
5
|
+
tree: StoredTreeItem[];
|
|
6
6
|
allItems: AllItems;
|
|
7
7
|
visibilityMap: VisibilityMap;
|
|
8
|
-
}) =>
|
|
8
|
+
}) => LocalTreeItem[];
|
|
9
9
|
export declare const flatTree: (tree: TreeItem[]) => TreeItem[];
|
|
10
10
|
export interface FetchData {
|
|
11
|
-
mainTree?:
|
|
11
|
+
mainTree?: LocalTreeItem[];
|
|
12
12
|
allItems?: SanityDocument[];
|
|
13
13
|
}
|
|
14
14
|
export declare const getUnaddedItems: (data: {
|
|
15
15
|
allItems: AllItems;
|
|
16
|
-
tree:
|
|
17
|
-
}) =>
|
|
18
|
-
export declare function normalizeNodeForStorage(item:
|
|
16
|
+
tree: StoredTreeItem[];
|
|
17
|
+
}) => EnhancedTreeItem[];
|
|
18
|
+
export declare function normalizeNodeForStorage(item: LocalTreeItem): StoredTreeItem;
|
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) {
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import { FullTree, NodeData,
|
|
2
|
-
import {
|
|
3
|
-
export declare type HandleMovedNodeData = Omit<NodeData & FullTree & OnMovePreviousAndNextLocation, 'prevPath' | 'prevTreeIndex' | 'path' | 'treeIndex'
|
|
1
|
+
import { FullTree, NodeData, OnMovePreviousAndNextLocation, TreeItem } from 'react-sortable-tree';
|
|
2
|
+
import { LocalTreeItem, NodeProps } from '../types';
|
|
3
|
+
export declare type HandleMovedNodeData = Omit<NodeData & FullTree & OnMovePreviousAndNextLocation, 'prevPath' | 'prevTreeIndex' | 'path' | 'treeIndex' | 'node'> & {
|
|
4
|
+
node: LocalTreeItem;
|
|
5
|
+
};
|
|
4
6
|
export declare type HandleMovedNode = (moveData: HandleMovedNodeData) => void;
|
|
5
|
-
export declare function getAddItemPatch(item:
|
|
6
|
-
export declare function getDuplicateItemPatch(nodeProps:
|
|
7
|
-
export declare function getRemoveItemPatch({ node }: Pick<
|
|
7
|
+
export declare function getAddItemPatch(item: LocalTreeItem): unknown[];
|
|
8
|
+
export declare function getDuplicateItemPatch(nodeProps: NodeProps): unknown[];
|
|
9
|
+
export declare function getRemoveItemPatch({ node }: Pick<NodeProps, 'node'>): unknown[];
|
|
8
10
|
export declare function getMovedNodePatch(data: HandleMovedNodeData): unknown[];
|
|
9
|
-
export declare function getMoveItemPatch({ nodeProps: { node, treeIndex
|
|
10
|
-
nodeProps:
|
|
11
|
+
export declare function getMoveItemPatch({ nodeProps: { node, treeIndex }, localTree, direction }: {
|
|
12
|
+
nodeProps: NodeProps;
|
|
11
13
|
localTree: TreeItem[];
|
|
12
14
|
direction: 'up' | 'down';
|
|
13
15
|
}): unknown[];
|