add replacetext command
This commit is contained in:
20
packages/tiptap-commands/src/commands/replaceText.js
Normal file
20
packages/tiptap-commands/src/commands/replaceText.js
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,6 +40,7 @@ import {
|
|||||||
|
|
||||||
import markInputRule from './commands/markInputRule'
|
import markInputRule from './commands/markInputRule'
|
||||||
import removeMark from './commands/removeMark'
|
import removeMark from './commands/removeMark'
|
||||||
|
import replaceText from './commands/replaceText'
|
||||||
import setInlineBlockType from './commands/setInlineBlockType'
|
import setInlineBlockType from './commands/setInlineBlockType'
|
||||||
import splitToDefaultListItem from './commands/splitToDefaultListItem'
|
import splitToDefaultListItem from './commands/splitToDefaultListItem'
|
||||||
import toggleBlockType from './commands/toggleBlockType'
|
import toggleBlockType from './commands/toggleBlockType'
|
||||||
@@ -87,6 +88,7 @@ export {
|
|||||||
// custom
|
// custom
|
||||||
markInputRule,
|
markInputRule,
|
||||||
removeMark,
|
removeMark,
|
||||||
|
replaceText,
|
||||||
setInlineBlockType,
|
setInlineBlockType,
|
||||||
splitToDefaultListItem,
|
splitToDefaultListItem,
|
||||||
toggleBlockType,
|
toggleBlockType,
|
||||||
|
|||||||
Reference in New Issue
Block a user