mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-22 16:17:50 +08:00
2f9ba324a0
* fix: InputRules does not work for ranges containing multiple text nodes. #3071 * chore: rename `to` to `sliceEndPos`
22 lines
541 B
TypeScript
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
|
|
}
|