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

[Writing Flow]: Try to fix multi-selection with shift+click #40687

Merged
merged 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import { store as blockEditorStore } from '../../store';
import { getBlockClientId } from '../../utils/dom';

export default function useClickSelection() {
const { multiSelect, selectBlock } = useDispatch( blockEditorStore );
const { selectBlock } = useDispatch( blockEditorStore );
const {
isSelectionEnabled,
getBlockParents,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These changes are unrelated - just some cleaning up.

getBlockSelectionStart,
hasMultiSelection,
} = useSelect( blockEditorStore );
Expand Down Expand Up @@ -54,10 +53,8 @@ export default function useClickSelection() {
};
},
[
multiSelect,
selectBlock,
isSelectionEnabled,
getBlockParents,
getBlockSelectionStart,
hasMultiSelection,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,43 +76,81 @@ export default function useSelectionObserver() {
const { multiSelect, selectBlock, selectionChange } = useDispatch(
blockEditorStore
);
const { getBlockParents } = useSelect( blockEditorStore );
const { getBlockParents, getBlockSelectionStart } = useSelect(
blockEditorStore
);
return useRefEffect(
( node ) => {
const { ownerDocument } = node;
const { defaultView } = ownerDocument;

function onSelectionChange() {
function onSelectionChange( event ) {
const selection = defaultView.getSelection();

// If no selection is found, end multi selection and disable the
// contentEditable wrapper.
if ( ! selection.rangeCount || selection.isCollapsed ) {
if ( ! selection.rangeCount ) {
setContentEditableWrapper( node, false );
return;
}
// If selection is collapsed and we haven't used `shift+click`,
// end multi selection and disable the contentEditable wrapper.
// We have to check about `shift+click` case because elements
// that don't support text selection might be involved, and we might
// update the clientIds to multi-select blocks.
// For now we check if the event is a `mouse` event.
const isClickShift = event.shiftKey && event.type === 'mouseup';
if ( selection.isCollapsed && ! isClickShift ) {
setContentEditableWrapper( node, false );
return;
}

const clientId = getBlockClientId(
let startClientId = getBlockClientId(
extractSelectionStartNode( selection )
);
const endClientId = getBlockClientId(
let endClientId = getBlockClientId(
extractSelectionEndNode( selection )
);
// If the selection has changed and we had pressed `shift+click`,
// we need to check if in an element that doesn't support
// text selection has been clicked.
if ( isClickShift ) {
const selectedClientId = getBlockSelectionStart();
const clickedClientId = getBlockClientId( event.target );
// `endClientId` is not defined if we end the selection by clicking a non-selectable block.
// We need to check if there was already a selection with a non-selectable focusNode.
const focusNodeIsNonSelectable =
clickedClientId !== endClientId;
if (
( startClientId === endClientId &&
selection.isCollapsed ) ||
! endClientId ||
focusNodeIsNonSelectable
) {
endClientId = clickedClientId;
}
// Handle the case when we have a non-selectable block
// selected and click another one.
if ( startClientId !== selectedClientId ) {
startClientId = selectedClientId;
}
Comment on lines +131 to +135
Copy link
Contributor

@mcsf mcsf May 4, 2022

Choose a reason for hiding this comment

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

This block could just as well be refactored to:

startClientId = selectedClientId;

… as there is no cost nor any side effect to the assignment.

And this brings me to asking the broader question: couldn't we simplify the logic here into something more straightforward, with fewer conditionals, as close as possible to:

if ( isClickShift ) { // This is the only branching that matters
  startClientId = getBlockSelectionStart();
  endClientId = getBlockClientId( event.target );
}

wdyt, @ntsekouras?

}

// If the selection did not involve a block, return early.
if ( clientId === undefined && endClientId === undefined ) {
// If the selection did not involve a block, return.
if (
startClientId === undefined &&
endClientId === undefined
) {
setContentEditableWrapper( node, false );
return;
}

const isSingularSelection = clientId === endClientId;

const isSingularSelection = startClientId === endClientId;
if ( isSingularSelection ) {
selectBlock( clientId );
selectBlock( startClientId );
} else {
const startPath = [
...getBlockParents( clientId ),
clientId,
...getBlockParents( startClientId ),
startClientId,
];
const endPath = [
...getBlockParents( endClientId ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
clickBlockToolbarButton,
clickButton,
clickMenuItem,
insertBlock,
openListView,
saveDraft,
transformBlockTo,
Expand Down Expand Up @@ -975,4 +976,51 @@ describe( 'Multi-block selection', () => {
// Expect two blocks with "&" in between.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
describe( 'shift+click multi-selection', () => {
it( 'should multi-select block with text selection and a block without text selection', async () => {
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'hi' );
await page.keyboard.press( 'Enter' );
await insertBlock( 'Spacer' );
await page.keyboard.press( 'ArrowUp' );

const spacerBlock = await page.waitForSelector(
'.wp-block.wp-block-spacer'
);
const boundingBox = await spacerBlock.boundingBox();
const mousePosition = {
x: boundingBox.x + boundingBox.width / 2,
y: boundingBox.y + boundingBox.height / 2,
};
await page.keyboard.down( 'Shift' );
await page.mouse.click( mousePosition.x, mousePosition.y );
await page.keyboard.up( 'Shift' );

const selectedBlocks = await page.$$(
'.wp-block.is-multi-selected'
);
expect( selectedBlocks.length ).toBe( 2 );
} );
it( 'should multi-select blocks without text selection', async () => {
await insertBlock( 'Spacer' );
// Get the first spacer block element.
const spacerBlock = await page.waitForSelector(
'.wp-block.wp-block-spacer'
);
const boundingBox = await spacerBlock.boundingBox();
await page.keyboard.press( 'Enter' );
await insertBlock( 'Spacer' );
const mousePosition = {
x: boundingBox.x + boundingBox.width / 2,
y: boundingBox.y + boundingBox.height / 2,
};
await page.keyboard.down( 'Shift' );
await page.mouse.click( mousePosition.x, mousePosition.y );
await page.keyboard.up( 'Shift' );
const selectedBlocks = await page.$$(
'.wp-block.is-multi-selected'
);
expect( selectedBlocks.length ).toBe( 2 );
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export function useInputAndSelection( props ) {
function onCompositionStart() {
isComposing = true;
// Do not update the selection when characters are being composed as
// this rerenders the component and might distroy internal browser
// this rerenders the component and might destroy internal browser
// editing state.
ownerDocument.removeEventListener(
'selectionchange',
Expand Down