import { createContext , useContext , useEffect , useRef } from 'react' ; import { AnyExtension , ErrorConstant , invariant , isFunction , isPlainObject , RemirrorEventListener , } from '@remirror/core' ; import type { ReactFrameworkOutput } from '../react-types' ; import { useForceUpdate } from './use-force-update' ; /**
 * The `ReactContext` for the Remirror editor.
 */ export const RemirrorContext = createContext < ReactFrameworkOutput < any > | null > ( null ) ; remarks export function useRemirrorContext < Extension extends AnyExtension = Remirror . Extensions > ( handler ? ) { const context = useContext ( RemirrorContext ) as ReactFrameworkOutput < Extension > | null ; const forceUpdate = useRef ( useForceUpdate ( ) ) ; invariant ( context , { code : ErrorConstant . REACT_PROVIDER_CONTEXT } ) ; const { addHandler } = context ; useEffect ( ( ) => { let updateHandler = handler ; if ( ! updateHandler ) { return ; } if ( isPlainObject ( updateHandler ) ) { const { autoUpdate } = updateHandler ; updateHandler = autoUpdate ? ( ) => forceUpdate . current ( ) : undefined ; } if ( ! isFunction ( updateHandler ) ) { return ; } return addHandler ( 'updated' , updateHandler ) ; } , [ addHandler , handler ] ) ; return context ; }