diff --git a/packages/tiptap-commands/src/commands/updateMark.js b/packages/tiptap-commands/src/commands/updateMark.js index 0082ae260..e359cd01a 100644 --- a/packages/tiptap-commands/src/commands/updateMark.js +++ b/packages/tiptap-commands/src/commands/updateMark.js @@ -2,25 +2,36 @@ import { getMarkRange } from 'tiptap-utils' export default function (type, attrs) { return (state, dispatch) => { - const { tr, selection, doc } = state - let { from, to } = selection - const { $from, empty } = selection + const { + tr, + selection, + doc, + } = state + + const { + ranges, + empty, + } = selection if (empty) { - const range = getMarkRange($from, type) + const { from, to } = getMarkRange(selection.$from, type) + if (doc.rangeHasMark(from, to, type)) { + tr.removeMark(from, to, type) + } - from = range.from - to = range.to + tr.addMark(from, to, type.create(attrs)) + } else { + ranges.forEach(ref$1 => { + const { $to, $from } = ref$1 + + if (doc.rangeHasMark($from.pos, $to.pos, type)) { + tr.removeMark($from.pos, $to.pos, type) + } + + tr.addMark($from.pos, $to.pos, type.create(attrs)) + }) } - const hasMark = doc.rangeHasMark(from, to, type) - - if (hasMark) { - tr.removeMark(from, to, type) - } - - tr.addMark(from, to, type.create(attrs)) - return dispatch(tr) } }