@sanity/hierarchical-document-list 0.1.0 → 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 CHANGED
@@ -1,6 +1,10 @@
1
1
  # sanity-plugin-hierarchical-document-list
2
2
 
3
- Plugin for editing hierarchical references in the [Sanity studio](https://www.sanity.io/docs/sanity-studio).
3
+ Plugin for visually organizing documents as hierarchies in the [Sanity studio](https://www.sanity.io/docs/sanity-studio). Applications include:
4
+
5
+ - Tables of content - such as a book's sections and chapters
6
+ - Navigational structure & menus - a website mega-menu with multiple levels, for example
7
+ - Taxonomy inheritance - "_Carbs_ is a parent of _Legumes_ which is a parent of _Beans_"
4
8
 
5
9
  ![Screenshot of the plugin](/screenshot-1.jpg)
6
10
 
@@ -15,9 +19,9 @@ If you're looking for a way to order documents on a flat list, refer to [@sanity
15
19
  sanity install @sanity/hierarchical-document-list
16
20
  ```
17
21
 
18
- With the plugin installed, you'll add the following to your Desk Structure:
22
+ Once you've installed the plugin the next step is to add one or more hierarchy documents to your Structure Builder.
19
23
 
20
- 💡 _If you don't have a custom desk structure, refer to the [Structure Builder docs](https://www.sanity.io/docs/overview-structure-builder) to learn how to do so._
24
+ 💡 _To learn about custom desk structures, refer to the [Structure Builder docs](https://www.sanity.io/docs/overview-structure-builder)._
21
25
 
22
26
  ```js
23
27
  // deskStructure.js
@@ -52,13 +56,15 @@ export default () => {
52
56
 
53
57
  ## How it works
54
58
 
55
- The hierarchical data is stored in the document specified by the `documentId` of your choosing. As compared to storing `parent` fields in each individual document in the hierarchy, this makes it easier to implement different hierarchies for the same content according to the context, and also simplifies querying the full structure - as you'll see in [Querying data](#querying-data) below.
59
+ The hierarchical data is stored in a centralized document with the `documentId` of your choosing. As compared to storing parent/child relationships in each individual document in the hierarchy, this makes it easier to implement different hierarchies for the same content according to the context.
60
+
61
+ This approach also simplifies querying the full structure - as you'll see in [querying data](#querying-data) below.
56
62
 
57
- Keep in mind that **this document is live-edited**, meaning it has no draft and every change by editors will directly affect its published version.
63
+ Keep in mind that this specified **document is live-edited**, meaning it has no draft and every change by editors will directly affect its published version.
58
64
 
59
- Instead of manually adding items one-by-one, the plugin will create a [GROQ](https://www.sanity.io/docs/overview-groq) query that matches all documents with a `_type` in `referenceTo`, that also match the optional `referenceOptions.filter`. From these documents, editors are able to drag, nest and re-order them at will from the "Items not added" list.
65
+ Instead of requiring editors to manually add items one-by-one, the plugin will create a [GROQ](https://www.sanity.io/docs/overview-groq) query that matches all documents with a `_type` in the `referenceTo` option you specify, that also match the optional `referenceOptions.filter`. From these documents, editors are able to drag, nest and re-order them at will from the "Add more items" list.
60
66
 
61
- If a document in the tree doesn't match the filters set, it'll still exist in the tree. This can happen if the document has a new, unfitting value, the configuration changed or it was deleted. Although the tree will still be publishable, editors will get a warning and won't be able to drag these entries around.
67
+ If a document in the tree doesn't match the filters set, it'll keep existing in the data. This can happen if the document has a new, unfitting value, the configuration changed or it was deleted. Although the tree will still be publishable, editors will get a warning and won't be able to drag these faulty entries around.
62
68
 
63
69
  ## Querying data
64
70
 
@@ -170,6 +176,37 @@ const hierarchyDocument = await client.fetch(`*[_id == "book-v3-review-a"][0]{
170
176
  }
171
177
  }`)
172
178
  const tree = flatDataToTree(data.tree)
179
+
180
+ /* Results in a recursively nested structure. Using the example data above:
181
+ {
182
+ "_key": "741b9edde2ba",
183
+ "_type": "hierarchy.node",
184
+ "value": {
185
+ "reference": {
186
+ "_ref": "75c47994-e6bb-487a-b8c9-b283f2436031",
187
+ "_type": "reference",
188
+ "_weak": true
189
+ },
190
+ "docType": "docs.article"
191
+ },
192
+ "parent": null,
193
+ "children": [
194
+ {
195
+ "_key": "f92eaeec96f7",
196
+ "_type": "hierarchy.node",
197
+ "value": {
198
+ "reference": {
199
+ "_ref": "7ad60a02-5d6e-47d8-92e2-6724cc130058",
200
+ "_type": "reference",
201
+ "_weak": true
202
+ },
203
+ "docType": "site.post"
204
+ },
205
+ "parent": "741b9edde2ba"
206
+ }
207
+ ]
208
+ }
209
+ */
173
210
  ```
174
211
 
175
212
  After the transformation above, nodes with nested entries will include a `children` array. This data structure is recursive.
@@ -1,17 +1,3 @@
1
1
  import React from 'react';
2
- interface ErrorInfo {
3
- title: string;
4
- description?: string;
5
- }
6
- declare class TreeEditorErrorBoundary extends React.Component<any, {
7
- error?: ErrorInfo;
8
- }> {
9
- constructor(props: any);
10
- static getDerivedStateFromError(error: unknown): {
11
- error: undefined;
12
- } | {
13
- error: ErrorInfo;
14
- };
15
- render(): JSX.Element;
16
- }
2
+ declare const TreeEditorErrorBoundary: React.FC;
17
3
  export default TreeEditorErrorBoundary;
@@ -1,29 +1,26 @@
1
- var __extends = (this && this.__extends) || (function () {
2
- var extendStatics = function (d, b) {
3
- extendStatics = Object.setPrototypeOf ||
4
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
- return extendStatics(d, b);
7
- };
8
- return function (d, b) {
9
- if (typeof b !== "function" && b !== null)
10
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
- extendStatics(d, b);
12
- function __() { this.constructor = d; }
13
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
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;
14
9
  };
15
- })();
10
+ return __assign.apply(this, arguments);
11
+ };
16
12
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
17
- import { useToast } from '@sanity/ui';
13
+ import { ErrorBoundary, useToast } from '@sanity/ui';
18
14
  import React from 'react';
15
+ var DISPLAY_ERROR = false;
19
16
  var ErrorToast = function (_a) {
20
17
  var error = _a.error;
21
18
  var push = useToast().push;
22
19
  React.useEffect(function () {
23
- if (error === null || error === void 0 ? void 0 : error.title) {
20
+ if ((error === null || error === void 0 ? void 0 : error.error) && DISPLAY_ERROR) {
24
21
  push({
25
- title: error.title,
26
- description: error.description,
22
+ title: error.error.name,
23
+ description: error.error.message,
27
24
  closable: true,
28
25
  status: 'error',
29
26
  id: 'hierarchical-error'
@@ -32,38 +29,10 @@ var ErrorToast = function (_a) {
32
29
  }, [error]);
33
30
  return null;
34
31
  };
35
- var TreeEditorErrorBoundary = /** @class */ (function (_super) {
36
- __extends(TreeEditorErrorBoundary, _super);
37
- function TreeEditorErrorBoundary(props) {
38
- var _this = _super.call(this, props) || this;
39
- _this.state = { error: undefined };
40
- return _this;
41
- }
42
- Object.defineProperty(TreeEditorErrorBoundary, "getDerivedStateFromError", {
43
- enumerable: false,
44
- configurable: true,
45
- writable: true,
46
- value: function (error) {
47
- if (!error) {
48
- return {
49
- error: undefined
50
- };
51
- }
52
- return {
53
- error: {
54
- title: 'Something went wrong'
55
- }
56
- };
57
- }
58
- });
59
- Object.defineProperty(TreeEditorErrorBoundary.prototype, "render", {
60
- enumerable: false,
61
- configurable: true,
62
- writable: true,
63
- value: function () {
64
- return (_jsxs(React.Fragment, { children: [_jsx(ErrorToast, { error: this.state.error }, void 0), this.props.children] }, void 0));
65
- }
66
- });
67
- return TreeEditorErrorBoundary;
68
- }(React.Component));
32
+ var TreeEditorErrorBoundary = function (props) {
33
+ var _a = React.useState(undefined), exception = _a[0], setException = _a[1];
34
+ return (_jsxs(ErrorBoundary, __assign({ onCatch: function (newException) {
35
+ setException(newException);
36
+ } }, { children: [_jsx(ErrorToast, { error: exception }, void 0), props.children] }), void 0));
37
+ };
69
38
  export default TreeEditorErrorBoundary;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/hierarchical-document-list",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "author": "Sanity <hello@sanity.io>",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",