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

View File

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

View File

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