mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-18 14:13:21 +08:00
feat: add updateSelection
option to insertContentAt
command
This commit is contained in:
parent
4303637a78
commit
9f2c36896b
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user