UNPKG

@websolutespa/payload-plugin-bowl

Version:

Bowl PayloadCms plugin of the BOM Repository

68 lines (67 loc) 2.32 kB
import { isEntity, isMedia } from '@websolutespa/bom-core'; import { deepMerge, hasUnresolvedRelations } from '@websolutespa/payload-utils'; import { richTextToHTMLAsync } from '../utils/richText'; const RICH_TEXT_AFTER_READ_ENABLED = true; export function getMedia(payload) { return async (node)=>{ if (isMedia(node.value)) { return node.value; } else if (isEntity(node.value)) { const fallbackLng = payload.config?.i18n.fallbackLanguage; const media = await payload.findByID({ collection: node.relationTo, id: node.value.id, fallbackLocale: typeof fallbackLng === 'string' ? fallbackLng : undefined, overrideAccess: true }); return media; } return null; }; } export const richTextAfterReadHook = async (args)=>{ const { data, findMany, context, operation, originalDoc, req, siblingData, value } = args; if (value && (req.query?.richText === 'false' || typeof req.context.market === 'string' && typeof req.context.locale === 'string' && req.context.richText !== true)) { // console.log('richTextAfterReadHook', operation, value); if (hasUnresolvedRelations(value)) { return value; } return await richTextToHTMLAsync(value, req.payload.config, context || req.context); } return value; }; export const RichTextDefaults = { name: 'richText', type: 'richText', localized: true }; export const withRichText = (options = {})=>{ const field = deepMerge(RichTextDefaults, options); if (RICH_TEXT_AFTER_READ_ENABLED) { field.hooks = { ...field.hooks, afterRead: [ ...field.hooks?.afterRead || [], richTextAfterReadHook ] }; } return field; }; export const withRichTextRequired = (options = {})=>{ const field = deepMerge({ ...RichTextDefaults, required: true }, options); if (RICH_TEXT_AFTER_READ_ENABLED) { field.hooks = { ...field.hooks, afterRead: [ ...field.hooks?.afterRead || [], richTextAfterReadHook ] }; } return field; }; //# sourceMappingURL=withRichText.js.map