remove mark before add mark, fix #452

This commit is contained in:
Philipp Kühn 2019-10-04 14:41:07 +02:00
parent 4f3260d685
commit 7a56da6315
2 changed files with 18 additions and 6 deletions

View File

@ -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)
}
}

View File

@ -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)
}
}