fix: fix error when calculating contentMatch within clearNodes command, fix #1361

This commit is contained in:
Philipp Kühn
2021-05-28 17:37:28 +02:00
parent a24e33350c
commit 370966847e

View File

@@ -18,23 +18,29 @@ export const clearNodes: RawCommands['clearNodes'] = () => ({ state, tr, dispatc
ranges.forEach(range => { ranges.forEach(range => {
state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => { state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
if (!node.type.isText) { if (node.type.isText) {
const fromPos = tr.doc.resolve(tr.mapping.map(pos + 1)) return
const toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize - 1)) }
const nodeRange = fromPos.blockRange(toPos)
const $fromPos = tr.doc.resolve(tr.mapping.map(pos))
const $toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize))
const nodeRange = $fromPos.blockRange($toPos)
if (!nodeRange) {
return
}
if (nodeRange) {
const targetLiftDepth = liftTarget(nodeRange) const targetLiftDepth = liftTarget(nodeRange)
if (node.type.isTextblock && dispatch) { if (node.type.isTextblock && dispatch) {
tr.setNodeMarkup(nodeRange.start, state.doc.type.contentMatch.defaultType) const { defaultType } = $fromPos.parent.contentMatchAt($fromPos.index())
tr.setNodeMarkup(nodeRange.start, defaultType)
} }
if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) { if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) {
tr.lift(nodeRange, targetLiftDepth) tr.lift(nodeRange, targetLiftDepth)
} }
}
}
}) })
}) })