UNPKG

@wordpress/block-library

Version:
105 lines (104 loc) • 3.85 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_components = require("@wordpress/components"); let _wordpress_ui = require("@wordpress/ui"); let _wordpress_element = require("@wordpress/element"); let _wordpress_data = require("@wordpress/data"); let _wordpress_compose = require("@wordpress/compose"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/math/edit.jsx function MathEdit({ attributes, setAttributes, isSelected }) { const { latex, mathML } = attributes; const [blockRef, setBlockRef] = (0, _wordpress_element.useState)(); const [error, setError] = (0, _wordpress_element.useState)(null); const [latexToMathML, setLatexToMathML] = (0, _wordpress_element.useState)(); const { __unstableMarkNextChangeAsNotPersistent } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); const renderLatest = (0, _wordpress_compose.useEvent)((convert) => { if (!latex) return; try { const newMathML = convert(latex, { displayMode: true }); __unstableMarkNextChangeAsNotPersistent(); setAttributes({ mathML: newMathML }); } catch (err) { setError(err.message); } }); (0, _wordpress_element.useEffect)(() => { import("@wordpress/latex-to-mathml").then((module) => { setLatexToMathML(() => module.default); renderLatest(module.default); }); }, [renderLatest]); const blockProps = (0, _wordpress_block_editor.useBlockProps)({ ref: setBlockRef, position: "relative" }); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { ...blockProps, children: [mathML ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("math", { display: "block", dangerouslySetInnerHTML: { __html: mathML } }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("math", { display: "block", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("semantics", { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("annotation", { encoding: "application/x-tex", children: latex || "​" }) }) }), isSelected && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Popover, { placement: "bottom-start", offset: 8, anchor: blockRef, focusOnMount: false, __unstableSlotName: "__unstable-block-tools-after", children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("form", { style: { padding: "16px", minWidth: "300px" }, onSubmit: (event) => event.preventDefault(), children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.ValidatedTextareaControl, { label: (0, _wordpress_i18n.__)("LaTeX math syntax"), value: latex ?? "", className: "wp-block-math__textarea-control", customValidity: error ? { type: "invalid", message: error } : void 0, onValueChange: (newLatex) => { if (!latexToMathML) { setAttributes({ latex: newLatex, mathML: "" }); return; } let newMathML = ""; try { newMathML = latexToMathML(newLatex, { displayMode: true }); setError(null); } catch (err) { setError(err.message); } setAttributes({ mathML: newMathML, latex: newLatex }); }, placeholder: (0, _wordpress_i18n.__)("e.g., x^2, \\frac{a}{b}, \\sqrt{x}") }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Link, { openInNewTab: true, className: "wp-block-math__learn-more", href: (0, _wordpress_i18n.__)("https://wordpress.org/documentation/article/math-block/"), children: (0, _wordpress_i18n.__)("Learn more about LaTeX syntax") })] }) })] }); } exports.default = MathEdit; //# sourceMappingURL=edit.cjs.map