payload-lexical-typography
Version:
PayloadCMS lexical-editor typography extension plugin
145 lines (140 loc) • 6.5 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/converters.ts
var converters_exports = {};
__export(converters_exports, {
TypographyHTMLConverters: () => TypographyHTMLConverters,
TypographyJSXConverters: () => TypographyJSXConverters
});
module.exports = __toCommonJS(converters_exports);
// src/converters/HTMLConverters/TextHTMLConverter.tsx
var import_lexical = require("@payloadcms/richtext-lexical/lexical");
var import_escape_html = __toESM(require("escape-html"));
var TextHTMLConverter = {
text: ({ node }) => {
let styles = "";
if (node.style) {
let match = /(?:^|;)\s?color: ([^;]+)/.exec(node.style);
if (match) {
styles = `${styles} color: ${match[1]};`;
}
match = /(?:^|;)\s?font-size: ([^;]+)/.exec(node.style);
if (match) {
styles = `${styles} font-size: ${match[1]};`;
}
match = /(?:^|;)\s?letter-spacing: ([^;]+)/.exec(node.style);
if (match) {
styles = `${styles} letter-spacing: ${match[1]};`;
}
match = /(?:^|;)\s?line-height: ([^;]+)/.exec(node.style);
if (match) {
styles = `${styles} line-height: ${match[1]};`;
}
match = /(?:^|;)\s?font-family: ([^;]+)/.exec(node.style);
if (match) {
styles = `${styles} font-family: ${match[1]};`;
}
}
const styleAttr = styles ? ` style="${styles}"` : "";
let html = (0, import_escape_html.default)(node.text);
if (!html) {
return "";
}
const formatters = {
[import_lexical.IS_BOLD]: (content) => `<strong>${content}</strong>`,
[import_lexical.IS_ITALIC]: (content) => `<em>${content}</em>`,
[import_lexical.IS_STRIKETHROUGH]: (content) => {
return `<span style="text-decoration: line-through">${content}</span>`;
},
[import_lexical.IS_UNDERLINE]: (content) => {
return `<span style="text-decoration: underline">${content}</span>`;
},
[import_lexical.IS_CODE]: (content) => `<code>${content}</code>`,
[import_lexical.IS_SUBSCRIPT]: (content) => `<sub>${content}</sub>`,
[import_lexical.IS_SUPERSCRIPT]: (content) => `<sup>${content}</sup>`
};
html = styles ? `<span${styleAttr}>${html}</span>` : html;
Object.entries(formatters).forEach(([formatFlag, formatter]) => {
if (node.format & Number(formatFlag)) {
html = formatter(html, styleAttr);
}
});
return html;
}
};
var TextHTMLConverterAsync = {
text: TextHTMLConverter.text
};
// src/converters/HTMLConverters/index.tsx
var TypographyHTMLConverters = { ...TextHTMLConverter };
var TypographyHTMLConvertersAsync = { ...TextHTMLConverterAsync };
// src/converters/JSXConverters/TextJSXConverter.tsx
var import_lexical2 = require("@payloadcms/richtext-lexical/lexical");
var import_jsx_runtime = require("react/jsx-runtime");
var TextJSXConverter = {
text: ({ node }) => {
const styles = {};
if (node.style) {
let match = /(?:^|;)\s?color: ([^;]+)/.exec(node.style);
if (match) styles.color = match[1];
match = /(?:^|;)\s?font-size: ([^;]+)/.exec(node.style);
if (match) styles.fontSize = match[1];
match = /(?:^|;)\s?letter-spacing: ([^;]+)/.exec(node.style);
if (match) styles.letterSpacing = match[1];
match = /(?:^|;)\s?line-height: ([^;]+)/.exec(node.style);
if (match) styles.lineHeight = match[1];
match = /(?:^|;)\s?font-family: ([^;]+)/.exec(node.style);
if (match) styles.fontFamily = match[1];
}
const formatters = {
[import_lexical2.IS_BOLD]: (el) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("strong", { children: el }),
[import_lexical2.IS_ITALIC]: (el) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("em", { children: el }),
[import_lexical2.IS_STRIKETHROUGH]: (el) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { style: { textDecoration: "line-through" }, children: el }),
[import_lexical2.IS_UNDERLINE]: (el) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { style: { textDecoration: "underline" }, children: el }),
[import_lexical2.IS_CODE]: (el) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("code", { children: el }),
[import_lexical2.IS_SUBSCRIPT]: (el) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("sub", { children: el }),
[import_lexical2.IS_SUPERSCRIPT]: (el) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("sup", { children: el })
};
let textElement = Object.keys(styles).length > 0 ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { style: styles, children: node.text }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: node.text });
Object.entries(formatters).forEach(([formatFlag, formatter]) => {
if (node.format & Number(formatFlag)) {
textElement = formatter(textElement);
}
});
return textElement;
}
};
// src/converters/JSXConverters/index.tsx
var TypographyJSXConverters = { ...TextJSXConverter };
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TypographyHTMLConverters,
TypographyJSXConverters
});
//# sourceMappingURL=converters.cjs.map
;