@foundrykit/menu-plugin 0.6.1 → 0.6.3
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/dist/activePlugins.d.ts +10 -0
- package/dist/activePlugins.js +27 -0
- package/dist/activePlugins.js.map +1 -0
- package/dist/collections/Menus.js +6 -0
- package/dist/collections/Menus.js.map +1 -1
- package/dist/hooks/stripDynamicMenuChildren.d.ts +28 -0
- package/dist/hooks/stripDynamicMenuChildren.js +50 -0
- package/dist/hooks/stripDynamicMenuChildren.js.map +1 -0
- package/dist/hooks/stripDynamicMenuChildren.test.d.ts +1 -0
- package/dist/hooks/stripDynamicMenuChildren.test.js +128 -0
- package/dist/hooks/stripDynamicMenuChildren.test.js.map +1 -0
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Config } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* Records this plugin as active in `config.custom.foundrykit.activePlugins`, the list
|
|
4
|
+
* `@foundrykit/guides-plugin` sends to the guides hub so the site gets this plugin's guides.
|
|
5
|
+
* Only call it on the enabled path: installed-but-disabled doesn't count.
|
|
6
|
+
*
|
|
7
|
+
* Every FoundryKit Payload plugin carries its own copy of this file (it's too small for a package);
|
|
8
|
+
* keep them identical.
|
|
9
|
+
*/
|
|
10
|
+
export declare const recordActivePlugin: (config: Config, name: string) => Config;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Records this plugin as active in `config.custom.foundrykit.activePlugins`, the list
|
|
3
|
+
* `@foundrykit/guides-plugin` sends to the guides hub so the site gets this plugin's guides.
|
|
4
|
+
* Only call it on the enabled path: installed-but-disabled doesn't count.
|
|
5
|
+
*
|
|
6
|
+
* Every FoundryKit Payload plugin carries its own copy of this file (it's too small for a package);
|
|
7
|
+
* keep them identical.
|
|
8
|
+
*/ export const recordActivePlugin = (config, name)=>{
|
|
9
|
+
const foundrykit = config.custom?.foundrykit ?? {};
|
|
10
|
+
return {
|
|
11
|
+
...config,
|
|
12
|
+
custom: {
|
|
13
|
+
...config.custom,
|
|
14
|
+
foundrykit: {
|
|
15
|
+
...foundrykit,
|
|
16
|
+
activePlugins: [
|
|
17
|
+
...new Set([
|
|
18
|
+
...foundrykit.activePlugins ?? [],
|
|
19
|
+
name
|
|
20
|
+
])
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=activePlugins.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/activePlugins.ts"],"sourcesContent":["import type { Config } from 'payload';\n\n/**\n * Records this plugin as active in `config.custom.foundrykit.activePlugins`, the list\n * `@foundrykit/guides-plugin` sends to the guides hub so the site gets this plugin's guides.\n * Only call it on the enabled path: installed-but-disabled doesn't count.\n *\n * Every FoundryKit Payload plugin carries its own copy of this file (it's too small for a package);\n * keep them identical.\n */\nexport const recordActivePlugin = (config: Config, name: string): Config => {\n const foundrykit = (config.custom?.foundrykit ?? {}) as {\n activePlugins?: string[];\n };\n return {\n ...config,\n custom: {\n ...config.custom,\n foundrykit: {\n ...foundrykit,\n activePlugins: [\n ...new Set([...(foundrykit.activePlugins ?? []), name]),\n ],\n },\n },\n };\n};\n"],"names":["recordActivePlugin","config","name","foundrykit","custom","activePlugins","Set"],"mappings":"AAEA;;;;;;;CAOC,GACD,OAAO,MAAMA,qBAAqB,CAACC,QAAgBC;IACjD,MAAMC,aAAcF,OAAOG,MAAM,EAAED,cAAc,CAAC;IAGlD,OAAO;QACL,GAAGF,MAAM;QACTG,QAAQ;YACN,GAAGH,OAAOG,MAAM;YAChBD,YAAY;gBACV,GAAGA,UAAU;gBACbE,eAAe;uBACV,IAAIC,IAAI;2BAAKH,WAAWE,aAAa,IAAI,EAAE;wBAAGH;qBAAK;iBACvD;YACH;QACF;IACF;AACF,EAAE"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { menuCache } from '../cache.js';
|
|
2
2
|
import { createMenuItemFields } from '../fields/MenuItem.js';
|
|
3
3
|
import { createResolveDynamicMenuHook } from '../hooks/resolveDynamicMenu.js';
|
|
4
|
+
import { stripDynamicMenuChildrenHook } from '../hooks/stripDynamicMenuChildren.js';
|
|
4
5
|
import { buildMenuIdentityKey, normalizeMenuLocale } from '../utilities/menuIdentity.js';
|
|
5
6
|
export const createMenusCollection = (options)=>({
|
|
6
7
|
slug: 'menus',
|
|
@@ -99,6 +100,11 @@ export const createMenusCollection = (options)=>({
|
|
|
99
100
|
}
|
|
100
101
|
],
|
|
101
102
|
...options?.disabled ? {} : {
|
|
103
|
+
// Pairs with the afterRead resolver: drop the resolved dynamic-entry
|
|
104
|
+
// data (whose children carry foreign ids) before it is persisted.
|
|
105
|
+
beforeChange: [
|
|
106
|
+
stripDynamicMenuChildrenHook
|
|
107
|
+
],
|
|
102
108
|
afterChange: [
|
|
103
109
|
({ doc, previousDoc })=>{
|
|
104
110
|
if (previousDoc?.slug) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/collections/Menus.ts"],"sourcesContent":["import type { CollectionConfig } from 'payload';\n\nimport { menuCache } from '../cache.js';\nimport {\n createMenuItemFields,\n type MenuItemFieldOptions,\n} from '../fields/MenuItem.js';\nimport { createResolveDynamicMenuHook } from '../hooks/resolveDynamicMenu.js';\nimport {\n buildMenuIdentityKey,\n normalizeMenuLocale,\n} from '../utilities/menuIdentity.js';\n\nexport type MenusCollectionOptions = {\n childrenDepth?: number;\n disabled?: boolean;\n} & MenuItemFieldOptions;\n\nexport const createMenusCollection = (\n options?: MenusCollectionOptions\n): CollectionConfig => ({\n slug: 'menus',\n access: {\n read: () => true,\n },\n admin: {\n defaultColumns: ['title', 'slug', 'locale', 'items'],\n useAsTitle: 'title',\n },\n fields: [\n {\n name: 'title',\n type: 'text',\n admin: {\n description:\n 'The name of the menu. E.g. \"Main Menu\", \"Footer Menu\", etc.',\n },\n required: true,\n },\n {\n name: 'slug',\n type: 'text',\n admin: {\n description:\n 'Unique identifier for the menu, used in API requests. E.g. \"main-menu\", \"footer-menu\", etc.',\n },\n index: true,\n required: true,\n },\n {\n name: 'description',\n type: 'textarea',\n admin: {\n description: 'Optional description for the menu.',\n position: 'sidebar',\n },\n },\n {\n name: 'locale',\n type: 'text',\n admin: {\n description: 'Used for localisation. E.g. \"en\", \"fr\", etc.',\n position: 'sidebar',\n },\n },\n ...(options?.disabled\n ? []\n : [\n {\n name: 'menuPreview',\n type: 'ui',\n admin: {\n components: {\n Field: '@foundrykit/menu-plugin/client#MenuPreview',\n },\n position: 'sidebar',\n },\n } as const,\n ]),\n {\n name: 'slugLocaleKey',\n type: 'text',\n admin: {\n hidden: true,\n },\n index: true,\n unique: true,\n },\n {\n name: 'items',\n type: 'array',\n admin: {\n ...(options?.disabled\n ? {}\n : {\n components: {\n RowLabel: '@foundrykit/menu-plugin/client#MenuItemRowLabel',\n },\n }),\n },\n fields: createMenuItemFields(options),\n },\n ],\n hooks: {\n beforeValidate: [\n ({ data }) => {\n if (!data) {\n return data;\n }\n\n const normalizedLocale = normalizeMenuLocale(data.locale);\n\n return {\n ...data,\n locale: normalizedLocale,\n slugLocaleKey: data.slug\n ? buildMenuIdentityKey(data.slug, normalizedLocale)\n : data.slugLocaleKey,\n };\n },\n ],\n ...(options?.disabled\n ? {}\n : {\n afterChange: [\n ({ doc, previousDoc }) => {\n if (previousDoc?.slug) {\n menuCache.invalidate(previousDoc.slug);\n }\n if (doc.slug) {\n menuCache.invalidate(doc.slug);\n }\n return doc;\n },\n ],\n afterDelete: [\n ({ doc }) => {\n if (doc.slug) {\n menuCache.invalidate(doc.slug);\n }\n return doc;\n },\n ],\n afterRead: [\n createResolveDynamicMenuHook({\n childrenDepth: options?.childrenDepth,\n dynamicSources: options?.dynamicSources,\n }),\n ],\n }),\n },\n labels: {\n plural: 'Menus',\n singular: 'Menu',\n },\n timestamps: true,\n});\n"],"names":["menuCache","createMenuItemFields","createResolveDynamicMenuHook","buildMenuIdentityKey","normalizeMenuLocale","createMenusCollection","options","slug","access","read","admin","defaultColumns","useAsTitle","fields","name","type","description","required","index","position","disabled","components","Field","hidden","unique","RowLabel","hooks","beforeValidate","data","normalizedLocale","locale","slugLocaleKey","afterChange","doc","previousDoc","invalidate","afterDelete","afterRead","childrenDepth","dynamicSources","labels","plural","singular","timestamps"],"mappings":"AAEA,SAASA,SAAS,QAAQ,cAAc;AACxC,SACEC,oBAAoB,QAEf,wBAAwB;AAC/B,SAASC,4BAA4B,QAAQ,iCAAiC;AAC9E,SACEC,oBAAoB,EACpBC,mBAAmB,QACd,+BAA+B;AAOtC,OAAO,MAAMC,wBAAwB,CACnCC,UACsB,CAAA;QACtBC,MAAM;QACNC,QAAQ;YACNC,MAAM,IAAM;QACd;QACAC,OAAO;YACLC,gBAAgB;gBAAC;gBAAS;gBAAQ;gBAAU;aAAQ;YACpDC,YAAY;QACd;QACAC,QAAQ;YACN;gBACEC,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACLM,aACE;gBACJ;gBACAC,UAAU;YACZ;YACA;gBACEH,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACLM,aACE;gBACJ;gBACAE,OAAO;gBACPD,UAAU;YACZ;YACA;gBACEH,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACLM,aAAa;oBACbG,UAAU;gBACZ;YACF;YACA;gBACEL,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACLM,aAAa;oBACbG,UAAU;gBACZ;YACF;eACIb,SAASc,WACT,EAAE,GACF;gBACE;oBACEN,MAAM;oBACNC,MAAM;oBACNL,OAAO;wBACLW,YAAY;4BACVC,OAAO;wBACT;wBACAH,UAAU;oBACZ;gBACF;aACD;YACL;gBACEL,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACLa,QAAQ;gBACV;gBACAL,OAAO;gBACPM,QAAQ;YACV;YACA;gBACEV,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACL,GAAIJ,SAASc,WACT,CAAC,IACD;wBACEC,YAAY;4BACVI,UAAU;wBACZ;oBACF,CAAC;gBACP;gBACAZ,
|
|
1
|
+
{"version":3,"sources":["../../src/collections/Menus.ts"],"sourcesContent":["import type { CollectionConfig } from 'payload';\n\nimport { menuCache } from '../cache.js';\nimport {\n createMenuItemFields,\n type MenuItemFieldOptions,\n} from '../fields/MenuItem.js';\nimport { createResolveDynamicMenuHook } from '../hooks/resolveDynamicMenu.js';\nimport { stripDynamicMenuChildrenHook } from '../hooks/stripDynamicMenuChildren.js';\nimport {\n buildMenuIdentityKey,\n normalizeMenuLocale,\n} from '../utilities/menuIdentity.js';\n\nexport type MenusCollectionOptions = {\n childrenDepth?: number;\n disabled?: boolean;\n} & MenuItemFieldOptions;\n\nexport const createMenusCollection = (\n options?: MenusCollectionOptions\n): CollectionConfig => ({\n slug: 'menus',\n access: {\n read: () => true,\n },\n admin: {\n defaultColumns: ['title', 'slug', 'locale', 'items'],\n useAsTitle: 'title',\n },\n fields: [\n {\n name: 'title',\n type: 'text',\n admin: {\n description:\n 'The name of the menu. E.g. \"Main Menu\", \"Footer Menu\", etc.',\n },\n required: true,\n },\n {\n name: 'slug',\n type: 'text',\n admin: {\n description:\n 'Unique identifier for the menu, used in API requests. E.g. \"main-menu\", \"footer-menu\", etc.',\n },\n index: true,\n required: true,\n },\n {\n name: 'description',\n type: 'textarea',\n admin: {\n description: 'Optional description for the menu.',\n position: 'sidebar',\n },\n },\n {\n name: 'locale',\n type: 'text',\n admin: {\n description: 'Used for localisation. E.g. \"en\", \"fr\", etc.',\n position: 'sidebar',\n },\n },\n ...(options?.disabled\n ? []\n : [\n {\n name: 'menuPreview',\n type: 'ui',\n admin: {\n components: {\n Field: '@foundrykit/menu-plugin/client#MenuPreview',\n },\n position: 'sidebar',\n },\n } as const,\n ]),\n {\n name: 'slugLocaleKey',\n type: 'text',\n admin: {\n hidden: true,\n },\n index: true,\n unique: true,\n },\n {\n name: 'items',\n type: 'array',\n admin: {\n ...(options?.disabled\n ? {}\n : {\n components: {\n RowLabel: '@foundrykit/menu-plugin/client#MenuItemRowLabel',\n },\n }),\n },\n fields: createMenuItemFields(options),\n },\n ],\n hooks: {\n beforeValidate: [\n ({ data }) => {\n if (!data) {\n return data;\n }\n\n const normalizedLocale = normalizeMenuLocale(data.locale);\n\n return {\n ...data,\n locale: normalizedLocale,\n slugLocaleKey: data.slug\n ? buildMenuIdentityKey(data.slug, normalizedLocale)\n : data.slugLocaleKey,\n };\n },\n ],\n ...(options?.disabled\n ? {}\n : {\n // Pairs with the afterRead resolver: drop the resolved dynamic-entry\n // data (whose children carry foreign ids) before it is persisted.\n beforeChange: [stripDynamicMenuChildrenHook],\n afterChange: [\n ({ doc, previousDoc }) => {\n if (previousDoc?.slug) {\n menuCache.invalidate(previousDoc.slug);\n }\n if (doc.slug) {\n menuCache.invalidate(doc.slug);\n }\n return doc;\n },\n ],\n afterDelete: [\n ({ doc }) => {\n if (doc.slug) {\n menuCache.invalidate(doc.slug);\n }\n return doc;\n },\n ],\n afterRead: [\n createResolveDynamicMenuHook({\n childrenDepth: options?.childrenDepth,\n dynamicSources: options?.dynamicSources,\n }),\n ],\n }),\n },\n labels: {\n plural: 'Menus',\n singular: 'Menu',\n },\n timestamps: true,\n});\n"],"names":["menuCache","createMenuItemFields","createResolveDynamicMenuHook","stripDynamicMenuChildrenHook","buildMenuIdentityKey","normalizeMenuLocale","createMenusCollection","options","slug","access","read","admin","defaultColumns","useAsTitle","fields","name","type","description","required","index","position","disabled","components","Field","hidden","unique","RowLabel","hooks","beforeValidate","data","normalizedLocale","locale","slugLocaleKey","beforeChange","afterChange","doc","previousDoc","invalidate","afterDelete","afterRead","childrenDepth","dynamicSources","labels","plural","singular","timestamps"],"mappings":"AAEA,SAASA,SAAS,QAAQ,cAAc;AACxC,SACEC,oBAAoB,QAEf,wBAAwB;AAC/B,SAASC,4BAA4B,QAAQ,iCAAiC;AAC9E,SAASC,4BAA4B,QAAQ,uCAAuC;AACpF,SACEC,oBAAoB,EACpBC,mBAAmB,QACd,+BAA+B;AAOtC,OAAO,MAAMC,wBAAwB,CACnCC,UACsB,CAAA;QACtBC,MAAM;QACNC,QAAQ;YACNC,MAAM,IAAM;QACd;QACAC,OAAO;YACLC,gBAAgB;gBAAC;gBAAS;gBAAQ;gBAAU;aAAQ;YACpDC,YAAY;QACd;QACAC,QAAQ;YACN;gBACEC,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACLM,aACE;gBACJ;gBACAC,UAAU;YACZ;YACA;gBACEH,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACLM,aACE;gBACJ;gBACAE,OAAO;gBACPD,UAAU;YACZ;YACA;gBACEH,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACLM,aAAa;oBACbG,UAAU;gBACZ;YACF;YACA;gBACEL,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACLM,aAAa;oBACbG,UAAU;gBACZ;YACF;eACIb,SAASc,WACT,EAAE,GACF;gBACE;oBACEN,MAAM;oBACNC,MAAM;oBACNL,OAAO;wBACLW,YAAY;4BACVC,OAAO;wBACT;wBACAH,UAAU;oBACZ;gBACF;aACD;YACL;gBACEL,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACLa,QAAQ;gBACV;gBACAL,OAAO;gBACPM,QAAQ;YACV;YACA;gBACEV,MAAM;gBACNC,MAAM;gBACNL,OAAO;oBACL,GAAIJ,SAASc,WACT,CAAC,IACD;wBACEC,YAAY;4BACVI,UAAU;wBACZ;oBACF,CAAC;gBACP;gBACAZ,QAAQb,qBAAqBM;YAC/B;SACD;QACDoB,OAAO;YACLC,gBAAgB;gBACd,CAAC,EAAEC,IAAI,EAAE;oBACP,IAAI,CAACA,MAAM;wBACT,OAAOA;oBACT;oBAEA,MAAMC,mBAAmBzB,oBAAoBwB,KAAKE,MAAM;oBAExD,OAAO;wBACL,GAAGF,IAAI;wBACPE,QAAQD;wBACRE,eAAeH,KAAKrB,IAAI,GACpBJ,qBAAqByB,KAAKrB,IAAI,EAAEsB,oBAChCD,KAAKG,aAAa;oBACxB;gBACF;aACD;YACD,GAAIzB,SAASc,WACT,CAAC,IACD;gBACE,qEAAqE;gBACrE,kEAAkE;gBAClEY,cAAc;oBAAC9B;iBAA6B;gBAC5C+B,aAAa;oBACX,CAAC,EAAEC,GAAG,EAAEC,WAAW,EAAE;wBACnB,IAAIA,aAAa5B,MAAM;4BACrBR,UAAUqC,UAAU,CAACD,YAAY5B,IAAI;wBACvC;wBACA,IAAI2B,IAAI3B,IAAI,EAAE;4BACZR,UAAUqC,UAAU,CAACF,IAAI3B,IAAI;wBAC/B;wBACA,OAAO2B;oBACT;iBACD;gBACDG,aAAa;oBACX,CAAC,EAAEH,GAAG,EAAE;wBACN,IAAIA,IAAI3B,IAAI,EAAE;4BACZR,UAAUqC,UAAU,CAACF,IAAI3B,IAAI;wBAC/B;wBACA,OAAO2B;oBACT;iBACD;gBACDI,WAAW;oBACTrC,6BAA6B;wBAC3BsC,eAAejC,SAASiC;wBACxBC,gBAAgBlC,SAASkC;oBAC3B;iBACD;YACH,CAAC;QACP;QACAC,QAAQ;YACNC,QAAQ;YACRC,UAAU;QACZ;QACAC,YAAY;IACd,CAAA,EAAG"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { CollectionBeforeChangeHook } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* Strip read-time-resolved data from dynamic mega entries before write.
|
|
4
|
+
*
|
|
5
|
+
* `createResolveDynamicMenuHook` (afterRead) fills each dynamic entry's
|
|
6
|
+
* `children` by querying the related collection, setting every child's `id` to
|
|
7
|
+
* the *related* document's id. The mega-children array is admin-hidden for
|
|
8
|
+
* dynamic entries (`admin.condition: source !== 'dynamic'`), but `condition`
|
|
9
|
+
* only hides the UI — the resolved rows remain in the admin form state and are
|
|
10
|
+
* sent back on save. Payload then validates each row's `id` against the array's
|
|
11
|
+
* own id column and rejects the foreign id with "The following field is invalid:
|
|
12
|
+
* id" (HTTP 400).
|
|
13
|
+
*
|
|
14
|
+
* This `beforeChange` hook drops that resolved data so it is never persisted.
|
|
15
|
+
* Dynamic children (and any dynamically-derived featured panel) are re-resolved
|
|
16
|
+
* on every read, so they must not be stored. It runs before field validation
|
|
17
|
+
* (`beforeChange` – Collection precedes `beforeChange` – Fields), so the offending
|
|
18
|
+
* rows are gone before Payload validates them.
|
|
19
|
+
*/
|
|
20
|
+
type AnyDoc = Record<string, unknown>;
|
|
21
|
+
/** Pure helper: mutate and return `data`, stripping resolved dynamic-entry data. */
|
|
22
|
+
export declare const stripDynamicMenuChildren: (data: AnyDoc | null | undefined) => AnyDoc | null | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* `beforeChange` collection hook that removes read-time-resolved data from
|
|
25
|
+
* dynamic mega entries so foreign ids never reach Payload's id validation.
|
|
26
|
+
*/
|
|
27
|
+
export declare const stripDynamicMenuChildrenHook: CollectionBeforeChangeHook;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const stripEntries = (items)=>{
|
|
2
|
+
if (!Array.isArray(items)) {
|
|
3
|
+
return;
|
|
4
|
+
}
|
|
5
|
+
for (const rawItem of items){
|
|
6
|
+
if (!rawItem || typeof rawItem !== 'object') {
|
|
7
|
+
continue;
|
|
8
|
+
}
|
|
9
|
+
const item = rawItem;
|
|
10
|
+
// Recurse into dropdown children, same as the resolver's walker.
|
|
11
|
+
if (item.itemType === 'dropdown') {
|
|
12
|
+
stripEntries(item.children);
|
|
13
|
+
}
|
|
14
|
+
if (item.itemType !== 'mega' || !Array.isArray(item.megaEntries)) {
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
for (const rawEntry of item.megaEntries){
|
|
18
|
+
if (!rawEntry || typeof rawEntry !== 'object') {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const entry = rawEntry;
|
|
22
|
+
if (entry.source !== 'dynamic') {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
// Resolved children carry the related doc's id, not a valid array-row id.
|
|
26
|
+
delete entry.children;
|
|
27
|
+
// A dynamically-derived featured panel is also resolved on read; keep only
|
|
28
|
+
// its mode so the stored value doesn't masquerade as manual data.
|
|
29
|
+
const featured = entry.featured;
|
|
30
|
+
if (featured && featured.mode === 'dynamic') {
|
|
31
|
+
entry.featured = {
|
|
32
|
+
mode: 'dynamic'
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
/** Pure helper: mutate and return `data`, stripping resolved dynamic-entry data. */ export const stripDynamicMenuChildren = (data)=>{
|
|
39
|
+
if (!data || typeof data !== 'object') {
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
stripEntries(data.items);
|
|
43
|
+
return data;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* `beforeChange` collection hook that removes read-time-resolved data from
|
|
47
|
+
* dynamic mega entries so foreign ids never reach Payload's id validation.
|
|
48
|
+
*/ export const stripDynamicMenuChildrenHook = ({ data })=>stripDynamicMenuChildren(data);
|
|
49
|
+
|
|
50
|
+
//# sourceMappingURL=stripDynamicMenuChildren.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/stripDynamicMenuChildren.ts"],"sourcesContent":["import type { CollectionBeforeChangeHook } from 'payload';\n\n/**\n * Strip read-time-resolved data from dynamic mega entries before write.\n *\n * `createResolveDynamicMenuHook` (afterRead) fills each dynamic entry's\n * `children` by querying the related collection, setting every child's `id` to\n * the *related* document's id. The mega-children array is admin-hidden for\n * dynamic entries (`admin.condition: source !== 'dynamic'`), but `condition`\n * only hides the UI — the resolved rows remain in the admin form state and are\n * sent back on save. Payload then validates each row's `id` against the array's\n * own id column and rejects the foreign id with \"The following field is invalid:\n * id\" (HTTP 400).\n *\n * This `beforeChange` hook drops that resolved data so it is never persisted.\n * Dynamic children (and any dynamically-derived featured panel) are re-resolved\n * on every read, so they must not be stored. It runs before field validation\n * (`beforeChange` – Collection precedes `beforeChange` – Fields), so the offending\n * rows are gone before Payload validates them.\n */\n\ntype AnyDoc = Record<string, unknown>;\n\nconst stripEntries = (items: unknown): void => {\n if (!Array.isArray(items)) {\n return;\n }\n\n for (const rawItem of items) {\n if (!rawItem || typeof rawItem !== 'object') {\n continue;\n }\n const item = rawItem as AnyDoc;\n\n // Recurse into dropdown children, same as the resolver's walker.\n if (item.itemType === 'dropdown') {\n stripEntries(item.children);\n }\n\n if (item.itemType !== 'mega' || !Array.isArray(item.megaEntries)) {\n continue;\n }\n\n for (const rawEntry of item.megaEntries) {\n if (!rawEntry || typeof rawEntry !== 'object') {\n continue;\n }\n const entry = rawEntry as AnyDoc;\n if (entry.source !== 'dynamic') {\n continue;\n }\n\n // Resolved children carry the related doc's id, not a valid array-row id.\n delete entry.children;\n\n // A dynamically-derived featured panel is also resolved on read; keep only\n // its mode so the stored value doesn't masquerade as manual data.\n const featured = entry.featured as AnyDoc | undefined;\n if (featured && featured.mode === 'dynamic') {\n entry.featured = { mode: 'dynamic' };\n }\n }\n }\n};\n\n/** Pure helper: mutate and return `data`, stripping resolved dynamic-entry data. */\nexport const stripDynamicMenuChildren = (\n data: AnyDoc | null | undefined\n): AnyDoc | null | undefined => {\n if (!data || typeof data !== 'object') {\n return data;\n }\n stripEntries((data as AnyDoc).items);\n return data;\n};\n\n/**\n * `beforeChange` collection hook that removes read-time-resolved data from\n * dynamic mega entries so foreign ids never reach Payload's id validation.\n */\nexport const stripDynamicMenuChildrenHook: CollectionBeforeChangeHook = ({\n data,\n}) => stripDynamicMenuChildren(data as AnyDoc) as never;\n"],"names":["stripEntries","items","Array","isArray","rawItem","item","itemType","children","megaEntries","rawEntry","entry","source","featured","mode","stripDynamicMenuChildren","data","stripDynamicMenuChildrenHook"],"mappings":"AAuBA,MAAMA,eAAe,CAACC;IACpB,IAAI,CAACC,MAAMC,OAAO,CAACF,QAAQ;QACzB;IACF;IAEA,KAAK,MAAMG,WAAWH,MAAO;QAC3B,IAAI,CAACG,WAAW,OAAOA,YAAY,UAAU;YAC3C;QACF;QACA,MAAMC,OAAOD;QAEb,iEAAiE;QACjE,IAAIC,KAAKC,QAAQ,KAAK,YAAY;YAChCN,aAAaK,KAAKE,QAAQ;QAC5B;QAEA,IAAIF,KAAKC,QAAQ,KAAK,UAAU,CAACJ,MAAMC,OAAO,CAACE,KAAKG,WAAW,GAAG;YAChE;QACF;QAEA,KAAK,MAAMC,YAAYJ,KAAKG,WAAW,CAAE;YACvC,IAAI,CAACC,YAAY,OAAOA,aAAa,UAAU;gBAC7C;YACF;YACA,MAAMC,QAAQD;YACd,IAAIC,MAAMC,MAAM,KAAK,WAAW;gBAC9B;YACF;YAEA,0EAA0E;YAC1E,OAAOD,MAAMH,QAAQ;YAErB,2EAA2E;YAC3E,kEAAkE;YAClE,MAAMK,WAAWF,MAAME,QAAQ;YAC/B,IAAIA,YAAYA,SAASC,IAAI,KAAK,WAAW;gBAC3CH,MAAME,QAAQ,GAAG;oBAAEC,MAAM;gBAAU;YACrC;QACF;IACF;AACF;AAEA,kFAAkF,GAClF,OAAO,MAAMC,2BAA2B,CACtCC;IAEA,IAAI,CAACA,QAAQ,OAAOA,SAAS,UAAU;QACrC,OAAOA;IACT;IACAf,aAAa,AAACe,KAAgBd,KAAK;IACnC,OAAOc;AACT,EAAE;AAEF;;;CAGC,GACD,OAAO,MAAMC,+BAA2D,CAAC,EACvED,IAAI,EACL,GAAKD,yBAAyBC,MAAyB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
import { stripDynamicMenuChildren } from './stripDynamicMenuChildren.js';
|
|
3
|
+
describe('stripDynamicMenuChildren', ()=>{
|
|
4
|
+
test('removes resolved children from a dynamic mega entry', ()=>{
|
|
5
|
+
const data = {
|
|
6
|
+
items: [
|
|
7
|
+
{
|
|
8
|
+
itemType: 'mega',
|
|
9
|
+
megaEntries: [
|
|
10
|
+
{
|
|
11
|
+
source: 'dynamic',
|
|
12
|
+
// Injected at read time with the related doc's id — this is what
|
|
13
|
+
// fails the array-row id validation on write.
|
|
14
|
+
children: [
|
|
15
|
+
{
|
|
16
|
+
id: 12,
|
|
17
|
+
label: 'Kenya',
|
|
18
|
+
type: 'custom',
|
|
19
|
+
url: '/local-regions/kenya'
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
id: 34,
|
|
23
|
+
label: 'Tanzania',
|
|
24
|
+
type: 'custom',
|
|
25
|
+
url: '/local-regions/tanzania'
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
};
|
|
33
|
+
stripDynamicMenuChildren(data);
|
|
34
|
+
expect(data.items[0].megaEntries[0]).not.toHaveProperty('children');
|
|
35
|
+
});
|
|
36
|
+
test('leaves manual mega entries untouched', ()=>{
|
|
37
|
+
const children = [
|
|
38
|
+
{
|
|
39
|
+
id: 'abc',
|
|
40
|
+
label: 'Manual',
|
|
41
|
+
type: 'custom',
|
|
42
|
+
url: '/x'
|
|
43
|
+
}
|
|
44
|
+
];
|
|
45
|
+
const data = {
|
|
46
|
+
items: [
|
|
47
|
+
{
|
|
48
|
+
itemType: 'mega',
|
|
49
|
+
megaEntries: [
|
|
50
|
+
{
|
|
51
|
+
children,
|
|
52
|
+
source: 'manual'
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
]
|
|
57
|
+
};
|
|
58
|
+
stripDynamicMenuChildren(data);
|
|
59
|
+
expect(data.items[0].megaEntries[0].children).toBe(children);
|
|
60
|
+
});
|
|
61
|
+
test('reduces a dynamically-derived featured panel to just its mode', ()=>{
|
|
62
|
+
const data = {
|
|
63
|
+
items: [
|
|
64
|
+
{
|
|
65
|
+
itemType: 'mega',
|
|
66
|
+
megaEntries: [
|
|
67
|
+
{
|
|
68
|
+
children: [
|
|
69
|
+
{
|
|
70
|
+
id: 1
|
|
71
|
+
}
|
|
72
|
+
],
|
|
73
|
+
featured: {
|
|
74
|
+
heading: 'Resolved',
|
|
75
|
+
image: {
|
|
76
|
+
id: 9
|
|
77
|
+
},
|
|
78
|
+
mode: 'dynamic'
|
|
79
|
+
},
|
|
80
|
+
source: 'dynamic'
|
|
81
|
+
}
|
|
82
|
+
]
|
|
83
|
+
}
|
|
84
|
+
]
|
|
85
|
+
};
|
|
86
|
+
stripDynamicMenuChildren(data);
|
|
87
|
+
expect(data.items[0].megaEntries[0].featured).toEqual({
|
|
88
|
+
mode: 'dynamic'
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
test('recurses into dropdown children that contain mega items', ()=>{
|
|
92
|
+
const data = {
|
|
93
|
+
items: [
|
|
94
|
+
{
|
|
95
|
+
children: [
|
|
96
|
+
{
|
|
97
|
+
itemType: 'mega',
|
|
98
|
+
megaEntries: [
|
|
99
|
+
{
|
|
100
|
+
children: [
|
|
101
|
+
{
|
|
102
|
+
id: 7
|
|
103
|
+
}
|
|
104
|
+
],
|
|
105
|
+
source: 'dynamic'
|
|
106
|
+
}
|
|
107
|
+
]
|
|
108
|
+
}
|
|
109
|
+
],
|
|
110
|
+
itemType: 'dropdown'
|
|
111
|
+
}
|
|
112
|
+
]
|
|
113
|
+
};
|
|
114
|
+
stripDynamicMenuChildren(data);
|
|
115
|
+
expect(data.items[0].children[0].megaEntries[0]).not.toHaveProperty('children');
|
|
116
|
+
});
|
|
117
|
+
test('is a no-op for data without items', ()=>{
|
|
118
|
+
expect(stripDynamicMenuChildren(undefined)).toBeUndefined();
|
|
119
|
+
expect(stripDynamicMenuChildren(null)).toBeNull();
|
|
120
|
+
expect(stripDynamicMenuChildren({
|
|
121
|
+
title: 'x'
|
|
122
|
+
})).toEqual({
|
|
123
|
+
title: 'x'
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
//# sourceMappingURL=stripDynamicMenuChildren.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/stripDynamicMenuChildren.test.ts"],"sourcesContent":["import { describe, expect, test } from 'vitest';\n\nimport { stripDynamicMenuChildren } from './stripDynamicMenuChildren.js';\n\ndescribe('stripDynamicMenuChildren', () => {\n test('removes resolved children from a dynamic mega entry', () => {\n const data = {\n items: [\n {\n itemType: 'mega',\n megaEntries: [\n {\n source: 'dynamic',\n // Injected at read time with the related doc's id — this is what\n // fails the array-row id validation on write.\n children: [\n {\n id: 12,\n label: 'Kenya',\n type: 'custom',\n url: '/local-regions/kenya',\n },\n {\n id: 34,\n label: 'Tanzania',\n type: 'custom',\n url: '/local-regions/tanzania',\n },\n ],\n },\n ],\n },\n ],\n };\n\n stripDynamicMenuChildren(data);\n\n expect(data.items[0].megaEntries[0]).not.toHaveProperty('children');\n });\n\n test('leaves manual mega entries untouched', () => {\n const children = [\n { id: 'abc', label: 'Manual', type: 'custom', url: '/x' },\n ];\n const data = {\n items: [\n { itemType: 'mega', megaEntries: [{ children, source: 'manual' }] },\n ],\n };\n\n stripDynamicMenuChildren(data);\n\n expect(data.items[0].megaEntries[0].children).toBe(children);\n });\n\n test('reduces a dynamically-derived featured panel to just its mode', () => {\n const data = {\n items: [\n {\n itemType: 'mega',\n megaEntries: [\n {\n children: [{ id: 1 }],\n featured: {\n heading: 'Resolved',\n image: { id: 9 },\n mode: 'dynamic',\n },\n source: 'dynamic',\n },\n ],\n },\n ],\n };\n\n stripDynamicMenuChildren(data);\n\n expect(data.items[0].megaEntries[0].featured).toEqual({ mode: 'dynamic' });\n });\n\n test('recurses into dropdown children that contain mega items', () => {\n const data = {\n items: [\n {\n children: [\n {\n itemType: 'mega',\n megaEntries: [{ children: [{ id: 7 }], source: 'dynamic' }],\n },\n ],\n itemType: 'dropdown',\n },\n ],\n };\n\n stripDynamicMenuChildren(data);\n\n expect(data.items[0].children[0].megaEntries[0]).not.toHaveProperty(\n 'children'\n );\n });\n\n test('is a no-op for data without items', () => {\n expect(stripDynamicMenuChildren(undefined)).toBeUndefined();\n expect(stripDynamicMenuChildren(null)).toBeNull();\n expect(stripDynamicMenuChildren({ title: 'x' })).toEqual({ title: 'x' });\n });\n});\n"],"names":["describe","expect","test","stripDynamicMenuChildren","data","items","itemType","megaEntries","source","children","id","label","type","url","not","toHaveProperty","toBe","featured","heading","image","mode","toEqual","undefined","toBeUndefined","toBeNull","title"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,MAAM,EAAEC,IAAI,QAAQ,SAAS;AAEhD,SAASC,wBAAwB,QAAQ,gCAAgC;AAEzEH,SAAS,4BAA4B;IACnCE,KAAK,uDAAuD;QAC1D,MAAME,OAAO;YACXC,OAAO;gBACL;oBACEC,UAAU;oBACVC,aAAa;wBACX;4BACEC,QAAQ;4BACR,iEAAiE;4BACjE,8CAA8C;4BAC9CC,UAAU;gCACR;oCACEC,IAAI;oCACJC,OAAO;oCACPC,MAAM;oCACNC,KAAK;gCACP;gCACA;oCACEH,IAAI;oCACJC,OAAO;oCACPC,MAAM;oCACNC,KAAK;gCACP;6BACD;wBACH;qBACD;gBACH;aACD;QACH;QAEAV,yBAAyBC;QAEzBH,OAAOG,KAAKC,KAAK,CAAC,EAAE,CAACE,WAAW,CAAC,EAAE,EAAEO,GAAG,CAACC,cAAc,CAAC;IAC1D;IAEAb,KAAK,wCAAwC;QAC3C,MAAMO,WAAW;YACf;gBAAEC,IAAI;gBAAOC,OAAO;gBAAUC,MAAM;gBAAUC,KAAK;YAAK;SACzD;QACD,MAAMT,OAAO;YACXC,OAAO;gBACL;oBAAEC,UAAU;oBAAQC,aAAa;wBAAC;4BAAEE;4BAAUD,QAAQ;wBAAS;qBAAE;gBAAC;aACnE;QACH;QAEAL,yBAAyBC;QAEzBH,OAAOG,KAAKC,KAAK,CAAC,EAAE,CAACE,WAAW,CAAC,EAAE,CAACE,QAAQ,EAAEO,IAAI,CAACP;IACrD;IAEAP,KAAK,iEAAiE;QACpE,MAAME,OAAO;YACXC,OAAO;gBACL;oBACEC,UAAU;oBACVC,aAAa;wBACX;4BACEE,UAAU;gCAAC;oCAAEC,IAAI;gCAAE;6BAAE;4BACrBO,UAAU;gCACRC,SAAS;gCACTC,OAAO;oCAAET,IAAI;gCAAE;gCACfU,MAAM;4BACR;4BACAZ,QAAQ;wBACV;qBACD;gBACH;aACD;QACH;QAEAL,yBAAyBC;QAEzBH,OAAOG,KAAKC,KAAK,CAAC,EAAE,CAACE,WAAW,CAAC,EAAE,CAACU,QAAQ,EAAEI,OAAO,CAAC;YAAED,MAAM;QAAU;IAC1E;IAEAlB,KAAK,2DAA2D;QAC9D,MAAME,OAAO;YACXC,OAAO;gBACL;oBACEI,UAAU;wBACR;4BACEH,UAAU;4BACVC,aAAa;gCAAC;oCAAEE,UAAU;wCAAC;4CAAEC,IAAI;wCAAE;qCAAE;oCAAEF,QAAQ;gCAAU;6BAAE;wBAC7D;qBACD;oBACDF,UAAU;gBACZ;aACD;QACH;QAEAH,yBAAyBC;QAEzBH,OAAOG,KAAKC,KAAK,CAAC,EAAE,CAACI,QAAQ,CAAC,EAAE,CAACF,WAAW,CAAC,EAAE,EAAEO,GAAG,CAACC,cAAc,CACjE;IAEJ;IAEAb,KAAK,qCAAqC;QACxCD,OAAOE,yBAAyBmB,YAAYC,aAAa;QACzDtB,OAAOE,yBAAyB,OAAOqB,QAAQ;QAC/CvB,OAAOE,yBAAyB;YAAEsB,OAAO;QAAI,IAAIJ,OAAO,CAAC;YAAEI,OAAO;QAAI;IACxE;AACF"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { recordActivePlugin } from './activePlugins.js';
|
|
1
2
|
import { menuCache } from './cache.js';
|
|
2
3
|
import { createMenusCollection } from './collections/Menus.js';
|
|
3
4
|
import { createExportMenusHandler } from './endpoints/exportMenusHandler.js';
|
|
@@ -48,7 +49,7 @@ export const menuPlugin = (pluginOptions)=>(config)=>{
|
|
|
48
49
|
}
|
|
49
50
|
] : []
|
|
50
51
|
];
|
|
51
|
-
return config;
|
|
52
|
+
return recordActivePlugin(config, '@foundrykit/menu-plugin');
|
|
52
53
|
};
|
|
53
54
|
|
|
54
55
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { CollectionSlug, Config } from 'payload';\nimport { menuCache } from './cache.js';\nimport { createMenusCollection } from './collections/Menus.js';\nimport { createExportMenusHandler } from './endpoints/exportMenusHandler.js';\nimport { createGetMenuHandler } from './endpoints/getMenuHandler.js';\nimport { createImportMenusHandler } from './endpoints/importMenusHandler.js';\nimport type { DynamicSource } from './types.js';\n\nexport { migrateMegaColumnsToEntries } from './migrations/megaColumnsToEntries.js';\nexport type { DynamicSource } from './types.js';\n\nexport type MenuPluginConfig = {\n baseUrl?: string;\n cacheTTL?: number;\n /** depth used when resolving dynamic mega entry children (default: 1) */\n childrenDepth?: number;\n disabled?: boolean;\n /** sources used to auto-populate dynamic mega entries from related collections */\n dynamicSources?: DynamicSource[];\n enableImportExport?: boolean;\n maxDepth?: number;\n mediaCollection?: CollectionSlug;\n relationTo?: CollectionSlug | CollectionSlug[];\n requireAdminForImportExport?: boolean;\n};\n\nexport const menuPlugin =\n (pluginOptions: MenuPluginConfig) =>\n (config: Config): Config => {\n const cacheTTL = pluginOptions.cacheTTL ?? 60_000;\n const enableImportExport = pluginOptions.enableImportExport ?? true;\n const requireAdminForImportExport =\n pluginOptions.requireAdminForImportExport ?? true;\n\n menuCache.configure(cacheTTL);\n\n config.collections = [\n ...(config.collections || []),\n createMenusCollection({\n baseUrl: pluginOptions.baseUrl,\n childrenDepth: pluginOptions.childrenDepth,\n disabled: pluginOptions.disabled,\n dynamicSources: pluginOptions.dynamicSources,\n maxDepth: pluginOptions.maxDepth,\n mediaCollection: pluginOptions.mediaCollection,\n relationTo: pluginOptions.relationTo,\n }),\n ];\n\n if (pluginOptions.disabled) {\n return config;\n }\n\n config.endpoints = [\n ...(config.endpoints || []),\n {\n handler: createGetMenuHandler(),\n method: 'get',\n path: '/api/menus/:slug',\n },\n ...(enableImportExport\n ? [\n {\n handler: createExportMenusHandler({\n requireAdmin: requireAdminForImportExport,\n }),\n method: 'get' as const,\n path: '/api/menus-export',\n },\n {\n handler: createImportMenusHandler({\n requireAdmin: requireAdminForImportExport,\n }),\n method: 'post' as const,\n path: '/api/menus-import',\n },\n ]\n : []),\n ];\n\n return config;\n };\n"],"names":["menuCache","createMenusCollection","createExportMenusHandler","createGetMenuHandler","createImportMenusHandler","migrateMegaColumnsToEntries","menuPlugin","pluginOptions","config","cacheTTL","enableImportExport","requireAdminForImportExport","configure","collections","baseUrl","childrenDepth","disabled","dynamicSources","maxDepth","mediaCollection","relationTo","endpoints","handler","method","path","requireAdmin"],"mappings":"AACA,SAASA,SAAS,QAAQ,aAAa;AACvC,SAASC,qBAAqB,QAAQ,yBAAyB;AAC/D,SAASC,wBAAwB,QAAQ,oCAAoC;AAC7E,SAASC,oBAAoB,QAAQ,gCAAgC;AACrE,SAASC,wBAAwB,QAAQ,oCAAoC;AAG7E,SAASC,2BAA2B,QAAQ,uCAAuC;AAkBnF,OAAO,MAAMC,aACX,CAACC,gBACD,CAACC;QACC,MAAMC,WAAWF,cAAcE,QAAQ,IAAI;QAC3C,MAAMC,qBAAqBH,cAAcG,kBAAkB,IAAI;QAC/D,MAAMC,8BACJJ,cAAcI,2BAA2B,IAAI;QAE/CX,UAAUY,SAAS,CAACH;QAEpBD,OAAOK,WAAW,GAAG;eACfL,OAAOK,WAAW,IAAI,EAAE;YAC5BZ,sBAAsB;gBACpBa,SAASP,cAAcO,OAAO;gBAC9BC,eAAeR,cAAcQ,aAAa;gBAC1CC,UAAUT,cAAcS,QAAQ;gBAChCC,gBAAgBV,cAAcU,cAAc;gBAC5CC,UAAUX,cAAcW,QAAQ;gBAChCC,iBAAiBZ,cAAcY,eAAe;gBAC9CC,YAAYb,cAAca,UAAU;YACtC;SACD;QAED,IAAIb,cAAcS,QAAQ,EAAE;YAC1B,OAAOR;QACT;QAEAA,OAAOa,SAAS,GAAG;eACbb,OAAOa,SAAS,IAAI,EAAE;YAC1B;gBACEC,SAASnB;gBACToB,QAAQ;gBACRC,MAAM;YACR;eACId,qBACA;gBACE;oBACEY,SAASpB,yBAAyB;wBAChCuB,cAAcd;oBAChB;oBACAY,QAAQ;oBACRC,MAAM;gBACR;gBACA;oBACEF,SAASlB,yBAAyB;wBAChCqB,cAAcd;oBAChB;oBACAY,QAAQ;oBACRC,MAAM;gBACR;aACD,GACD,EAAE;SACP;QAED,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { CollectionSlug, Config } from 'payload';\nimport { recordActivePlugin } from './activePlugins.js';\nimport { menuCache } from './cache.js';\nimport { createMenusCollection } from './collections/Menus.js';\nimport { createExportMenusHandler } from './endpoints/exportMenusHandler.js';\nimport { createGetMenuHandler } from './endpoints/getMenuHandler.js';\nimport { createImportMenusHandler } from './endpoints/importMenusHandler.js';\nimport type { DynamicSource } from './types.js';\n\nexport { migrateMegaColumnsToEntries } from './migrations/megaColumnsToEntries.js';\nexport type { DynamicSource } from './types.js';\n\nexport type MenuPluginConfig = {\n baseUrl?: string;\n cacheTTL?: number;\n /** depth used when resolving dynamic mega entry children (default: 1) */\n childrenDepth?: number;\n disabled?: boolean;\n /** sources used to auto-populate dynamic mega entries from related collections */\n dynamicSources?: DynamicSource[];\n enableImportExport?: boolean;\n maxDepth?: number;\n mediaCollection?: CollectionSlug;\n relationTo?: CollectionSlug | CollectionSlug[];\n requireAdminForImportExport?: boolean;\n};\n\nexport const menuPlugin =\n (pluginOptions: MenuPluginConfig) =>\n (config: Config): Config => {\n const cacheTTL = pluginOptions.cacheTTL ?? 60_000;\n const enableImportExport = pluginOptions.enableImportExport ?? true;\n const requireAdminForImportExport =\n pluginOptions.requireAdminForImportExport ?? true;\n\n menuCache.configure(cacheTTL);\n\n config.collections = [\n ...(config.collections || []),\n createMenusCollection({\n baseUrl: pluginOptions.baseUrl,\n childrenDepth: pluginOptions.childrenDepth,\n disabled: pluginOptions.disabled,\n dynamicSources: pluginOptions.dynamicSources,\n maxDepth: pluginOptions.maxDepth,\n mediaCollection: pluginOptions.mediaCollection,\n relationTo: pluginOptions.relationTo,\n }),\n ];\n\n if (pluginOptions.disabled) {\n return config;\n }\n\n config.endpoints = [\n ...(config.endpoints || []),\n {\n handler: createGetMenuHandler(),\n method: 'get',\n path: '/api/menus/:slug',\n },\n ...(enableImportExport\n ? [\n {\n handler: createExportMenusHandler({\n requireAdmin: requireAdminForImportExport,\n }),\n method: 'get' as const,\n path: '/api/menus-export',\n },\n {\n handler: createImportMenusHandler({\n requireAdmin: requireAdminForImportExport,\n }),\n method: 'post' as const,\n path: '/api/menus-import',\n },\n ]\n : []),\n ];\n\n return recordActivePlugin(config, '@foundrykit/menu-plugin');\n };\n"],"names":["recordActivePlugin","menuCache","createMenusCollection","createExportMenusHandler","createGetMenuHandler","createImportMenusHandler","migrateMegaColumnsToEntries","menuPlugin","pluginOptions","config","cacheTTL","enableImportExport","requireAdminForImportExport","configure","collections","baseUrl","childrenDepth","disabled","dynamicSources","maxDepth","mediaCollection","relationTo","endpoints","handler","method","path","requireAdmin"],"mappings":"AACA,SAASA,kBAAkB,QAAQ,qBAAqB;AACxD,SAASC,SAAS,QAAQ,aAAa;AACvC,SAASC,qBAAqB,QAAQ,yBAAyB;AAC/D,SAASC,wBAAwB,QAAQ,oCAAoC;AAC7E,SAASC,oBAAoB,QAAQ,gCAAgC;AACrE,SAASC,wBAAwB,QAAQ,oCAAoC;AAG7E,SAASC,2BAA2B,QAAQ,uCAAuC;AAkBnF,OAAO,MAAMC,aACX,CAACC,gBACD,CAACC;QACC,MAAMC,WAAWF,cAAcE,QAAQ,IAAI;QAC3C,MAAMC,qBAAqBH,cAAcG,kBAAkB,IAAI;QAC/D,MAAMC,8BACJJ,cAAcI,2BAA2B,IAAI;QAE/CX,UAAUY,SAAS,CAACH;QAEpBD,OAAOK,WAAW,GAAG;eACfL,OAAOK,WAAW,IAAI,EAAE;YAC5BZ,sBAAsB;gBACpBa,SAASP,cAAcO,OAAO;gBAC9BC,eAAeR,cAAcQ,aAAa;gBAC1CC,UAAUT,cAAcS,QAAQ;gBAChCC,gBAAgBV,cAAcU,cAAc;gBAC5CC,UAAUX,cAAcW,QAAQ;gBAChCC,iBAAiBZ,cAAcY,eAAe;gBAC9CC,YAAYb,cAAca,UAAU;YACtC;SACD;QAED,IAAIb,cAAcS,QAAQ,EAAE;YAC1B,OAAOR;QACT;QAEAA,OAAOa,SAAS,GAAG;eACbb,OAAOa,SAAS,IAAI,EAAE;YAC1B;gBACEC,SAASnB;gBACToB,QAAQ;gBACRC,MAAM;YACR;eACId,qBACA;gBACE;oBACEY,SAASpB,yBAAyB;wBAChCuB,cAAcd;oBAChB;oBACAY,QAAQ;oBACRC,MAAM;gBACR;gBACA;oBACEF,SAASlB,yBAAyB;wBAChCqB,cAAcd;oBAChB;oBACAY,QAAQ;oBACRC,MAAM;gBACR;aACD,GACD,EAAE;SACP;QAED,OAAOzB,mBAAmBS,QAAQ;IACpC,EAAE"}
|