mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 07:13:08 -05:00
fe0dfdf6c4
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>
37 lines
975 B
TypeScript
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 };
|