- buildRevalidateHook skips outside Next (no NEXT_RUNTIME) and with context.disableRevalidate - BuilderForm focuses only on a real step change (no scroll to the form on remount) - mediaImageProps: placeholder colour only for JPEG (placeholderColor); hook stores no colour for transparent images - buildLinkField: interface name per shape (CmsLink, CmsLinkPlain, CmsLinkNoLabel) + interfaceName option - Cookie Settings: buttons.save -> buttons.savePreferences (Mongoose reserved name) - ThemeScript, ThemeSwitcher, useTheme; Site Settings defaultTheme 'system' + themeToggleLabel; Turnstile follows page theme - extractHeadings, RenderRichText headingIds, TableOfContents, buildTableOfContentsField - createPageRoute onMissing: 'redirectHome' - Form builder: localized placeholder on text/email/textarea/number, new time field - ipal-audit: no false positive for object keys between JSX elements; /md route not reported as dynamic Co-Authored-By: Claude Opus 5.5 <[email protected]>
174 lines
6.1 KiB
TypeScript
174 lines
6.1 KiB
TypeScript
import type { ArrayField, Field, GroupField } from 'payload'
|
|
|
|
import { rowLabel } from '../admin-ui/rowLabel.js'
|
|
|
|
/** The default styles. A project may use any names of its own — see `appearances`. */
|
|
export type LinkAppearance = 'link' | 'primary' | 'secondary'
|
|
|
|
/** One style the editor may pick: a value (`'highlight'`) or `{ label, value }` for a friendlier panel label. */
|
|
export type LinkAppearanceOption = { label: string; value: string } | string
|
|
|
|
export type BuildLinkFieldOptions = {
|
|
/**
|
|
* Visual styles the editor may pick — the project's own names, mapped to its
|
|
* classes in Button.tsx / the menu: `['primary', 'secondary', { label: 'Highlighted', value: 'highlight' }]`.
|
|
* `false` → no appearance select. Default primary / secondary / link.
|
|
*/
|
|
appearances?: false | readonly LinkAppearanceOption[]
|
|
/** Hide the label — for links that wrap something (a whole card, an image). */
|
|
disableLabel?: boolean
|
|
/**
|
|
* Name of the generated TypeScript type. Default depends on the shape, so links of
|
|
* different shapes never share one name (payload generate:types would keep only the
|
|
* last one processed): `CmsLink`, `CmsLinkPlain` (appearances: false),
|
|
* `CmsLinkNoLabel` (disableLabel), `CmsLinkPlainNoLabel` (both). Set it yourself
|
|
* when you mix different `appearances` lists or `relationTo` values.
|
|
*/
|
|
interfaceName?: string
|
|
/** Field name. Default 'link'. */
|
|
name?: string
|
|
/** Collection the "Page" option points at. Default 'pages'. */
|
|
relationTo?: readonly string[] | string
|
|
required?: boolean
|
|
/** Upload collection for "File" links (PDF, documents). Default 'media'. */
|
|
uploadTo?: string
|
|
}
|
|
|
|
const URL_PATTERN = /^(?:https?:\/\/|mailto:|tel:|#)/
|
|
|
|
type SiblingWithType = { type?: string } | undefined
|
|
|
|
/**
|
|
* The ONE link shape for a project: a page, an external URL (also mailto:,
|
|
* tel:, #anchor) or an uploaded file — plus label, new-tab flag and visual
|
|
* style. Every CTA, card link and download in every block uses this field, so
|
|
* there is no zoo of ctaLabel / buttonUrl / pdfUrl fields and one renderer
|
|
* (resolveLinkHref + the project's CMSLink) handles them all.
|
|
*
|
|
* Generates a named TypeScript type in payload-types.ts — `CmsLink` for the default
|
|
* shape, a suffixed name for others (see `interfaceName`). All are assignable to `CmsLink`.
|
|
*
|
|
* fields: [buildLinkField()] // single link
|
|
* fields: [buildLinkField({ disableLabel: true, appearances: false })] // clickable card
|
|
*/
|
|
export function buildLinkField({
|
|
name = 'link',
|
|
appearances = ['primary', 'secondary', 'link'],
|
|
disableLabel = false,
|
|
interfaceName,
|
|
relationTo = 'pages',
|
|
required = false,
|
|
uploadTo = 'media',
|
|
}: BuildLinkFieldOptions = {}): GroupField {
|
|
const hasAppearance = Boolean(appearances && appearances.length)
|
|
const pageAdmin = { condition: (_: unknown, s: SiblingWithType) => s?.type === 'internal' }
|
|
const fields: Field[] = [
|
|
{
|
|
type: 'row',
|
|
fields: [
|
|
{
|
|
name: 'type',
|
|
type: 'radio',
|
|
admin: { layout: 'horizontal', width: '50%' },
|
|
defaultValue: 'internal',
|
|
options: [
|
|
{ label: 'Page', value: 'internal' },
|
|
{ label: 'URL', value: 'external' },
|
|
{ label: 'File', value: 'file' },
|
|
],
|
|
},
|
|
{
|
|
name: 'newTab',
|
|
type: 'checkbox',
|
|
admin: { style: { alignSelf: 'flex-end' }, width: '50%' },
|
|
label: 'Open in new tab',
|
|
},
|
|
],
|
|
},
|
|
// One collection → plain relation; several (pages + content entries) → polymorphic.
|
|
typeof relationTo === 'string'
|
|
? { name: 'page', type: 'relationship', admin: pageAdmin, relationTo, required }
|
|
: { name: 'page', type: 'relationship', admin: pageAdmin, relationTo: [...relationTo], required },
|
|
{
|
|
name: 'url',
|
|
type: 'text',
|
|
admin: {
|
|
condition: (_: unknown, s: SiblingWithType) => s?.type === 'external',
|
|
description: 'Full URL (https://…), mailto:, tel: or #section-anchor.',
|
|
},
|
|
required,
|
|
validate: (v: null | string | undefined) =>
|
|
!v || URL_PATTERN.test(v) || 'Use https://…, mailto:, tel: or #anchor',
|
|
},
|
|
{
|
|
name: 'file',
|
|
type: 'upload',
|
|
admin: {
|
|
condition: (_: unknown, s: SiblingWithType) => s?.type === 'file',
|
|
description: 'Upload a PDF / document / image. Never paste a file URL.',
|
|
},
|
|
relationTo: uploadTo,
|
|
required,
|
|
},
|
|
]
|
|
|
|
if (!disableLabel) {
|
|
fields.push({ name: 'label', type: 'text', localized: true, required })
|
|
}
|
|
|
|
if (appearances && hasAppearance) {
|
|
const options = appearances.map((a) => (typeof a === 'string' ? { label: a, value: a } : a))
|
|
fields.push({
|
|
name: 'appearance',
|
|
type: 'select',
|
|
defaultValue: options[0].value,
|
|
options,
|
|
})
|
|
}
|
|
|
|
return {
|
|
name,
|
|
type: 'group',
|
|
admin: { hideGutter: true },
|
|
fields,
|
|
interfaceName: interfaceName ?? `CmsLink${hasAppearance ? '' : 'Plain'}${disableLabel ? 'NoLabel' : ''}`,
|
|
}
|
|
}
|
|
|
|
export type BuildLinksFieldOptions = {
|
|
/**
|
|
* What one row is called in the panel — the add button reads "Add <singular>".
|
|
* Default { singular: 'Button', plural: 'Buttons' }; e.g. { singular: 'Link', plural: 'Links' } for a link list.
|
|
*/
|
|
labels?: { plural: string; singular: string }
|
|
/** Max number of buttons. Default 2. */
|
|
maxRows?: number
|
|
/** Field name. Default 'links'. */
|
|
name?: string
|
|
} & Omit<BuildLinkFieldOptions, 'disableLabel' | 'name'>
|
|
|
|
/**
|
|
* 0..N buttons — use for every "CTA(s)" of a section. Each row holds one
|
|
* `link` (buildLinkField). Render: `links.map(({ id, link }) => <CMSLink … />)`.
|
|
*/
|
|
export function buildLinksField({
|
|
name = 'links',
|
|
labels = { plural: 'Buttons', singular: 'Button' },
|
|
maxRows = 2,
|
|
...linkOptions
|
|
}: BuildLinksFieldOptions = {}): ArrayField {
|
|
return {
|
|
name,
|
|
type: 'array',
|
|
admin: {
|
|
initCollapsed: true,
|
|
// Rows read "Contact us" / "Download catalogue", not "Button 01". Needs the importMap entry.
|
|
components: rowLabel(['link.label', 'link.url'], labels.singular),
|
|
},
|
|
fields: [buildLinkField(linkOptions)],
|
|
label: labels.plural,
|
|
labels,
|
|
maxRows,
|
|
}
|
|
}
|