import { type EditorProps as DraftEditorProps, EditorState } from 'draft-js'; import { useCallback } from 'react'; import addNewBlockAt from '../utils/addNewBlockAt.js'; import getCurrentBlock from '../utils/getCurrentBlock.js'; type UseReturnHandlerRes = NonNullable; /** * Adds a new unstyled block if the user presses the return key. */ const useReturnHandler = ( onChange: (value: EditorState) => void, handler: UseReturnHandlerRes ): UseReturnHandlerRes => useCallback( (e, editorState) => { const currentBlock = getCurrentBlock(editorState); const currentBlockType = currentBlock.getType(); if ( currentBlockType.startsWith('atomic') || currentBlockType.startsWith('header') ) { const nextEditorState = addNewBlockAt( editorState, currentBlock.getKey() ); onChange(nextEditorState); return 'handled'; } return handler(e, editorState); }, [onChange, handler] ); export default useReturnHandler;