@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,22 +1,59 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import React from 'react';
3
- import TreeNodeRendererScaffold from './TreeNodeRendererScaffold';
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
16
+ }) : (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ o[k2] = m[k];
19
+ }));
20
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
22
+ }) : function(o, v) {
23
+ o["default"] = v;
24
+ });
25
+ var __importStar = (this && this.__importStar) || function (mod) {
26
+ if (mod && mod.__esModule) return mod;
27
+ var result = {};
28
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29
+ __setModuleDefault(result, mod);
30
+ return result;
31
+ };
32
+ var __importDefault = (this && this.__importDefault) || function (mod) {
33
+ return (mod && mod.__esModule) ? mod : { "default": mod };
34
+ };
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ var jsx_runtime_1 = require("react/jsx-runtime");
37
+ var React = __importStar(require("react"));
38
+ var TreeNodeRendererScaffold_1 = __importDefault(require("./TreeNodeRendererScaffold"));
4
39
  /**
5
40
  * To prevent expand buttons from overflowing on the left, we add a minimum left padding to all entries
6
41
  */
7
- const BASE_LEFT_PADDING = 10;
8
- const NESTING_PADDING = 14;
9
- const TreeNodeRenderer = (props) => {
10
- const { children, lowerSiblingCounts, connectDropTarget, isOver, draggedNode, canDrop } = props;
42
+ var BASE_LEFT_PADDING = 10;
43
+ var NESTING_PADDING = 14;
44
+ var TreeNodeRenderer = function (props) {
45
+ var children = props.children, lowerSiblingCounts = props.lowerSiblingCounts, connectDropTarget = props.connectDropTarget, isOver = props.isOver, draggedNode = props.draggedNode, canDrop = props.canDrop;
11
46
  // Construct the scaffold representing the structure of the tree
12
- const scaffoldBlockCount = lowerSiblingCounts.length;
13
- return connectDropTarget(_jsxs("div", { style: props.style, children: [_jsx("div", { style: {
47
+ var scaffoldBlockCount = lowerSiblingCounts.length;
48
+ return connectDropTarget((0, jsx_runtime_1.jsxs)("div", __assign({ style: props.style }, { children: [(0, jsx_runtime_1.jsx)("div", __assign({ style: {
14
49
  // prettier-ignore
15
- paddingLeft: `${BASE_LEFT_PADDING + (NESTING_PADDING * scaffoldBlockCount)}px`
16
- }, children: React.Children.map(children, (child) => React.cloneElement(child, {
17
- isOver,
18
- canDrop,
19
- draggedNode
20
- })) }, void 0), _jsx(TreeNodeRendererScaffold, { ...props }, void 0)] }, void 0));
50
+ paddingLeft: "".concat(BASE_LEFT_PADDING + (NESTING_PADDING * scaffoldBlockCount), "px")
51
+ } }, { children: React.Children.map(children, function (child) {
52
+ return React.cloneElement(child, {
53
+ isOver: isOver,
54
+ canDrop: canDrop,
55
+ draggedNode: draggedNode
56
+ });
57
+ }) }), void 0), (0, jsx_runtime_1.jsx)(TreeNodeRendererScaffold_1.default, __assign({}, props), void 0)] }), void 0));
21
58
  };
22
- export default TreeNodeRenderer;
59
+ exports.default = TreeNodeRenderer;
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import * as React from 'react';
2
2
  import { TreeRendererProps } from 'react-sortable-tree';
3
3
  declare const TreeNodeRendererScaffold: React.FC<TreeRendererProps>;
4
4
  export default TreeNodeRendererScaffold;
@@ -1,145 +1,24 @@
1
- import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { blue } from '@sanity/color';
3
- import { createGlobalStyle } from 'styled-components';
1
+ "use strict";
2
+ var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
3
+ if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
4
+ return cooked;
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ var jsx_runtime_1 = require("react/jsx-runtime");
8
+ var color_1 = require("@sanity/color");
9
+ var styled_components_1 = require("styled-components");
4
10
  // Adapted from react-sortable-tree/src/tree-node.js
5
- const ScaffoldStyles = createGlobalStyle `
6
- .rst__lineBlock,
7
- .rst__absoluteLineBlock {
8
- height: 100%;
9
- position: relative;
10
- display: inline-block;
11
- --stroke-width: 3px;
12
- }
13
-
14
- .rst__absoluteLineBlock {
15
- position: absolute;
16
- top: 0;
17
- }
18
-
19
- /* Highlight line for pointing to dragged row destination
20
- ========================================================================== */
21
- /**
22
- * +--+--+
23
- * | | |
24
- * | | |
25
- * | | |
26
- * +--+--+
27
- */
28
- .rst__highlightLineVertical {
29
- z-index: 3;
30
- }
31
- .rst__highlightLineVertical::before {
32
- position: absolute;
33
- content: '';
34
- background-color: ${blue[400].hex};
35
- width: calc(var(--stroke-width) * 2);
36
- margin-left: calc(var(--stroke-width) * -1);
37
- left: 50%;
38
- top: 0;
39
- height: 100%;
40
- }
41
-
42
- @keyframes arrow-pulse {
43
- 0% {
44
- transform: translate(0, 0);
45
- opacity: 0;
46
- }
47
- 30% {
48
- transform: translate(0, 300%);
49
- opacity: 1;
50
- }
51
- 70% {
52
- transform: translate(0, 700%);
53
- opacity: 1;
54
- }
55
- 100% {
56
- transform: translate(0, 1000%);
57
- opacity: 0;
58
- }
59
- }
60
- .rst__highlightLineVertical::after {
61
- content: '';
62
- position: absolute;
63
- height: 0;
64
- margin-left: calc(var(--stroke-width) * -1);
65
- left: 50%;
66
- top: 0;
67
- border-left: var(--stroke-width) solid transparent;
68
- border-right: var(--stroke-width) solid transparent;
69
- border-top: var(--stroke-width) solid white;
70
- animation: arrow-pulse 1s infinite linear both;
71
- }
72
-
73
- /**
74
- * +-----+
75
- * | |
76
- * | +--+
77
- * | | |
78
- * +--+--+
79
- */
80
- .rst__highlightTopLeftCorner::before {
81
- z-index: 3;
82
- content: '';
83
- position: absolute;
84
- border-top: solid calc(var(--stroke-width) * 2) ${blue[400].hex};
85
- border-left: solid calc(var(--stroke-width) * 2) ${blue[400].hex};
86
- box-sizing: border-box;
87
- height: calc(50% + var(--stroke-width));
88
- top: 50%;
89
- margin-top: calc(var(--stroke-width) * -1);
90
- right: 0;
91
- width: calc(50% + var(--stroke-width));
92
- }
93
-
94
- /**
95
- * +--+--+
96
- * | | |
97
- * | | |
98
- * | +->|
99
- * +-----+
100
- */
101
- .rst__highlightBottomLeftCorner {
102
- z-index: 3;
103
- }
104
- .rst__highlightBottomLeftCorner::before {
105
- content: '';
106
- position: absolute;
107
- border-bottom: solid calc(var(--stroke-width) * 2) ${blue[400].hex};
108
- border-left: solid calc(var(--stroke-width) * 2) ${blue[400].hex};
109
- box-sizing: border-box;
110
- height: calc(100% + var(--stroke-width));
111
- top: 0;
112
- right: calc(var(--stroke-width) * 3);
113
- width: calc(50% - calc(var(--stroke-width) * 2));
114
- }
115
-
116
- .rst__highlightBottomLeftCorner::after {
117
- content: '';
118
- position: absolute;
119
- height: 0;
120
- right: 0;
121
- top: 100%;
122
- margin-top: calc(var(--stroke-width) * -3);
123
- border-top: calc(var(--stroke-width) * 3) solid transparent;
124
- border-bottom: calc(var(--stroke-width) * 3) solid transparent;
125
- border-left: calc(var(--stroke-width) * 3) solid ${blue[400].hex};
126
- }
127
-
128
- .rst__unclickable {
129
- pointer-events: none;
130
- margin-top: -calc(var(--stroke-width) * 3);
131
- }
132
- `;
133
- const TreeNodeRendererScaffold = (props) => {
134
- const { lowerSiblingCounts, scaffoldBlockPxWidth, listIndex, swapDepth, swapFrom, swapLength, treeIndex } = props;
11
+ var ScaffoldStyles = (0, styled_components_1.createGlobalStyle)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n .rst__lineBlock,\n .rst__absoluteLineBlock {\n height: 100%;\n position: relative;\n display: inline-block;\n --stroke-width: 3px;\n }\n\n .rst__absoluteLineBlock {\n position: absolute;\n top: 0;\n }\n\n /* Highlight line for pointing to dragged row destination\n ========================================================================== */\n /**\n * +--+--+\n * | | |\n * | | |\n * | | |\n * +--+--+\n */\n .rst__highlightLineVertical {\n z-index: 3;\n }\n .rst__highlightLineVertical::before {\n position: absolute;\n content: '';\n background-color: ", ";\n width: calc(var(--stroke-width) * 2);\n margin-left: calc(var(--stroke-width) * -1);\n left: 50%;\n top: 0;\n height: 100%;\n }\n\n @keyframes arrow-pulse {\n 0% {\n transform: translate(0, 0);\n opacity: 0;\n }\n 30% {\n transform: translate(0, 300%);\n opacity: 1;\n }\n 70% {\n transform: translate(0, 700%);\n opacity: 1;\n }\n 100% {\n transform: translate(0, 1000%);\n opacity: 0;\n }\n }\n .rst__highlightLineVertical::after {\n content: '';\n position: absolute;\n height: 0;\n margin-left: calc(var(--stroke-width) * -1);\n left: 50%;\n top: 0;\n border-left: var(--stroke-width) solid transparent;\n border-right: var(--stroke-width) solid transparent;\n border-top: var(--stroke-width) solid white;\n animation: arrow-pulse 1s infinite linear both;\n }\n\n /**\n * +-----+\n * | |\n * | +--+\n * | | |\n * +--+--+\n */\n .rst__highlightTopLeftCorner::before {\n z-index: 3;\n content: '';\n position: absolute;\n border-top: solid calc(var(--stroke-width) * 2) ", ";\n border-left: solid calc(var(--stroke-width) * 2) ", ";\n box-sizing: border-box;\n height: calc(50% + var(--stroke-width));\n top: 50%;\n margin-top: calc(var(--stroke-width) * -1);\n right: 0;\n width: calc(50% + var(--stroke-width));\n }\n\n /**\n * +--+--+\n * | | |\n * | | |\n * | +->|\n * +-----+\n */\n .rst__highlightBottomLeftCorner {\n z-index: 3;\n }\n .rst__highlightBottomLeftCorner::before {\n content: '';\n position: absolute;\n border-bottom: solid calc(var(--stroke-width) * 2) ", ";\n border-left: solid calc(var(--stroke-width) * 2) ", ";\n box-sizing: border-box;\n height: calc(100% + var(--stroke-width));\n top: 0;\n right: calc(var(--stroke-width) * 3);\n width: calc(50% - calc(var(--stroke-width) * 2));\n }\n\n .rst__highlightBottomLeftCorner::after {\n content: '';\n position: absolute;\n height: 0;\n right: 0;\n top: 100%;\n margin-top: calc(var(--stroke-width) * -3);\n border-top: calc(var(--stroke-width) * 3) solid transparent;\n border-bottom: calc(var(--stroke-width) * 3) solid transparent;\n border-left: calc(var(--stroke-width) * 3) solid ", ";\n }\n\n .rst__unclickable {\n pointer-events: none;\n margin-top: -calc(var(--stroke-width) * 3);\n }\n"], ["\n .rst__lineBlock,\n .rst__absoluteLineBlock {\n height: 100%;\n position: relative;\n display: inline-block;\n --stroke-width: 3px;\n }\n\n .rst__absoluteLineBlock {\n position: absolute;\n top: 0;\n }\n\n /* Highlight line for pointing to dragged row destination\n ========================================================================== */\n /**\n * +--+--+\n * | | |\n * | | |\n * | | |\n * +--+--+\n */\n .rst__highlightLineVertical {\n z-index: 3;\n }\n .rst__highlightLineVertical::before {\n position: absolute;\n content: '';\n background-color: ", ";\n width: calc(var(--stroke-width) * 2);\n margin-left: calc(var(--stroke-width) * -1);\n left: 50%;\n top: 0;\n height: 100%;\n }\n\n @keyframes arrow-pulse {\n 0% {\n transform: translate(0, 0);\n opacity: 0;\n }\n 30% {\n transform: translate(0, 300%);\n opacity: 1;\n }\n 70% {\n transform: translate(0, 700%);\n opacity: 1;\n }\n 100% {\n transform: translate(0, 1000%);\n opacity: 0;\n }\n }\n .rst__highlightLineVertical::after {\n content: '';\n position: absolute;\n height: 0;\n margin-left: calc(var(--stroke-width) * -1);\n left: 50%;\n top: 0;\n border-left: var(--stroke-width) solid transparent;\n border-right: var(--stroke-width) solid transparent;\n border-top: var(--stroke-width) solid white;\n animation: arrow-pulse 1s infinite linear both;\n }\n\n /**\n * +-----+\n * | |\n * | +--+\n * | | |\n * +--+--+\n */\n .rst__highlightTopLeftCorner::before {\n z-index: 3;\n content: '';\n position: absolute;\n border-top: solid calc(var(--stroke-width) * 2) ", ";\n border-left: solid calc(var(--stroke-width) * 2) ", ";\n box-sizing: border-box;\n height: calc(50% + var(--stroke-width));\n top: 50%;\n margin-top: calc(var(--stroke-width) * -1);\n right: 0;\n width: calc(50% + var(--stroke-width));\n }\n\n /**\n * +--+--+\n * | | |\n * | | |\n * | +->|\n * +-----+\n */\n .rst__highlightBottomLeftCorner {\n z-index: 3;\n }\n .rst__highlightBottomLeftCorner::before {\n content: '';\n position: absolute;\n border-bottom: solid calc(var(--stroke-width) * 2) ", ";\n border-left: solid calc(var(--stroke-width) * 2) ", ";\n box-sizing: border-box;\n height: calc(100% + var(--stroke-width));\n top: 0;\n right: calc(var(--stroke-width) * 3);\n width: calc(50% - calc(var(--stroke-width) * 2));\n }\n\n .rst__highlightBottomLeftCorner::after {\n content: '';\n position: absolute;\n height: 0;\n right: 0;\n top: 100%;\n margin-top: calc(var(--stroke-width) * -3);\n border-top: calc(var(--stroke-width) * 3) solid transparent;\n border-bottom: calc(var(--stroke-width) * 3) solid transparent;\n border-left: calc(var(--stroke-width) * 3) solid ", ";\n }\n\n .rst__unclickable {\n pointer-events: none;\n margin-top: -calc(var(--stroke-width) * 3);\n }\n"])), color_1.blue[400].hex, color_1.blue[400].hex, color_1.blue[400].hex, color_1.blue[400].hex, color_1.blue[400].hex, color_1.blue[400].hex);
12
+ var TreeNodeRendererScaffold = function (props) {
13
+ var lowerSiblingCounts = props.lowerSiblingCounts, scaffoldBlockPxWidth = props.scaffoldBlockPxWidth, listIndex = props.listIndex, swapDepth = props.swapDepth, swapFrom = props.swapFrom, swapLength = props.swapLength, treeIndex = props.treeIndex;
135
14
  // Construct the scaffold representing the structure of the tree
136
- const scaffold = lowerSiblingCounts.map((lowerSiblingCount, i) => {
15
+ var scaffold = lowerSiblingCounts.map(function (lowerSiblingCount, i) {
137
16
  if (lowerSiblingCount < 0 || treeIndex === listIndex || i !== swapDepth) {
138
17
  return null;
139
18
  }
140
19
  // This row has been shifted, and is at the depth of
141
20
  // the line pointing to the new destination
142
- let highlightLineClass = '';
21
+ var highlightLineClass = '';
143
22
  if (listIndex === (swapFrom || 0) + (swapLength || 0) - 1) {
144
23
  // This block is on the bottom (target) line
145
24
  // This block points at the target block (where the row will go when released)
@@ -153,12 +32,13 @@ const TreeNodeRendererScaffold = (props) => {
153
32
  // This block is between the bottom and top
154
33
  highlightLineClass = 'rst__highlightLineVertical';
155
34
  }
156
- const style = {
35
+ var style = {
157
36
  width: scaffoldBlockPxWidth,
158
37
  left: scaffoldBlockPxWidth * i
159
38
  };
160
- return (_jsx("div", { style: style, className: `rst__unclickable rst__absoluteLineBlock ${highlightLineClass || ''}`, tabIndex: -1 }, i));
39
+ return ((0, jsx_runtime_1.jsx)("div", { style: style, className: "rst__unclickable rst__absoluteLineBlock ".concat(highlightLineClass || ''), tabIndex: -1 }, i));
161
40
  });
162
- return (_jsxs(_Fragment, { children: [scaffold, _jsx(ScaffoldStyles, {}, void 0)] }, void 0));
41
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [scaffold, (0, jsx_runtime_1.jsx)(ScaffoldStyles, {}, void 0)] }, void 0));
163
42
  };
164
- export default TreeNodeRendererScaffold;
43
+ exports.default = TreeNodeRendererScaffold;
44
+ var templateObject_1;
@@ -1,47 +1,77 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import S from '@sanity/desk-tool/structure-builder';
3
- import { AddIcon } from '@sanity/icons';
4
- import TreeDeskStructure from './TreeDeskStructure';
5
- const deskTreeValidator = (props) => {
6
- const { documentId, referenceTo } = props;
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 structure_builder_1 = __importDefault(require("@sanity/desk-tool/structure-builder"));
19
+ var icons_1 = require("@sanity/icons");
20
+ var schema_1 = __importDefault(require("part:@sanity/base/schema"));
21
+ var TreeDeskStructure_1 = __importDefault(require("./TreeDeskStructure"));
22
+ var throwError_1 = __importDefault(require("./utils/throwError"));
23
+ var deskTreeValidator = function (props) {
24
+ var documentId = props.documentId, referenceTo = props.referenceTo;
7
25
  if (typeof documentId !== 'string' && !documentId) {
8
- throw new Error('[hierarchical input] Please add a documentId to your tree');
26
+ (0, throwError_1.default)('invalidDocumentId');
9
27
  }
10
28
  if (!Array.isArray(referenceTo)) {
11
- throw new Error(`[hierarchical input] Missing valid 'referenceTo' in createDeskHierarchy (documentId "${documentId}")`);
29
+ (0, throwError_1.default)('invalidReferenceTo', "(documentId \"".concat(documentId, "\")"));
12
30
  }
13
- return (deskProps) => _jsx(TreeDeskStructure, { ...deskProps, options: props }, void 0);
31
+ return function (deskProps) { return (0, jsx_runtime_1.jsx)(TreeDeskStructure_1.default, __assign({}, deskProps, { options: props }), void 0); };
14
32
  };
15
- export default function createDeskHierarchy(props) {
16
- const { documentId, referenceTo, referenceOptions } = props;
17
- let mainList = (referenceTo?.length === 1
18
- ? S.documentTypeList(referenceTo[0]).schemaType(referenceTo[0])
19
- : S.documentList().filter('_type in $types').params({ types: referenceTo }))
33
+ function createDeskHierarchy(props) {
34
+ var documentId = props.documentId, referenceTo = props.referenceTo, referenceOptions = props.referenceOptions;
35
+ // @TODO: allow creating all types once upstream desk fix is shipped
36
+ /**
37
+ * Context: With multiple referenced document types we can’t set S.documentList().schemaType(),
38
+ * which only accepts one type. So the desk doesn’t have an expanded schemaType to access and
39
+ * try creating a new document without that, which breaks resolveEnabledActions (and probably more)
40
+ * in packages\@sanity\base\src\actions\utils\legacy_documentActionUtils.js
41
+ */
42
+ var safelyCreatableTypes = referenceTo.slice(0, 1);
43
+ var mainList = ((safelyCreatableTypes === null || safelyCreatableTypes === void 0 ? void 0 : safelyCreatableTypes.length) === 1
44
+ ? structure_builder_1.default.documentTypeList(safelyCreatableTypes[0]).schemaType(safelyCreatableTypes[0])
45
+ : structure_builder_1.default.documentList().filter('_type in $types').params({ types: safelyCreatableTypes }))
20
46
  .id(documentId)
21
- .menuItems((referenceTo || []).map((schemaType) => S.menuItem()
22
- .intent({
23
- type: 'create',
24
- params: { type: schemaType, template: schemaType }
25
- })
26
- // @TODO: get the title for each schema type
27
- .title(schemaType)
28
- .icon(AddIcon)))
29
- .canHandleIntent((intent, context) => {
47
+ .menuItems((safelyCreatableTypes || []).map(function (schemaType) {
48
+ var _a, _b;
49
+ return structure_builder_1.default.menuItem()
50
+ .intent({
51
+ type: 'create',
52
+ params: { type: schemaType }
53
+ })
54
+ .title("Create ".concat((_a = schema_1.default.get(schemaType)) === null || _a === void 0 ? void 0 : _a.title))
55
+ .icon(((_b = schema_1.default.get(schemaType)) === null || _b === void 0 ? void 0 : _b.icon) || icons_1.AddIcon);
56
+ }))
57
+ .canHandleIntent(function (intent, context) {
58
+ // Can edit itself
30
59
  if (intent === 'edit' && context.id === props.documentId) {
31
60
  return true;
32
61
  }
33
- if (intent === 'create' && referenceTo.includes(context.type)) {
62
+ // Can create & edit referenced document types
63
+ if (safelyCreatableTypes.includes(context.type)) {
34
64
  return true;
35
65
  }
36
66
  return false;
37
67
  });
38
- if (referenceOptions?.filter) {
68
+ if (referenceOptions === null || referenceOptions === void 0 ? void 0 : referenceOptions.filter) {
39
69
  mainList = mainList.filter(referenceOptions.filter);
40
70
  }
41
- if (referenceOptions?.filterParams) {
71
+ if (referenceOptions === null || referenceOptions === void 0 ? void 0 : referenceOptions.filterParams) {
42
72
  mainList = mainList.params(referenceOptions.filterParams);
43
73
  }
44
- return S.listItem()
74
+ return structure_builder_1.default.listItem()
45
75
  .id(documentId)
46
76
  .title(props.title || documentId)
47
77
  .icon(props.icon)
@@ -52,3 +82,4 @@ export default function createDeskHierarchy(props) {
52
82
  __preserveInstance: true
53
83
  }, props.title ? { title: props.title } : {}));
54
84
  }
85
+ exports.default = createDeskHierarchy;
@@ -0,0 +1,78 @@
1
+ import { ArraySchemaType } from '@sanity/types';
2
+ import * as React from 'react';
3
+ import { TreeDeskStructureProps, TreeFieldSchema } from './types';
4
+ declare type SchemaOptions = Omit<TreeDeskStructureProps, 'documentId' | 'maxDepth'>;
5
+ export declare function createHierarchicalField({ name, title, options, ...rest }: TreeFieldSchema): Omit<ArraySchemaType, 'type' | 'jsonType' | 'of'> & {
6
+ type: string;
7
+ inputComponent: React.FC<any>;
8
+ of?: any[];
9
+ };
10
+ export default function createHierarchicalSchemas(options: SchemaOptions): ({
11
+ name: string;
12
+ title: string;
13
+ type: string;
14
+ fields: ({
15
+ name: string;
16
+ type: string;
17
+ } | {
18
+ name: string;
19
+ type: string;
20
+ title: string;
21
+ fields: ({
22
+ name: string;
23
+ type: string;
24
+ weak?: undefined;
25
+ to?: undefined;
26
+ options?: undefined;
27
+ } | {
28
+ name: string;
29
+ type: string;
30
+ weak: boolean;
31
+ to: {
32
+ type: string;
33
+ }[];
34
+ options: {
35
+ filter?: string | undefined;
36
+ filterParams?: Record<string, unknown> | undefined;
37
+ } | undefined;
38
+ })[];
39
+ })[];
40
+ } | {
41
+ name: string;
42
+ title: string;
43
+ type: string;
44
+ of: ({
45
+ name: string;
46
+ title: string;
47
+ type: string;
48
+ fields: ({
49
+ name: string;
50
+ type: string;
51
+ } | {
52
+ name: string;
53
+ type: string;
54
+ title: string;
55
+ fields: ({
56
+ name: string;
57
+ type: string;
58
+ weak?: undefined;
59
+ to?: undefined;
60
+ options?: undefined;
61
+ } | {
62
+ name: string;
63
+ type: string;
64
+ weak: boolean;
65
+ to: {
66
+ type: string;
67
+ }[];
68
+ options: {
69
+ filter?: string | undefined;
70
+ filterParams?: Record<string, unknown> | undefined;
71
+ } | undefined;
72
+ })[];
73
+ })[];
74
+ } | {
75
+ type: string;
76
+ })[];
77
+ })[];
78
+ export {};
@@ -0,0 +1,138 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
25
+ return (mod && mod.__esModule) ? mod : { "default": mod };
26
+ };
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.createHierarchicalField = void 0;
29
+ var TreeDeskStructure_1 = require("./TreeDeskStructure");
30
+ var TreeInputComponent_1 = __importDefault(require("./TreeInputComponent"));
31
+ var injectNodeTypeInPatches_1 = require("./utils/injectNodeTypeInPatches");
32
+ var throwError_1 = __importDefault(require("./utils/throwError"));
33
+ function createHierarchicalNodeValueType(_a) {
34
+ var referenceTo = _a.referenceTo, referenceOptions = _a.referenceOptions, documentType = _a.documentType;
35
+ return {
36
+ // when used inside the field, name & type are overwritten by createHierarchicalNodeType
37
+ name: documentType ? (0, injectNodeTypeInPatches_1.getSchemaTypeName)(documentType, 'nodeValue') : injectNodeTypeInPatches_1.INTERNAL_NODE_VALUE_TYPE,
38
+ type: 'object',
39
+ title: "Hierarchical node value (".concat(documentType, ")"),
40
+ fields: [
41
+ { name: 'docType', type: 'string' },
42
+ {
43
+ name: 'reference',
44
+ type: 'reference',
45
+ weak: true,
46
+ to: referenceTo.map(function (type) { return ({ type: type }); }),
47
+ options: referenceOptions
48
+ }
49
+ ]
50
+ };
51
+ }
52
+ function createHierarchicalNodeType(options) {
53
+ return {
54
+ // name & type are overwritten by createHierarchicalField
55
+ name: options.documentType
56
+ ? (0, injectNodeTypeInPatches_1.getSchemaTypeName)(options.documentType, 'node')
57
+ : injectNodeTypeInPatches_1.INTERNAL_NODE_TYPE,
58
+ title: "Hierarchical node (".concat(options.documentType, ")"),
59
+ type: 'object',
60
+ fields: [
61
+ { name: 'parent', type: 'string' },
62
+ options.documentType
63
+ ? { name: 'value', type: (0, injectNodeTypeInPatches_1.getSchemaTypeName)(options.documentType, 'nodeValue') }
64
+ : // If no documentType is defined, use an anonymized inline object to avoid
65
+ __assign(__assign({}, createHierarchicalNodeValueType(options)), { name: 'value', type: 'object' })
66
+ ]
67
+ };
68
+ }
69
+ function createHierarchicalArrayType(options) {
70
+ return {
71
+ // name & type are overwritten by createHierarchicalField
72
+ name: options.documentType
73
+ ? (0, injectNodeTypeInPatches_1.getSchemaTypeName)(options.documentType, 'array')
74
+ : injectNodeTypeInPatches_1.INTERNAL_NODE_ARRAY_TYPE,
75
+ title: "Hierarchical array of nodes (".concat(options.documentType, ")"),
76
+ type: 'array',
77
+ of: [
78
+ options.documentType
79
+ ? { type: (0, injectNodeTypeInPatches_1.getSchemaTypeName)(options.documentType, 'node') }
80
+ : createHierarchicalNodeType(options)
81
+ ]
82
+ };
83
+ }
84
+ function createHierarchicalField(_a) {
85
+ var name = _a.name, title = _a.title, options = _a.options, rest = __rest(_a, ["name", "title", "options"]);
86
+ if (!Array.isArray(options === null || options === void 0 ? void 0 : options.referenceTo)) {
87
+ (0, throwError_1.default)('invalidReferenceTo', "(field of name \"".concat(name, "\")"));
88
+ }
89
+ return __assign(__assign(__assign({}, rest), { name: name, title: title, inputComponent: TreeInputComponent_1.default, options: options }), (options.documentType
90
+ ? { type: (0, injectNodeTypeInPatches_1.getSchemaTypeName)(options.documentType, 'array') }
91
+ : __assign(__assign({}, createHierarchicalArrayType(options)), { name: name })));
92
+ }
93
+ exports.createHierarchicalField = createHierarchicalField;
94
+ function createHierarchicalDocType(options) {
95
+ return {
96
+ name: options.documentType,
97
+ title: 'Hierarchical tree',
98
+ type: 'document',
99
+ // The plugin needs to define a `schemaType` with liveEdit enabled so that
100
+ // `useDocumentOperation` in TreeDeskStructure.tsx doesn't create drafts at every patch.
101
+ liveEdit: true,
102
+ fields: [
103
+ createHierarchicalField({
104
+ name: options.fieldKeyInDocument || TreeDeskStructure_1.DEFAULT_FIELD_KEY,
105
+ title: 'Hierarchical Tree',
106
+ options: options
107
+ })
108
+ ],
109
+ preview: {
110
+ select: {
111
+ id: '_id',
112
+ tree: 'tree'
113
+ },
114
+ prepare: function (_a) {
115
+ var id = _a.id, tree = _a.tree;
116
+ return {
117
+ title: "Hierarchical documents (ID: ".concat(id, ")"),
118
+ subtitle: "".concat((tree === null || tree === void 0 ? void 0 : tree.length) || 0, " document(s) in its list.")
119
+ };
120
+ }
121
+ }
122
+ };
123
+ }
124
+ function createHierarchicalSchemas(options) {
125
+ if (!Array.isArray(options.referenceTo) || options.referenceTo.length <= 0) {
126
+ (0, throwError_1.default)('invalidReferenceTo');
127
+ }
128
+ if (!options.documentType) {
129
+ (0, throwError_1.default)('invalidDocumentType');
130
+ }
131
+ return [
132
+ createHierarchicalDocType(options),
133
+ createHierarchicalArrayType(options),
134
+ createHierarchicalNodeType(options),
135
+ createHierarchicalNodeValueType(options)
136
+ ];
137
+ }
138
+ exports.default = createHierarchicalSchemas;
File without changes