97b28398ea
* Add a store to indicate whether input trigger is active Button state is then indicated by: caretIsInBold XOR boldTriggerActive * Fix surrounding where normalization is tripped up by empty text nodes * Add failing test for unsurrounding * Fix failing test * prohibitOverlapse does not need to be active, if aboveEnd is null * Reinsert Italic and Underline button * Refactor find-adjacent to use sum types * Simplify return value of normalizeAdjacent
99 lines
3.1 KiB
TypeScript
99 lines
3.1 KiB
TypeScript
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
import { nodeIsElement, nodeIsText, elementIsEmpty } from "../../lib/dom";
|
|
import { hasOnlyChild } from "../../lib/node";
|
|
import type { ChildNodeRange } from "./child-node-range";
|
|
import { MatchResult } from "./matcher";
|
|
import type { ElementMatcher, FoundAlong, FoundAdjacent } from "./matcher";
|
|
|
|
/**
|
|
* These functions will not ascend on the starting node, but will descend on the neighbor node
|
|
*/
|
|
function adjacentNodeInner(getter: (node: Node) => ChildNode | null) {
|
|
function findAdjacentNodeInner(
|
|
node: Node,
|
|
matches: FoundAdjacent[],
|
|
matcher: ElementMatcher,
|
|
): void {
|
|
let current = getter(node);
|
|
|
|
const maybeAlong: (Element | Text)[] = [];
|
|
while (
|
|
current &&
|
|
((nodeIsElement(current) && elementIsEmpty(current)) ||
|
|
(nodeIsText(current) && current.length === 0))
|
|
) {
|
|
maybeAlong.push(current);
|
|
current = getter(current);
|
|
}
|
|
|
|
while (current && nodeIsElement(current)) {
|
|
const element: Element = current;
|
|
const matchResult = matcher(element);
|
|
|
|
if (matchResult) {
|
|
matches.push(
|
|
...maybeAlong.map(
|
|
(along: Element | Text): FoundAlong => ({
|
|
element: along,
|
|
matchType: MatchResult.ALONG,
|
|
}),
|
|
),
|
|
);
|
|
|
|
matches.push({
|
|
element,
|
|
matchType: matchResult,
|
|
});
|
|
|
|
return findAdjacentNodeInner(element, matches, matcher);
|
|
}
|
|
|
|
// descend down into element
|
|
current =
|
|
hasOnlyChild(current) && nodeIsElement(element.firstChild!)
|
|
? element.firstChild
|
|
: null;
|
|
}
|
|
}
|
|
|
|
return findAdjacentNodeInner;
|
|
}
|
|
|
|
const findBeforeNodeInner = adjacentNodeInner(
|
|
(node: Node): ChildNode | null => node.previousSibling,
|
|
);
|
|
|
|
function findBeforeNode(node: Node, matcher: ElementMatcher): FoundAdjacent[] {
|
|
const matches: FoundAdjacent[] = [];
|
|
findBeforeNodeInner(node, matches, matcher);
|
|
return matches;
|
|
}
|
|
|
|
export function findBefore(
|
|
childNodeRange: ChildNodeRange,
|
|
matcher: ElementMatcher,
|
|
): FoundAdjacent[] {
|
|
const { parent, startIndex } = childNodeRange;
|
|
return findBeforeNode(parent.childNodes[startIndex], matcher);
|
|
}
|
|
|
|
const findAfterNodeInner = adjacentNodeInner(
|
|
(node: Node): ChildNode | null => node.nextSibling,
|
|
);
|
|
|
|
function findAfterNode(node: Node, matcher: ElementMatcher): FoundAdjacent[] {
|
|
const matches: FoundAdjacent[] = [];
|
|
findAfterNodeInner(node, matches, matcher);
|
|
return matches;
|
|
}
|
|
|
|
export function findAfter(
|
|
childNodeRange: ChildNodeRange,
|
|
matcher: ElementMatcher,
|
|
): FoundAdjacent[] {
|
|
const { parent, endIndex } = childNodeRange;
|
|
return findAfterNode(parent.childNodes[endIndex - 1], matcher);
|
|
}
|