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

Multi-selection: allow partial block selection #38892

Merged
merged 67 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
d765a15
Initial attempt
ellatrix Feb 17, 2022
60457c3
Fix selection from bottom to top
ellatrix Feb 18, 2022
06cc562
Ensure native selection spans multiple blocks
ellatrix Feb 18, 2022
4a84339
Fix isSelected
ellatrix Feb 18, 2022
b4e5245
Fix ctrl+A
ellatrix Feb 18, 2022
5d59308
Fix e2e tests
ellatrix Feb 22, 2022
a56eef6
Fix text color button
ellatrix Feb 23, 2022
a467c2d
Fix early return
ellatrix Feb 23, 2022
e0b5a9a
Prevent default behaviour for everything except Backspace and Delete
ellatrix Feb 23, 2022
2eabf19
Polish
ellatrix Feb 23, 2022
3f6a302
Fix forward delete
ellatrix Feb 23, 2022
05bf622
Fix typo
ellatrix Feb 23, 2022
19cc8d9
Avoid DOM checks
ellatrix Feb 24, 2022
5516657
Fix unit test
ellatrix Feb 24, 2022
fb8db5a
Allow partial selection with shift + arrow keys
ellatrix Feb 24, 2022
f0047f3
Fix e2e tests
ellatrix Feb 24, 2022
4f5ab5d
Further fixes
ellatrix Feb 24, 2022
150b23f
Fix forward delete
ellatrix Feb 25, 2022
4089e7a
Add Enter handling
ellatrix Feb 25, 2022
21a7932
Remove lines, fix occasional selection failure
ellatrix Mar 9, 2022
9485788
Stabilise new e2e tests
ellatrix Mar 10, 2022
b63e96d
Allow input over selection
ellatrix Mar 10, 2022
243aea3
Remove obsolete comment
ellatrix Mar 15, 2022
734bc92
Remove scroll to multi-selection
ellatrix Mar 15, 2022
77d449f
Polish highlight and block select style.
jasmussen Mar 15, 2022
d999acc
Fix for Firefox
ellatrix Mar 15, 2022
eae5615
Fix keyboard nav for Firefox
ellatrix Mar 15, 2022
8292c5a
Use getDefaultBlockName
ellatrix Mar 16, 2022
3240d51
insert '' => remove
ellatrix Mar 16, 2022
020bdfe
Simplify selection filling
ellatrix Mar 16, 2022
1f890f3
Fix Firefox focus issue
ellatrix Mar 16, 2022
78007ec
Fix e2e tests
ellatrix Mar 16, 2022
5c6c5f3
Fix selection after merge
ellatrix Mar 16, 2022
0096310
Move selection tracking to writing flow
ellatrix Mar 18, 2022
722b640
Fix import
ellatrix Mar 18, 2022
8c66742
Fix e2e test
ellatrix Mar 18, 2022
85bdcd5
Separate click to select hook
ellatrix Mar 18, 2022
b6c9cdd
Fix keyboard selection with non rich text
ellatrix Mar 19, 2022
6d3babf
Restrict filling in selection if block already has selection
ellatrix Mar 20, 2022
81c5b85
Fix paste e2e test
ellatrix Mar 20, 2022
fa1cbf6
Fix race condition in e2e tests
ellatrix Mar 21, 2022
fd016d0
Allow partial selection with shift+click
ellatrix Mar 21, 2022
8b0c80d
wip
ellatrix Mar 21, 2022
bd3b1f1
Fix selection issue after quote merge
ellatrix Mar 22, 2022
bc16541
Fix e2e tests
ellatrix Mar 22, 2022
f3e2242
wip
ellatrix Mar 23, 2022
ea4189f
Expand selection on Delete if content can't be merged
ellatrix Mar 23, 2022
92cfa15
Refine
ellatrix Mar 23, 2022
660e9ca
Polish
ellatrix Mar 23, 2022
0219ce0
Revert firefox compat move
ellatrix Mar 25, 2022
a573615
Revert unnessary changes for this PR
ellatrix Mar 25, 2022
07e13e7
Remove unnessary cloning
ellatrix Mar 25, 2022
022f9fb
Fix typos
ellatrix Mar 25, 2022
aab3f6c
Move input handling to writing flow
ellatrix Mar 26, 2022
8597e0f
Fix e2e test
ellatrix Mar 26, 2022
88a848b
Add more comments
ellatrix Mar 26, 2022
dfc4cb2
Try removing duplicate code
ellatrix Mar 26, 2022
dfdcf59
Make e2e tests pass again
ellatrix Mar 27, 2022
67a0c20
Remove raf hack
ellatrix Mar 27, 2022
50d2407
Fix block first focus
ellatrix Mar 27, 2022
fb09ae3
Fix comments in e2e tests about raf
ellatrix Mar 27, 2022
f04a150
Fix shift+click selection and add e2e test
ellatrix Mar 27, 2022
dca35d9
Remove unnecessary code
ellatrix Mar 27, 2022
593194b
Check to see which tests are failing without this
ellatrix Mar 28, 2022
ab19bcf
Check if this is still needed
ellatrix Mar 28, 2022
347c6b7
Fix shift+click selection inside block
ellatrix Mar 28, 2022
1659b37
Fix selection inside list and quote blocks
ellatrix Mar 28, 2022
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
4 changes: 2 additions & 2 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,7 @@ Action that changes the position of the user caret.

_Parameters_

- _clientId_ `string`: The selected block client ID.
- _clientId_ `string|WPSelection`: The selected block client ID.
- _attributeKey_ `string`: The selected block attribute key.
- _startOffset_ `number`: The start offset.
- _endOffset_ `number`: The end offset.
Expand Down Expand Up @@ -1462,7 +1462,7 @@ _Parameters_

- _rootClientId_ `?string`: Optional root client ID of block list on which to insert.
- _index_ `?number`: Index at which block should be inserted.
- _\_\_unstableOptions_ `Object`: Wether or not to show an inserter button.
- _\_\_unstableOptions_ `Object`: Whether or not to show an inserter button.

_Returns_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ function BlockListAppender( {
'block-list-appender wp-block',
className
) }
// Needed in case the whole editor is content editable (for multi
// selection). It fixes an edge case where ArrowDown and ArrowRight
// should collapse the selection to the end of that selection and
// not into the appender.
contentEditable={ false }
// The appender exists to let you add the first Paragraph before
// any is inserted. To that end, this appender should visually be
// presented as a block. That means theme CSS should style it as if
Expand Down
9 changes: 4 additions & 5 deletions packages/block-editor/src/components/block-list/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
// When selecting multiple blocks, we provide an additional selection indicator.
&.is-navigate-mode .block-editor-block-list__block.is-selected,
&.is-navigate-mode .block-editor-block-list__block.is-hovered,
.block-editor-block-list__block.is-multi-selected:not([contenteditable]),
.block-editor-block-list__block.is-highlighted,
.block-editor-block-list__block.is-multi-selected {

.block-editor-block-list__block.is-highlighted ~ .is-multi-selected {
Copy link
Contributor

Choose a reason for hiding this comment

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

This change makes blocks look partially selected, like so:
before

If we change this rule to the following instead, it appears to thread the needle for me:

.block-editor-block-list__block.is-multi-selected:not(.rich-text) {

after

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that seems better. Can we check it still provides the right result for quotes, lists, headings, etc?

&::after {
// Show selection borders around every non-nested block's actual footprint.
position: absolute;
Expand All @@ -41,7 +41,7 @@
right: $border-width;

// Everything else.
box-shadow: 0 0 0 $border-width var(--wp-admin-theme-color);
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
border-radius: $radius-block-ui - $border-width; // Border is outset, so subtract the width to achieve correct radius.

// Windows High Contrast mode will show this outline.
Expand All @@ -66,11 +66,10 @@
}

.block-editor-block-list__block.is-highlighted::after {
box-shadow: 0 0 0 $border-width var(--wp-admin-theme-color);
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
outline: $border-width solid transparent;
}

.block-editor-block-list__block.is-multi-selected::after,
&.is-navigate-mode .block-editor-block-list__block.is-selected::after,
& .is-block-moving-mode.block-editor-block-list__block.has-child-selected {
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ import { useBlockMovingModeClassNames } from './use-block-moving-mode-class-name
import { useFocusHandler } from './use-focus-handler';
import { useEventHandlers } from './use-selected-block-event-handlers';
import { useNavModeExit } from './use-nav-mode-exit';
import { useScrollIntoView } from './use-scroll-into-view';
import { useBlockRefProvider } from './use-block-refs';
import { useMultiSelection } from './use-multi-selection';
import { useIntersectionObserver } from './use-intersection-observer';
import { store as blockEditorStore } from '../../../store';

Expand Down Expand Up @@ -115,11 +113,8 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
const mergedRefs = useMergeRefs( [
props.ref,
useFocusFirstElement( clientId ),
// Must happen after focus because we check for focus in the block.
useScrollIntoView( clientId ),
useBlockRefProvider( clientId ),
useFocusHandler( clientId ),
useMultiSelection( clientId ),
useEventHandlers( clientId ),
useNavModeExit( clientId ),
useIsHovered(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { useSelect } from '@wordpress/data';
*/
import { isInsideRootBlock } from '../../../utils/dom';
import { store as blockEditorStore } from '../../../store';
import { setContentEditableWrapper } from './use-multi-selection';

/** @typedef {import('@wordpress/element').RefObject} RefObject */

Expand All @@ -32,7 +31,6 @@ function useInitialPosition( clientId ) {
( select ) => {
const {
getSelectedBlocksInitialCaretPosition,
isMultiSelecting,
isNavigationMode,
isBlockSelected,
} = select( blockEditorStore );
Expand All @@ -41,7 +39,7 @@ function useInitialPosition( clientId ) {
return;
}

if ( isMultiSelecting() || isNavigationMode() ) {
if ( isNavigationMode() ) {
return;
}

Expand Down Expand Up @@ -73,11 +71,11 @@ function isFormElement( element ) {
export function useFocusFirstElement( clientId ) {
const ref = useRef();
const initialPosition = useInitialPosition( clientId );
const { isBlockSelected } = useSelect( blockEditorStore );
const { isBlockSelected, isMultiSelecting } = useSelect( blockEditorStore );

useEffect( () => {
// Check if the block is still selected at the time this effect runs.
if ( ! isBlockSelected( clientId ) ) {
if ( ! isBlockSelected( clientId ) || isMultiSelecting() ) {
return;
}

Expand Down Expand Up @@ -130,8 +128,6 @@ export function useFocusFirstElement( clientId ) {
}
}

setContentEditableWrapper( ref.current, false );

placeCaretAtHorizontalEdge( target, isReverse );
}, [ initialPosition, clientId ] );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export function useFocusHandler( clientId ) {
* @param {FocusEvent} event Focus event.
*/
function onFocus( event ) {
// When the whole editor is editable, let writing flow handle
// selection.
if (
node.parentElement.closest( '[contenteditable="true"]' )
) {
return;
}

// Check synchronously because a non-selected block might be
// getting data through `useSelect` asynchronously.
if ( isBlockSelected( clientId ) ) {
Expand Down

This file was deleted.

Loading