UNPKG

@gechiui/block-editor

Version:
29 lines (26 loc) 741 B
/** * GeChiUI dependencies */ import { useEffect } from '@gechiui/element'; import { useDispatch, useSelect } from '@gechiui/data'; /** * Internal dependencies */ import { store as blockEditorStore } from '../../store'; export function useCaretInFormat( { value } ) { const hasActiveFormats = value.activeFormats && !! value.activeFormats.length; const { isCaretWithinFormattedText } = useSelect( blockEditorStore ); const { enterFormattedText, exitFormattedText } = useDispatch( blockEditorStore ); useEffect( () => { if ( hasActiveFormats ) { if ( ! isCaretWithinFormattedText() ) { enterFormattedText(); } } else if ( isCaretWithinFormattedText() ) { exitFormattedText(); } }, [ hasActiveFormats ] ); }