Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
1 line
6.1 KiB
Plaintext
1 line
6.1 KiB
Plaintext
{"version":3,"file":"handleLivePreview.js","names":["getLivePreviewConfig","collectionConfig","config","globalConfig","isLivePreviewEnabled","admin","livePreview","Boolean","globals","includes","slug","collections","handleLivePreview","collectionSlug","data","globalSlug","operation","req","payload","undefined","find","g","enabled","livePreviewConfig","livePreviewURL","url","result","locale","code","label","err","logger","error","msg"],"sources":["../../src/utilities/handleLivePreview.ts"],"sourcesContent":["import type {\n CollectionConfig,\n GlobalConfig,\n LivePreviewConfig,\n LivePreviewURLType,\n Operation,\n PayloadRequest,\n SanitizedConfig,\n} from 'payload'\n\nexport const getLivePreviewConfig = ({\n collectionConfig,\n config,\n globalConfig,\n isLivePreviewEnabled,\n}: {\n collectionConfig?: CollectionConfig\n config: SanitizedConfig\n globalConfig?: GlobalConfig\n isLivePreviewEnabled: boolean\n}) => ({\n ...(isLivePreviewEnabled ? config.admin.livePreview : {}),\n ...(collectionConfig?.admin?.livePreview || {}),\n ...(globalConfig?.admin?.livePreview || {}),\n})\n\n/**\n * Multi-level check to determine whether live preview is enabled on a collection or global.\n * For example, live preview can be enabled at both the root config level, or on the entity's config.\n * If a collectionConfig/globalConfig is provided, checks if it is enabled at the root level,\n * or on the entity's own config.\n */\nexport const isLivePreviewEnabled = ({\n collectionConfig,\n config,\n globalConfig,\n}: {\n collectionConfig?: CollectionConfig\n config: SanitizedConfig\n globalConfig?: GlobalConfig\n}): boolean => {\n if (globalConfig) {\n return Boolean(\n config.admin?.livePreview?.globals?.includes(globalConfig.slug) ||\n globalConfig.admin?.livePreview,\n )\n }\n\n if (collectionConfig) {\n return Boolean(\n config.admin?.livePreview?.collections?.includes(collectionConfig.slug) ||\n collectionConfig.admin?.livePreview,\n )\n }\n}\n\n/**\n * 1. Looks up the relevant live preview config, which could have been enabled:\n * a. At the root level, e.g. `collections: ['posts']`\n * b. On the collection or global config, e.g. `admin: { livePreview: { ... } }`\n * 2. Determines if live preview is enabled, and if not, early returns.\n * 3. Merges the config with the root config, if necessary.\n * 4. Executes the `url` function, if necessary.\n *\n * Notice: internal function only. Subject to change at any time. Use at your own risk.\n */\nexport const handleLivePreview = async ({\n collectionSlug,\n config,\n data,\n globalSlug,\n operation,\n req,\n}: {\n collectionSlug?: string\n config: SanitizedConfig\n data: Record<string, unknown>\n globalSlug?: string\n operation?: Operation\n req: PayloadRequest\n}): Promise<{\n isLivePreviewEnabled?: boolean\n livePreviewConfig?: LivePreviewConfig\n livePreviewURL?: LivePreviewURLType\n}> => {\n const collectionConfig = collectionSlug\n ? req.payload.collections[collectionSlug]?.config\n : undefined\n\n const globalConfig = globalSlug ? config.globals.find((g) => g.slug === globalSlug) : undefined\n\n const enabled = isLivePreviewEnabled({\n collectionConfig,\n config,\n globalConfig,\n })\n\n if (!enabled) {\n return {}\n }\n\n const livePreviewConfig = getLivePreviewConfig({\n collectionConfig,\n config,\n globalConfig,\n isLivePreviewEnabled: enabled,\n })\n\n let livePreviewURL: string | undefined\n\n if (typeof livePreviewConfig?.url === 'string') {\n livePreviewURL = livePreviewConfig.url\n }\n\n if (typeof livePreviewConfig?.url === 'function' && operation !== 'create') {\n try {\n const result = await livePreviewConfig.url({\n collectionConfig,\n data,\n globalConfig,\n locale: { code: req.locale, label: '' },\n payload: req.payload,\n req,\n })\n\n if (typeof result === 'string') {\n livePreviewURL = result\n }\n } catch (err) {\n req.payload.logger.error({\n err,\n msg: `There was an error executing the live preview URL function for ${collectionSlug || globalSlug}`,\n })\n }\n }\n\n return { isLivePreviewEnabled: enabled, livePreviewConfig, livePreviewURL }\n}\n"],"mappings":"AAUA,OAAO,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