tiptap/packages/core/src/helpers/findParentNodeClosestToPos.ts
2021-01-25 13:39:35 +01:00

23 lines
538 B
TypeScript

import { ResolvedPos, Node as ProseMirrorNode } from 'prosemirror-model'
import { Predicate } from '../types'
export default function findParentNodeClosestToPos($pos: ResolvedPos, predicate: Predicate): ({
pos: number,
start: number,
depth: number,
node: ProseMirrorNode,
} | undefined) {
for (let i = $pos.depth; i > 0; i -= 1) {
const node = $pos.node(i)
if (predicate(node)) {
return {
pos: i > 0 ? $pos.before(i) : 0,
start: $pos.start(i),
depth: i,
node,
}
}
}
}