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`
This commit is contained in:
hamflx 2022-09-29 21:05:03 +08:00 committed by GitHub
parent 539afce4fd
commit 2f9ba324a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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