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