0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00
Squire/source/keyboard/Tab.ts
Neil Jenkins fe0dfdf6c4 Squire 2.0
This is a massive refactor to port Squire to TypeScript, fix a bunch of small
bugs and modernise our tooling. The development was done on an internal
repository, so apologies to anyone following externally for the commit dump;
updates from here should come as real commits again.

Co-authored-by: Joe Woods <woods@fastmailteam.com>
2023-01-23 13:18:29 +11:00

48 lines
1.5 KiB
TypeScript

import {
rangeDoesStartAtBlockBoundary,
getStartBlockOfRange,
} from '../range/Block';
import { getNearest } from '../node/Node';
import type { Squire } from '../Editor';
// ---
const Tab = (self: Squire, event: KeyboardEvent, range: Range): void => {
const root = self._root;
self._removeZWS();
// If no selection and at start of block
if (range.collapsed && rangeDoesStartAtBlockBoundary(range, root)) {
let node: Node = getStartBlockOfRange(range, root)!;
// Iterate through the block's parents
let parent: Node | null;
while ((parent = node.parentNode)) {
// If we find a UL or OL (so are in a list, node must be an LI)
if (parent.nodeName === 'UL' || parent.nodeName === 'OL') {
// Then increase the list level
event.preventDefault();
self.increaseListLevel(range);
break;
}
node = parent;
}
}
};
const ShiftTab = (self: Squire, event: KeyboardEvent, range: Range): void => {
const root = self._root;
self._removeZWS();
// If no selection and at start of block
if (range.collapsed && rangeDoesStartAtBlockBoundary(range, root)) {
// Break list
const node = range.startContainer;
if (getNearest(node, root, 'UL') || getNearest(node, root, 'OL')) {
event.preventDefault();
self.decreaseListLevel(range);
}
}
};
// ---
export { Tab, ShiftTab };