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

39 lines
892 B
TypeScript
Raw Normal View History

2020-11-05 05:38:52 +08:00
import { MarkType } from 'prosemirror-model'
2021-02-17 01:36:37 +08:00
import { Command, RawCommands } 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
2021-02-11 01:05:02 +08:00
declare module '@tiptap/core' {
2021-02-17 01:39:37 +08:00
interface Commands {
2021-02-16 18:27:58 +08:00
unsetMark: {
/**
* Remove all marks in the current selection.
*/
unsetMark: (typeOrName: string | MarkType) => Command,
}
2021-02-11 01:05:02 +08:00
}
}
2021-02-17 01:36:37 +08:00
export const unsetMark: RawCommands['unsetMark'] = typeOrName => ({ 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)
2020-12-01 19:45:30 +08:00
tr.removeStoredMark(type)
2020-11-05 05:38:52 +08:00
}
return true
}