@zapal/payload-lexical-react
Version:
Payload CMS rich text Lexical editor to React JSX renderer
2 lines (1 loc) • 3.42 kB
JavaScript
(function(c,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],l):(c=typeof globalThis<"u"?globalThis:c||self,l(c["payload-lexical-react"]={},c.jsxRuntime,c.React))})(this,function(c,l,s){"use strict";const f=({indent:r,format:i})=>{const a={};return r&&r>0&&(a.marginInlineStart=`${r*1.5}rem`),(i==="right"||i==="center"||i==="justify")&&(a.textAlign=i),a},p={heading:r=>{const i=r.tag;return l.jsx(i,{style:f(r),children:r.children})},list:r=>{const i=r.tag;return l.jsx(i,{style:f(r),children:r.children})},listItem:r=>l.jsx("li",{style:f(r),children:r.children}),paragraph:r=>l.jsx("p",{style:f(r),children:r.children}),link:r=>l.jsx("a",{href:r.fields.url,target:r.fields.newTab?"_blank":"_self",style:f(r),children:r.children}),autolink:r=>l.jsx("a",{href:r.fields.url,target:r.fields.newTab?"_blank":"_self",style:f(r),children:r.children}),quote:r=>l.jsx("blockquote",{style:f(r),children:r.children}),lineBreak:()=>l.jsx("br",{}),tab:()=>l.jsx("br",{}),upload:r=>{var i;return typeof r.value=="object"&&r.value.url&&((i=r.value.mimeType)!=null&&i.includes("image"))?l.jsx("img",{src:r.value.url,alt:r.value.alt}):null}},n=({text:r,...i})=>{const a={};return i.bold&&(a.fontWeight="bold"),i.italic&&(a.fontStyle="italic"),i.underline&&(a.textDecoration="underline"),i.strikethrough&&(a.textDecoration="line-through"),i.code?l.jsx("code",{children:r}):i.highlight?l.jsx("mark",{style:a,children:r}):i.subscript?l.jsx("sub",{style:a,children:r}):i.superscript?l.jsx("sup",{style:a,children:r}):Object.keys(a)?l.jsx("span",{style:a,children:r}):l.jsx(l.Fragment,{children:r})};function g({content:r,elements:i=p,mark:a=n,blocks:S={}}){const o=(t,e)=>{if(!i)throw new Error("'elements' prop not provided.");if(t.type==="link"&&t.fields)return i.link({...t,children:e});if(t.type==="autolink"&&t.fields)return i.autolink({...t,children:e});if(t.type==="heading")return i.heading({...t,children:e});if(t.type==="paragraph")return i.paragraph({...t,children:e});if(t.type==="list")return i.list({...t,children:e});if(t.type==="listitem")return i.listItem({...t,children:e});if(t.type==="quote")return i.quote({...t,children:e});if(t.type==="linebreak")return i.lineBreak();if(t.type==="tab")return i.tab(t);if(t.type==="upload")return i.upload(t);throw new Error(`Missing element renderer for node type '${t.type}'`)},b=t=>{if(!a)throw new Error("'mark' prop not provided.");return t.format?a({text:t.text,bold:(t.format&1)>0,italic:(t.format&2)>0,underline:(t.format&8)>0,strikethrough:(t.format&4)>0,code:(t.format&16)>0,subscript:(t.format&32)>0,superscript:(t.format&64)>0,highlight:(t.format&128)>0}):a({text:t.text})},h=t=>!t||!Array.isArray(t)?null:t.map((e,u)=>{if(e.type==="text")return l.jsx(s.Fragment,{children:b(e)},u);if(e.type==="block"){const y=e,I=S[y.fields.blockType];if(typeof I!="function")throw new Error(`Missing block renderer for block type '${y.fields.blockType}'`);return l.jsx(s.Fragment,{children:I(e)},u)}return e.type==="linebreak"||e.type==="tab"||e.type==="upload"?l.jsx(s.Fragment,{children:o(e)},u):l.jsx(s.Fragment,{children:o(e,h((e==null?void 0:e.children)||[]))},u)});return l.jsx(l.Fragment,{children:h(r.root.children)})}c.PayloadLexicalReact=g,c.defaultElements=p,c.defaultMark=n,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});