feat: add findChildrenInRange helper
This commit is contained in:
@@ -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[] = []
|
||||
|
||||
20
packages/core/src/helpers/findChildrenInRange.ts
Normal file
20
packages/core/src/helpers/findChildrenInRange.ts
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user