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,22 +18,28 @@ 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)
if (nodeRange) { const $fromPos = tr.doc.resolve(tr.mapping.map(pos))
const targetLiftDepth = liftTarget(nodeRange) const $toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize))
const nodeRange = $fromPos.blockRange($toPos)
if (node.type.isTextblock && dispatch) { if (!nodeRange) {
tr.setNodeMarkup(nodeRange.start, state.doc.type.contentMatch.defaultType) return
} }
if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) { const targetLiftDepth = liftTarget(nodeRange)
tr.lift(nodeRange, targetLiftDepth)
} if (node.type.isTextblock && dispatch) {
} const { defaultType } = $fromPos.parent.contentMatchAt($fromPos.index())
tr.setNodeMarkup(nodeRange.start, defaultType)
}
if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) {
tr.lift(nodeRange, targetLiftDepth)
} }
}) })
}) })