tiptap/packages/core/src/helpers/getTextContentFromNodes.ts
hamflx 2f9ba324a0
fix(core): InputRules does not work for ranges containing multiple text nodes (#3205)
* fix: InputRules does not work for ranges containing multiple text nodes. #3071

* chore: rename `to` to `sliceEndPos`
2022-09-29 15:05:03 +02:00

22 lines
541 B
TypeScript

import { ResolvedPos } from 'prosemirror-model'
export const getTextContentFromNodes = ($from: ResolvedPos, maxMatch = 500) => {
let textBefore = ''
const sliceEndPos = $from.parentOffset
$from.parent.nodesBetween(
Math.max(0, sliceEndPos - maxMatch),
sliceEndPos,
(node, pos, parent, index) => {
const chunk = node.type.spec.toText?.({
node, pos, parent, index,
}) || node.textContent || '%leaf%'
textBefore += chunk.slice(0, Math.max(0, sliceEndPos - pos))
},
)
return textBefore
}