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