diff --git a/packages/tiptap-commands/src/commands/removeMark.js b/packages/tiptap-commands/src/commands/removeMark.js index 6483d2749..158caf47a 100644 --- a/packages/tiptap-commands/src/commands/removeMark.js +++ b/packages/tiptap-commands/src/commands/removeMark.js @@ -1,6 +1,17 @@ +import { getMarkRange } from 'tiptap-utils' + export default function (type) { return (state, dispatch) => { - const { from, to } = state.selection + let { from, to } = state.selection + const { $from, empty } = state.selection + + if (empty) { + const range = getMarkRange($from, type) + + from = range.from + to = range.to + } + return dispatch(state.tr.removeMark(from, to, type)) } } diff --git a/packages/tiptap-commands/src/commands/updateMark.js b/packages/tiptap-commands/src/commands/updateMark.js index eb6c4a16b..4c217cf1f 100644 --- a/packages/tiptap-commands/src/commands/updateMark.js +++ b/packages/tiptap-commands/src/commands/updateMark.js @@ -1,6 +1,17 @@ +import { getMarkRange } from 'tiptap-utils' + export default function (type, attrs) { return (state, dispatch) => { - const { from, to } = state.selection + let { from, to } = state.selection + const { $from, empty } = state.selection + + if (empty) { + const range = getMarkRange($from, type) + + from = range.from + to = range.to + } + return dispatch(state.tr.addMark(from, to, type.create(attrs))) } }