Skip to content

Commit

Permalink
Partial multi selection: limit if selection contains unmergeable block
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Aug 3, 2022
1 parent 7ba4f95 commit 651d13c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/block-editor/src/components/block-list/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
}

// Provide exceptions for placeholders.
.components-placeholder {
.components-placeholder,
.block-editor-block-list__block.is-multi-selected:not(.is-partially-selected) {
::selection {
background: transparent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function useBlockClassNames( clientId ) {
hasSelectedInnerBlock,
isTyping,
__unstableIsFullySelected,
__unstableSelectionHasUnmergeableBlock,
} = select( blockEditorStore );
const { outlineMode } = getSettings();
const isDragging = isBlockBeingDragged( clientId );
Expand All @@ -51,7 +52,9 @@ export function useBlockClassNames( clientId ) {
'is-highlighted': isBlockHighlighted( clientId ),
'is-multi-selected': isMultiSelected,
'is-partially-selected':
isMultiSelected && ! __unstableIsFullySelected(),
isMultiSelected &&
! __unstableIsFullySelected() &&
! __unstableSelectionHasUnmergeableBlock(),
'is-reusable': isReusableBlock( getBlockType( name ) ),
'is-dragging': isDragging,
'has-child-selected': isAncestorOfSelectedBlock,
Expand Down
16 changes: 12 additions & 4 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,14 @@ export function __unstableIsSelectionCollapsed( state ) {
);
}

export function __unstableSelectionHasUnmergeableBlock( state ) {
return getSelectedBlockClientIds( state ).some( ( clientId ) => {
const blockName = getBlockName( state, clientId );
const blockType = getBlockType( blockName );
return ! blockType.merge;
} );
}

/**
* Check whether the selection is mergeable.
*
Expand Down Expand Up @@ -1010,19 +1018,19 @@ export function __unstableIsSelectionMergeable( state, isForward ) {
? selectionStart.clientId
: selectionEnd.clientId;

const targetBlock = getBlock( state, targetBlockClientId );
const targetBlockType = getBlockType( targetBlock.name );
const targetBlockName = getBlockName( state, targetBlockClientId );
const targetBlockType = getBlockType( targetBlockName );

if ( ! targetBlockType.merge ) return false;

const blockToMerge = getBlock( state, blockToMergeClientId );

// It's mergeable if the blocks are of the same type.
if ( blockToMerge.name === targetBlock.name ) return true;
if ( blockToMerge.name === targetBlockName ) return true;

// If the blocks are of a different type, try to transform the block being
// merged into the same type of block.
const blocksToMerge = switchToBlockType( blockToMerge, targetBlock.name );
const blocksToMerge = switchToBlockType( blockToMerge, targetBlockName );

return blocksToMerge && blocksToMerge.length;
}
Expand Down

0 comments on commit 651d13c

Please sign in to comment.