2021-05-17 17:29:41 +08:00
|
|
|
import { Node as ProseMirrorNode } from 'prosemirror-model'
|
2022-06-08 20:10:25 +08:00
|
|
|
|
|
|
|
import { NodeWithPos, Predicate, Range } from '../types'
|
2021-05-17 17:29:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as `findChildren` but searches only within a `range`.
|
|
|
|
*/
|
2021-12-06 19:00:09 +08:00
|
|
|
export function findChildrenInRange(node: ProseMirrorNode, range: Range, predicate: Predicate): NodeWithPos[] {
|
2021-05-17 17:29:41 +08:00
|
|
|
const nodesWithPos: NodeWithPos[] = []
|
|
|
|
|
2021-05-18 03:00:22 +08:00
|
|
|
// if (range.from === range.to) {
|
|
|
|
// const nodeAt = node.nodeAt(range.from)
|
|
|
|
|
|
|
|
// if (nodeAt) {
|
|
|
|
// nodesWithPos.push({
|
|
|
|
// node: nodeAt,
|
|
|
|
// pos: range.from,
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2021-05-17 17:29:41 +08:00
|
|
|
node.nodesBetween(range.from, range.to, (child, pos) => {
|
|
|
|
if (predicate(child)) {
|
|
|
|
nodesWithPos.push({
|
|
|
|
node: child,
|
|
|
|
pos,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return nodesWithPos
|
|
|
|
}
|