tiptap/packages/core/src/helpers/findParentNodeClosestToPos.ts
Dominik 8c6751f0c6
add precommit hook for linting and automatic eslint fixes + update eslint packages (#2862)
* chore: add precommit hook for eslint fixes, fix linting issues
* chore: add eslint import sort plugin
2022-06-08 14:10:25 +02:00

24 lines
531 B
TypeScript

import { Node as ProseMirrorNode, ResolvedPos } from 'prosemirror-model'
import { Predicate } from '../types'
export 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,
}
}
}
}