Added module Links
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
import type { ArrayField, Field, GroupField } from 'payload'
|
||||
|
||||
export type LinkAppearance = 'link' | 'primary' | 'secondary'
|
||||
|
||||
export type BuildLinkFieldOptions = {
|
||||
/** Visual styles the editor may pick. `false` → no appearance select. Default all three. */
|
||||
appearances?: false | LinkAppearance[]
|
||||
/** Hide the label — for links that wrap something (a whole card, an image). */
|
||||
disableLabel?: boolean
|
||||
/** Field name. Default 'link'. */
|
||||
name?: string
|
||||
/** Collection the "Page" option points at. Default 'pages'. */
|
||||
relationTo?: 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 `CmsLink` in payload-types.ts.
|
||||
*
|
||||
* fields: [buildLinkField()] // single link
|
||||
* fields: [buildLinkField({ disableLabel: true, appearances: false })] // clickable card
|
||||
*/
|
||||
export function buildLinkField({
|
||||
name = 'link',
|
||||
appearances = ['primary', 'secondary', 'link'],
|
||||
disableLabel = false,
|
||||
relationTo = 'pages',
|
||||
required = false,
|
||||
uploadTo = 'media',
|
||||
}: BuildLinkFieldOptions = {}): GroupField {
|
||||
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',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'page',
|
||||
type: 'relationship',
|
||||
admin: { condition: (_: unknown, s: SiblingWithType) => s?.type === 'internal' },
|
||||
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 && appearances.length) {
|
||||
fields.push({
|
||||
name: 'appearance',
|
||||
type: 'select',
|
||||
defaultValue: appearances[0],
|
||||
options: appearances.map((a) => ({ label: a, value: a })),
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
type: 'group',
|
||||
admin: { hideGutter: true },
|
||||
fields,
|
||||
interfaceName: 'CmsLink',
|
||||
}
|
||||
}
|
||||
|
||||
export type BuildLinksFieldOptions = {
|
||||
/** 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',
|
||||
maxRows = 2,
|
||||
...linkOptions
|
||||
}: BuildLinksFieldOptions = {}): ArrayField {
|
||||
return {
|
||||
name,
|
||||
type: 'array',
|
||||
admin: { initCollapsed: true },
|
||||
fields: [buildLinkField(linkOptions)],
|
||||
maxRows,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export { buildLinkField, buildLinksField } from './buildLinkField.js'
|
||||
export type { BuildLinkFieldOptions, BuildLinksFieldOptions, LinkAppearance } from './buildLinkField.js'
|
||||
export { resolveLinkHref } from './resolveLinkHref.js'
|
||||
export type { LinkValue, ResolveLinkHrefOptions } from './resolveLinkHref.js'
|
||||
@@ -0,0 +1,71 @@
|
||||
import type { I18nConfig } from '../i18n/index.js'
|
||||
|
||||
import { buildLocalizedPath } from '../i18n/index.js'
|
||||
|
||||
/**
|
||||
* Minimal shape of a link value (the project's generated `CmsLink` fits it).
|
||||
* The plugin doesn't know the project's types — only this contract.
|
||||
*/
|
||||
export type LinkValue = {
|
||||
appearance?: null | string
|
||||
file?: unknown
|
||||
label?: null | string
|
||||
newTab?: boolean | null
|
||||
page?: unknown
|
||||
type?: 'external' | 'file' | 'internal' | null
|
||||
url?: null | string
|
||||
}
|
||||
|
||||
export type ResolveLinkHrefOptions = {
|
||||
config: I18nConfig
|
||||
/**
|
||||
* Slug(s) of the homepage so it collapses to /{locale}. A per-locale map or a
|
||||
* string. Usually from SiteSettings.homepage.
|
||||
*/
|
||||
homeSlug?: Record<string, string> | string
|
||||
locale: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns a link value (buildLinkField) into an href — the single place that
|
||||
* knows the link types. Returns null when the link is empty or its target is
|
||||
* missing in this locale (deleted page, page not populated, no file).
|
||||
*
|
||||
* Internal links need the related page POPULATED (block depth ≥ 2) — a bare ID
|
||||
* resolves to null.
|
||||
*
|
||||
* resolveLinkHref(link, { locale, config: i18nConfig, homeSlug })
|
||||
* // internal → '/en/about' · external → 'https://…' · file → media URL
|
||||
*/
|
||||
export function resolveLinkHref(
|
||||
link: LinkValue | null | undefined,
|
||||
{ config, homeSlug, locale }: ResolveLinkHrefOptions,
|
||||
): null | string {
|
||||
if (!link) {return null}
|
||||
|
||||
if (link.type === 'external') {return link.url?.trim() || null}
|
||||
|
||||
if (link.type === 'file') {
|
||||
const file = link.file
|
||||
if (file && typeof file === 'object' && 'url' in file) {
|
||||
const url = (file as { url?: unknown }).url
|
||||
return typeof url === 'string' && url ? url : null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// internal (default)
|
||||
const page = link.page
|
||||
if (!page || typeof page !== 'object') {return null}
|
||||
const slug = (page as { slug?: unknown }).slug
|
||||
if (typeof slug !== 'string' || !slug) {return null}
|
||||
|
||||
return (
|
||||
buildLocalizedPath({
|
||||
config,
|
||||
locale,
|
||||
slugs: { [locale]: slug },
|
||||
...(homeSlug ? { homeSlug } : {}),
|
||||
}) ?? null
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user