chore: Upgrade all of prosemirror (#5366)

Co-authored-by: Apoorv Mishra <apoorvmishra101092@gmail.com>
This commit is contained in:
Tom Moor
2023-05-24 22:24:05 -04:00
committed by GitHub
parent e340e568e2
commit d5341a486c
77 changed files with 875 additions and 675 deletions

View File

@@ -0,0 +1,44 @@
import { Node, ResolvedPos } from "prosemirror-model";
import { Selection } from "prosemirror-state";
type Predicate = (node: Node) => boolean;
type ContentNodeWithPos = {
pos: number;
start: number;
depth: number;
node: Node;
};
export const findParentNode =
(predicate: Predicate) =>
({ $from }: Selection) =>
findParentNodeClosestToPos($from, predicate);
/**
* Iterates over parent nodes starting from the given `$pos`, returning the
* closest node and its start position `predicate` returns truthy for. `start`
* points to the start position of the node, `pos` points directly before the node.
*
* @param $pos position to start from
* @param predicate filtering predicate function
* @returns node and its start position
*/
export const findParentNodeClosestToPos = (
$pos: ResolvedPos,
predicate: Predicate
): ContentNodeWithPos | undefined => {
for (let i = $pos.depth; i > 0; i--) {
const node = $pos.node(i);
if (predicate(node)) {
return {
pos: i > 0 ? $pos.before(i) : 0,
start: $pos.start(i),
depth: i,
node,
};
}
}
return undefined;
};