Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote v2: implement exiting on Enter at end #39911

Merged
merged 6 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import {
import { createBlock } from '@wordpress/blocks';
import { formatLtr } from '@wordpress/icons';

/**
* Internal dependencies
*/
import { useOnEnter } from './use-enter';

const name = 'core/paragraph';

function ParagraphRTLControl( { direction, setDirection } ) {
Expand Down Expand Up @@ -57,6 +62,7 @@ function ParagraphBlock( {
const { align, content, direction, dropCap, placeholder } = attributes;
const isDropCapFeatureEnabled = useSetting( 'typography.dropCap' );
const blockProps = useBlockProps( {
ref: useOnEnter( { clientId, content } ),
className: classnames( {
'has-drop-cap': dropCap,
[ `has-text-align-${ align }` ]: align,
Expand Down
103 changes: 103 additions & 0 deletions packages/block-library/src/paragraph/use-enter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
import { useRefEffect } from '@wordpress/compose';
import { ENTER } from '@wordpress/keycodes';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { hasBlockSupport, createBlock } from '@wordpress/blocks';

export function useOnEnter( props ) {
const { batch } = useRegistry();
const {
moveBlocksToPosition,
replaceInnerBlocks,
duplicateBlocks,
insertBlock,
} = useDispatch( blockEditorStore );
const {
getBlockRootClientId,
getBlockIndex,
getBlockOrder,
getBlockName,
getBlock,
getNextBlockClientId,
} = useSelect( blockEditorStore );
const propsRef = useRef( props );
propsRef.current = props;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we should make a useFreshRef utility hook in compose.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should be wrapped in a useEffect, it seems weird to assign current in render

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken this looks like a "Latest Ref Pattern":

function useLatestRef(value) {
  const ref = useRef(value);
  useLayoutEffect(() => {
    ref.current = value;
  });
  return ref;
}

return useRefEffect( ( element ) => {
function onKeyDown( event ) {
if ( event.defaultPrevented ) {
return;
}

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

const { content, clientId } = propsRef.current;

// The paragraph should be empty.
if ( content.length ) {
return;
}

const wrapperClientId = getBlockRootClientId( clientId );

if (
! hasBlockSupport(
getBlockName( wrapperClientId ),
'__experimentalOnEnter',
false
)
) {
return;
}

const order = getBlockOrder( wrapperClientId );

event.preventDefault();

const position = order.indexOf( clientId );

// If it is the last block, exit.
if ( position === order.length - 1 ) {
moveBlocksToPosition(
[ clientId ],
wrapperClientId,
getBlockRootClientId( wrapperClientId ),
getBlockIndex( wrapperClientId ) + 1
);
return;
}

// If it is in the middle, split the block in two.
const wrapperBlock = getBlock( wrapperClientId );
batch( () => {
duplicateBlocks( [ wrapperClientId ] );
const blockIndex = getBlockIndex( wrapperClientId );

replaceInnerBlocks(
wrapperClientId,
wrapperBlock.innerBlocks.slice( 0, position )
);
replaceInnerBlocks(
getNextBlockClientId( wrapperClientId ),
wrapperBlock.innerBlocks.slice( position + 1 )
);
insertBlock(
createBlock( 'core/paragraph' ),
blockIndex + 1,
getBlockRootClientId( wrapperClientId ),
true
);
} );
}

element.addEventListener( 'keydown', onKeyDown );
return () => {
element.removeEventListener( 'keydown', onKeyDown );
};
}, [] );
}
1 change: 1 addition & 0 deletions packages/block-library/src/quote/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"supports": {
"anchor": true,
"__experimentalSlashInserter": true,
"__experimentalOnEnter": true,
"typography": {
"fontSize": true,
"lineHeight": true,
Expand Down