UNPKG

@zapal/payload-lexical-react

Version:

Payload CMS rich text Lexical editor to React JSX renderer

81 lines (80 loc) 4.21 kB
import { jsx as u, Fragment as o } from "react/jsx-runtime"; import { Fragment as s } from "react"; const b = 1, I = 2, k = 4, S = 8, T = 16, E = 32, _ = 64, w = 128, c = ({ 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; }, d = { heading: (r) => { const i = r.tag; return /* @__PURE__ */ u(i, { style: c(r), children: r.children }); }, list: (r) => { const i = r.tag; return /* @__PURE__ */ u(i, { style: c(r), children: r.children }); }, listItem: (r) => /* @__PURE__ */ u("li", { style: c(r), children: r.children }), paragraph: (r) => /* @__PURE__ */ u("p", { style: c(r), children: r.children }), link: (r) => /* @__PURE__ */ u("a", { href: r.fields.url, target: r.fields.newTab ? "_blank" : "_self", style: c(r), children: r.children }), autolink: (r) => /* @__PURE__ */ u("a", { href: r.fields.url, target: r.fields.newTab ? "_blank" : "_self", style: c(r), children: r.children }), quote: (r) => /* @__PURE__ */ u("blockquote", { style: c(r), children: r.children }), lineBreak: () => /* @__PURE__ */ u("br", {}), tab: () => /* @__PURE__ */ u("br", {}), upload: (r) => { var i; return typeof r.value == "object" && r.value.url && ((i = r.value.mimeType) != null && i.includes("image")) ? /* @__PURE__ */ u("img", { src: r.value.url, alt: r.value.alt }) : null; } }, v = ({ 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 ? /* @__PURE__ */ u("code", { children: r }) : i.highlight ? /* @__PURE__ */ u("mark", { style: a, children: r }) : i.subscript ? /* @__PURE__ */ u("sub", { style: a, children: r }) : i.superscript ? /* @__PURE__ */ u("sup", { style: a, children: r }) : Object.keys(a) ? /* @__PURE__ */ u("span", { style: a, children: r }) : /* @__PURE__ */ u(o, { children: r }); }; function H({ content: r, elements: i = d, mark: a = v, blocks: y = {} }) { const f = (t, l) => { if (!i) throw new Error("'elements' prop not provided."); if (t.type === "link" && t.fields) return i.link({ ...t, children: l }); if (t.type === "autolink" && t.fields) return i.autolink({ ...t, children: l }); if (t.type === "heading") return i.heading({ ...t, children: l }); if (t.type === "paragraph") return i.paragraph({ ...t, children: l }); if (t.type === "list") return i.list({ ...t, children: l }); if (t.type === "listitem") return i.listItem({ ...t, children: l }); if (t.type === "quote") return i.quote({ ...t, children: l }); 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}'`); }, g = (t) => { if (!a) throw new Error("'mark' prop not provided."); return t.format ? a({ text: t.text, bold: (t.format & b) > 0, italic: (t.format & I) > 0, underline: (t.format & S) > 0, strikethrough: (t.format & k) > 0, code: (t.format & T) > 0, subscript: (t.format & E) > 0, superscript: (t.format & _) > 0, highlight: (t.format & w) > 0 }) : a({ text: t.text }); }, n = (t) => !t || !Array.isArray(t) ? null : t.map((l, e) => { if (l.type === "text") return /* @__PURE__ */ u(s, { children: g(l) }, e); if (l.type === "block") { const p = l, h = y[p.fields.blockType]; if (typeof h != "function") throw new Error(`Missing block renderer for block type '${p.fields.blockType}'`); return /* @__PURE__ */ u(s, { children: h(l) }, e); } return l.type === "linebreak" || l.type === "tab" || l.type === "upload" ? /* @__PURE__ */ u(s, { children: f(l) }, e) : /* @__PURE__ */ u(s, { children: f(l, n((l == null ? void 0 : l.children) || [])) }, e); }); return /* @__PURE__ */ u(o, { children: n(r.root.children) }); } export { H as PayloadLexicalReact, d as defaultElements, v as defaultMark };