@sanity/hierarchical-document-list 1.1.0 → 2.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.
- package/LICENSE +1 -1
- package/README.md +95 -59
- package/dist/index.d.ts +240 -0
- package/dist/index.esm.js +18 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/package.json +80 -51
- package/sanity.json +2 -6
- package/src/TreeDeskStructure.tsx +80 -0
- package/src/TreeInputComponent.tsx +41 -0
- package/src/components/DeskWarning.tsx +40 -0
- package/src/components/DocumentInNode.tsx +133 -0
- package/src/components/DocumentPreviewStatus.tsx +70 -0
- package/src/components/NodeActions.tsx +85 -0
- package/src/components/NodeContentRenderer.tsx +141 -0
- package/src/components/PlaceholderDropzone.tsx +45 -0
- package/src/components/TreeEditor.tsx +167 -0
- package/{lib/components/TreeEditorErrorBoundary.d.ts → src/components/TreeEditorErrorBoundary.tsx} +2 -4
- package/src/components/TreeNodeRenderer.tsx +37 -0
- package/src/components/TreeNodeRendererScaffold.tsx +193 -0
- package/src/createDeskHierarchy.tsx +110 -0
- package/src/createHierarchicalSchemas.tsx +151 -0
- package/src/hooks/useAllItems.ts +119 -0
- package/src/hooks/useLocalTree.ts +40 -0
- package/src/hooks/useTreeOperations.ts +25 -0
- package/src/hooks/useTreeOperationsProvider.ts +86 -0
- package/src/index.ts +25 -0
- package/src/schemas/hierarchy.tree.ts +19 -0
- package/src/types.ts +148 -0
- package/src/utils/flatDataToTree.ts +20 -0
- package/src/utils/getAdjescentNodes.ts +30 -0
- package/src/utils/getCommonTreeProps.tsx +28 -0
- package/src/utils/getTreeHeight.ts +10 -0
- package/src/utils/gradientPatchAdapter.ts +43 -0
- package/src/utils/idUtils.ts +7 -0
- package/src/utils/injectNodeTypeInPatches.ts +60 -0
- package/src/utils/moveItemInArray.ts +26 -0
- package/src/utils/throwError.ts +9 -0
- package/src/utils/treeData.tsx +119 -0
- package/src/utils/treePatches.ts +171 -0
- package/v2-incompatible.js +11 -0
- package/.husky/commit-msg +0 -4
- package/.husky/pre-commit +0 -4
- package/.idea/hierarchical-document-list.iml +0 -11
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/prettier.xml +0 -7
- package/.idea/vcs.xml +0 -6
- package/CHANGELOG.md +0 -15
- package/commitlint.config.js +0 -3
- package/lib/TreeDeskStructure.d.ts +0 -8
- package/lib/TreeDeskStructure.js +0 -96
- package/lib/TreeInputComponent.d.ts +0 -19
- package/lib/TreeInputComponent.js +0 -50
- package/lib/components/DeskWarning.d.ts +0 -6
- package/lib/components/DeskWarning.js +0 -46
- package/lib/components/DocumentInNode.d.ts +0 -11
- package/lib/components/DocumentInNode.js +0 -81
- package/lib/components/DocumentPreviewStatus.d.ts +0 -7
- package/lib/components/DocumentPreviewStatus.js +0 -39
- package/lib/components/NodeActions.d.ts +0 -10
- package/lib/components/NodeActions.js +0 -61
- package/lib/components/NodeContentRenderer.d.ts +0 -8
- package/lib/components/NodeContentRenderer.js +0 -105
- package/lib/components/PlaceholderDropzone.d.ts +0 -9
- package/lib/components/PlaceholderDropzone.js +0 -30
- package/lib/components/SuppressedDnDManager.d.ts +0 -2
- package/lib/components/SuppressedDnDManager.js +0 -59
- package/lib/components/TreeEditor.d.ts +0 -12
- package/lib/components/TreeEditor.js +0 -74
- package/lib/components/TreeEditorErrorBoundary.js +0 -74
- package/lib/components/TreeNodeRenderer.d.ts +0 -3
- package/lib/components/TreeNodeRenderer.js +0 -59
- package/lib/components/TreeNodeRendererScaffold.d.ts +0 -4
- package/lib/components/TreeNodeRendererScaffold.js +0 -44
- package/lib/createDeskHierarchy.d.ts +0 -14
- package/lib/createDeskHierarchy.js +0 -84
- package/lib/createHierarchicalSchemas.d.ts +0 -98
- package/lib/createHierarchicalSchemas.js +0 -138
- package/lib/hooks/useAllItems.d.ts +0 -7
- package/lib/hooks/useAllItems.js +0 -119
- package/lib/hooks/useLocalTree.d.ts +0 -17
- package/lib/hooks/useLocalTree.js +0 -59
- package/lib/hooks/useTreeOperations.d.ts +0 -9
- package/lib/hooks/useTreeOperations.js +0 -39
- package/lib/hooks/useTreeOperationsProvider.d.ts +0 -14
- package/lib/hooks/useTreeOperationsProvider.js +0 -85
- package/lib/index.d.ts +0 -3
- package/lib/index.js +0 -12
- package/lib/schemas/hierarchy.tree.d.ts +0 -13
- package/lib/schemas/hierarchy.tree.js +0 -19
- package/lib/types.d.ts +0 -128
- package/lib/types.js +0 -2
- package/lib/utils/flatDataToTree.d.ts +0 -6
- package/lib/utils/flatDataToTree.js +0 -26
- package/lib/utils/getAdjescentNodes.d.ts +0 -12
- package/lib/utils/getAdjescentNodes.js +0 -19
- package/lib/utils/getCommonTreeProps.d.ts +0 -7
- package/lib/utils/getCommonTreeProps.js +0 -33
- package/lib/utils/getTreeHeight.d.ts +0 -3
- package/lib/utils/getTreeHeight.js +0 -11
- package/lib/utils/gradientPatchAdapter.d.ts +0 -4
- package/lib/utils/gradientPatchAdapter.js +0 -40
- package/lib/utils/idUtils.d.ts +0 -2
- package/lib/utils/idUtils.js +0 -13
- package/lib/utils/injectNodeTypeInPatches.d.ts +0 -12
- package/lib/utils/injectNodeTypeInPatches.js +0 -59
- package/lib/utils/moveItemInArray.d.ts +0 -5
- package/lib/utils/moveItemInArray.js +0 -26
- package/lib/utils/throwError.d.ts +0 -7
- package/lib/utils/throwError.js +0 -12
- package/lib/utils/treeData.d.ts +0 -18
- package/lib/utils/treeData.js +0 -118
- package/lib/utils/treePatches.d.ts +0 -15
- package/lib/utils/treePatches.js +0 -171
- package/lint-staged.config.js +0 -4
- package/screenshot-1.jpg +0 -0
- package/tsconfig.json +0 -20
package/lib/types.d.ts
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { SanityDocument } from '@sanity/client';
|
|
2
|
-
import { ArraySchemaType, ObjectSchemaType } from '@sanity/types';
|
|
3
|
-
import { NodeRendererProps, TreeItem } from 'react-sortable-tree';
|
|
4
|
-
import { INTERNAL_NODE_TYPE, INTERNAL_NODE_VALUE_TYPE } from './utils/injectNodeTypeInPatches';
|
|
5
|
-
interface SanityReference {
|
|
6
|
-
_type: 'reference';
|
|
7
|
-
_ref: string;
|
|
8
|
-
_weak?: boolean;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Objects saved to tree documents in Sanity's Content Lake
|
|
12
|
-
*/
|
|
13
|
-
export interface StoredTreeItem {
|
|
14
|
-
_key: string;
|
|
15
|
-
_type: typeof INTERNAL_NODE_TYPE | string;
|
|
16
|
-
value?: {
|
|
17
|
-
_type: typeof INTERNAL_NODE_VALUE_TYPE | string;
|
|
18
|
-
reference?: SanityReference;
|
|
19
|
-
docType?: string;
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* _key of parent node
|
|
23
|
-
*/
|
|
24
|
-
parent?: string | null;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Tree items enhanced locally in the client with info from `allItems` and `visibilityMap`.
|
|
28
|
-
* `allItems` stop here and never become LocalTreeItems as they aren't added to react-sortable-tree.
|
|
29
|
-
*
|
|
30
|
-
* See `useLocalTree.ts` and `dataToEditorTree()`.
|
|
31
|
-
*/
|
|
32
|
-
export interface EnhancedTreeItem extends StoredTreeItem {
|
|
33
|
-
expanded?: boolean | undefined;
|
|
34
|
-
/**
|
|
35
|
-
* Used by DocumentInNode to render the preview for drafts if they exist.
|
|
36
|
-
* Also informs document status icons.
|
|
37
|
-
*/
|
|
38
|
-
draftId?: string;
|
|
39
|
-
draftUpdatedAt?: string;
|
|
40
|
-
/**
|
|
41
|
-
* If not present, DocumentInNode will show up an error for invalid document.
|
|
42
|
-
* - undefined `publishedId` could mean the document is either deleted, or it doesn't match GROQ filters anymore
|
|
43
|
-
*/
|
|
44
|
-
publishedId?: string;
|
|
45
|
-
publishedUpdatedAt?: string;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Tree items as found in the sortable tree itself.
|
|
49
|
-
*/
|
|
50
|
-
export declare type LocalTreeItem = EnhancedTreeItem & Pick<TreeItem, 'title' | 'children'>;
|
|
51
|
-
export interface TreeInputOptions {
|
|
52
|
-
/**
|
|
53
|
-
* What document types this hierarchy can refer to.
|
|
54
|
-
* Similar to the `to` property of the [reference field](https://www.sanity.io/docs/reference-type).
|
|
55
|
-
*/
|
|
56
|
-
referenceTo: string[];
|
|
57
|
-
/**
|
|
58
|
-
* Used to provide fine-grained filtering for documents.
|
|
59
|
-
*/
|
|
60
|
-
referenceOptions?: {
|
|
61
|
-
/**
|
|
62
|
-
* Static filter to apply to tree document queries.
|
|
63
|
-
*/
|
|
64
|
-
filter?: string;
|
|
65
|
-
/**
|
|
66
|
-
* Parameters / variables to pass to the GROQ query ran to fetch documents.
|
|
67
|
-
*/
|
|
68
|
-
filterParams?: Record<string, unknown>;
|
|
69
|
-
};
|
|
70
|
-
/**
|
|
71
|
-
* How deep should editors be allowed to nest items.
|
|
72
|
-
*/
|
|
73
|
-
maxDepth?: number;
|
|
74
|
-
/**
|
|
75
|
-
* Schema type for your hierarchical documents.
|
|
76
|
-
* Refer to documentation on how to provide these schemas in your studio.
|
|
77
|
-
*
|
|
78
|
-
* Defautlt: 'hierarchy.tree' - this schema is bundled with the plugin
|
|
79
|
-
*/
|
|
80
|
-
documentType?: string;
|
|
81
|
-
}
|
|
82
|
-
export interface TreeFieldSchema extends Omit<ArraySchemaType, 'of' | 'type' | 'inputComponent' | 'jsonType'> {
|
|
83
|
-
options: ArraySchemaType['options'] & TreeInputOptions;
|
|
84
|
-
}
|
|
85
|
-
export interface TreeNodeObjectSchema extends Omit<ObjectSchemaType, 'name' | 'fields' | 'type' | 'inputComponent' | 'jsonType'> {
|
|
86
|
-
options: ObjectSchemaType['options'] & TreeInputOptions;
|
|
87
|
-
}
|
|
88
|
-
export interface TreeDeskStructureProps extends TreeInputOptions {
|
|
89
|
-
/**
|
|
90
|
-
* _id of the document that will hold the tree data.
|
|
91
|
-
*/
|
|
92
|
-
documentId: string;
|
|
93
|
-
/**
|
|
94
|
-
* (Optional)
|
|
95
|
-
* Key for the field representing the hierarchical tree inside the document.
|
|
96
|
-
* `tree` by default.
|
|
97
|
-
*/
|
|
98
|
-
fieldKeyInDocument?: string;
|
|
99
|
-
}
|
|
100
|
-
export interface DocumentPair {
|
|
101
|
-
draft?: SanityDocument;
|
|
102
|
-
published?: SanityDocument;
|
|
103
|
-
}
|
|
104
|
-
export interface AllItems {
|
|
105
|
-
[publishedId: string]: DocumentPair | undefined;
|
|
106
|
-
}
|
|
107
|
-
declare type DocumentOperation<Payload = unknown> = {
|
|
108
|
-
execute?: (payload?: Payload) => void;
|
|
109
|
-
disabled?: boolean | string;
|
|
110
|
-
};
|
|
111
|
-
export interface DocumentOperations {
|
|
112
|
-
patch?: DocumentOperation<unknown[]>;
|
|
113
|
-
commit?: DocumentOperation;
|
|
114
|
-
del?: DocumentOperation;
|
|
115
|
-
delete?: DocumentOperation;
|
|
116
|
-
discardChanges?: DocumentOperation;
|
|
117
|
-
duplicate?: DocumentOperation;
|
|
118
|
-
restore?: DocumentOperation;
|
|
119
|
-
unpublish?: DocumentOperation;
|
|
120
|
-
publish?: DocumentOperation;
|
|
121
|
-
}
|
|
122
|
-
export interface VisibilityMap {
|
|
123
|
-
[_key: string]: boolean;
|
|
124
|
-
}
|
|
125
|
-
export interface NodeProps extends NodeRendererProps {
|
|
126
|
-
node: LocalTreeItem;
|
|
127
|
-
}
|
|
128
|
-
export {};
|
package/lib/types.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
var react_sortable_tree_1 = require("react-sortable-tree");
|
|
15
|
-
function flatDataToTree(data) {
|
|
16
|
-
return (0, react_sortable_tree_1.getTreeFromFlatData)({
|
|
17
|
-
flatData: data.map(function (item) { return (__assign(__assign({}, item), {
|
|
18
|
-
// if parent: undefined, the tree won't be constructed
|
|
19
|
-
parent: item.parent || null })); }),
|
|
20
|
-
getKey: function (item) { return item._key; },
|
|
21
|
-
getParentKey: function (item) { return item.parent; },
|
|
22
|
-
// without rootKey, the tree won't be constructed
|
|
23
|
-
rootKey: null
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
exports.default = flatDataToTree;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { FlatDataItem, TreeItem } from 'react-sortable-tree';
|
|
2
|
-
/**
|
|
3
|
-
* Gets adjescent non-children nodes of a given treeIndex.
|
|
4
|
-
*/
|
|
5
|
-
export default function getAdjescentNodes({ flatTree, node, treeIndex }: {
|
|
6
|
-
flatTree: FlatDataItem[];
|
|
7
|
-
node: TreeItem;
|
|
8
|
-
treeIndex: number;
|
|
9
|
-
}): {
|
|
10
|
-
leadingNode?: FlatDataItem;
|
|
11
|
-
followingNode?: FlatDataItem;
|
|
12
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
/**
|
|
4
|
-
* Gets adjescent non-children nodes of a given treeIndex.
|
|
5
|
-
*/
|
|
6
|
-
function getAdjescentNodes(_a) {
|
|
7
|
-
var flatTree = _a.flatTree, node = _a.node, treeIndex = _a.treeIndex;
|
|
8
|
-
var leadingNode = flatTree
|
|
9
|
-
.slice(0, treeIndex)
|
|
10
|
-
.reverse()
|
|
11
|
-
// Disregard children nodes - these include the current node's key in their `path` array
|
|
12
|
-
.find(function (item) { return !item.path.includes(node._key); });
|
|
13
|
-
var followingNode = flatTree.slice(treeIndex + 1).find(function (item) { return !item.path.includes(node._key); });
|
|
14
|
-
return {
|
|
15
|
-
leadingNode: leadingNode,
|
|
16
|
-
followingNode: followingNode
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
exports.default = getAdjescentNodes;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
18
|
-
var NodeContentRenderer_1 = __importDefault(require("../components/NodeContentRenderer"));
|
|
19
|
-
var PlaceholderDropzone_1 = __importDefault(require("../components/PlaceholderDropzone"));
|
|
20
|
-
var TreeNodeRenderer_1 = __importDefault(require("../components/TreeNodeRenderer"));
|
|
21
|
-
var getTreeHeight_1 = require("./getTreeHeight");
|
|
22
|
-
function getCommonTreeProps(_a) {
|
|
23
|
-
var placeholder = _a.placeholder;
|
|
24
|
-
return {
|
|
25
|
-
theme: {
|
|
26
|
-
nodeContentRenderer: NodeContentRenderer_1.default,
|
|
27
|
-
placeholderRenderer: function (props) { return (0, jsx_runtime_1.jsx)(PlaceholderDropzone_1.default, __assign({}, placeholder, props), void 0); },
|
|
28
|
-
treeNodeRenderer: TreeNodeRenderer_1.default,
|
|
29
|
-
rowHeight: getTreeHeight_1.ROW_HEIGHT
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
exports.default = getCommonTreeProps;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ROW_HEIGHT = void 0;
|
|
4
|
-
var react_sortable_tree_1 = require("react-sortable-tree");
|
|
5
|
-
exports.ROW_HEIGHT = 51;
|
|
6
|
-
function getTreeHeight(treeData) {
|
|
7
|
-
var visibleNodeCount = (0, react_sortable_tree_1.getVisibleNodeCount)({ treeData: treeData });
|
|
8
|
-
// prettier-ignore
|
|
9
|
-
return "".concat(50 + (exports.ROW_HEIGHT * visibleNodeCount), "px");
|
|
10
|
-
}
|
|
11
|
-
exports.default = getTreeHeight;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toGradient = void 0;
|
|
4
|
-
// Adapted from @sanity/form-builder/src/sanity/utils/gradientPatchAdapter.ts
|
|
5
|
-
var mutator_1 = require("@sanity/mutator");
|
|
6
|
-
function toGradient(patches) {
|
|
7
|
-
return patches.map(toGradientPatch);
|
|
8
|
-
}
|
|
9
|
-
exports.toGradient = toGradient;
|
|
10
|
-
function toGradientPatch(patch) {
|
|
11
|
-
var _a, _b, _c, _d;
|
|
12
|
-
var matchPath = (0, mutator_1.arrayToJSONMatchPath)(patch.path || []);
|
|
13
|
-
if (patch.type === 'insert') {
|
|
14
|
-
var position = patch.position, items = patch.items;
|
|
15
|
-
return {
|
|
16
|
-
insert: (_a = {},
|
|
17
|
-
_a[position] = matchPath,
|
|
18
|
-
_a.items = items,
|
|
19
|
-
_a)
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
if (patch.type === 'unset') {
|
|
23
|
-
return {
|
|
24
|
-
unset: [matchPath]
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
if (!patch.type) {
|
|
28
|
-
throw new Error("Missing patch type in patch ".concat(JSON.stringify(patch)));
|
|
29
|
-
}
|
|
30
|
-
if (matchPath) {
|
|
31
|
-
return _b = {},
|
|
32
|
-
_b[patch.type] = (_c = {},
|
|
33
|
-
_c[matchPath] = patch.value,
|
|
34
|
-
_c),
|
|
35
|
-
_b;
|
|
36
|
-
}
|
|
37
|
-
return _d = {},
|
|
38
|
-
_d[patch.type] = patch.value,
|
|
39
|
-
_d;
|
|
40
|
-
}
|
package/lib/utils/idUtils.d.ts
DELETED
package/lib/utils/idUtils.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isDraft = exports.unprefixId = void 0;
|
|
4
|
-
function unprefixId(_id) {
|
|
5
|
-
if (_id === void 0) { _id = ''; }
|
|
6
|
-
return _id.replace('drafts.', '');
|
|
7
|
-
}
|
|
8
|
-
exports.unprefixId = unprefixId;
|
|
9
|
-
function isDraft(_id) {
|
|
10
|
-
if (_id === void 0) { _id = ''; }
|
|
11
|
-
return _id.startsWith('drafts.');
|
|
12
|
-
}
|
|
13
|
-
exports.isDraft = isDraft;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export declare function getSchemaTypeName(documentType: string, type: 'document' | 'node' | 'nodeValue' | 'array'): string;
|
|
2
|
-
/**
|
|
3
|
-
* Temporary value of nodes' `_type` before they are normalized and persisted as the user-defined choice.
|
|
4
|
-
*/
|
|
5
|
-
export declare const DEFAULT_DOC_TYPE = "hierarchy.tree";
|
|
6
|
-
export declare const INTERNAL_NODE_TYPE: string;
|
|
7
|
-
export declare const INTERNAL_NODE_VALUE_TYPE: string;
|
|
8
|
-
export declare const INTERNAL_NODE_ARRAY_TYPE: string;
|
|
9
|
-
/**
|
|
10
|
-
* Barebones recursive utility to inject the desired nodeObjectType in patches generated in deeply nested components and utilities.
|
|
11
|
-
*/
|
|
12
|
-
export default function injectNodeTypeInPatches(patchData: any, documentType: string): any;
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.INTERNAL_NODE_ARRAY_TYPE = exports.INTERNAL_NODE_VALUE_TYPE = exports.INTERNAL_NODE_TYPE = exports.DEFAULT_DOC_TYPE = exports.getSchemaTypeName = void 0;
|
|
15
|
-
function getSchemaTypeName(documentType, type) {
|
|
16
|
-
switch (type) {
|
|
17
|
-
case 'document':
|
|
18
|
-
return documentType;
|
|
19
|
-
case 'array':
|
|
20
|
-
return "".concat(documentType, ".array");
|
|
21
|
-
case 'node':
|
|
22
|
-
return "".concat(documentType, ".node");
|
|
23
|
-
case 'nodeValue':
|
|
24
|
-
return "".concat(documentType, ".nodeValue");
|
|
25
|
-
default:
|
|
26
|
-
return documentType;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
exports.getSchemaTypeName = getSchemaTypeName;
|
|
30
|
-
/**
|
|
31
|
-
* Temporary value of nodes' `_type` before they are normalized and persisted as the user-defined choice.
|
|
32
|
-
*/
|
|
33
|
-
exports.DEFAULT_DOC_TYPE = 'hierarchy.tree';
|
|
34
|
-
exports.INTERNAL_NODE_TYPE = getSchemaTypeName(exports.DEFAULT_DOC_TYPE, 'node');
|
|
35
|
-
exports.INTERNAL_NODE_VALUE_TYPE = getSchemaTypeName(exports.DEFAULT_DOC_TYPE, 'nodeValue');
|
|
36
|
-
exports.INTERNAL_NODE_ARRAY_TYPE = getSchemaTypeName(exports.DEFAULT_DOC_TYPE, 'array');
|
|
37
|
-
/**
|
|
38
|
-
* Barebones recursive utility to inject the desired nodeObjectType in patches generated in deeply nested components and utilities.
|
|
39
|
-
*/
|
|
40
|
-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
41
|
-
function injectNodeTypeInPatches(patchData, documentType) {
|
|
42
|
-
if (Array.isArray(patchData)) {
|
|
43
|
-
return patchData.map(function (child) { return injectNodeTypeInPatches(child, documentType); });
|
|
44
|
-
}
|
|
45
|
-
if (typeof patchData === 'object' && patchData !== null) {
|
|
46
|
-
return Object.keys(patchData).reduce(function (newObject, key) {
|
|
47
|
-
var _a, _b;
|
|
48
|
-
var value = patchData[key];
|
|
49
|
-
if (key === '_type' &&
|
|
50
|
-
typeof value === 'string' &&
|
|
51
|
-
[exports.INTERNAL_NODE_TYPE, exports.INTERNAL_NODE_VALUE_TYPE].includes(value)) {
|
|
52
|
-
return __assign(__assign({}, newObject), (_a = {}, _a[key] = getSchemaTypeName(documentType, value === exports.INTERNAL_NODE_TYPE ? 'node' : 'nodeValue'), _a));
|
|
53
|
-
}
|
|
54
|
-
return __assign(__assign({}, newObject), (_b = {}, _b[key] = injectNodeTypeInPatches(value, documentType), _b));
|
|
55
|
-
}, {});
|
|
56
|
-
}
|
|
57
|
-
return patchData;
|
|
58
|
-
}
|
|
59
|
-
exports.default = injectNodeTypeInPatches;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
-
if (ar || !(i in from)) {
|
|
5
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
-
ar[i] = from[i];
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
function moveItemInArray(_a) {
|
|
13
|
-
var array = _a.array, fromIndex = _a.fromIndex, toIndex = _a.toIndex;
|
|
14
|
-
if (fromIndex === toIndex) {
|
|
15
|
-
return array;
|
|
16
|
-
}
|
|
17
|
-
var newArray = __spreadArray([], array, true);
|
|
18
|
-
var target = newArray[fromIndex];
|
|
19
|
-
var inc = toIndex < fromIndex ? -1 : 1;
|
|
20
|
-
for (var i = fromIndex; i !== toIndex; i += inc) {
|
|
21
|
-
newArray[i] = newArray[i + inc];
|
|
22
|
-
}
|
|
23
|
-
newArray[toIndex] = target;
|
|
24
|
-
return newArray;
|
|
25
|
-
}
|
|
26
|
-
exports.default = moveItemInArray;
|
package/lib/utils/throwError.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var ERROR_MESSAGES = {
|
|
4
|
-
invalidDocumentId: 'Please add a documentId to your tree',
|
|
5
|
-
invalidDocumentType: 'Please add a valid documentType to createHierarchicalSchemas',
|
|
6
|
-
invalidReferenceTo: "Missing valid 'referenceTo' value"
|
|
7
|
-
};
|
|
8
|
-
function throwError(message, extraContext) {
|
|
9
|
-
if (extraContext === void 0) { extraContext = ''; }
|
|
10
|
-
throw new Error("[hierarchical input] ".concat(ERROR_MESSAGES[message], " ").concat(extraContext));
|
|
11
|
-
}
|
|
12
|
-
exports.default = throwError;
|
package/lib/utils/treeData.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { SanityDocument } from '@sanity/client';
|
|
2
|
-
import { TreeItem } from 'react-sortable-tree';
|
|
3
|
-
import { AllItems, EnhancedTreeItem, LocalTreeItem, StoredTreeItem, VisibilityMap } from '../types';
|
|
4
|
-
export declare const dataToEditorTree: ({ tree, allItems, visibilityMap }: {
|
|
5
|
-
tree: StoredTreeItem[];
|
|
6
|
-
allItems: AllItems;
|
|
7
|
-
visibilityMap: VisibilityMap;
|
|
8
|
-
}) => LocalTreeItem[];
|
|
9
|
-
export declare const flatTree: (tree: TreeItem[]) => TreeItem[];
|
|
10
|
-
export interface FetchData {
|
|
11
|
-
mainTree?: LocalTreeItem[];
|
|
12
|
-
allItems?: SanityDocument[];
|
|
13
|
-
}
|
|
14
|
-
export declare const getUnaddedItems: (data: {
|
|
15
|
-
allItems: AllItems;
|
|
16
|
-
tree: StoredTreeItem[];
|
|
17
|
-
}) => EnhancedTreeItem[];
|
|
18
|
-
export declare function normalizeNodeForStorage(item: LocalTreeItem): StoredTreeItem;
|
package/lib/utils/treeData.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
14
|
-
var t = {};
|
|
15
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
16
|
-
t[p] = s[p];
|
|
17
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
18
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
19
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
20
|
-
t[p[i]] = s[p[i]];
|
|
21
|
-
}
|
|
22
|
-
return t;
|
|
23
|
-
};
|
|
24
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
25
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
26
|
-
if (ar || !(i in from)) {
|
|
27
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
28
|
-
ar[i] = from[i];
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
32
|
-
};
|
|
33
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
34
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
35
|
-
};
|
|
36
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
-
exports.normalizeNodeForStorage = exports.getUnaddedItems = exports.flatTree = exports.dataToEditorTree = void 0;
|
|
38
|
-
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
39
|
-
var content_1 = require("@sanity/util/content");
|
|
40
|
-
var DocumentInNode_1 = __importDefault(require("../components/DocumentInNode"));
|
|
41
|
-
var NodeActions_1 = __importDefault(require("../components/NodeActions"));
|
|
42
|
-
var flatDataToTree_1 = __importDefault(require("./flatDataToTree"));
|
|
43
|
-
var injectNodeTypeInPatches_1 = require("./injectNodeTypeInPatches");
|
|
44
|
-
var dataToEditorTree = function (_a) {
|
|
45
|
-
var tree = _a.tree, allItems = _a.allItems, visibilityMap = _a.visibilityMap;
|
|
46
|
-
var itemsWithTitle = tree
|
|
47
|
-
.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; })
|
|
48
|
-
.map(function (item) {
|
|
49
|
-
var _a, _b;
|
|
50
|
-
var refId = (_b = (_a = item.value) === null || _a === void 0 ? void 0 : _a.reference) === null || _b === void 0 ? void 0 : _b._ref;
|
|
51
|
-
var docPair = refId ? allItems[refId] : undefined;
|
|
52
|
-
var draftDoc = docPair === null || docPair === void 0 ? void 0 : docPair.draft;
|
|
53
|
-
var publishedDoc = docPair === null || docPair === void 0 ? void 0 : docPair.published;
|
|
54
|
-
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 });
|
|
55
|
-
return __assign(__assign({}, enhancedItem), { title: function (nodeProps) { return ((0, jsx_runtime_1.jsx)(DocumentInNode_1.default, { item: enhancedItem, action: (0, jsx_runtime_1.jsx)(NodeActions_1.default, { nodeProps: nodeProps }, void 0) }, void 0)); }, children: [] });
|
|
56
|
-
});
|
|
57
|
-
return (0, flatDataToTree_1.default)(itemsWithTitle);
|
|
58
|
-
};
|
|
59
|
-
exports.dataToEditorTree = dataToEditorTree;
|
|
60
|
-
var documentPairToNode = function (doc) {
|
|
61
|
-
var _a, _b, _c, _d;
|
|
62
|
-
if (!((_a = doc === null || doc === void 0 ? void 0 : doc.published) === null || _a === void 0 ? void 0 : _a._id)) {
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
return {
|
|
66
|
-
_key: (0, content_1.randomKey)(12),
|
|
67
|
-
_type: injectNodeTypeInPatches_1.INTERNAL_NODE_TYPE,
|
|
68
|
-
draftId: (_b = doc.draft) === null || _b === void 0 ? void 0 : _b._id,
|
|
69
|
-
draftUpdatedAt: (_c = doc.draft) === null || _c === void 0 ? void 0 : _c._updatedAt,
|
|
70
|
-
publishedId: doc.published._id,
|
|
71
|
-
publishedUpdatedAt: (_d = doc.published) === null || _d === void 0 ? void 0 : _d._updatedAt,
|
|
72
|
-
value: {
|
|
73
|
-
_type: injectNodeTypeInPatches_1.INTERNAL_NODE_VALUE_TYPE,
|
|
74
|
-
reference: {
|
|
75
|
-
_ref: doc.published._id,
|
|
76
|
-
_type: 'reference',
|
|
77
|
-
_weak: true
|
|
78
|
-
},
|
|
79
|
-
docType: doc.published._type
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
};
|
|
83
|
-
var flatTree = function (tree) {
|
|
84
|
-
return tree.reduce(function (flattened, item) {
|
|
85
|
-
var children = item.children, node = __rest(item, ["children"]);
|
|
86
|
-
return __spreadArray(__spreadArray(__spreadArray([], flattened, true), [node], false), (Array.isArray(children) ? (0, exports.flatTree)(children) : []), true);
|
|
87
|
-
}, []);
|
|
88
|
-
};
|
|
89
|
-
exports.flatTree = flatTree;
|
|
90
|
-
var getUnaddedItems = function (data) {
|
|
91
|
-
if (!data.tree) {
|
|
92
|
-
return Object.entries(data.allItems)
|
|
93
|
-
.map(function (value) { return documentPairToNode(value[1]); })
|
|
94
|
-
.filter(Boolean);
|
|
95
|
-
}
|
|
96
|
-
return Object.entries(data.allItems)
|
|
97
|
-
.filter(function (_a) {
|
|
98
|
-
var publishedId = _a[0];
|
|
99
|
-
return publishedId &&
|
|
100
|
-
// unadded items shouldn't be in the tree
|
|
101
|
-
!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; });
|
|
102
|
-
})
|
|
103
|
-
.map(function (_a) {
|
|
104
|
-
var documentPair = _a[1];
|
|
105
|
-
return documentPairToNode(documentPair);
|
|
106
|
-
})
|
|
107
|
-
.filter(Boolean);
|
|
108
|
-
};
|
|
109
|
-
exports.getUnaddedItems = getUnaddedItems;
|
|
110
|
-
function normalizeNodeForStorage(item) {
|
|
111
|
-
return {
|
|
112
|
-
_key: item._key,
|
|
113
|
-
_type: item._type || injectNodeTypeInPatches_1.INTERNAL_NODE_TYPE,
|
|
114
|
-
value: item.value,
|
|
115
|
-
parent: item.parent
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
exports.normalizeNodeForStorage = normalizeNodeForStorage;
|
|
@@ -1,15 +0,0 @@
|
|
|
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
|
-
};
|
|
6
|
-
export declare type HandleMovedNode = (moveData: HandleMovedNodeData) => void;
|
|
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[];
|
|
10
|
-
export declare function getMovedNodePatch(data: HandleMovedNodeData): unknown[];
|
|
11
|
-
export declare function getMoveItemPatch({ nodeProps: { node, treeIndex }, localTree, direction }: {
|
|
12
|
-
nodeProps: NodeProps;
|
|
13
|
-
localTree: TreeItem[];
|
|
14
|
-
direction: 'up' | 'down';
|
|
15
|
-
}): unknown[];
|