Skip to content

Commit

Permalink
Optimize keydown event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Dec 19, 2022
1 parent 29341d6 commit 07ac595
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
8 changes: 4 additions & 4 deletions packages/block-editor/src/components/rich-text/use-enter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export function useEnter( props ) {
return;
}

if ( event.keyCode !== ENTER ) {
return;
}

const {
removeEditorOnlyFormats,
value,
Expand All @@ -40,10 +44,6 @@ export function useEnter( props ) {
onSplitAtEnd,
} = propsRef.current;

if ( event.keyCode !== ENTER ) {
return;
}

event.preventDefault();

const _value = { ...value };
Expand Down
48 changes: 20 additions & 28 deletions packages/block-editor/src/components/writing-flow/use-arrow-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ export default function useArrowNav() {
}

function onKeyDown( event ) {
// Abort if navigation has already been handled (e.g. RichText
// inline boundaries).
if ( event.defaultPrevented ) {
return;
}

const { keyCode, target, shiftKey, ctrlKey, altKey, metaKey } =
event;
const isUp = keyCode === UP;
Expand All @@ -182,24 +188,20 @@ export default function useArrowNav() {
const { ownerDocument } = node;
const { defaultView } = ownerDocument;

if ( ! isNav ) {
return;
}

// If there is a multi-selection, the arrow keys should collapse the
// selection to the start or end of the selection.
if ( hasMultiSelection() ) {
// Only handle if we have a full selection (not a native partial
// selection).
if ( ! __unstableIsFullySelected() ) {
return;
}

if ( event.defaultPrevented ) {
return;
}

if ( ! isNav ) {
if ( shiftKey ) {
return;
}

if ( shiftKey ) {
// Only handle if we have a full selection (not a native partial
// selection).
if ( ! __unstableIsFullySelected() ) {
return;
}

Expand All @@ -214,6 +216,12 @@ export default function useArrowNav() {
return;
}

// Abort if our current target is not a candidate for navigation
// (e.g. preserve native input behaviors).
if ( ! isNavigationCandidate( target, keyCode, hasModifier ) ) {
return;
}

// When presing any key other than up or down, the initial vertical
// position must ALWAYS be reset. The vertical position is saved so
// it can be restored as well as possible on sebsequent vertical
Expand All @@ -227,22 +235,6 @@ export default function useArrowNav() {
verticalRect = computeCaretRect( defaultView );
}

// Abort if navigation has already been handled (e.g. RichText
// inline boundaries).
if ( event.defaultPrevented ) {
return;
}

if ( ! isNav ) {
return;
}

// Abort if our current target is not a candidate for navigation
// (e.g. preserve native input behaviors).
if ( ! isNavigationCandidate( target, keyCode, hasModifier ) ) {
return;
}

// In the case of RTL scripts, right means previous and left means
// next, which is the exact reverse of LTR.
const isReverseDir = isRTL( target ) ? ! isReverse : isReverse;
Expand Down

1 comment on commit 07ac595

@github-actions
Copy link

@github-actions github-actions bot commented on 07ac595 Dec 19, 2022

Choose a reason for hiding this comment

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

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3731928874
📝 Reported issues:

Please sign in to comment.