Skip to content

Commit

Permalink
fix: insertContent keeps marks when using plain text, fix #2406
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Kühn authored and Philipp Kühn committed Jan 20, 2022
1 parent 27983bf commit d242706
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/core/src/commands/insertContentAt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit d242706

Please sign in to comment.