From 53ffce501835f727dbc39f07a8759903245a3f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 10 Nov 2021 21:16:02 +0100 Subject: [PATCH] fix: prevent removing inline nodes when using insertContentAt, fix #2156 --- packages/core/src/commands/insertContentAt.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/core/src/commands/insertContentAt.ts b/packages/core/src/commands/insertContentAt.ts index 10f1f69f..87e7890c 100644 --- a/packages/core/src/commands/insertContentAt.ts +++ b/packages/core/src/commands/insertContentAt.ts @@ -1,4 +1,4 @@ -import { ParseOptions } from 'prosemirror-model' +import { Fragment, Node as ProseMirrorNode, ParseOptions } from 'prosemirror-model' import createNodeFromContent from '../helpers/createNodeFromContent' import selectionToInsertionEnd from '../helpers/selectionToInsertionEnd' import { @@ -25,6 +25,10 @@ declare module '@tiptap/core' { } } +const isFragment = (nodeOrFragment: ProseMirrorNode | Fragment): nodeOrFragment is Fragment => { + return nodeOrFragment.toString().startsWith('<') +} + export const insertContentAt: RawCommands['insertContentAt'] = (position, value, options) => ({ tr, dispatch, editor }) => { if (dispatch) { options = { @@ -50,8 +54,11 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value, : position let isOnlyBlockContent = true + const nodes = isFragment(content) + ? content + : [content] - content.forEach(node => { + nodes.forEach(node => { isOnlyBlockContent = isOnlyBlockContent ? node.isBlock : false @@ -63,10 +70,10 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value, // replace an empty paragraph by an inserted image // instead of inserting the image below the paragraph if (from === to && isOnlyBlockContent) { - const $from = tr.doc.resolve(from) - const isEmptyTextBlock = $from.parent.isTextblock - && !$from.parent.type.spec.code - && !$from.parent.textContent + const { parent } = tr.doc.resolve(from) + const isEmptyTextBlock = parent.isTextblock + && !parent.type.spec.code + && !parent.childCount if (isEmptyTextBlock) { from -= 1