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: Improve the Select/Navigation mode and keep it persistent #65055

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 3 additions & 7 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,6 @@ function BlockListBlockProvider( props ) {
hasBlockMovingClientId,
canInsertBlockType,
__unstableHasActiveBlockOverlayActive,
__unstableGetEditorMode,
getSelectedBlocksInitialCaretPosition,
} = unlock( select( blockEditorStore ) );
const blockWithoutAttributes =
Expand Down Expand Up @@ -678,12 +677,9 @@ function BlockListBlockProvider( props ) {
hasOverlay:
__unstableHasActiveBlockOverlayActive( clientId ) &&
! isDragging(),
initialPosition:
_isSelected &&
( __unstableGetEditorMode() === 'edit' ||
__unstableGetEditorMode() === 'zoom-out' ) // Don't recalculate the initialPosition when toggling in/out of zoom-out mode
? getSelectedBlocksInitialCaretPosition()
: undefined,
initialPosition: _isSelected
? getSelectedBlocksInitialCaretPosition()
: undefined,
isHighlighted: isBlockHighlighted( clientId ),
isMultiSelected,
isPartiallySelected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ export function useNavModeExit( clientId ) {
function onMouseDown( event ) {
// Don't select a block if it's already handled by a child
// block.
if ( isNavigationMode() && ! event.defaultPrevented ) {
// Prevent focus from moving to the block.
event.preventDefault();
event.stopPropagation();

// When clicking on a selected block, exit navigation mode.
if ( isBlockSelected( clientId ) ) {
setNavigationMode( false );
} else {
if ( isNavigationMode() && ! event.defaultPrevented ) {
if ( ! isBlockSelected( clientId ) ) {
// Prevent focus from moving to the block.
event.preventDefault();
selectBlock( clientId );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import clsx from 'clsx';
import { dragHandle } from '@wordpress/icons';
import { Button, Flex, FlexItem } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { forwardRef, useEffect } from '@wordpress/element';
import { forwardRef, useEffect, useRef } from '@wordpress/element';
import {
BACKSPACE,
DELETE,
Expand Down Expand Up @@ -90,15 +90,20 @@ function BlockSelectionButton( { clientId, rootClientId }, ref ) {
[ clientId, rootClientId ]
);
const { label, icon, blockMovingMode, editorMode, canMove } = selected;
const { setNavigationMode, removeBlock } = useDispatch( blockEditorStore );
const { removeBlock } = useDispatch( blockEditorStore );

// Focus the breadcrumb in navigation mode.
const labelRef = useRef( label );
useEffect( () => {
labelRef.current = label;
}, [ label ] );
useEffect( () => {
if ( editorMode === 'navigation' ) {
ref.current.focus();
speak( label );
speak( labelRef.current );
}
}, [ label, editorMode ] );
}, [ editorMode ] );

const blockElement = useBlockElement( clientId );

const {
Expand Down Expand Up @@ -279,7 +284,7 @@ function BlockSelectionButton( { clientId, rootClientId }, ref ) {
ref={ ref }
onClick={
editorMode === 'navigation'
? () => setNavigationMode( false )
? () => blockElement?.focus()
: undefined
}
onKeyDown={ onKeyDown }
Expand Down
5 changes: 0 additions & 5 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1797,11 +1797,6 @@ export const blockListSettings = ( state = {}, action ) => {
* @return {string} Updated state.
*/
export function editorMode( state = 'edit', action ) {
// Let inserting block in navigation mode always trigger Edit mode.
if ( action.type === 'INSERT_BLOCKS' && state === 'navigation' ) {
return 'edit';
}

if ( action.type === 'SET_EDITOR_MODE' ) {
return action.mode;
}
Expand Down
Loading