From d2427064ff1765004173f3c718cd1e4eba4dc355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BChn?= Date: Thu, 20 Jan 2022 22:15:12 +0100 Subject: [PATCH] fix: insertContent keeps marks when using plain text, fix #2406 --- packages/core/src/commands/insertContentAt.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/core/src/commands/insertContentAt.ts b/packages/core/src/commands/insertContentAt.ts index d94a88e4..6ab9db80 100644 --- a/packages/core/src/commands/insertContentAt.ts +++ b/packages/core/src/commands/insertContentAt.ts @@ -53,6 +53,7 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value, ? { from: position, to: position } : position + let isOnlyTextContent = true let isOnlyBlockContent = true const nodes = isFragment(content) ? content @@ -62,6 +63,10 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value, // check if added node is valid node.check() + isOnlyTextContent = isOnlyTextContent + ? node.isText && node.marks.length === 0 + : false + isOnlyBlockContent = isOnlyBlockContent ? node.isBlock : false @@ -84,7 +89,13 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value, } } - tr.replaceWith(from, to, content) + // if there is only plain text we have to use `insertText` + // because this will keep the current marks + if (isOnlyTextContent) { + tr.insertText(value as string, from, to) + } else { + tr.replaceWith(from, to, content) + } // set cursor at end of inserted content if (options.updateSelection) {