Skip to content

Commit

Permalink
Let ArrowRight key always break out of <code>
Browse files Browse the repository at this point in the history
  • Loading branch information
neilj committed Feb 22, 2023
1 parent cf30a69 commit 8304fd3
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion source/keyboard/KeyHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { Backspace } from './Backspace';
import { Delete } from './Delete';
import { ShiftTab, Tab } from './Tab';
import { Space } from './Space';
import { rangeDoesEndAtBlockBoundary } from '../range/Block';
import { moveRangeBoundariesDownTree } from '../range/Boundaries';

// ---

Expand Down Expand Up @@ -116,8 +118,32 @@ const keyHandlers: Record<string, KeyHandler> = {
'ArrowLeft'(self: Squire): void {
self._removeZWS();
},
'ArrowRight'(self: Squire): void {
'ArrowRight'(self: Squire, event: KeyboardEvent, range: Range): void {
self._removeZWS();
// Allow right arrow to always break out of <code> block.
const root = self.getRoot();
if (rangeDoesEndAtBlockBoundary(range, root)) {
moveRangeBoundariesDownTree(range);
let node: Node | null = range.endContainer;
do {
if (node.nodeName === 'CODE') {
let next = node.nextSibling;
if (!(next instanceof Text)) {
const textNode = document.createTextNode(' '); // nbsp
node.parentNode!.insertBefore(textNode, next);
next = textNode;
}
range.setStart(next, 1);
self.setSelection(range);
event.preventDefault();
break;
}
} while (
!node.nextSibling &&
(node = node.parentNode) &&
node !== root
);
}
},
};

Expand Down

0 comments on commit 8304fd3

Please sign in to comment.