tiptap/packages/core/src/commands/unsetMark.ts

30 lines
689 B
TypeScript
Raw Normal View History

2020-11-05 05:38:52 +08:00
import { MarkType } from 'prosemirror-model'
2020-11-17 04:42:35 +08:00
import { Command } from '../types'
2020-11-30 16:42:53 +08:00
import getMarkType from '../helpers/getMarkType'
import getMarkRange from '../helpers/getMarkRange'
2020-11-05 05:38:52 +08:00
2020-11-18 23:43:27 +08:00
/**
* Remove all marks in the current selection.
*/
2020-11-19 00:38:16 +08:00
export const unsetMark = (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => {
2020-11-05 05:38:52 +08:00
const { selection } = tr
const type = getMarkType(typeOrName, state.schema)
let { from, to } = selection
const { $from, empty } = selection
if (empty) {
const range = getMarkRange($from, type)
if (range) {
from = range.from
to = range.to
}
}
if (dispatch) {
tr.removeMark(from, to, type)
}
return true
}