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 createNodeFromContent from '../helpers/createNodeFromContent'
|
||||||
import selectionToInsertionEnd from '../helpers/selectionToInsertionEnd'
|
import selectionToInsertionEnd from '../helpers/selectionToInsertionEnd'
|
||||||
import {
|
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 }) => {
|
export const insertContentAt: RawCommands['insertContentAt'] = (position, value, options) => ({ tr, dispatch, editor }) => {
|
||||||
if (dispatch) {
|
if (dispatch) {
|
||||||
options = {
|
options = {
|
||||||
@@ -50,8 +54,11 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value,
|
|||||||
: position
|
: position
|
||||||
|
|
||||||
let isOnlyBlockContent = true
|
let isOnlyBlockContent = true
|
||||||
|
const nodes = isFragment(content)
|
||||||
|
? content
|
||||||
|
: [content]
|
||||||
|
|
||||||
content.forEach(node => {
|
nodes.forEach(node => {
|
||||||
isOnlyBlockContent = isOnlyBlockContent
|
isOnlyBlockContent = isOnlyBlockContent
|
||||||
? node.isBlock
|
? node.isBlock
|
||||||
: false
|
: false
|
||||||
@@ -63,10 +70,10 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value,
|
|||||||
// replace an empty paragraph by an inserted image
|
// replace an empty paragraph by an inserted image
|
||||||
// instead of inserting the image below the paragraph
|
// instead of inserting the image below the paragraph
|
||||||
if (from === to && isOnlyBlockContent) {
|
if (from === to && isOnlyBlockContent) {
|
||||||
const $from = tr.doc.resolve(from)
|
const { parent } = tr.doc.resolve(from)
|
||||||
const isEmptyTextBlock = $from.parent.isTextblock
|
const isEmptyTextBlock = parent.isTextblock
|
||||||
&& !$from.parent.type.spec.code
|
&& !parent.type.spec.code
|
||||||
&& !$from.parent.textContent
|
&& !parent.childCount
|
||||||
|
|
||||||
if (isEmptyTextBlock) {
|
if (isEmptyTextBlock) {
|
||||||
from -= 1
|
from -= 1
|
||||||
|
|||||||
Reference in New Issue
Block a user