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: fix Shift+Arrow partial selection for lists & quote #42885

Merged
merged 2 commits into from
Aug 4, 2022
Merged
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 @@ -17,7 +17,7 @@ import { useRefEffect } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { isInSameBlock } from '../../utils/dom';
import { getBlockClientId } from '../../utils/dom';
import { store as blockEditorStore } from '../../store';

/**
Expand Down Expand Up @@ -130,11 +130,8 @@ export function getClosestTabbable(

export default function useArrowNav() {
const {
getSelectedBlockClientId,
getMultiSelectedBlocksStartClientId,
getMultiSelectedBlocksEndClientId,
getPreviousBlockClientId,
getNextBlockClientId,
getSettings,
hasMultiSelection,
__unstableIsFullySelected,
Expand All @@ -150,26 +147,13 @@ export default function useArrowNav() {
verticalRect = null;
}

/**
* Returns true if the given target field is the last in its block which
* can be considered for tab transition. For example, in a block with
* two text fields, this would return true when reversing from the first
* of the two fields, but false when reversing from the second.
*
* @param {Element} target Currently focused text field.
* @param {boolean} isReverse True if considering as the first field.
*
* @return {boolean} Whether field is at edge for tab transition.
*/
function isTabbableEdge( target, isReverse ) {
function isClosestTabbableABlock( target, isReverse ) {
const closestTabbable = getClosestTabbable(
target,
isReverse,
node
);
return (
! closestTabbable || ! isInSameBlock( target, closestTabbable )
);
return closestTabbable && getBlockClientId( closestTabbable );
}

function onKeyDown( event ) {
Expand Down Expand Up @@ -254,23 +238,10 @@ export default function useArrowNav() {
// next, which is the exact reverse of LTR.
const isReverseDir = isRTL( target ) ? ! isReverse : isReverse;
const { keepCaretInsideBlock } = getSettings();
const selectedBlockClientId = getSelectedBlockClientId();

if ( isShift ) {
const selectionEndClientId =
getMultiSelectedBlocksEndClientId();
const selectionBeforeEndClientId = getPreviousBlockClientId(
selectionEndClientId || selectedBlockClientId
);
const selectionAfterEndClientId = getNextBlockClientId(
selectionEndClientId || selectedBlockClientId
);

if (
// Ensure that there is a target block.
( ( isReverse && selectionBeforeEndClientId ) ||
( ! isReverse && selectionAfterEndClientId ) ) &&
isTabbableEdge( target, isReverse ) &&
isClosestTabbableABlock( target, isReverse ) &&
isNavEdge( target, isReverse )
) {
node.contentEditable = true;
Expand Down