From 7a56da631516f8e620893a63b4d34857942ea44d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 4 Oct 2019 14:41:07 +0200 Subject: [PATCH] remove mark before add mark, fix #452 --- .../tiptap-commands/src/commands/removeMark.js | 9 ++++++--- .../tiptap-commands/src/commands/updateMark.js | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/tiptap-commands/src/commands/removeMark.js b/packages/tiptap-commands/src/commands/removeMark.js index 158caf47a..4c967a698 100644 --- a/packages/tiptap-commands/src/commands/removeMark.js +++ b/packages/tiptap-commands/src/commands/removeMark.js @@ -2,8 +2,9 @@ import { getMarkRange } from 'tiptap-utils' export default function (type) { return (state, dispatch) => { - let { from, to } = state.selection - const { $from, empty } = state.selection + const { tr, selection } = state + let { from, to } = selection + const { $from, empty } = selection if (empty) { const range = getMarkRange($from, type) @@ -12,6 +13,8 @@ export default function (type) { to = range.to } - return dispatch(state.tr.removeMark(from, to, type)) + tr.removeMark(from, to, type) + + return dispatch(tr) } } diff --git a/packages/tiptap-commands/src/commands/updateMark.js b/packages/tiptap-commands/src/commands/updateMark.js index 4c217cf1f..0082ae260 100644 --- a/packages/tiptap-commands/src/commands/updateMark.js +++ b/packages/tiptap-commands/src/commands/updateMark.js @@ -2,8 +2,9 @@ import { getMarkRange } from 'tiptap-utils' export default function (type, attrs) { return (state, dispatch) => { - let { from, to } = state.selection - const { $from, empty } = state.selection + const { tr, selection, doc } = state + let { from, to } = selection + const { $from, empty } = selection if (empty) { const range = getMarkRange($from, type) @@ -12,6 +13,14 @@ export default function (type, attrs) { to = range.to } - return dispatch(state.tr.addMark(from, to, 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) } }