fix: prevent removing inline nodes when using insertContentAt, fix #2156
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user