Files
Tom Moor bf45e97641 chore: Enforce type import consistency (#10968)
* Update types

* fix circular dep

* type imports

* lint type imports and --fix
2025-12-19 23:07:02 -05:00

34 lines
1.1 KiB
TypeScript

import type { Node } from "prosemirror-model";
import type { NodeWithPos } from "../types";
import { findBlockNodes } from "./findChildren";
export function findCollapsedNodes(doc: Node): NodeWithPos[] {
const blocks = findBlockNodes(doc);
const nodes: NodeWithPos[] = [];
const collapsedStack: number[] = [];
for (const block of blocks) {
if (collapsedStack.length) {
const top = collapsedStack[collapsedStack.length - 1];
// if the block encountered same or higher level heading, pop the stack
if (block.node.type.name === "heading" && block.node.attrs.level <= top) {
collapsedStack.pop();
// if the block is a heading and it is collapsed, push it to the stack
if (block.node.attrs.collapsed) {
collapsedStack.push(block.node.attrs.level);
}
} else {
// the deepest level or non-heading block should be added to the nodes
nodes.push(block);
}
} else {
if (block.node.type.name === "heading" && block.node.attrs.collapsed) {
collapsedStack.push(block.node.attrs.level);
}
}
}
return nodes;
}