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

Block editor: fix focus handler in Safari #31103

Merged
merged 2 commits into from
Apr 23, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import { useBlockClassNames } from './use-block-class-names';
import { useBlockDefaultClassName } from './use-block-default-class-name';
import { useBlockCustomClassName } from './use-block-custom-class-name';
import { useBlockMovingModeClassNames } from './use-block-moving-mode-class-names';
import { useEventHandlers } from './use-event-handlers';
import { useFocusHandler } from './use-focus-handler';
import { useEventHandlers } from './use-selected-block-event-handlers';
import { useNavModeExit } from './use-nav-mode-exit';
import { useBlockNodes } from './use-block-nodes';
import { useScrollIntoView } from './use-scroll-into-view';
Expand Down Expand Up @@ -106,6 +107,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
// Must happen after focus because we check for focus in the block.
useScrollIntoView( clientId ),
useBlockNodes( clientId ),
useFocusHandler( clientId ),
useEventHandlers( clientId ),
useNavModeExit( clientId ),
useIsHovered(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useRefEffect } from '@wordpress/compose';

/**
* Internal dependencies
*/
import { isInsideRootBlock } from '../../../utils/dom';
import { store as blockEditorStore } from '../../../store';

/**
* Selects the block if it receives focus.
*
* @param {string} clientId Block client ID.
*/
export function useFocusHandler( clientId ) {
const { isBlockSelected } = useSelect( blockEditorStore );
const { selectBlock } = useDispatch( blockEditorStore );

return useRefEffect(
( node ) => {
/**
* Marks the block as selected when focused and not already
* selected. This specifically handles the case where block does not
* set focus on its own (via `setFocus`), typically if there is no
* focusable input in the block.
*
* @param {FocusEvent} event Focus event.
*/
function onFocus( event ) {
// Check synchronously because a non-selected block might be
// getting data through `useSelect` asynchronously.
if ( isBlockSelected( clientId ) ) {
return;
}

// If an inner block is focussed, that block is resposible for
// setting the selected block.
if ( ! isInsideRootBlock( node, event.target ) ) {
return;
}

selectBlock( clientId );
}

node.addEventListener( 'focusin', onFocus );

return () => {
node.removeEventListener( 'focusin', onFocus );
};
},
[ isBlockSelected, selectBlock ]
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import { useRefEffect } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { isInsideRootBlock } from '../../../utils/dom';
import { SelectionStart } from '../../writing-flow';
import { store as blockEditorStore } from '../../../store';

/**
* Adds block behaviour:
* - Selects the block if it receives focus.
* - Removes the block on BACKSPACE.
* - Inserts a default block on ENTER.
* - Initiates selection start for multi-selection.
Expand All @@ -26,52 +24,19 @@ import { store as blockEditorStore } from '../../../store';
*/
export function useEventHandlers( clientId ) {
const onSelectionStart = useContext( SelectionStart );
const { isSelected, rootClientId, index } = useSelect(
( select ) => {
const {
isBlockSelected,
getBlockRootClientId,
getBlockIndex,
} = select( blockEditorStore );

return {
isSelected: isBlockSelected( clientId ),
rootClientId: getBlockRootClientId( clientId ),
index: getBlockIndex( clientId ),
};
},
const isSelected = useSelect(
( select ) => select( blockEditorStore ).isBlockSelected( clientId ),
[ clientId ]
);
const { insertDefaultBlock, removeBlock, selectBlock } = useDispatch(
const { getBlockRootClientId, getBlockIndex } = useSelect(
blockEditorStore
);
const { insertDefaultBlock, removeBlock } = useDispatch( blockEditorStore );

return useRefEffect(
( node ) => {
if ( ! isSelected ) {
/**
* Marks the block as selected when focused and not already
* selected. This specifically handles the case where block does not
* set focus on its own (via `setFocus`), typically if there is no
* focusable input in the block.
*
* @param {FocusEvent} event Focus event.
*/
function onFocus( event ) {
// If an inner block is focussed, that block is resposible for
// setting the selected block.
if ( ! isInsideRootBlock( node, event.target ) ) {
return;
}

selectBlock( clientId );
}

node.addEventListener( 'focusin', onFocus );

return () => {
node.removeEventListener( 'focusin', onFocus );
};
return;
}

/**
Expand Down Expand Up @@ -101,7 +66,11 @@ export function useEventHandlers( clientId ) {
event.preventDefault();

if ( keyCode === ENTER ) {
insertDefaultBlock( {}, rootClientId, index + 1 );
insertDefaultBlock(
{},
getBlockRootClientId( clientId ),
getBlockIndex( clientId ) + 1
);
} else {
removeBlock( clientId );
}
Expand Down Expand Up @@ -136,13 +105,13 @@ export function useEventHandlers( clientId ) {
};
},
[
clientId,
isSelected,
rootClientId,
index,
getBlockRootClientId,
getBlockIndex,
onSelectionStart,
insertDefaultBlock,
removeBlock,
selectBlock,
]
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ function BlockNavigationDropdown(
isEnabled={ isEnabled }
/>
) }
renderContent={ ( { onClose } ) => (
renderContent={ () => (
<BlockNavigation
onSelect={ onClose }
__experimentalFeatures={ __experimentalFeatures }
/>
) }
Expand Down
1 change: 1 addition & 0 deletions packages/e2e-tests/specs/editor/blocks/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ describe( 'List', () => {
await clickBlockAppender();
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '* ' );
await page.evaluate( () => new Promise( window.requestIdleCallback ) );
Copy link
Member Author

Choose a reason for hiding this comment

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

@youknowriad I found the issue I think. Feel free to include in your PR. I don't mind which PR we go with. We need to wait for this:

MARK_AUTOMATIC_CHANGE_FINAL_CONTROL: createRegistryControl(
( registry ) => () => {
const {
requestIdleCallback = ( callback ) =>
setTimeout( callback, 100 ),
} = window;
requestIdleCallback( () =>
registry
.dispatch( blockEditorStore )
.__unstableMarkAutomaticChangeFinal()
);
}
),

await page.keyboard.press( 'ArrowUp' );
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.press( 'Backspace' );
Expand Down