sanity-plugin-documents-pane 1.0.9 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -6
- package/lib/Debug.js +1 -1
- package/lib/Debug.js.map +1 -1
- package/lib/Documents.js +12 -5
- package/lib/Documents.js.map +1 -1
- package/lib/DocumentsPane.js +31 -21
- package/lib/DocumentsPane.js.map +1 -1
- package/lib/NewDocument.js +55 -0
- package/lib/NewDocument.js.map +1 -0
- package/lib/resolveInitialValueTemplates.js +18 -0
- package/lib/resolveInitialValueTemplates.js.map +1 -0
- package/lib/resolveParams.js +44 -0
- package/lib/resolveParams.js.map +1 -0
- package/lib/types.js +2 -0
- package/lib/types.js.map +1 -0
- package/package.json +4 -2
- package/src/Debug.tsx +11 -9
- package/src/Documents.tsx +26 -17
- package/src/DocumentsPane.tsx +42 -26
- package/src/NewDocument.tsx +38 -0
- package/src/resolveInitialValueTemplates.ts +20 -0
- package/src/resolveParams.ts +36 -0
- package/src/types.ts +34 -0
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ sanity install documents-pane
|
|
|
12
12
|
|
|
13
13
|
This plugin is designed to be used as a [Component inside of a View](https://www.sanity.io/docs/structure-builder-reference#c0c8284844b7).
|
|
14
14
|
|
|
15
|
-
The example below illustrates using the current Document being used to query for all
|
|
15
|
+
The example below illustrates using the current Document being used to query for all published documents that reference it.
|
|
16
16
|
|
|
17
17
|
```js
|
|
18
18
|
// ./src/deskStructure.js
|
|
@@ -25,18 +25,64 @@ S.view
|
|
|
25
25
|
.options({
|
|
26
26
|
query: `*[!(_id in path("drafts.**")) && references($id)]`,
|
|
27
27
|
params: {id: `_id`},
|
|
28
|
-
useDraft: false,
|
|
29
|
-
debug: true,
|
|
30
28
|
})
|
|
31
29
|
.title('Incoming References')
|
|
32
30
|
```
|
|
33
31
|
|
|
34
32
|
The `.options()` configuration works as follows:
|
|
35
33
|
|
|
36
|
-
- `query` (string, required)
|
|
37
|
-
- `params` (object, optional)
|
|
38
|
-
-
|
|
34
|
+
- `query` (string, required) A string defining the entire GROQ query that will select documents to list.
|
|
35
|
+
- `params` (object or function, optional)
|
|
36
|
+
- Object: a [dot-notated string](https://www.npmjs.com/package/dlv) from the document object to a field, to use as variables in the query.
|
|
37
|
+
- Function: a function that receives the various displayed, draft, and published versions of the document, and returns an object of query parameters. Return null if the parameters cannot be resolved.
|
|
38
|
+
- `useDraft` (bool, optional, default: `false`) When populating the `params` values, it will use the `published` version of the document by default. Not permitted if using a function for `params` as the function will determine which version of the document to use.
|
|
39
39
|
- `debug` (bool, optional, default: `false`) In case of an error or the query returning no documents, setting to `true` will display the query and params that were used.
|
|
40
|
+
- `initialValueTemplates` (function, optional) A function that receives the various displayed, draft, and published versions of the document, and returns a list of initial value templates. These will be used to define buttons at the top of the list so users can create new related documents.
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
## Resolving query parameters with a function and providing initial value templates
|
|
44
|
+
Providing a function for `params` allows us to modify values from the current document, for example to list references to a draft document. Providing a function for the `initialValueTemplates` option allows us to determine which buttons to show and what parameters will be used for the new document.
|
|
45
|
+
```js
|
|
46
|
+
const options = {
|
|
47
|
+
query: `*[_type=="post" && author._ref == $id]`,
|
|
48
|
+
params: ({document}) => {
|
|
49
|
+
// references will never point to a draft ID, so extract the regular ID
|
|
50
|
+
const id = document.displayed._id?.replace('drafts.', '')
|
|
51
|
+
|
|
52
|
+
// we don't have to worry about undefined parameters,
|
|
53
|
+
// as the plugin will handle them and show an appropriate message
|
|
54
|
+
return {id}
|
|
55
|
+
},
|
|
56
|
+
initialValueTemplates: ({document}) => {
|
|
57
|
+
const templates = []
|
|
58
|
+
|
|
59
|
+
// references must point to a non-draft ID, so if using the ID in the template,
|
|
60
|
+
// be sure it doesn't start with `drafts.`
|
|
61
|
+
const id = document?.displayed?._id.replace('drafts.', '')
|
|
62
|
+
const name = document?.displayed?.name || 'author'
|
|
63
|
+
|
|
64
|
+
if (id) {
|
|
65
|
+
templates.push({
|
|
66
|
+
// the name of the schema type that should be created (required)
|
|
67
|
+
schemaType: 'post',
|
|
68
|
+
// the title that should appear on the button - we can customize it (required)
|
|
69
|
+
title: `New post by ${name}`,
|
|
70
|
+
// the name of the template that should be used (optional)
|
|
71
|
+
template: 'postWithAuthor',
|
|
72
|
+
// values for parameters that can be passed to the template referenced above (optional)
|
|
73
|
+
parameters: {
|
|
74
|
+
authorId: id,
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
// we could push more templates if needed.
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// must always return a list, even if empty
|
|
82
|
+
return templates
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
```
|
|
40
86
|
|
|
41
87
|
## Thanks!
|
|
42
88
|
|
package/lib/Debug.js
CHANGED
|
@@ -16,7 +16,7 @@ function Debug(_ref) {
|
|
|
16
16
|
params = _ref.params;
|
|
17
17
|
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.Stack, {
|
|
18
18
|
space: 4
|
|
19
|
-
}, /*#__PURE__*/_react.default.createElement(_ui.Box, null, /*#__PURE__*/_react.default.createElement(_ui.Label, null, "Query")), /*#__PURE__*/_react.default.createElement(_ui.Box, null, /*#__PURE__*/_react.default.createElement(_ui.Code, null, query))), /*#__PURE__*/_react.default.createElement(_ui.Stack, {
|
|
19
|
+
}, /*#__PURE__*/_react.default.createElement(_ui.Box, null, /*#__PURE__*/_react.default.createElement(_ui.Label, null, "Query")), /*#__PURE__*/_react.default.createElement(_ui.Box, null, /*#__PURE__*/_react.default.createElement(_ui.Code, null, query))), params && /*#__PURE__*/_react.default.createElement(_ui.Stack, {
|
|
20
20
|
space: 4
|
|
21
21
|
}, /*#__PURE__*/_react.default.createElement(_ui.Box, null, /*#__PURE__*/_react.default.createElement(_ui.Label, null, "Params")), /*#__PURE__*/_react.default.createElement(_ui.Box, null, /*#__PURE__*/_react.default.createElement(_ui.Code, null, JSON.stringify(params)))));
|
|
22
22
|
}
|
package/lib/Debug.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Debug.js","names":["Debug","query","params","JSON","stringify"],"sources":["../src/Debug.tsx"],"sourcesContent":["import React from 'react'\nimport {Code, Box, Label, Stack} from '@sanity/ui'\n\nexport default function Debug({query, params}: {query: string; params
|
|
1
|
+
{"version":3,"file":"Debug.js","names":["Debug","query","params","JSON","stringify"],"sources":["../src/Debug.tsx"],"sourcesContent":["import React from 'react'\nimport {Code, Box, Label, Stack} from '@sanity/ui'\n\nexport default function Debug({query, params}: {query: string; params?: {[key: string]: string}}) {\n return (\n <>\n <Stack space={4}>\n <Box>\n <Label>Query</Label>\n </Box>\n <Box>\n <Code>{query}</Code>\n </Box>\n </Stack>\n {params && (\n <Stack space={4}>\n <Box>\n <Label>Params</Label>\n </Box>\n <Box>\n <Code>{JSON.stringify(params)}</Code>\n </Box>\n </Stack>\n )}\n </>\n )\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;;;AAEe,SAASA,KAAT,OAAmF;EAAA,IAAnEC,KAAmE,QAAnEA,KAAmE;EAAA,IAA5DC,MAA4D,QAA5DA,MAA4D;EAChG,oBACE,yEACE,6BAAC,SAAD;IAAO,KAAK,EAAE;EAAd,gBACE,6BAAC,OAAD,qBACE,6BAAC,SAAD,gBADF,CADF,eAIE,6BAAC,OAAD,qBACE,6BAAC,QAAD,QAAOD,KAAP,CADF,CAJF,CADF,EASGC,MAAM,iBACL,6BAAC,SAAD;IAAO,KAAK,EAAE;EAAd,gBACE,6BAAC,OAAD,qBACE,6BAAC,SAAD,iBADF,CADF,eAIE,6BAAC,OAAD,qBACE,6BAAC,QAAD,QAAOC,IAAI,CAACC,SAAL,CAAeF,MAAf,CAAP,CADF,CAJF,CAVJ,CADF;AAsBD"}
|
package/lib/Documents.js
CHANGED
|
@@ -23,6 +23,8 @@ var _Feedback = _interopRequireDefault(require("./Feedback"));
|
|
|
23
23
|
|
|
24
24
|
var _useListeningQuery2 = _interopRequireDefault(require("./hooks/useListeningQuery"));
|
|
25
25
|
|
|
26
|
+
var _NewDocument = _interopRequireDefault(require("./NewDocument"));
|
|
27
|
+
|
|
26
28
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
27
29
|
|
|
28
30
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
@@ -32,7 +34,8 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
32
34
|
function Documents(props) {
|
|
33
35
|
var query = props.query,
|
|
34
36
|
params = props.params,
|
|
35
|
-
debug = props.debug
|
|
37
|
+
debug = props.debug,
|
|
38
|
+
initialValueTemplates = props.initialValueTemplates;
|
|
36
39
|
|
|
37
40
|
var _usePaneRouter = (0, _deskTool.usePaneRouter)(),
|
|
38
41
|
routerPanesState = _usePaneRouter.routerPanesState,
|
|
@@ -81,16 +84,20 @@ function Documents(props) {
|
|
|
81
84
|
}
|
|
82
85
|
|
|
83
86
|
if (!(data !== null && data !== void 0 && data.length)) {
|
|
84
|
-
return /*#__PURE__*/_react.default.createElement(
|
|
87
|
+
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_NewDocument.default, {
|
|
88
|
+
initialValueTemplates: initialValueTemplates
|
|
89
|
+
}), /*#__PURE__*/_react.default.createElement(_ui.Stack, {
|
|
85
90
|
padding: 4,
|
|
86
91
|
space: 5
|
|
87
92
|
}, /*#__PURE__*/_react.default.createElement(_Feedback.default, null, "No Documents found"), debug && /*#__PURE__*/_react.default.createElement(_Debug.default, {
|
|
88
93
|
query: query,
|
|
89
94
|
params: params
|
|
90
|
-
}));
|
|
95
|
+
})));
|
|
91
96
|
}
|
|
92
97
|
|
|
93
|
-
return /*#__PURE__*/_react.default.createElement(
|
|
98
|
+
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_NewDocument.default, {
|
|
99
|
+
initialValueTemplates: initialValueTemplates
|
|
100
|
+
}), /*#__PURE__*/_react.default.createElement(_ui.Stack, {
|
|
94
101
|
padding: 2,
|
|
95
102
|
space: 1
|
|
96
103
|
}, data.map(doc => /*#__PURE__*/_react.default.createElement(_ui.Button, {
|
|
@@ -101,6 +108,6 @@ function Documents(props) {
|
|
|
101
108
|
}, /*#__PURE__*/_react.default.createElement(_preview.default, {
|
|
102
109
|
value: doc,
|
|
103
110
|
type: _schema.default.get(doc._type)
|
|
104
|
-
}))));
|
|
111
|
+
})))));
|
|
105
112
|
}
|
|
106
113
|
//# sourceMappingURL=Documents.js.map
|
package/lib/Documents.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Documents.js","names":["Documents","props","query","params","debug","usePaneRouter","routerPanesState","groupIndex","handleEditReference","useListeningQuery","loading","error","data","handleClick","useCallback","id","type","childParams","parentRefPath","pathFromString","template","length","map","doc","_id","_type","schema","get"],"sources":["../src/Documents.tsx"],"sourcesContent":["import React, {useCallback} from 'react'\nimport {Box, Button, Stack, Flex, Spinner} from '@sanity/ui'\nimport {fromString as pathFromString} from '@sanity/util/paths'\nimport {usePaneRouter} from '@sanity/desk-tool'\nimport Preview from 'part:@sanity/base/preview'\nimport schema from 'part:@sanity/base/schema'\n\nimport Debug from './Debug'\nimport Feedback from './Feedback'\nimport useListeningQuery from './hooks/useListeningQuery'\n\ntype DocumentsProps = {\n query: string\n params: {[key: string]: string}\n debug: boolean\n}\n\nexport default function Documents(props: DocumentsProps) {\n const {query, params, debug} = props\n const {routerPanesState, groupIndex, handleEditReference} = usePaneRouter()\n\n const {loading, error, data} = useListeningQuery(query, params)\n\n const handleClick = useCallback(\n (id, type) => {\n const childParams = routerPanesState[groupIndex + 1]?.[0].params || {}\n const {parentRefPath} = childParams\n\n handleEditReference({\n id,\n type,\n // Uncertain that this works as intended\n parentRefPath: parentRefPath ? pathFromString(parentRefPath) : [``],\n // Added this to satisfy TS\n template: type,\n })\n },\n [routerPanesState, groupIndex, handleEditReference]\n )\n\n if (loading) {\n return (\n <Box padding={4}>\n <Flex justify=\"center\" align=\"center\">\n <Spinner muted />\n </Flex>\n </Box>\n )\n }\n\n if (error) {\n return (\n <Stack padding={4} space={5}>\n <Feedback>There was en error performing this query</Feedback>\n {debug && <Debug query={query} params={params} />}\n </Stack>\n )\n }\n\n if (!data?.length) {\n return (\n <Stack padding={4} space={5}>\n
|
|
1
|
+
{"version":3,"file":"Documents.js","names":["Documents","props","query","params","debug","initialValueTemplates","usePaneRouter","routerPanesState","groupIndex","handleEditReference","useListeningQuery","loading","error","data","handleClick","useCallback","id","type","childParams","parentRefPath","pathFromString","template","length","map","doc","_id","_type","schema","get"],"sources":["../src/Documents.tsx"],"sourcesContent":["import React, {useCallback} from 'react'\nimport {Box, Button, Stack, Flex, Spinner} from '@sanity/ui'\nimport {fromString as pathFromString} from '@sanity/util/paths'\nimport {usePaneRouter} from '@sanity/desk-tool'\nimport Preview from 'part:@sanity/base/preview'\nimport schema from 'part:@sanity/base/schema'\n\nimport Debug from './Debug'\nimport Feedback from './Feedback'\nimport useListeningQuery from './hooks/useListeningQuery'\nimport {DocumentsPaneInitialValueTemplate} from './types'\nimport NewDocument from './NewDocument'\n\ntype DocumentsProps = {\n query: string\n params: {[key: string]: string}\n debug: boolean\n initialValueTemplates: DocumentsPaneInitialValueTemplate[]\n}\n\nexport default function Documents(props: DocumentsProps) {\n const {query, params, debug, initialValueTemplates} = props\n const {routerPanesState, groupIndex, handleEditReference} = usePaneRouter()\n\n const {loading, error, data} = useListeningQuery(query, params)\n\n const handleClick = useCallback(\n (id, type) => {\n const childParams = routerPanesState[groupIndex + 1]?.[0].params || {}\n const {parentRefPath} = childParams\n\n handleEditReference({\n id,\n type,\n // Uncertain that this works as intended\n parentRefPath: parentRefPath ? pathFromString(parentRefPath) : [``],\n // Added this to satisfy TS\n template: type,\n })\n },\n [routerPanesState, groupIndex, handleEditReference]\n )\n\n if (loading) {\n return (\n <Box padding={4}>\n <Flex justify=\"center\" align=\"center\">\n <Spinner muted />\n </Flex>\n </Box>\n )\n }\n\n if (error) {\n return (\n <Stack padding={4} space={5}>\n <Feedback>There was en error performing this query</Feedback>\n {debug && <Debug query={query} params={params} />}\n </Stack>\n )\n }\n\n if (!data?.length) {\n return (\n <>\n <NewDocument initialValueTemplates={initialValueTemplates} />\n <Stack padding={4} space={5}>\n <Feedback>No Documents found</Feedback>\n {debug && <Debug query={query} params={params} />}\n </Stack>\n </>\n )\n }\n\n return (\n <>\n <NewDocument initialValueTemplates={initialValueTemplates} />\n <Stack padding={2} space={1}>\n {data.map((doc) => (\n <Button\n key={doc._id}\n onClick={() => handleClick(doc._id, doc._type)}\n padding={2}\n mode=\"bleed\"\n >\n <Preview value={doc} type={schema.get(doc._type)} />\n </Button>\n ))}\n </Stack>\n </>\n )\n}\n"],"mappings":"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"}
|
package/lib/DocumentsPane.js
CHANGED
|
@@ -7,8 +7,6 @@ exports.default = DocumentsPane;
|
|
|
7
7
|
|
|
8
8
|
var _react = _interopRequireDefault(require("react"));
|
|
9
9
|
|
|
10
|
-
var _dlv = _interopRequireDefault(require("dlv"));
|
|
11
|
-
|
|
12
10
|
var _ui = require("@sanity/ui");
|
|
13
11
|
|
|
14
12
|
var _Documents = _interopRequireDefault(require("./Documents"));
|
|
@@ -17,44 +15,56 @@ var _Feedback = _interopRequireDefault(require("./Feedback"));
|
|
|
17
15
|
|
|
18
16
|
var _Debug = _interopRequireDefault(require("./Debug"));
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
18
|
+
var _resolveParams = _interopRequireDefault(require("./resolveParams"));
|
|
23
19
|
|
|
24
|
-
|
|
20
|
+
var _resolveInitialValueTemplates = _interopRequireDefault(require("./resolveInitialValueTemplates"));
|
|
25
21
|
|
|
26
|
-
function
|
|
22
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
27
23
|
|
|
28
24
|
function DocumentsPane(props) {
|
|
29
|
-
var
|
|
25
|
+
var document = props.document,
|
|
30
26
|
options = props.options;
|
|
31
27
|
var query = options.query,
|
|
32
28
|
params = options.params,
|
|
33
|
-
useDraft = options.useDraft,
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
_options$useDraft = options.useDraft,
|
|
30
|
+
useDraft = _options$useDraft === void 0 ? false : _options$useDraft,
|
|
31
|
+
_options$debug = options.debug,
|
|
32
|
+
debug = _options$debug === void 0 ? false : _options$debug,
|
|
33
|
+
initialValueTemplatesResolver = options.initialValueTemplates;
|
|
36
34
|
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
if (useDraft && typeof params === 'function') {
|
|
36
|
+
return /*#__PURE__*/_react.default.createElement(_ui.Stack, {
|
|
37
|
+
padding: 4,
|
|
38
|
+
space: 5
|
|
39
|
+
}, /*#__PURE__*/_react.default.createElement(_Feedback.default, null, /*#__PURE__*/_react.default.createElement("code", null, "useDraft"), " should not be ", /*#__PURE__*/_react.default.createElement("code", null, "true"), " when supplying a function for", /*#__PURE__*/_react.default.createElement("code", null, "params")), debug && /*#__PURE__*/_react.default.createElement(_Debug.default, {
|
|
40
|
+
query: query
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
39
43
|
|
|
40
|
-
var paramValues =
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
var paramValues = (0, _resolveParams.default)({
|
|
45
|
+
document,
|
|
46
|
+
params,
|
|
47
|
+
useDraft
|
|
48
|
+
});
|
|
49
|
+
var initialValueTemplates = (0, _resolveInitialValueTemplates.default)({
|
|
50
|
+
resolver: initialValueTemplatesResolver,
|
|
51
|
+
document
|
|
52
|
+
});
|
|
43
53
|
|
|
44
|
-
if (!
|
|
54
|
+
if (!paramValues) {
|
|
45
55
|
return /*#__PURE__*/_react.default.createElement(_ui.Stack, {
|
|
46
56
|
padding: 4,
|
|
47
57
|
space: 5
|
|
48
|
-
}, /*#__PURE__*/_react.default.createElement(_Feedback.default, null, "
|
|
49
|
-
query: query
|
|
50
|
-
params: params
|
|
58
|
+
}, /*#__PURE__*/_react.default.createElement(_Feedback.default, null, "Parameters for this query could not be resolved. This may mean the document does not yet exist or is incomplete."), debug && /*#__PURE__*/_react.default.createElement(_Debug.default, {
|
|
59
|
+
query: query
|
|
51
60
|
}));
|
|
52
61
|
}
|
|
53
62
|
|
|
54
63
|
return /*#__PURE__*/_react.default.createElement(_Documents.default, {
|
|
55
64
|
query: query,
|
|
56
65
|
params: paramValues,
|
|
57
|
-
debug: debug
|
|
66
|
+
debug: debug,
|
|
67
|
+
initialValueTemplates: initialValueTemplates
|
|
58
68
|
});
|
|
59
69
|
}
|
|
60
70
|
//# sourceMappingURL=DocumentsPane.js.map
|
package/lib/DocumentsPane.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DocumentsPane.js","names":["DocumentsPane","props","
|
|
1
|
+
{"version":3,"file":"DocumentsPane.js","names":["DocumentsPane","props","document","options","query","params","useDraft","debug","initialValueTemplatesResolver","initialValueTemplates","paramValues","resolveParams","resolveInitialValueTemplates","resolver"],"sources":["../src/DocumentsPane.tsx"],"sourcesContent":["import React from 'react'\nimport {Stack} from '@sanity/ui'\n\nimport Documents from './Documents'\nimport Feedback from './Feedback'\nimport Debug from './Debug'\nimport {DocumentsPaneProps} from './types'\nimport resolveParams from './resolveParams'\nimport resolveInitialValueTemplates from './resolveInitialValueTemplates'\n\nexport default function DocumentsPane(props: DocumentsPaneProps) {\n const {document, options} = props\n const {\n query,\n params,\n useDraft = false,\n debug = false,\n initialValueTemplates: initialValueTemplatesResolver,\n } = options\n\n if (useDraft && typeof params === 'function') {\n return (\n <Stack padding={4} space={5}>\n <Feedback>\n <code>useDraft</code> should not be <code>true</code> when supplying a function for\n <code>params</code>\n </Feedback>\n {debug && <Debug query={query} />}\n </Stack>\n )\n }\n\n const paramValues = resolveParams({document, params, useDraft})\n\n const initialValueTemplates = resolveInitialValueTemplates({\n resolver: initialValueTemplatesResolver,\n document,\n })\n\n if (!paramValues) {\n return (\n <Stack padding={4} space={5}>\n <Feedback>\n Parameters for this query could not be resolved. This may mean the document does not yet\n exist or is incomplete.\n </Feedback>\n {debug && <Debug query={query} />}\n </Stack>\n )\n }\n\n return (\n <Documents\n query={query}\n params={paramValues}\n debug={debug}\n initialValueTemplates={initialValueTemplates}\n />\n )\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;AAEA;;AACA;;AACA;;AAEA;;AACA;;;;AAEe,SAASA,aAAT,CAAuBC,KAAvB,EAAkD;EAC/D,IAAOC,QAAP,GAA4BD,KAA5B,CAAOC,QAAP;EAAA,IAAiBC,OAAjB,GAA4BF,KAA5B,CAAiBE,OAAjB;EACA,IACEC,KADF,GAMID,OANJ,CACEC,KADF;EAAA,IAEEC,MAFF,GAMIF,OANJ,CAEEE,MAFF;EAAA,wBAMIF,OANJ,CAGEG,QAHF;EAAA,IAGEA,QAHF,kCAGa,KAHb;EAAA,qBAMIH,OANJ,CAIEI,KAJF;EAAA,IAIEA,KAJF,+BAIU,KAJV;EAAA,IAKyBC,6BALzB,GAMIL,OANJ,CAKEM,qBALF;;EAQA,IAAIH,QAAQ,IAAI,OAAOD,MAAP,KAAkB,UAAlC,EAA8C;IAC5C,oBACE,6BAAC,SAAD;MAAO,OAAO,EAAE,CAAhB;MAAmB,KAAK,EAAE;IAA1B,gBACE,6BAAC,iBAAD,qBACE,sDADF,kCACsC,kDADtC,iDAEE,oDAFF,CADF,EAKGE,KAAK,iBAAI,6BAAC,cAAD;MAAO,KAAK,EAAEH;IAAd,EALZ,CADF;EASD;;EAED,IAAMM,WAAW,GAAG,IAAAC,sBAAA,EAAc;IAACT,QAAD;IAAWG,MAAX;IAAmBC;EAAnB,CAAd,CAApB;EAEA,IAAMG,qBAAqB,GAAG,IAAAG,qCAAA,EAA6B;IACzDC,QAAQ,EAAEL,6BAD+C;IAEzDN;EAFyD,CAA7B,CAA9B;;EAKA,IAAI,CAACQ,WAAL,EAAkB;IAChB,oBACE,6BAAC,SAAD;MAAO,OAAO,EAAE,CAAhB;MAAmB,KAAK,EAAE;IAA1B,gBACE,6BAAC,iBAAD,2HADF,EAKGH,KAAK,iBAAI,6BAAC,cAAD;MAAO,KAAK,EAAEH;IAAd,EALZ,CADF;EASD;;EAED,oBACE,6BAAC,kBAAD;IACE,KAAK,EAAEA,KADT;IAEE,MAAM,EAAEM,WAFV;IAGE,KAAK,EAAEH,KAHT;IAIE,qBAAqB,EAAEE;EAJzB,EADF;AAQD"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = NewDocument;
|
|
7
|
+
|
|
8
|
+
var _ui = require("@sanity/ui");
|
|
9
|
+
|
|
10
|
+
var _react = _interopRequireDefault(require("react"));
|
|
11
|
+
|
|
12
|
+
var _icons = require("@sanity/icons");
|
|
13
|
+
|
|
14
|
+
var _deskTool = require("@sanity/desk-tool");
|
|
15
|
+
|
|
16
|
+
var _uuid = require("@sanity/uuid");
|
|
17
|
+
|
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
+
|
|
20
|
+
function NewDocument(props) {
|
|
21
|
+
var _props$initialValueTe = props.initialValueTemplates,
|
|
22
|
+
initialValueTemplates = _props$initialValueTe === void 0 ? [] : _props$initialValueTe;
|
|
23
|
+
|
|
24
|
+
var _usePaneRouter = (0, _deskTool.usePaneRouter)(),
|
|
25
|
+
ReferenceChildLink = _usePaneRouter.ReferenceChildLink;
|
|
26
|
+
|
|
27
|
+
if (!initialValueTemplates.length) return null;
|
|
28
|
+
return /*#__PURE__*/_react.default.createElement(_ui.Card, {
|
|
29
|
+
borderBottom: true,
|
|
30
|
+
padding: 2
|
|
31
|
+
}, /*#__PURE__*/_react.default.createElement(_ui.Flex, {
|
|
32
|
+
justify: "flex-end",
|
|
33
|
+
gap: 1
|
|
34
|
+
}, initialValueTemplates.map(template => {
|
|
35
|
+
return /*#__PURE__*/_react.default.createElement(ReferenceChildLink, {
|
|
36
|
+
documentId: (0, _uuid.uuid)(),
|
|
37
|
+
documentType: template.schemaType,
|
|
38
|
+
template: {
|
|
39
|
+
id: template.template,
|
|
40
|
+
params: template.parameters
|
|
41
|
+
},
|
|
42
|
+
parentRefPath: [],
|
|
43
|
+
key: "".concat(template.schemaType, "-").concat(template.template),
|
|
44
|
+
style: {
|
|
45
|
+
textDecoration: 'none'
|
|
46
|
+
}
|
|
47
|
+
}, /*#__PURE__*/_react.default.createElement(_ui.Button, {
|
|
48
|
+
icon: /*#__PURE__*/_react.default.createElement(_icons.ComposeIcon, null),
|
|
49
|
+
text: template.title,
|
|
50
|
+
mode: "bleed",
|
|
51
|
+
as: "span"
|
|
52
|
+
}));
|
|
53
|
+
})));
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=NewDocument.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NewDocument.js","names":["NewDocument","props","initialValueTemplates","usePaneRouter","ReferenceChildLink","length","map","template","uuid","schemaType","id","params","parameters","textDecoration","title"],"sources":["../src/NewDocument.tsx"],"sourcesContent":["import {Button, Card, Flex} from '@sanity/ui'\nimport React from 'react'\nimport {DocumentsPaneInitialValueTemplate} from './types'\nimport {ComposeIcon} from '@sanity/icons'\nimport {usePaneRouter} from '@sanity/desk-tool'\nimport {uuid} from \"@sanity/uuid\"\n\ninterface NewDocumentProps {\n initialValueTemplates: DocumentsPaneInitialValueTemplate[]\n}\n\nexport default function NewDocument(props: NewDocumentProps) {\n const {initialValueTemplates = []} = props\n const {ReferenceChildLink} = usePaneRouter()\n\n if (!initialValueTemplates.length) return null\n\n return (\n <Card borderBottom={true} padding={2}>\n <Flex justify=\"flex-end\" gap={1}>\n {initialValueTemplates.map((template) => {\n return (\n <ReferenceChildLink\n documentId={uuid()}\n documentType={template.schemaType}\n template={{id: template.template, params: template.parameters}}\n parentRefPath={[]}\n key={`${template.schemaType}-${template.template}`}\n style={{textDecoration: 'none'}}\n >\n <Button icon={<ComposeIcon />} text={template.title} mode=\"bleed\" as=\"span\" />\n </ReferenceChildLink>\n )\n })}\n </Flex>\n </Card>\n )\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;AAEA;;AACA;;AACA;;;;AAMe,SAASA,WAAT,CAAqBC,KAArB,EAA8C;EAC3D,4BAAqCA,KAArC,CAAOC,qBAAP;EAAA,IAAOA,qBAAP,sCAA+B,EAA/B;;EACA,qBAA6B,IAAAC,uBAAA,GAA7B;EAAA,IAAOC,kBAAP,kBAAOA,kBAAP;;EAEA,IAAI,CAACF,qBAAqB,CAACG,MAA3B,EAAmC,OAAO,IAAP;EAEnC,oBACE,6BAAC,QAAD;IAAM,YAAY,EAAE,IAApB;IAA0B,OAAO,EAAE;EAAnC,gBACE,6BAAC,QAAD;IAAM,OAAO,EAAC,UAAd;IAAyB,GAAG,EAAE;EAA9B,GACGH,qBAAqB,CAACI,GAAtB,CAA2BC,QAAD,IAAc;IACvC,oBACE,6BAAC,kBAAD;MACE,UAAU,EAAE,IAAAC,UAAA,GADd;MAEE,YAAY,EAAED,QAAQ,CAACE,UAFzB;MAGE,QAAQ,EAAE;QAACC,EAAE,EAAEH,QAAQ,CAACA,QAAd;QAAwBI,MAAM,EAAEJ,QAAQ,CAACK;MAAzC,CAHZ;MAIE,aAAa,EAAE,EAJjB;MAKE,GAAG,YAAKL,QAAQ,CAACE,UAAd,cAA4BF,QAAQ,CAACA,QAArC,CALL;MAME,KAAK,EAAE;QAACM,cAAc,EAAE;MAAjB;IANT,gBAQE,6BAAC,UAAD;MAAQ,IAAI,eAAE,6BAAC,kBAAD,OAAd;MAA+B,IAAI,EAAEN,QAAQ,CAACO,KAA9C;MAAqD,IAAI,EAAC,OAA1D;MAAkE,EAAE,EAAC;IAArE,EARF,CADF;EAYD,CAbA,CADH,CADF,CADF;AAoBD"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = resolveInitialValueTemplates;
|
|
7
|
+
|
|
8
|
+
function resolveInitialValueTemplates(options) {
|
|
9
|
+
var _ref = options || {},
|
|
10
|
+
resolver = _ref.resolver,
|
|
11
|
+
document = _ref.document;
|
|
12
|
+
|
|
13
|
+
if (!resolver) return [];
|
|
14
|
+
return resolver({
|
|
15
|
+
document
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=resolveInitialValueTemplates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveInitialValueTemplates.js","names":["resolveInitialValueTemplates","options","resolver","document"],"sources":["../src/resolveInitialValueTemplates.ts"],"sourcesContent":["import {\n DocumentsPaneInitialValueTemplate,\n DocumentsPaneInitialValueTemplateResolver,\n DocumentVersionsCollection,\n} from './types'\n\ninterface ResolveInitialValueTemplatesOptions {\n resolver: DocumentsPaneInitialValueTemplateResolver | undefined\n document: DocumentVersionsCollection\n}\n\nexport default function resolveInitialValueTemplates(\n options: ResolveInitialValueTemplatesOptions\n): DocumentsPaneInitialValueTemplate[] {\n const {resolver, document} = options || {}\n\n if (!resolver) return []\n\n return resolver({document})\n}\n"],"mappings":";;;;;;;AAWe,SAASA,4BAAT,CACbC,OADa,EAEwB;EACrC,WAA6BA,OAAO,IAAI,EAAxC;EAAA,IAAOC,QAAP,QAAOA,QAAP;EAAA,IAAiBC,QAAjB,QAAiBA,QAAjB;;EAEA,IAAI,CAACD,QAAL,EAAe,OAAO,EAAP;EAEf,OAAOA,QAAQ,CAAC;IAACC;EAAD,CAAD,CAAf;AACD"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = resolveParams;
|
|
7
|
+
|
|
8
|
+
var _dlv = _interopRequireDefault(require("dlv"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
13
|
+
|
|
14
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
15
|
+
|
|
16
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
17
|
+
|
|
18
|
+
function defaultResolver(options) {
|
|
19
|
+
var params = options.params,
|
|
20
|
+
document = options.document,
|
|
21
|
+
useDraft = options.useDraft; // params is optional
|
|
22
|
+
|
|
23
|
+
if (!params) return {}; // legacy useDraft behaviour
|
|
24
|
+
|
|
25
|
+
var doc = useDraft ? document.displayed : document.published;
|
|
26
|
+
return Object.keys(params).reduce((acc, key) => _objectSpread(_objectSpread({}, acc), {}, {
|
|
27
|
+
[key]: (0, _dlv.default)(doc, params[key])
|
|
28
|
+
}), {});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function resolveParams(options) {
|
|
32
|
+
var params = options.params,
|
|
33
|
+
document = options.document;
|
|
34
|
+
var resolvedParams = typeof params == 'function' ? params({
|
|
35
|
+
document
|
|
36
|
+
}) : defaultResolver(options); // if any of the parameters are undefined, the query will error
|
|
37
|
+
// so return undefined so the UI can show a more appropriate message
|
|
38
|
+
|
|
39
|
+
if (Object.values(resolvedParams).includes(undefined)) return undefined; // Typescript can't tell that we've guarded against any value being undefined,
|
|
40
|
+
// so forcing the type
|
|
41
|
+
|
|
42
|
+
return resolvedParams;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=resolveParams.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveParams.js","names":["defaultResolver","options","params","document","useDraft","doc","displayed","published","Object","keys","reduce","acc","key","delve","resolveParams","resolvedParams","values","includes","undefined"],"sources":["../src/resolveParams.ts"],"sourcesContent":["import {DocumentsPaneQueryParams, DocumentVersionsCollection} from './types'\nimport delve from 'dlv'\n\ninterface ResolveParamsOptions {\n params?: DocumentsPaneQueryParams\n document: DocumentVersionsCollection\n useDraft: boolean\n}\n\ntype ResolveParamsReturn = undefined | {[key: string]: string}\n\nfunction defaultResolver(options: ResolveParamsOptions): {[key: string]: string | undefined} {\n const {params, document, useDraft} = options\n\n // params is optional\n if (!params) return {}\n\n // legacy useDraft behaviour\n const doc = useDraft ? document.displayed : document.published\n\n return Object.keys(params).reduce((acc, key) => ({...acc, [key]: delve(doc, params[key])}), {})\n}\n\nexport default function resolveParams(options: ResolveParamsOptions): ResolveParamsReturn {\n const {params, document} = options\n\n const resolvedParams = typeof params == 'function' ? params({document}) : defaultResolver(options)\n\n // if any of the parameters are undefined, the query will error\n // so return undefined so the UI can show a more appropriate message\n if (Object.values(resolvedParams).includes(undefined)) return undefined\n\n // Typescript can't tell that we've guarded against any value being undefined,\n // so forcing the type\n return resolvedParams as {[key: string]: string}\n}\n"],"mappings":";;;;;;;AACA;;;;;;;;;;AAUA,SAASA,eAAT,CAAyBC,OAAzB,EAA6F;EAC3F,IAAOC,MAAP,GAAqCD,OAArC,CAAOC,MAAP;EAAA,IAAeC,QAAf,GAAqCF,OAArC,CAAeE,QAAf;EAAA,IAAyBC,QAAzB,GAAqCH,OAArC,CAAyBG,QAAzB,CAD2F,CAG3F;;EACA,IAAI,CAACF,MAAL,EAAa,OAAO,EAAP,CAJ8E,CAM3F;;EACA,IAAMG,GAAG,GAAGD,QAAQ,GAAGD,QAAQ,CAACG,SAAZ,GAAwBH,QAAQ,CAACI,SAArD;EAEA,OAAOC,MAAM,CAACC,IAAP,CAAYP,MAAZ,EAAoBQ,MAApB,CAA2B,CAACC,GAAD,EAAMC,GAAN,qCAAmBD,GAAnB;IAAwB,CAACC,GAAD,GAAO,IAAAC,YAAA,EAAMR,GAAN,EAAWH,MAAM,CAACU,GAAD,CAAjB;EAA/B,EAA3B,EAAqF,EAArF,CAAP;AACD;;AAEc,SAASE,aAAT,CAAuBb,OAAvB,EAA2E;EACxF,IAAOC,MAAP,GAA2BD,OAA3B,CAAOC,MAAP;EAAA,IAAeC,QAAf,GAA2BF,OAA3B,CAAeE,QAAf;EAEA,IAAMY,cAAc,GAAG,OAAOb,MAAP,IAAiB,UAAjB,GAA8BA,MAAM,CAAC;IAACC;EAAD,CAAD,CAApC,GAAmDH,eAAe,CAACC,OAAD,CAAzF,CAHwF,CAKxF;EACA;;EACA,IAAIO,MAAM,CAACQ,MAAP,CAAcD,cAAd,EAA8BE,QAA9B,CAAuCC,SAAvC,CAAJ,EAAuD,OAAOA,SAAP,CAPiC,CASxF;EACA;;EACA,OAAOH,cAAP;AACD"}
|
package/lib/types.js
ADDED
package/lib/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","names":[],"sources":["../src/types.ts"],"sourcesContent":["import {SanityDocument} from '@sanity/client'\n\nexport interface DocumentVersionsCollection {\n displayed: SanityDocument\n published: SanityDocument\n draft: SanityDocument\n historical: SanityDocument\n}\n\n// eslint-disable-next-line prettier/prettier\nexport type DocumentsPaneQueryParams = (params: {document: DocumentVersionsCollection}) => ({[key: string]: string}) | {[key: string]: string}\n\nexport interface DocumentsPaneInitialValueTemplate {\n schemaType: string\n template?: string\n parameters?: {[key: string]: any}\n title: string\n}\n\n// eslint-disable-next-line prettier/prettier\nexport type DocumentsPaneInitialValueTemplateResolver = (params: {document: DocumentVersionsCollection}) => DocumentsPaneInitialValueTemplate[]\n\nexport type DocumentsPaneOptions = {\n query: string\n params?: DocumentsPaneQueryParams\n debug?: boolean\n useDraft?: boolean\n initialValueTemplates?: DocumentsPaneInitialValueTemplateResolver\n}\n\nexport type DocumentsPaneProps = {\n document: DocumentVersionsCollection\n options: DocumentsPaneOptions\n}\n"],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sanity-plugin-documents-pane",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Displays the results of a GROQ query in a View Pane",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
"author": "Sanity.io <hello@sanity.io>",
|
|
30
30
|
"license": "MIT",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@sanity/
|
|
32
|
+
"@sanity/icons": "^1.3.4",
|
|
33
|
+
"@sanity/ui": "^0.38.0",
|
|
33
34
|
"dlv": "^1.1.3",
|
|
34
35
|
"react-fast-compare": "^3.2.0",
|
|
35
36
|
"rxjs": "^6.5.6"
|
|
@@ -37,6 +38,7 @@
|
|
|
37
38
|
"peerDependencies": {
|
|
38
39
|
"@sanity/base": "^2.30.1",
|
|
39
40
|
"@sanity/desk-tool": "^2.30.1",
|
|
41
|
+
"@sanity/uuid": "^3.0.1",
|
|
40
42
|
"@sanity/util": "^2.29.5",
|
|
41
43
|
"react": "^16.0.0 || ^17.0.0"
|
|
42
44
|
},
|
package/src/Debug.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from 'react'
|
|
2
2
|
import {Code, Box, Label, Stack} from '@sanity/ui'
|
|
3
3
|
|
|
4
|
-
export default function Debug({query, params}: {query: string; params
|
|
4
|
+
export default function Debug({query, params}: {query: string; params?: {[key: string]: string}}) {
|
|
5
5
|
return (
|
|
6
6
|
<>
|
|
7
7
|
<Stack space={4}>
|
|
@@ -12,14 +12,16 @@ export default function Debug({query, params}: {query: string; params: {[key: st
|
|
|
12
12
|
<Code>{query}</Code>
|
|
13
13
|
</Box>
|
|
14
14
|
</Stack>
|
|
15
|
-
|
|
16
|
-
<
|
|
17
|
-
<
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
<
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
{params && (
|
|
16
|
+
<Stack space={4}>
|
|
17
|
+
<Box>
|
|
18
|
+
<Label>Params</Label>
|
|
19
|
+
</Box>
|
|
20
|
+
<Box>
|
|
21
|
+
<Code>{JSON.stringify(params)}</Code>
|
|
22
|
+
</Box>
|
|
23
|
+
</Stack>
|
|
24
|
+
)}
|
|
23
25
|
</>
|
|
24
26
|
)
|
|
25
27
|
}
|
package/src/Documents.tsx
CHANGED
|
@@ -8,15 +8,18 @@ import schema from 'part:@sanity/base/schema'
|
|
|
8
8
|
import Debug from './Debug'
|
|
9
9
|
import Feedback from './Feedback'
|
|
10
10
|
import useListeningQuery from './hooks/useListeningQuery'
|
|
11
|
+
import {DocumentsPaneInitialValueTemplate} from './types'
|
|
12
|
+
import NewDocument from './NewDocument'
|
|
11
13
|
|
|
12
14
|
type DocumentsProps = {
|
|
13
15
|
query: string
|
|
14
16
|
params: {[key: string]: string}
|
|
15
17
|
debug: boolean
|
|
18
|
+
initialValueTemplates: DocumentsPaneInitialValueTemplate[]
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
export default function Documents(props: DocumentsProps) {
|
|
19
|
-
const {query, params, debug} = props
|
|
22
|
+
const {query, params, debug, initialValueTemplates} = props
|
|
20
23
|
const {routerPanesState, groupIndex, handleEditReference} = usePaneRouter()
|
|
21
24
|
|
|
22
25
|
const {loading, error, data} = useListeningQuery(query, params)
|
|
@@ -59,25 +62,31 @@ export default function Documents(props: DocumentsProps) {
|
|
|
59
62
|
|
|
60
63
|
if (!data?.length) {
|
|
61
64
|
return (
|
|
62
|
-
|
|
63
|
-
<
|
|
64
|
-
|
|
65
|
-
|
|
65
|
+
<>
|
|
66
|
+
<NewDocument initialValueTemplates={initialValueTemplates} />
|
|
67
|
+
<Stack padding={4} space={5}>
|
|
68
|
+
<Feedback>No Documents found</Feedback>
|
|
69
|
+
{debug && <Debug query={query} params={params} />}
|
|
70
|
+
</Stack>
|
|
71
|
+
</>
|
|
66
72
|
)
|
|
67
73
|
}
|
|
68
74
|
|
|
69
75
|
return (
|
|
70
|
-
|
|
71
|
-
{
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
76
|
+
<>
|
|
77
|
+
<NewDocument initialValueTemplates={initialValueTemplates} />
|
|
78
|
+
<Stack padding={2} space={1}>
|
|
79
|
+
{data.map((doc) => (
|
|
80
|
+
<Button
|
|
81
|
+
key={doc._id}
|
|
82
|
+
onClick={() => handleClick(doc._id, doc._type)}
|
|
83
|
+
padding={2}
|
|
84
|
+
mode="bleed"
|
|
85
|
+
>
|
|
86
|
+
<Preview value={doc} type={schema.get(doc._type)} />
|
|
87
|
+
</Button>
|
|
88
|
+
))}
|
|
89
|
+
</Stack>
|
|
90
|
+
</>
|
|
82
91
|
)
|
|
83
92
|
}
|
package/src/DocumentsPane.tsx
CHANGED
|
@@ -1,44 +1,60 @@
|
|
|
1
1
|
import React from 'react'
|
|
2
|
-
import delve from 'dlv'
|
|
3
2
|
import {Stack} from '@sanity/ui'
|
|
4
|
-
import {SanityDocument} from '@sanity/client'
|
|
5
3
|
|
|
6
4
|
import Documents from './Documents'
|
|
7
5
|
import Feedback from './Feedback'
|
|
8
6
|
import Debug from './Debug'
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
params: {[key: string]: string}
|
|
13
|
-
debug: boolean
|
|
14
|
-
useDraft: boolean
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
type DocumentsPaneProps = {
|
|
18
|
-
document: SanityDocument
|
|
19
|
-
options: DocumentsPaneOptions
|
|
20
|
-
}
|
|
7
|
+
import {DocumentsPaneProps} from './types'
|
|
8
|
+
import resolveParams from './resolveParams'
|
|
9
|
+
import resolveInitialValueTemplates from './resolveInitialValueTemplates'
|
|
21
10
|
|
|
22
11
|
export default function DocumentsPane(props: DocumentsPaneProps) {
|
|
23
|
-
const {document
|
|
24
|
-
const {
|
|
12
|
+
const {document, options} = props
|
|
13
|
+
const {
|
|
14
|
+
query,
|
|
15
|
+
params,
|
|
16
|
+
useDraft = false,
|
|
17
|
+
debug = false,
|
|
18
|
+
initialValueTemplates: initialValueTemplatesResolver,
|
|
19
|
+
} = options
|
|
20
|
+
|
|
21
|
+
if (useDraft && typeof params === 'function') {
|
|
22
|
+
return (
|
|
23
|
+
<Stack padding={4} space={5}>
|
|
24
|
+
<Feedback>
|
|
25
|
+
<code>useDraft</code> should not be <code>true</code> when supplying a function for
|
|
26
|
+
<code>params</code>
|
|
27
|
+
</Feedback>
|
|
28
|
+
{debug && <Debug query={query} />}
|
|
29
|
+
</Stack>
|
|
30
|
+
)
|
|
31
|
+
}
|
|
25
32
|
|
|
26
|
-
const
|
|
27
|
-
const {_rev} = doc ?? {}
|
|
33
|
+
const paramValues = resolveParams({document, params, useDraft})
|
|
28
34
|
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
)
|
|
35
|
+
const initialValueTemplates = resolveInitialValueTemplates({
|
|
36
|
+
resolver: initialValueTemplatesResolver,
|
|
37
|
+
document,
|
|
38
|
+
})
|
|
33
39
|
|
|
34
|
-
if (!
|
|
40
|
+
if (!paramValues) {
|
|
35
41
|
return (
|
|
36
42
|
<Stack padding={4} space={5}>
|
|
37
|
-
<Feedback>
|
|
38
|
-
|
|
43
|
+
<Feedback>
|
|
44
|
+
Parameters for this query could not be resolved. This may mean the document does not yet
|
|
45
|
+
exist or is incomplete.
|
|
46
|
+
</Feedback>
|
|
47
|
+
{debug && <Debug query={query} />}
|
|
39
48
|
</Stack>
|
|
40
49
|
)
|
|
41
50
|
}
|
|
42
51
|
|
|
43
|
-
return
|
|
52
|
+
return (
|
|
53
|
+
<Documents
|
|
54
|
+
query={query}
|
|
55
|
+
params={paramValues}
|
|
56
|
+
debug={debug}
|
|
57
|
+
initialValueTemplates={initialValueTemplates}
|
|
58
|
+
/>
|
|
59
|
+
)
|
|
44
60
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {Button, Card, Flex} from '@sanity/ui'
|
|
2
|
+
import React from 'react'
|
|
3
|
+
import {DocumentsPaneInitialValueTemplate} from './types'
|
|
4
|
+
import {ComposeIcon} from '@sanity/icons'
|
|
5
|
+
import {usePaneRouter} from '@sanity/desk-tool'
|
|
6
|
+
import {uuid} from "@sanity/uuid"
|
|
7
|
+
|
|
8
|
+
interface NewDocumentProps {
|
|
9
|
+
initialValueTemplates: DocumentsPaneInitialValueTemplate[]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default function NewDocument(props: NewDocumentProps) {
|
|
13
|
+
const {initialValueTemplates = []} = props
|
|
14
|
+
const {ReferenceChildLink} = usePaneRouter()
|
|
15
|
+
|
|
16
|
+
if (!initialValueTemplates.length) return null
|
|
17
|
+
|
|
18
|
+
return (
|
|
19
|
+
<Card borderBottom={true} padding={2}>
|
|
20
|
+
<Flex justify="flex-end" gap={1}>
|
|
21
|
+
{initialValueTemplates.map((template) => {
|
|
22
|
+
return (
|
|
23
|
+
<ReferenceChildLink
|
|
24
|
+
documentId={uuid()}
|
|
25
|
+
documentType={template.schemaType}
|
|
26
|
+
template={{id: template.template, params: template.parameters}}
|
|
27
|
+
parentRefPath={[]}
|
|
28
|
+
key={`${template.schemaType}-${template.template}`}
|
|
29
|
+
style={{textDecoration: 'none'}}
|
|
30
|
+
>
|
|
31
|
+
<Button icon={<ComposeIcon />} text={template.title} mode="bleed" as="span" />
|
|
32
|
+
</ReferenceChildLink>
|
|
33
|
+
)
|
|
34
|
+
})}
|
|
35
|
+
</Flex>
|
|
36
|
+
</Card>
|
|
37
|
+
)
|
|
38
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DocumentsPaneInitialValueTemplate,
|
|
3
|
+
DocumentsPaneInitialValueTemplateResolver,
|
|
4
|
+
DocumentVersionsCollection,
|
|
5
|
+
} from './types'
|
|
6
|
+
|
|
7
|
+
interface ResolveInitialValueTemplatesOptions {
|
|
8
|
+
resolver: DocumentsPaneInitialValueTemplateResolver | undefined
|
|
9
|
+
document: DocumentVersionsCollection
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default function resolveInitialValueTemplates(
|
|
13
|
+
options: ResolveInitialValueTemplatesOptions
|
|
14
|
+
): DocumentsPaneInitialValueTemplate[] {
|
|
15
|
+
const {resolver, document} = options || {}
|
|
16
|
+
|
|
17
|
+
if (!resolver) return []
|
|
18
|
+
|
|
19
|
+
return resolver({document})
|
|
20
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {DocumentsPaneQueryParams, DocumentVersionsCollection} from './types'
|
|
2
|
+
import delve from 'dlv'
|
|
3
|
+
|
|
4
|
+
interface ResolveParamsOptions {
|
|
5
|
+
params?: DocumentsPaneQueryParams
|
|
6
|
+
document: DocumentVersionsCollection
|
|
7
|
+
useDraft: boolean
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type ResolveParamsReturn = undefined | {[key: string]: string}
|
|
11
|
+
|
|
12
|
+
function defaultResolver(options: ResolveParamsOptions): {[key: string]: string | undefined} {
|
|
13
|
+
const {params, document, useDraft} = options
|
|
14
|
+
|
|
15
|
+
// params is optional
|
|
16
|
+
if (!params) return {}
|
|
17
|
+
|
|
18
|
+
// legacy useDraft behaviour
|
|
19
|
+
const doc = useDraft ? document.displayed : document.published
|
|
20
|
+
|
|
21
|
+
return Object.keys(params).reduce((acc, key) => ({...acc, [key]: delve(doc, params[key])}), {})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default function resolveParams(options: ResolveParamsOptions): ResolveParamsReturn {
|
|
25
|
+
const {params, document} = options
|
|
26
|
+
|
|
27
|
+
const resolvedParams = typeof params == 'function' ? params({document}) : defaultResolver(options)
|
|
28
|
+
|
|
29
|
+
// if any of the parameters are undefined, the query will error
|
|
30
|
+
// so return undefined so the UI can show a more appropriate message
|
|
31
|
+
if (Object.values(resolvedParams).includes(undefined)) return undefined
|
|
32
|
+
|
|
33
|
+
// Typescript can't tell that we've guarded against any value being undefined,
|
|
34
|
+
// so forcing the type
|
|
35
|
+
return resolvedParams as {[key: string]: string}
|
|
36
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {SanityDocument} from '@sanity/client'
|
|
2
|
+
|
|
3
|
+
export interface DocumentVersionsCollection {
|
|
4
|
+
displayed: SanityDocument
|
|
5
|
+
published: SanityDocument
|
|
6
|
+
draft: SanityDocument
|
|
7
|
+
historical: SanityDocument
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// eslint-disable-next-line prettier/prettier
|
|
11
|
+
export type DocumentsPaneQueryParams = (params: {document: DocumentVersionsCollection}) => ({[key: string]: string}) | {[key: string]: string}
|
|
12
|
+
|
|
13
|
+
export interface DocumentsPaneInitialValueTemplate {
|
|
14
|
+
schemaType: string
|
|
15
|
+
template?: string
|
|
16
|
+
parameters?: {[key: string]: any}
|
|
17
|
+
title: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// eslint-disable-next-line prettier/prettier
|
|
21
|
+
export type DocumentsPaneInitialValueTemplateResolver = (params: {document: DocumentVersionsCollection}) => DocumentsPaneInitialValueTemplate[]
|
|
22
|
+
|
|
23
|
+
export type DocumentsPaneOptions = {
|
|
24
|
+
query: string
|
|
25
|
+
params?: DocumentsPaneQueryParams
|
|
26
|
+
debug?: boolean
|
|
27
|
+
useDraft?: boolean
|
|
28
|
+
initialValueTemplates?: DocumentsPaneInitialValueTemplateResolver
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type DocumentsPaneProps = {
|
|
32
|
+
document: DocumentVersionsCollection
|
|
33
|
+
options: DocumentsPaneOptions
|
|
34
|
+
}
|