@websolutespa/payload-plugin-bowl
Version:
Bowl PayloadCms plugin of the BOM Repository
56 lines (55 loc) • 1.81 kB
JavaScript
import { deepMerge, textToSlug } from '@websolutespa/payload-utils';
import { withText } from './withText';
import { withTitle } from './withTitle';
export const AnchorDefaults = {
name: 'anchor',
type: 'group',
hooks: {
afterRead: [
({ value, siblingData, req })=>{
if ((!value.title || !value.hash) && [
'admin',
'editor'
].indexOf(req.user?.role) === -1) {
siblingData.anchor = undefined;
}
}
]
},
validate: (value)=>{
const v = value;
if (v && v.title && !v.hash) {
return `missing hash for title "${v.title}"`;
}
if (v && v.hash && !v.title) {
return `missing title for hash "${v.hash}"`;
}
return true;
},
fields: [
withTitle(),
withText({
name: 'hash',
localized: true,
hooks: {
beforeValidate: [
({ value, siblingData, previousSiblingDoc })=>{
if (typeof value === 'string' && value !== '') {
return textToSlug(value);
}
const fallbackData = siblingData && siblingData.title || previousSiblingDoc && previousSiblingDoc.title;
if (fallbackData && typeof fallbackData === 'string') {
return textToSlug(fallbackData);
}
return value;
}
]
}
})
]
};
export const withAnchor = (options = {})=>{
const field = deepMerge(AnchorDefaults, options);
return field;
};
//# sourceMappingURL=withAnchor.js.map