add insertNode command

This commit is contained in:
Philipp Kühn
2021-04-02 21:55:25 +02:00
parent 4d882af5d7
commit a5e1030e4f
3 changed files with 35 additions and 0 deletions

View File

@@ -132,6 +132,7 @@ Have a look at all of the core commands listed below. They should give you a goo
| --------------- | ------------------------------------------------ | | --------------- | ------------------------------------------------ |
| .clearContent() | Clear the whole document. | | .clearContent() | Clear the whole document. |
| .insertHTML() | Insert a string of HTML at the current position. | | .insertHTML() | Insert a string of HTML at the current position. |
| .insertNode() | Insert a node at the current position. |
| .insertText() | Insert a string of text at the current position. | | .insertText() | Insert a string of text at the current position. |
| .setContent() | Replace the whole document with new content. | | .setContent() | Replace the whole document with new content. |

View File

@@ -0,0 +1,31 @@
import { NodeType } from 'prosemirror-model'
import getNodeType from '../helpers/getNodeType'
import { Command, RawCommands, AnyObject } from '../types'
declare module '@tiptap/core' {
interface Commands {
insertNode: {
/**
* Insert a node at the current position.
*/
insertNode: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
}
export const insertNode: RawCommands['insertNode'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
const { selection } = tr
const type = getNodeType(typeOrName, state.schema)
if (!type) {
return false
}
const node = type.create(attributes)
if (dispatch) {
tr.insert(selection.anchor, node)
}
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 insertHTML from '../commands/insertHTML' import * as insertHTML from '../commands/insertHTML'
import * as insertNode from '../commands/insertNode'
import * as insertText from '../commands/insertText' import * as insertText from '../commands/insertText'
import * as joinBackward from '../commands/joinBackward' import * as joinBackward from '../commands/joinBackward'
import * as joinForward from '../commands/joinForward' import * as joinForward from '../commands/joinForward'
@@ -58,6 +59,7 @@ export { extendMarkRange }
export { first } export { first }
export { focus } export { focus }
export { insertHTML } export { insertHTML }
export { insertNode }
export { insertText } export { insertText }
export { joinBackward } export { joinBackward }
export { joinForward } export { joinForward }
@@ -109,6 +111,7 @@ export const Commands = Extension.create({
...first, ...first,
...focus, ...focus,
...insertHTML, ...insertHTML,
...insertNode,
...insertText, ...insertText,
...joinBackward, ...joinBackward,
...joinForward, ...joinForward,