import { __ } from '@wordpress/i18n'; import { useBlockProps, store as blockEditorStore, } from '@wordpress/block-editor'; import { Popover as WCPopover } from '@wordpress/components'; import { // eslint-disable-next-line @wordpress/use-recommended-components ValidatedTextareaControl, Link, } from '@wordpress/ui'; import type { BlockEditProps } from '@wordpress/blocks'; import { useState, useEffect } from '@wordpress/element'; import { useDispatch } from '@wordpress/data'; import { useEvent } from '@wordpress/compose'; import type { LatexToMathML, MathAttributes } from './types'; export default function MathEdit( { attributes, setAttributes, isSelected, }: BlockEditProps< MathAttributes > ) { const { latex, mathML } = attributes; const [ blockRef, setBlockRef ] = useState< HTMLElement | null >(); const [ error, setError ] = useState< string | null >( null ); const [ latexToMathML, setLatexToMathML ] = useState< LatexToMathML | undefined >(); const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); // Re-render once the converter loads, so MathML saved from a corrupted // source is repaired and a source typed before the converter loaded is // rendered. The converter loads asynchronously, and the user can type in // the meantime, so read the source at that time rather than on mount. const renderLatest = useEvent( ( convert: LatexToMathML ) => { if ( ! latex ) { return; } try { const newMathML = convert( latex, { displayMode: true } ); __unstableMarkNextChangeAsNotPersistent(); setAttributes( { mathML: newMathML } ); } catch ( err ) { setError( ( err as Error ).message ); } } ); useEffect( () => { import( '@wordpress/latex-to-mathml' ).then( ( module ) => { setLatexToMathML( () => module.default ); renderLatest( module.default ); } ); }, [ renderLatest ] ); const blockProps = useBlockProps( { ref: setBlockRef, position: 'relative', } ); return (