feat: add findChildrenInRange helper

This commit is contained in:
Philipp Kühn
2021-05-17 11:29:41 +02:00
parent 2bd17c7dc6
commit 463661c5ed
4 changed files with 27 additions and 6 deletions

View File

@@ -1,10 +1,5 @@
import { Node as ProseMirrorNode } from 'prosemirror-model'
import { Predicate } from '../types'
type NodeWithPos = {
node: ProseMirrorNode,
pos: number,
}
import { Predicate, NodeWithPos } from '../types'
export default function findChildren(node: ProseMirrorNode, predicate: Predicate): NodeWithPos[] {
const nodesWithPos: NodeWithPos[] = []

View File

@@ -0,0 +1,20 @@
import { Node as ProseMirrorNode } from 'prosemirror-model'
import { Predicate, Range, NodeWithPos } from '../types'
/**
* Same as `findChildren` but searches only within a `range`.
*/
export default function findChildrenInRange(node: ProseMirrorNode, range: Range, predicate: Predicate): NodeWithPos[] {
const nodesWithPos: NodeWithPos[] = []
node.nodesBetween(range.from, range.to, (child, pos) => {
if (predicate(child)) {
nodesWithPos.push({
node: child,
pos,
})
}
})
return nodesWithPos
}