Skip to content

Commit

Permalink
More things!
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed May 27, 2023
1 parent 9354a81 commit 4274a5e
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export function BlockSettingsDropdown( {
},
[ firstBlockClientId ]
);
const { getBlockOrder } = useSelect( blockEditorStore );
const { getBlockOrder, getSelectedBlockClientIds } =
useSelect( blockEditorStore );

const shortcuts = useSelect( ( select ) => {
const { getShortcutRepresentation } = select( keyboardShortcutsStore );
Expand All @@ -124,13 +125,14 @@ export function BlockSettingsDropdown( {

const { selectBlock, toggleBlockHighlight } =
useDispatch( blockEditorStore );
const hasSelectedBlocks = selectedBlockClientIds.length > 0;

const updateSelectionAfterDuplicate = useCallback(
async ( clientIdsPromise ) => {
if ( __experimentalSelectBlock ) {
const ids = await clientIdsPromise;
if ( ids && ids[ 0 ] ) {
__experimentalSelectBlock( ids[ 0 ] );
__experimentalSelectBlock( ids[ 0 ], false );
}
}
},
Expand All @@ -139,20 +141,26 @@ export function BlockSettingsDropdown( {

const updateSelectionAfterRemove = useCallback( () => {
if ( __experimentalSelectBlock ) {
let blockToSelect = previousBlockClientId || firstParentClientId;
let blockToFocus = previousBlockClientId || firstParentClientId;

// Select the first block if there's no previous block nor parent block.
if ( ! blockToSelect ) {
blockToSelect = getBlockOrder()[ 0 ];
// Focus the first block if there's no previous block nor parent block.
if ( ! blockToFocus ) {
blockToFocus = getBlockOrder()[ 0 ];
}

__experimentalSelectBlock( blockToSelect );
// Only update the selection if the original selection is removed.
const shouldUpdateSelection =
hasSelectedBlocks && getSelectedBlockClientIds().length === 0;

__experimentalSelectBlock( blockToFocus, shouldUpdateSelection );
}
}, [
__experimentalSelectBlock,
previousBlockClientId,
firstParentClientId,
getBlockOrder,
hasSelectedBlocks,
getSelectedBlockClientIds,
] );

const removeBlockLabel =
Expand Down Expand Up @@ -209,28 +217,35 @@ export function BlockSettingsDropdown( {
if ( event.defaultPrevented ) return;

if (
isMatch( 'core/block-editor/remove', event )
isMatch( 'core/block-editor/remove', event ) &&
canRemove
) {
event.preventDefault();
updateSelectionAfterRemove( onRemove() );
} else if (
isMatch( 'core/block-editor/duplicate', event )
isMatch(
'core/block-editor/duplicate',
event
) &&
canDuplicate
) {
event.preventDefault();
updateSelectionAfterDuplicate( onDuplicate() );
} else if (
isMatch(
'core/block-editor/insert-after',
event
)
) &&
canInsertDefaultBlock
) {
event.preventDefault();
onInsertAfter();
} else if (
isMatch(
'core/block-editor/insert-before',
event
)
) &&
canInsertDefaultBlock
) {
event.preventDefault();
onInsertBefore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function ListViewBlockSelectButton(
isExpanded,
ariaLabel,
ariaDescribedBy,
updateSelection,
updateFocusAndSelection,
},
ref
) {
Expand All @@ -56,6 +56,7 @@ function ListViewBlockSelectButton(
getPreviousBlockClientId,
getBlockRootClientId,
getBlockOrder,
canRemoveBlocks,
} = useSelect( blockEditorStore );
const { removeBlocks } = useDispatch( blockEditorStore );
const isMatch = useShortcutEventMatch();
Expand Down Expand Up @@ -86,26 +87,37 @@ function ListViewBlockSelectButton(
const firstBlockClientId = isDeletingSelectedBlocks
? selectedBlockClientIds[ 0 ]
: clientId;
const firstBlockRootClientId =
getBlockRootClientId( firstBlockClientId );

const blocksToDelete = isDeletingSelectedBlocks
? selectedBlockClientIds
: [ clientId ];

// Don't update the selection if the blocks cannot be deleted.
if ( ! canRemoveBlocks( blocksToDelete, firstBlockRootClientId ) ) {
return;
}

let blockToSelect =
let blockToFocus =
getPreviousBlockClientId( firstBlockClientId ) ??
// If the previous block is not found (when the first block is deleted),
// fallback to focus the parent block.
getBlockRootClientId( firstBlockClientId );
firstBlockRootClientId;

removeBlocks( blocksToDelete, false );

removeBlocks(
isDeletingSelectedBlocks
? selectedBlockClientIds
: [ clientId ],
false
);
// Update the selection if the original selection has been removed.
const shouldUpdateSelection =
selectedBlockClientIds.length > 0 &&
getSelectedBlockClientIds().length === 0;

// If there's no previous block nor parent block, focus the first block.
if ( ! blockToSelect ) {
blockToSelect = getBlockOrder()[ 0 ];
if ( ! blockToFocus ) {
blockToFocus = getBlockOrder()[ 0 ];
}

updateSelection( blockToSelect );
updateFocusAndSelection( blockToFocus, shouldUpdateSelection );
}
}

Expand Down
18 changes: 9 additions & 9 deletions packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,15 @@ function ListViewBlock( {
[ clientId, selectBlock ]
);

const updateSelection = useCallback(
( newClientId ) => {
const selectedBlockClientIds = getSelectedBlockClientIds();
// Select the block to be focused if there isn't any block selected.
if ( ! selectedBlockClientIds.length ) {
selectBlock( undefined, newClientId, null, null );
const updateFocusAndSelection = useCallback(
( focusClientId, shouldSelectBlock ) => {
if ( shouldSelectBlock ) {
selectBlock( undefined, focusClientId, null, null );
}

const getFocusElement = () => {
const row = treeGridElementRef.current?.querySelector(
`[role=row][data-block="${ newClientId }"]`
`[role=row][data-block="${ focusClientId }"]`
);
if ( ! row ) return null;
// Focus the first focusable in the row, which is the ListViewBlockSelectButton.
Expand Down Expand Up @@ -297,7 +295,7 @@ function ListViewBlock( {
selectedClientIds={ selectedClientIds }
ariaLabel={ blockAriaLabel }
ariaDescribedBy={ descriptionId }
updateSelection={ updateSelection }
updateFocusAndSelection={ updateFocusAndSelection }
/>
<div
className="block-editor-list-view-block-select-button__description"
Expand Down Expand Up @@ -358,10 +356,12 @@ function ListViewBlock( {
onFocus,
} }
disableOpenOnArrowDown
__experimentalSelectBlock={ updateSelection }
expand={ expand }
expandedState={ expandedState }
setInsertedBlock={ setInsertedBlock }
__experimentalSelectBlock={
updateFocusAndSelection
}
/>
) }
</TreeGridCell>
Expand Down
Loading

0 comments on commit 4274a5e

Please sign in to comment.