tiptap/packages/core/src/helpers/findChildrenInRange.ts
Dominik 8c6751f0c6
add precommit hook for linting and automatic eslint fixes + update eslint packages (#2862)
* chore: add precommit hook for eslint fixes, fix linting issues
* chore: add eslint import sort plugin
2022-06-08 14:10:25 +02:00

33 lines
751 B
TypeScript

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