mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-01 06:37:51 +08:00
39 lines
892 B
TypeScript
39 lines
892 B
TypeScript
import { MarkType } from 'prosemirror-model'
|
|
import { Command, RawCommands } from '../types'
|
|
import getMarkType from '../helpers/getMarkType'
|
|
import getMarkRange from '../helpers/getMarkRange'
|
|
|
|
declare module '@tiptap/core' {
|
|
interface Commands {
|
|
unsetMark: {
|
|
/**
|
|
* Remove all marks in the current selection.
|
|
*/
|
|
unsetMark: (typeOrName: string | MarkType) => Command,
|
|
}
|
|
}
|
|
}
|
|
|
|
export const unsetMark: RawCommands['unsetMark'] = typeOrName => ({ tr, state, dispatch }) => {
|
|
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)
|
|
tr.removeStoredMark(type)
|
|
}
|
|
|
|
return true
|
|
}
|