2021-01-25 20:39:35 +08:00
|
|
|
import { ResolvedPos, Node as ProseMirrorNode } from 'prosemirror-model'
|
|
|
|
import { Predicate } from '../types'
|
2021-01-25 06:28:51 +08:00
|
|
|
|
2021-12-06 19:00:09 +08:00
|
|
|
export function findParentNodeClosestToPos($pos: ResolvedPos, predicate: Predicate): ({
|
2021-01-25 06:28:51 +08:00
|
|
|
pos: number,
|
|
|
|
start: number,
|
|
|
|
depth: number,
|
2021-01-25 20:39:35 +08:00
|
|
|
node: ProseMirrorNode,
|
2021-01-25 06:28:51 +08:00
|
|
|
} | 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|