Skip to content

Commit

Permalink
fix: prevent removing inline nodes when using insertContentAt, fix #2156
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Nov 10, 2021
1 parent 75e55e5 commit 53ffce5
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions packages/core/src/commands/insertContentAt.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 = {
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 53ffce5

Please sign in to comment.