0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 15:23:29 -05:00
Squire/source/node/Block.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

37 lines
975 B
TypeScript

import { TreeIterator, SHOW_ELEMENT } from './TreeIterator';
import { isBlock } from './Category';
// ---
const getBlockWalker = (
node: Node,
root: Element | DocumentFragment,
): TreeIterator<HTMLElement> => {
const walker = new TreeIterator<HTMLElement>(root, SHOW_ELEMENT, isBlock);
walker.currentNode = node;
return walker;
};
const getPreviousBlock = (
node: Node,
root: Element | DocumentFragment,
): HTMLElement | null => {
const block = getBlockWalker(node, root).previousNode();
return block !== root ? block : null;
};
const getNextBlock = (
node: Node,
root: Element | DocumentFragment,
): HTMLElement | null => {
const block = getBlockWalker(node, root).nextNode();
return block !== root ? block : null;
};
const isEmptyBlock = (block: Element): boolean => {
return !block.textContent && !block.querySelector('IMG');
};
// ---
export { getBlockWalker, getPreviousBlock, getNextBlock, isEmptyBlock };