Skip to content

Commit

Permalink
Insert block when use enter in disabled rich text
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Feb 26, 2024
1 parent 6554533 commit 1bad234
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions packages/block-editor/src/components/rich-text/use-enter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import { useRef } from '@wordpress/element';
import { useRefEffect } from '@wordpress/compose';
import { ENTER } from '@wordpress/keycodes';
import { insert, remove } from '@wordpress/rich-text';
import { getBlockTransforms, findTransform } from '@wordpress/blocks';
import { useDispatch, useRegistry } from '@wordpress/data';
import {
createBlock,
getBlockTransforms,
findTransform,
} from '@wordpress/blocks';
import { useDispatch, useRegistry, useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -16,20 +20,33 @@ import { splitValue } from './split-value';

export function useEnter( props ) {
const registry = useRegistry();
const { __unstableMarkAutomaticChange } = useDispatch( blockEditorStore );
const { insertBlock, __unstableMarkAutomaticChange } =
useDispatch( blockEditorStore );
const {
getBlockIndex,
getBlockName,
getBlockRootClientId,
getSelectedBlockClientId,
} = useSelect( blockEditorStore );
const propsRef = useRef( props );
propsRef.current = props;
return useRefEffect( ( element ) => {
function onKeyDown( event ) {
if ( event.target.contentEditable !== 'true' ) {
if ( event.defaultPrevented ) {
return;
}

if ( event.defaultPrevented ) {
if ( event.keyCode !== ENTER ) {
return;
}

if ( event.keyCode !== ENTER ) {
if ( event.target.contentEditable !== 'true' ) {
const clientId = getSelectedBlockClientId();
insertBlock(
createBlock( getBlockName( clientId ), {} ),
getBlockIndex( clientId ) + 1,
getBlockRootClientId( clientId )
);
return;
}

Expand Down

0 comments on commit 1bad234

Please sign in to comment.