import { memo, useEffect, useState } from "react"; import type { ExcalidrawProps } from "@excalidraw/excalidraw/types"; import type { ExcalidrawEmbeddableElement, NonDeleted, } from "@excalidraw/excalidraw/element/types"; import { getCodeBlockCustomData, isCodeBlockEmbeddable } from "../codeblock"; import { renderHighlightedCodeBlockHtml } from "../codeblockHighlight"; type CodeBlockEmbeddableProps = { element: NonDeleted; }; const CodeBlockEmbeddable = memo(function CodeBlockEmbeddable({ element, }: CodeBlockEmbeddableProps) { const customData = getCodeBlockCustomData(element); const [html, setHtml] = useState(null); useEffect(() => { if (!customData) { setHtml(null); return; } let cancelled = false; void renderHighlightedCodeBlockHtml(customData.code, customData.language) .then((nextHtml) => { if (!cancelled) { setHtml(nextHtml); } }) .catch(() => { if (!cancelled) { setHtml(null); } }); return () => { cancelled = true; }; }, [customData]); if (!customData) { return null; } return (
{html ? (
) : (
          {customData.code}
        
)}
); }); export const renderCodeBlockEmbeddable: NonNullable< ExcalidrawProps["renderEmbeddable"] > = (element) => { if (!isCodeBlockEmbeddable(element)) { return null; } return ; };