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

Revert "Allow multi-select on iOS Safari/touch devices" #65414

Merged
merged 1 commit into from
Sep 25, 2024
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 @@ -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 ) {
Comment on lines -37 to +32
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we just need to keep the code here as is. Otherwise, the following two problems will occur:

  • Formatting is not applied when formatted text is copied continuously
  • Links cannot be pasted into formatted text

This problem was reported in #63434, and I tried to solve this problem in #63779, but #63671 ended up solving this problem as result. Please see this discussion for why such code is needed.

trunk (expected)

expected.mp4

This PR (not expected)

not-expected.mp4

Copy link
Member

Choose a reason for hiding this comment

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

@t-hamano Let's maybe reopen your PR and fix it again separately.

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 @@ -107,12 +107,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 +191,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;
}
12 changes: 5 additions & 7 deletions packages/dom/src/dom/place-caret-at-edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ export default function placeCaretAtEdge( container, isReverse, x ) {
return;
}

const { ownerDocument } = container;
const { defaultView } = ownerDocument;
assertIsDefined( defaultView, 'defaultView' );
const selection = defaultView.getSelection();
assertIsDefined( selection, 'selection' );

if ( ! container.isContentEditable ) {
selection.removeAllRanges();
return;
}

Expand All @@ -86,6 +79,11 @@ export default function placeCaretAtEdge( container, isReverse, x ) {
return;
}

const { ownerDocument } = container;
const { defaultView } = ownerDocument;
assertIsDefined( defaultView, 'defaultView' );
const selection = defaultView.getSelection();
assertIsDefined( selection, 'selection' );
selection.removeAllRanges();
selection.addRange( range );
}
Loading
Loading