From 9f2c36896b5b8510cea87d727dc4e94742fdf980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Thu, 14 Oct 2021 12:08:39 +0200 Subject: [PATCH] feat: add `updateSelection` option to `insertContentAt` command --- packages/core/src/commands/insertContent.ts | 10 ++++++-- packages/core/src/commands/insertContentAt.ts | 24 +++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/core/src/commands/insertContent.ts b/packages/core/src/commands/insertContent.ts index 5acd40b7..adebb699 100644 --- a/packages/core/src/commands/insertContent.ts +++ b/packages/core/src/commands/insertContent.ts @@ -1,4 +1,4 @@ -import { CreateNodeFromContentOptions } from '../helpers/createNodeFromContent' +import { ParseOptions } from 'prosemirror-model' import { RawCommands, Content } from '../types' declare module '@tiptap/core' { @@ -7,7 +7,13 @@ declare module '@tiptap/core' { /** * Insert a node or string of HTML at the current position. */ - insertContent: (value: Content, options?: CreateNodeFromContentOptions) => ReturnType, + insertContent: ( + value: Content, + options?: { + parseOptions: ParseOptions, + updateSelection: boolean, + }, + ) => ReturnType, } } } diff --git a/packages/core/src/commands/insertContentAt.ts b/packages/core/src/commands/insertContentAt.ts index db971232..b92811f4 100644 --- a/packages/core/src/commands/insertContentAt.ts +++ b/packages/core/src/commands/insertContentAt.ts @@ -1,4 +1,5 @@ -import createNodeFromContent, { CreateNodeFromContentOptions } from '../helpers/createNodeFromContent' +import { ParseOptions } from 'prosemirror-model' +import createNodeFromContent from '../helpers/createNodeFromContent' import selectionToInsertionEnd from '../helpers/selectionToInsertionEnd' import { RawCommands, @@ -12,18 +13,31 @@ declare module '@tiptap/core' { /** * Insert a node or string of HTML at a specific position. */ - insertContentAt: (position: number | Range, value: Content, options?: CreateNodeFromContentOptions) => ReturnType, + insertContentAt: ( + position: number | Range, + value: Content, + options?: { + parseOptions: ParseOptions, + updateSelection: boolean, + }, + ) => ReturnType, } } } export const insertContentAt: RawCommands['insertContentAt'] = (position, value, options) => ({ tr, dispatch, editor }) => { if (dispatch) { + options = { + parseOptions: {}, + updateSelection: true, + ...options, + } + const content = createNodeFromContent(value, editor.schema, { parseOptions: { preserveWhitespace: 'full', + ...options.parseOptions, }, - ...(options || {}), }) // don’t dispatch an empty fragment because this can lead to strange errors @@ -38,7 +52,9 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value, tr.replaceWith(from, to, content) // set cursor at end of inserted content - selectionToInsertionEnd(tr, tr.steps.length - 1, 1) + if (options.updateSelection) { + selectionToInsertionEnd(tr, tr.steps.length - 1, 1) + } } return true