UNPKG

@rtdui/editor

Version:

React rich text editor based on tiptap

76 lines (73 loc) 2.64 kB
const require_runtime = require('../../../_virtual/_rolldown/runtime.cjs'); let _tiptap_core = require("@tiptap/core"); let katex = require("katex"); katex = require_runtime.__toESM(katex); let prosemirror_state = require("prosemirror-state"); let prosemirror_view = require("prosemirror-view"); //#region packages/editor/src/RichTextEditor/tiptap_extensions/extension-math/math-katex-inline-plugin.ts function getDecorations({ editor, doc, name, katexOptions, selection }) { const decorations = []; const texts = []; (0, _tiptap_core.findChildren)(doc, (node) => node.type.name === "paragraph").forEach((block) => { const pos = block.pos + 1; texts.push({ pos, text: block.node.textContent }); }); texts.forEach((txt) => { if (txt.text) { const inlineMatchs = txt.text.matchAll(/\$([^$]+)\$/g); for (const match of inlineMatchs) { if (!match[1]) continue; match[1] = match[1].replace(/\s\\\s/g, " \\\\ "); const mathStart = txt.pos + (match.index ?? 0); const mathEnd = txt.pos + (match.index ?? 0) + match[0].length; if (selection.$anchor.pos < mathStart || selection.$anchor.pos > mathEnd || !editor.isEditable) { const mathRender = document.createElement("span"); mathRender.classList.add("tiptap-math-render"); katex.default.render(match[1], mathRender, { output: "html" }); const decoration = prosemirror_view.Decoration.widget(mathStart, mathRender); decorations.push(decoration); const decoration2 = prosemirror_view.Decoration.inline(mathStart, mathEnd, { class: "titap-math-source hidden" }); decorations.push(decoration2); } else if (editor.isEditable) { const decoration3 = prosemirror_view.Decoration.inline(mathStart, mathEnd, { class: "titap-math-source" }); decorations.push(decoration3); } } } }); return prosemirror_view.DecorationSet.create(doc, decorations); } /** 处理粘贴的数学公式 */ function KatexInlinePlugin({ editor, name, katexOptions }) { const katexInlinePlugin = new prosemirror_state.Plugin({ key: new prosemirror_state.PluginKey("mathKatex"), state: { init: (_, { doc, selection }) => getDecorations({ editor, doc, name, katexOptions, selection }), apply: (transaction, decorationSet, oldState, newState) => { const { selection } = newState; return getDecorations({ editor, doc: transaction.doc, name, katexOptions, selection }); } }, props: { decorations(state) { return katexInlinePlugin.getState(state); } } }); return katexInlinePlugin; } //#endregion exports.KatexInlinePlugin = KatexInlinePlugin;