init commit for iPAL-kit plugin
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import type { Config, Field, TabsField } from 'payload'
|
||||
|
||||
import type { SeoOption } from './types.js'
|
||||
|
||||
/**
|
||||
* Wraps each SEO-enabled collection's fields into a tabbed UI: a "Content" tab
|
||||
* holding the collection's own fields, and an "SEO" tab holding the `meta`
|
||||
* group that @payloadcms/plugin-seo added.
|
||||
*
|
||||
* This replaces plugin-seo's own `tabbedUI`, which merges tabs by inspecting
|
||||
* the first field and breaks when other plugins/fields (roles, slug) already
|
||||
* sit in the collection — producing an empty SEO tab. Running this AFTER
|
||||
* plugin-seo (so `meta` already exists) and after other field injections lets
|
||||
* us build the tabs deterministically.
|
||||
*
|
||||
* If a collection's first field is already a tabs field, the SEO tab is
|
||||
* appended to it instead of creating a new wrapper.
|
||||
*/
|
||||
export function injectSeoTabs(config: Config, seo: SeoOption): Config {
|
||||
return {
|
||||
...config,
|
||||
collections: (config.collections ?? []).map((collection) => {
|
||||
if (!seo.collections.includes(collection.slug)) {
|
||||
return collection
|
||||
}
|
||||
|
||||
const fields = collection.fields ?? []
|
||||
|
||||
// Separate the meta group (added by plugin-seo) from the rest.
|
||||
const metaField = fields.find(
|
||||
(f): f is { name: string } & Field => 'name' in f && f.name === 'meta',
|
||||
)
|
||||
const otherFields = fields.filter(
|
||||
(f) => !('name' in f && f.name === 'meta'),
|
||||
)
|
||||
|
||||
// Nothing to do if plugin-seo hasn't added meta (shouldn't happen).
|
||||
if (!metaField) {return collection}
|
||||
|
||||
// If the collection already leads with a tabs field, append an SEO tab.
|
||||
const firstField = otherFields[0]
|
||||
if (firstField && firstField.type === 'tabs') {
|
||||
const existingTabs = firstField
|
||||
const withSeoTab: TabsField = {
|
||||
...existingTabs,
|
||||
tabs: [...existingTabs.tabs, { fields: [metaField], label: 'SEO' }],
|
||||
}
|
||||
return {
|
||||
...collection,
|
||||
fields: [withSeoTab, ...otherFields.slice(1)],
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise wrap everything: Content tab + SEO tab.
|
||||
const tabs: TabsField = {
|
||||
type: 'tabs',
|
||||
tabs: [
|
||||
{ fields: otherFields, label: collection.labels?.singular?.toString() || 'Content' },
|
||||
{ fields: [metaField], label: 'SEO' },
|
||||
],
|
||||
}
|
||||
|
||||
return {
|
||||
...collection,
|
||||
fields: [tabs],
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user