diff --git a/packages/editor/src/extensions/math/block.tsx b/packages/editor/src/extensions/math/block.tsx index dbeb62bba..2c1fd63c8 100644 --- a/packages/editor/src/extensions/math/block.tsx +++ b/packages/editor/src/extensions/math/block.tsx @@ -22,7 +22,7 @@ import { useEffect, useRef } from "react"; import { Button } from "../../components/button"; import { useTimer } from "../../hooks/use-timer"; import { SelectionBasedReactNodeViewProps } from "../react/types"; -import { MathBlock, MathBlockAttributes } from "./math-block"; +import { MathBlockAttributes } from "./math-block"; import { loadKatex } from "./plugin/renderers/katex"; import { useThemeEngineStore } from "@notesnook/theme"; import SimpleBar from "simplebar-react"; @@ -32,34 +32,37 @@ export function MathBlockComponent( props: SelectionBasedReactNodeViewProps ) { const { editor, node, forwardRef, getPos } = props; - const { indentLength, indentType, caretPosition } = node.attrs; + const { caretPosition } = node.attrs; - const isActive = editor.isActive(MathBlock.name); - const elementRef = useRef(); - const codeElementRef = useRef(); - const toolbarRef = useRef(null); + const pos = getPos(); + const { from, to } = editor.state.selection; + const isActive = from >= pos && to < pos + node.nodeSize; + const mathRendererRef = useRef(); + const mathInputElementRef = useRef(); const theme = useThemeEngineStore((store) => store.theme); const { enabled, start } = useTimer(1000); - console.log("Rerendering MathBlockComponent", isActive); useEffect(() => { - if (isActive) return; (async function () { const pos = getPos(); const node = editor.state.doc.nodeAt(pos); const text = node?.textContent; - if (text && elementRef.current) { - const katex = await loadKatex(); + if (mathRendererRef.current) { + if (text && text.trim()) { + const katex = await loadKatex(); - elementRef.current.innerHTML = katex.renderToString(text, { - displayMode: true, - globalGroup: true, - throwOnError: false - }); + mathRendererRef.current.innerHTML = katex.renderToString(text, { + displayMode: true, + globalGroup: true, + throwOnError: false + }); + } else { + mathRendererRef.current.innerHTML = ""; + } } })(); - }, [isActive]); + }, [isActive, node.textContent]); return ( <> @@ -68,7 +71,7 @@ export function MathBlockComponent( flexDirection: "column", borderRadius: "default", overflow: "hidden", - ...(isActive ? {} : { height: "1px", width: 0, visibility: "hidden" }) + display: isActive ? "flex" : "none" }} > { - codeElementRef.current = ref ?? undefined; + mathInputElementRef.current = ref ?? undefined; forwardRef?.(ref); }} autoCorrect="off" @@ -89,7 +92,6 @@ export function MathBlockComponent( pre: { fontFamily: "inherit !important", tabSize: "inherit !important", - // background: "transparent !important", padding: "10px !important", margin: "0px !important", width: "100%", @@ -105,13 +107,10 @@ export function MathBlockComponent( } }, fontFamily: "monospace", - whiteSpace: "pre", // TODO !important + whiteSpace: "pre", tabSize: 1, position: "relative", lineHeight: "20px", - // bg: "var(--background-secondary)", - // color: "white", - // overflowX: "hidden", display: "flex" }} spellCheck={false} @@ -119,7 +118,6 @@ export function MathBlockComponent( ) : null} - - -