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

Reverts both selection change multiselect issues #65416

Closed
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export function useFocusFirstElement( { clientId, initialPosition } ) {
textInputs[ isReverse ? textInputs.length - 1 : 0 ] || ref.current;

if ( ! isInsideRootBlock( ref.current, target ) ) {
ownerDocument.defaultView.getSelection().removeAllRanges();
ref.current.focus();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,10 @@ export default ( props ) => ( element ) => {
preserveWhiteSpace,
pastePlainText,
} = props.current;
const { ownerDocument } = element;
const { defaultView } = ownerDocument;
const { anchorNode, focusNode } = defaultView.getSelection();
const containsSelection =
element.contains( anchorNode ) && element.contains( focusNode );

// The event listener is attached to the window, so we need to check if
// the target is the element.
if ( ! containsSelection ) {
if ( event.target !== element ) {
return;
}

Expand Down
14 changes: 1 addition & 13 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,7 @@ export function RichTextWrapper(
const inputEvents = useRef( new Set() );

function onFocus() {
let element = anchorRef.current;

if ( ! element ) {
return;
}

// Writing flow might be editable, so we should make sure focus goes to
// the root editable element.
while ( element.parentElement?.isContentEditable ) {
element = element.parentElement;
}

element.focus();
anchorRef.current?.focus();
}

const registry = useRegistry();
Expand Down
2 changes: 0 additions & 2 deletions packages/block-editor/src/components/writing-flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import useSelectionObserver from './use-selection-observer';
import useClickSelection from './use-click-selection';
import useInput from './use-input';
import useClipboardHandler from './use-clipboard-handler';
import useEventRedirect from './use-event-redirect';
import { store as blockEditorStore } from '../../store';

export function useWritingFlow() {
Expand Down Expand Up @@ -66,7 +65,6 @@ export function useWritingFlow() {
},
[ hasMultiSelection ]
),
useEventRedirect(),
] ),
after,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { useRefEffect } from '@wordpress/compose';
*/
import { getBlockClientId, isInSameBlock } from '../../utils/dom';
import { store as blockEditorStore } from '../../store';
import { getSelectionRoot } from './utils';

/**
* Returns true if the element should consider edge navigation upon a keyboard
Expand Down Expand Up @@ -191,7 +190,8 @@ export default function useArrowNav() {
return;
}

const { keyCode, shiftKey, ctrlKey, altKey, metaKey } = event;
const { keyCode, target, shiftKey, ctrlKey, altKey, metaKey } =
event;
const isUp = keyCode === UP;
const isDown = keyCode === DOWN;
const isLeft = keyCode === LEFT;
Expand Down Expand Up @@ -233,11 +233,6 @@ export default function useArrowNav() {
return;
}

const target =
ownerDocument.activeElement === node
? getSelectionRoot( ownerDocument )
: event.target;

// Abort if our current target is not a candidate for navigation
// (e.g. preserve native input behaviors).
if ( ! isNavigationCandidate( target, keyCode, hasModifier ) ) {
Expand Down Expand Up @@ -279,7 +274,6 @@ export default function useArrowNav() {
( altKey ? isHorizontalEdge( target, isReverseDir ) : true ) &&
! keepCaretInsideBlock
) {
node.contentEditable = false;
const closestTabbable = getClosestTabbable(
target,
isReverse,
Expand All @@ -303,7 +297,6 @@ export default function useArrowNav() {
isHorizontalEdge( target, isReverseDir ) &&
! keepCaretInsideBlock
) {
node.contentEditable = false;
const closestTabbable = getClosestTabbable(
target,
isReverseDir,
Expand Down

This file was deleted.

37 changes: 1 addition & 36 deletions packages/block-editor/src/components/writing-flow/use-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { getSelectionRoot } from './utils';

/**
* Handles input for selections across blocks.
Expand Down Expand Up @@ -50,24 +49,7 @@ export default function useInput() {
// DOM. This will cause React errors (and the DOM should only be
// altered in a controlled fashion).
if ( node.contentEditable === 'true' ) {
const selection = node.ownerDocument.defaultView.getSelection();
const range = selection.rangeCount
? selection.getRangeAt( 0 )
: null;
const root = getSelectionRoot( node.ownerDocument );

// If selection is contained within a nested editable, allow
// input. We need to ensure that selection is maintained.
if ( root ) {
node.contentEditable = false;
root.focus();
selection.removeAllRanges();
if ( range ) {
selection.addRange( range );
}
} else {
event.preventDefault();
}
event.preventDefault();
}
}

Expand All @@ -77,23 +59,6 @@ export default function useInput() {
}

if ( ! hasMultiSelection() ) {
const { ownerDocument } = node;
if ( node === ownerDocument.activeElement ) {
if ( event.key === 'End' || event.key === 'Home' ) {
const selectionRoot = getSelectionRoot( ownerDocument );
const selection =
ownerDocument.defaultView.getSelection();
selection.selectAllChildren( selectionRoot );
const method =
event.key === 'End'
? 'collapseToEnd'
: 'collapseToStart';
selection[ method ]();
event.preventDefault();
return;
}
}

if ( event.keyCode === ENTER ) {
if ( event.shiftKey || __unstableIsFullySelected() ) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { useRefEffect } from '@wordpress/compose';
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { getSelectionRoot } from './utils';

export default function useSelectAll() {
const { getBlockOrder, getSelectedBlockClientIds, getBlockRootClientId } =
Expand All @@ -24,27 +23,12 @@ export default function useSelectAll() {
return;
}

const selectionRoot = getSelectionRoot( node.ownerDocument );
const selectedClientIds = getSelectedBlockClientIds();

// Abort if there is selection, but it is not within a block.
if ( selectionRoot && ! selectedClientIds.length ) {
return;
}

if (
selectionRoot &&
selectedClientIds.length < 2 &&
! isEntirelySelected( selectionRoot )
! isEntirelySelected( event.target )
) {
if ( node === node.ownerDocument.activeElement ) {
event.preventDefault();
node.ownerDocument.defaultView
.getSelection()
.selectAllChildren( selectionRoot );
return;
}

return;
}

Expand All @@ -61,7 +45,6 @@ export default function useSelectAll() {
node.ownerDocument.defaultView
.getSelection()
.removeAllRanges();
node.contentEditable = 'false';
selectBlock( rootClientId );
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { useSelect, useDispatch } from '@wordpress/data';
import { useRefEffect } from '@wordpress/compose';
import { create } from '@wordpress/rich-text';
import { isSelectionForward } from '@wordpress/dom';

/**
* Internal dependencies
Expand Down Expand Up @@ -54,14 +53,6 @@ function extractSelectionEndNode( selection ) {
return focusNode;
}

// When the selection is forward (the selection ends with the focus node),
// the selection may extend into the next element with an offset of 0. This
// may trigger multi selection even though the selection does not visually
// end in the next block.
if ( focusOffset === 0 && isSelectionForward( selection ) ) {
return focusNode.previousSibling ?? focusNode.parentElement;
}

return focusNode.childNodes[ focusOffset ];
}

Expand Down Expand Up @@ -107,12 +98,8 @@ function getRichTextElement( node ) {
export default function useSelectionObserver() {
const { multiSelect, selectBlock, selectionChange } =
useDispatch( blockEditorStore );
const {
getBlockParents,
getBlockSelectionStart,
isMultiSelecting,
getSelectedBlockClientId,
} = useSelect( blockEditorStore );
const { getBlockParents, getBlockSelectionStart, isMultiSelecting } =
useSelect( blockEditorStore );
return useRefEffect(
( node ) => {
const { ownerDocument } = node;
Expand Down Expand Up @@ -195,17 +182,10 @@ export default function useSelectionObserver() {
return;
}

setContentEditableWrapper(
node,
!! ( startClientId && endClientId )
);

const isSingularSelection = startClientId === endClientId;
if ( isSingularSelection ) {
if ( ! isMultiSelecting() ) {
if ( getSelectedBlockClientId() !== startClientId ) {
selectBlock( startClientId );
}
selectBlock( startClientId );
} else {
multiSelect( startClientId, startClientId );
}
Expand Down
30 changes: 0 additions & 30 deletions packages/block-editor/src/components/writing-flow/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,3 @@ function toPlainText( html ) {
// Merge any consecutive line breaks
return plainText.replace( /\n\n+/g, '\n\n' );
}

/**
* Gets the current content editable root element based on the selection.
* @param {Document} ownerDocument
* @return {Element|undefined} The content editable root element.
*/
export function getSelectionRoot( ownerDocument ) {
const { defaultView } = ownerDocument;
const { anchorNode, focusNode } = defaultView.getSelection();

if ( ! anchorNode || ! focusNode ) {
return;
}

const anchorElement = (
anchorNode.nodeType === anchorNode.ELEMENT_NODE
? anchorNode
: anchorNode.parentElement
).closest( '[contenteditable]' );

if ( ! anchorElement ) {
return;
}

if ( ! anchorElement.contains( focusNode ) ) {
return;
}

return anchorElement;
}
16 changes: 0 additions & 16 deletions packages/dom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,6 @@ _Returns_

- `boolean`: True if rtl, false if ltr.

### isSelectionForward

Returns true if the given selection object is in the forward direction, or false otherwise.

_Related_

- <https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition>

_Parameters_

- _selection_ `Selection`: Selection object to check.

_Returns_

- `boolean`: Whether the selection is forward.

### isTextContent

_Parameters_
Expand Down
Loading
Loading