From 370966847efd7bbc1b033cf5952f2427e62c1525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 28 May 2021 17:37:28 +0200 Subject: [PATCH] fix: fix error when calculating contentMatch within clearNodes command, fix #1361 --- packages/core/src/commands/clearNodes.ts | 38 ++++++++++++++---------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/core/src/commands/clearNodes.ts b/packages/core/src/commands/clearNodes.ts index ac4bbba057e..7e4cfd59f63 100644 --- a/packages/core/src/commands/clearNodes.ts +++ b/packages/core/src/commands/clearNodes.ts @@ -18,22 +18,28 @@ export const clearNodes: RawCommands['clearNodes'] = () => ({ state, tr, dispatc ranges.forEach(range => { state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => { - if (!node.type.isText) { - const fromPos = tr.doc.resolve(tr.mapping.map(pos + 1)) - const toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize - 1)) - const nodeRange = fromPos.blockRange(toPos) - - if (nodeRange) { - const targetLiftDepth = liftTarget(nodeRange) - - if (node.type.isTextblock && dispatch) { - tr.setNodeMarkup(nodeRange.start, state.doc.type.contentMatch.defaultType) - } - - if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) { - tr.lift(nodeRange, targetLiftDepth) - } - } + if (node.type.isText) { + return + } + + 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 + } + + const targetLiftDepth = liftTarget(nodeRange) + + 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) } }) })