@payloadcms/plugin-nested-docs 3.0.0-alpha.49 → 3.0.0-alpha.50
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/package.json +8 -4
- package/src/index.ts +71 -0
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/plugin-nested-docs",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.50",
|
|
4
4
|
"description": "The official Nested Docs plugin for Payload",
|
|
5
|
-
"repository":
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/payloadcms/payload.git",
|
|
8
|
+
"directory": "packages/plugin-nested-docs"
|
|
9
|
+
},
|
|
6
10
|
"license": "MIT",
|
|
7
11
|
"homepage": "https://payloadcms.com",
|
|
8
12
|
"author": "Payload CMS, Inc.",
|
|
@@ -11,10 +15,10 @@
|
|
|
11
15
|
"type": "module",
|
|
12
16
|
"devDependencies": {
|
|
13
17
|
"@payloadcms/eslint-config": "1.1.1",
|
|
14
|
-
"payload": "3.0.0-alpha.
|
|
18
|
+
"payload": "3.0.0-alpha.50"
|
|
15
19
|
},
|
|
16
20
|
"peerDependencies": {
|
|
17
|
-
"payload": "3.0.0-alpha.
|
|
21
|
+
"payload": "3.0.0-alpha.50"
|
|
18
22
|
},
|
|
19
23
|
"exports": {
|
|
20
24
|
".": {
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Plugin } from 'payload/config'
|
|
2
|
+
import type { SingleRelationshipField } from 'payload/types'
|
|
3
|
+
|
|
4
|
+
import type { PluginConfig } from './types.js'
|
|
5
|
+
|
|
6
|
+
import createBreadcrumbsField from './fields/breadcrumbs.js'
|
|
7
|
+
import createParentField from './fields/parent.js'
|
|
8
|
+
import parentFilterOptions from './fields/parentFilterOptions.js'
|
|
9
|
+
import resaveChildren from './hooks/resaveChildren.js'
|
|
10
|
+
import resaveSelfAfterCreate from './hooks/resaveSelfAfterCreate.js'
|
|
11
|
+
import populateBreadcrumbs from './utilities/populateBreadcrumbs.js'
|
|
12
|
+
|
|
13
|
+
const nestedDocs =
|
|
14
|
+
(pluginConfig: PluginConfig): Plugin =>
|
|
15
|
+
(config) => ({
|
|
16
|
+
...config,
|
|
17
|
+
collections: (config.collections || []).map((collection) => {
|
|
18
|
+
if (pluginConfig.collections.indexOf(collection.slug) > -1) {
|
|
19
|
+
const fields = [...(collection?.fields || [])]
|
|
20
|
+
|
|
21
|
+
const existingBreadcrumbField = collection.fields.find(
|
|
22
|
+
(field) =>
|
|
23
|
+
'name' in field && field.name === (pluginConfig?.breadcrumbsFieldSlug || 'breadcrumbs'),
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
const existingParentField = collection.fields.find(
|
|
27
|
+
(field) => 'name' in field && field.name === (pluginConfig?.parentFieldSlug || 'parent'),
|
|
28
|
+
) as SingleRelationshipField
|
|
29
|
+
|
|
30
|
+
const defaultFilterOptions = parentFilterOptions(pluginConfig?.breadcrumbsFieldSlug)
|
|
31
|
+
|
|
32
|
+
if (existingParentField) {
|
|
33
|
+
if (!existingParentField.filterOptions) {
|
|
34
|
+
existingParentField.filterOptions = defaultFilterOptions
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!existingParentField && !pluginConfig.parentFieldSlug) {
|
|
39
|
+
const defaultParentField = createParentField(collection.slug)
|
|
40
|
+
defaultParentField.filterOptions = defaultFilterOptions
|
|
41
|
+
fields.push(defaultParentField)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!existingBreadcrumbField && !pluginConfig.breadcrumbsFieldSlug) {
|
|
45
|
+
fields.push(createBreadcrumbsField(collection.slug))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
...collection,
|
|
50
|
+
fields,
|
|
51
|
+
hooks: {
|
|
52
|
+
...(collection.hooks || {}),
|
|
53
|
+
afterChange: [
|
|
54
|
+
resaveChildren(pluginConfig, collection),
|
|
55
|
+
resaveSelfAfterCreate(pluginConfig, collection),
|
|
56
|
+
...(collection?.hooks?.afterChange || []),
|
|
57
|
+
],
|
|
58
|
+
beforeChange: [
|
|
59
|
+
async ({ data, originalDoc, req }) =>
|
|
60
|
+
populateBreadcrumbs(req, pluginConfig, collection, data, originalDoc),
|
|
61
|
+
...(collection?.hooks?.beforeChange || []),
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return collection
|
|
68
|
+
}),
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
export default nestedDocs
|