Block navigation doesn't work when arrow keys are held down
Issue Overview
When holding down the UP or DOWN keys, the cursor does not move to the next block once it hits the boundary of that block. It does navigate to the next block as soon as you release the arrow key, however. This is because the arrow key navigation is bound to keyup instead of keydown.
Steps to Reproduce (for bugs)
- Create a page with several blocks
- Focus the first block
- Hold down the arrow key (for five seconds)
Expected Behavior
The cursor should move down the blocks
Current Behavior
The cursor stays in the same spot, until the key is released. Then, it moves by one block
Possible Solution
Allow keydown to navigate between blocks.
I can see two ways of doing this
- Eliminate keyup altogether in writing-flow. This means we won't have the preventDefault, which may be a problem (not sure what it was there for). This also gets rid of the shouldMove state entirely.
- Keep keyup, but make keydown have an alternate if condition that says firstly if shouldMove is already true (so keydown occurred first without a keyup), then move like keyup does. This keeps the keyup handling, but means that holding keydown keeps navigating after the first one fires as long as the user has the key held down.
I'll setup a PR with (1) because the code is cleaner, but if that's a problem, we can look at doing (2).
Note, we'll probably also have to define what Shift + Up and Shift + Down should do. My preference would be that they would allow the same multi-block selection that mouse does.
There's a reason arrow navigation is using onKeyUp instead of onKeyDown https://github.com/WordPress/gutenberg/pull/2894#issuecomment-334690488
Maybe it's fixable though with some exploration.
So basically the keyup was firing an extra move afterwards? So then would it be possible to make the keydown still the thing that moves the block, and just make keyup prevent default if shouldMove is true?
Or did
causing the caret to move unexpectedly
not mean that it was doing an exra move in the right direction, but that it was doing some random move?
Yes, it was doing that and I tried preventing back then with no success. But this extra move is not happening in my last testing
Fixed by #2979.