remove insertText from insertContent

This commit is contained in:
Philipp Kühn 2021-05-05 14:13:58 +02:00
parent 63902d4bdb
commit b5c51723ea
3 changed files with 18 additions and 23 deletions

View File

@ -1,7 +1,10 @@
import createNodeFromContent from '../helpers/createNodeFromContent'
import selectionToInsertionEnd from '../helpers/selectionToInsertionEnd'
import {
Command, RawCommands, Content, Range,
Command,
RawCommands,
Content,
Range,
} from '../types'
declare module '@tiptap/core' {
@ -17,19 +20,12 @@ declare module '@tiptap/core' {
export const insertContentAt: RawCommands['insertContentAt'] = (range, value) => ({ tr, dispatch, editor }) => {
if (dispatch) {
const content = createNodeFromContent(value, editor.schema)
if (typeof content === 'string') {
tr.insertText(content)
tr.scrollIntoView()
return true
}
if (range.from !== range.to) {
tr.delete(range.from, range.to)
tr.deleteRange(range.from, range.to)
}
const content = createNodeFromContent(value, editor.schema)
tr.insert(range.from, content)
selectionToInsertionEnd(tr, tr.steps.length - 1, -1)
tr.scrollIntoView()

View File

@ -17,15 +17,19 @@ export default function createNodeFromContent(
content: Content,
schema: Schema,
options?: CreateNodeFromContentOptions,
): string | ProseMirrorNode | Fragment {
): ProseMirrorNode | Fragment {
options = {
slice: true,
parseOptions: {},
...options,
}
if (content && typeof content === 'object') {
if (typeof content === 'object' && content !== null) {
try {
if (Array.isArray(content)) {
return Fragment.fromArray(content.map(item => schema.nodeFromJSON(item)))
}
return schema.nodeFromJSON(content)
} catch (error) {
console.warn(
@ -41,17 +45,11 @@ export default function createNodeFromContent(
}
if (typeof content === 'string') {
const isHTML = content.trim().startsWith('<') && content.trim().endsWith('>')
const parser = DOMParser.fromSchema(schema)
if (isHTML || !options.slice) {
const parser = DOMParser.fromSchema(schema)
return options.slice
? parser.parseSlice(elementFromString(content), options.parseOptions).content
: parser.parse(elementFromString(content), options.parseOptions)
}
return content
return options.slice
? parser.parseSlice(elementFromString(content), options.parseOptions).content
: parser.parse(elementFromString(content), options.parseOptions)
}
return createNodeFromContent('', schema, options)

View File

@ -66,6 +66,7 @@ export type JSONContent = {
type: string,
attrs?: Record<string, any>,
content?: JSONContent[],
text?: string,
[key: string]: any,
}