@wordpress/format-library
Version:
Format library for the WordPress editor.
154 lines (153 loc) • 3.94 kB
JavaScript
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { __ } from "@wordpress/i18n";
import { useState, useEffect } from "@wordpress/element";
import { insertObject, useAnchor } from "@wordpress/rich-text";
import { RichTextToolbarButton } from "@wordpress/block-editor";
import {
Popover,
TextControl,
__experimentalVStack as VStack,
privateApis as componentsPrivateApis
} from "@wordpress/components";
import { math as icon } from "@wordpress/icons";
import { unlock } from "../lock-unlock";
const { Badge } = unlock(componentsPrivateApis);
const name = "core/math";
const title = __("Math");
function InlineUI({
value,
onChange,
activeAttributes,
contentRef,
latexToMathML
}) {
const [latex, setLatex] = useState(
activeAttributes?.["data-latex"] || ""
);
const [error, setError] = useState(null);
const popoverAnchor = useAnchor({
editableContentElement: contentRef.current,
settings: math
});
const handleLatexChange = (newLatex) => {
let mathML;
setLatex(newLatex);
try {
mathML = latexToMathML(newLatex, { displayMode: false });
setError(null);
} catch (err) {
setError(err.message);
return;
}
const newReplacements = value.replacements.slice();
newReplacements[value.start] = {
type: name,
attributes: {
"data-latex": newLatex
},
innerHTML: mathML
};
onChange({
...value,
replacements: newReplacements
});
};
return /* @__PURE__ */ jsx(
Popover,
{
placement: "bottom-start",
offset: 8,
focusOnMount: false,
anchor: popoverAnchor,
className: "block-editor-format-toolbar__math-popover",
children: /* @__PURE__ */ jsx("div", { style: { minWidth: "300px", padding: "4px" }, children: /* @__PURE__ */ jsxs(VStack, { spacing: 1, children: [
/* @__PURE__ */ jsx(
TextControl,
{
__nextHasNoMarginBottom: true,
__next40pxDefaultSize: true,
hideLabelFromVision: true,
label: __("LaTeX math syntax"),
value: latex,
onChange: handleLatexChange,
placeholder: __("e.g., x^2, \\frac{a}{b}"),
autoComplete: "off"
}
),
error && /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(
Badge,
{
intent: "error",
className: "wp-block-math__error",
children: error
}
),
/* @__PURE__ */ jsx("style", { children: ".wp-block-math__error .components-badge__content{white-space:normal}" })
] })
] }) })
}
);
}
function Edit({
value,
onChange,
onFocus,
isObjectActive,
activeObjectAttributes,
contentRef
}) {
const [latexToMathML, setLatexToMathML] = useState();
useEffect(() => {
import("@wordpress/latex-to-mathml").then((module) => {
setLatexToMathML(() => module.default);
});
}, []);
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(
RichTextToolbarButton,
{
icon,
title,
onClick: () => {
const newValue = insertObject(value, {
type: name,
attributes: {
"data-latex": ""
},
innerHTML: ""
});
newValue.start = newValue.end - 1;
onChange(newValue);
onFocus();
},
isActive: isObjectActive
}
),
isObjectActive && /* @__PURE__ */ jsx(
InlineUI,
{
value,
onChange,
activeAttributes: activeObjectAttributes,
contentRef,
latexToMathML
}
)
] });
}
const math = {
name,
title,
tagName: "math",
className: null,
attributes: {
"data-latex": "data-latex"
},
contentEditable: false,
edit: Edit
};
export {
math
};
//# sourceMappingURL=index.js.map