add insertContentAt command

This commit is contained in:
Philipp Kühn
2021-05-05 13:00:30 +02:00
parent ab3261e12b
commit 9ff7e2400e
3 changed files with 44 additions and 23 deletions

View File

@@ -1,5 +1,3 @@
import createNodeFromContent from '../helpers/createNodeFromContent'
import selectionToInsertionEnd from '../helpers/selectionToInsertionEnd'
import { Command, RawCommands, Content } from '../types' import { Command, RawCommands, Content } from '../types'
declare module '@tiptap/core' { declare module '@tiptap/core' {
@@ -13,25 +11,6 @@ declare module '@tiptap/core' {
} }
} }
export const insertContent: RawCommands['insertContent'] = value => ({ tr, dispatch, editor }) => { export const insertContent: RawCommands['insertContent'] = value => ({ tr, commands }) => {
if (dispatch) { return commands.insertContentAt({ from: tr.selection.from, to: tr.selection.to }, value)
const content = createNodeFromContent(value, editor.schema)
if (typeof content === 'string') {
tr.insertText(content)
tr.scrollIntoView()
return true
}
if (!tr.selection.empty) {
tr.deleteSelection()
}
tr.insert(tr.selection.anchor, content)
selectionToInsertionEnd(tr, tr.steps.length - 1, -1)
tr.scrollIntoView()
}
return true
} }

View File

@@ -0,0 +1,39 @@
import createNodeFromContent from '../helpers/createNodeFromContent'
import selectionToInsertionEnd from '../helpers/selectionToInsertionEnd'
import {
Command, RawCommands, Content, Range,
} from '../types'
declare module '@tiptap/core' {
interface Commands {
insertContentAt: {
/**
* Insert a node or string of HTML at the current position.
*/
insertContentAt: (range: Range, value: Content) => Command,
}
}
}
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 (!tr.selection.empty) {
tr.deleteRange(range.from, range.to)
}
tr.insert(range.from, content)
selectionToInsertionEnd(tr, tr.steps.length - 1, -1)
tr.scrollIntoView()
}
return true
}

View File

@@ -12,6 +12,7 @@ import * as extendMarkRange from '../commands/extendMarkRange'
import * as first from '../commands/first' import * as first from '../commands/first'
import * as focus from '../commands/focus' import * as focus from '../commands/focus'
import * as insertContent from '../commands/insertContent' import * as insertContent from '../commands/insertContent'
import * as insertContentAt from '../commands/insertContentAt'
import * as insertHTML from '../commands/insertHTML' import * as insertHTML from '../commands/insertHTML'
import * as insertNode from '../commands/insertNode' import * as insertNode from '../commands/insertNode'
import * as insertText from '../commands/insertText' import * as insertText from '../commands/insertText'
@@ -64,6 +65,7 @@ export { extendMarkRange }
export { first } export { first }
export { focus } export { focus }
export { insertContent } export { insertContent }
export { insertContentAt }
export { insertHTML } export { insertHTML }
export { insertNode } export { insertNode }
export { insertText } export { insertText }
@@ -121,6 +123,7 @@ export const Commands = Extension.create({
...first, ...first,
...focus, ...focus,
...insertContent, ...insertContent,
...insertContentAt,
...insertHTML, ...insertHTML,
...insertNode, ...insertNode,
...insertText, ...insertText,