diff --git a/packages/tiptap-commands/src/commands/replaceText.js b/packages/tiptap-commands/src/commands/replaceText.js new file mode 100644 index 00000000..af410be8 --- /dev/null +++ b/packages/tiptap-commands/src/commands/replaceText.js @@ -0,0 +1,20 @@ +export default function (pos, type, attrs = {}) { + return (state, dispatch) => { + const { $from } = state.selection + const index = $from.index() + + if (!$from.parent.canReplaceWith(index, index, type)) { + return false + } + + if (dispatch) { + // const transform = state.tr + // .replaceWith(pos.from, pos.to, state.schema.nodes.mention.create({ id: 2, type: 'user', label: 'loool' })) + const transform = state.tr.replaceWith(pos.from, pos.to, type.create(attrs)) + + dispatch(transform) + } + + return true + } +} diff --git a/packages/tiptap-commands/src/index.js b/packages/tiptap-commands/src/index.js index 020df252..e2d9a318 100644 --- a/packages/tiptap-commands/src/index.js +++ b/packages/tiptap-commands/src/index.js @@ -40,6 +40,7 @@ import { import markInputRule from './commands/markInputRule' import removeMark from './commands/removeMark' +import replaceText from './commands/replaceText' import setInlineBlockType from './commands/setInlineBlockType' import splitToDefaultListItem from './commands/splitToDefaultListItem' import toggleBlockType from './commands/toggleBlockType' @@ -87,6 +88,7 @@ export { // custom markInputRule, removeMark, + replaceText, setInlineBlockType, splitToDefaultListItem, toggleBlockType,