tiptap/packages/core/src/helpers/findChildren.ts

23 lines
473 B
TypeScript
Raw Normal View History

2021-04-03 05:53:04 +08:00
import { Node as ProseMirrorNode } from 'prosemirror-model'
import { Predicate } from '../types'
type NodeWithPos = {
node: ProseMirrorNode,
pos: number,
}
export default function findChildren(node: ProseMirrorNode, predicate: Predicate): NodeWithPos[] {
const nodesWithPos: NodeWithPos[] = []
node.descendants((child, pos) => {
if (predicate(child)) {
nodesWithPos.push({
node: child,
pos,
})
}
})
return nodesWithPos
}