2022-05-20 23:10:30 +08:00
|
|
|
import { ResolvedPos } from 'prosemirror-model'
|
|
|
|
|
2022-06-20 17:45:37 +08:00
|
|
|
export const getTextContentFromNodes = ($from: ResolvedPos, maxMatch = 500) => {
|
2022-05-20 23:10:30 +08:00
|
|
|
let textBefore = ''
|
|
|
|
|
2022-09-29 21:05:03 +08:00
|
|
|
const sliceEndPos = $from.parentOffset
|
|
|
|
|
2022-05-20 23:10:30 +08:00
|
|
|
$from.parent.nodesBetween(
|
2022-09-29 21:05:03 +08:00
|
|
|
Math.max(0, sliceEndPos - maxMatch),
|
|
|
|
sliceEndPos,
|
2022-05-20 23:10:30 +08:00
|
|
|
(node, pos, parent, index) => {
|
2022-09-29 21:05:03 +08:00
|
|
|
const chunk = node.type.spec.toText?.({
|
2022-05-20 23:10:30 +08:00
|
|
|
node, pos, parent, index,
|
2022-09-29 21:05:03 +08:00
|
|
|
}) || node.textContent || '%leaf%'
|
|
|
|
|
|
|
|
textBefore += chunk.slice(0, Math.max(0, sliceEndPos - pos))
|
2022-05-20 23:10:30 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return textBefore
|
|
|
|
}
|