From a5e1030e4fcb4e75787e8cc3979037ae6f2ad1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 2 Apr 2021 21:55:25 +0200 Subject: [PATCH] add insertNode command --- docs/src/docPages/api/commands.md | 1 + packages/core/src/commands/insertNode.ts | 31 ++++++++++++++++++++++++ packages/core/src/extensions/commands.ts | 3 +++ 3 files changed, 35 insertions(+) create mode 100644 packages/core/src/commands/insertNode.ts diff --git a/docs/src/docPages/api/commands.md b/docs/src/docPages/api/commands.md index 23fd60e9..4419036a 100644 --- a/docs/src/docPages/api/commands.md +++ b/docs/src/docPages/api/commands.md @@ -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. | | .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. | | .setContent() | Replace the whole document with new content. | diff --git a/packages/core/src/commands/insertNode.ts b/packages/core/src/commands/insertNode.ts new file mode 100644 index 00000000..412851e2 --- /dev/null +++ b/packages/core/src/commands/insertNode.ts @@ -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 +} diff --git a/packages/core/src/extensions/commands.ts b/packages/core/src/extensions/commands.ts index a8c13067..5bce588b 100644 --- a/packages/core/src/extensions/commands.ts +++ b/packages/core/src/extensions/commands.ts @@ -12,6 +12,7 @@ import * as extendMarkRange from '../commands/extendMarkRange' import * as first from '../commands/first' import * as focus from '../commands/focus' import * as insertHTML from '../commands/insertHTML' +import * as insertNode from '../commands/insertNode' import * as insertText from '../commands/insertText' import * as joinBackward from '../commands/joinBackward' import * as joinForward from '../commands/joinForward' @@ -58,6 +59,7 @@ export { extendMarkRange } export { first } export { focus } export { insertHTML } +export { insertNode } export { insertText } export { joinBackward } export { joinForward } @@ -109,6 +111,7 @@ export const Commands = Extension.create({ ...first, ...focus, ...insertHTML, + ...insertNode, ...insertText, ...joinBackward, ...joinForward,