Files
tiptap/packages/core/src/commands/unsetMark.ts
Philipp Kühn e07a5b625d refactor: Use named exports instead of default exports (#2238)
* use named exports instead of default exports

* fix tests

Co-authored-by: Philipp Kühn <philippkuehn@MacBook-Pro-von-Philipp.local>
2021-12-06 12:00:09 +01:00

56 lines
1.4 KiB
TypeScript

import { MarkType } from 'prosemirror-model'
import { RawCommands } from '../types'
import { getMarkType } from '../helpers/getMarkType'
import { getMarkRange } from '../helpers/getMarkRange'
declare module '@tiptap/core' {
interface Commands<ReturnType> {
unsetMark: {
/**
* Remove all marks in the current selection.
*/
unsetMark: (
typeOrName: string | MarkType,
options?: {
/**
* Removes the mark even across the current selection. Defaults to `false`.
*/
extendEmptyMarkRange?: boolean,
},
) => ReturnType,
}
}
}
export const unsetMark: RawCommands['unsetMark'] = (typeOrName, options = {}) => ({ tr, state, dispatch }) => {
const { extendEmptyMarkRange = false } = options
const { selection } = tr
const type = getMarkType(typeOrName, state.schema)
const { $from, empty, ranges } = selection
if (!dispatch) {
return true
}
if (empty && extendEmptyMarkRange) {
let { from, to } = selection
const attrs = $from.marks().find(mark => mark.type === type)?.attrs
const range = getMarkRange($from, type, attrs)
if (range) {
from = range.from
to = range.to
}
tr.removeMark(from, to, type)
} else {
ranges.forEach(range => {
tr.removeMark(range.$from.pos, range.$to.pos, type)
})
}
tr.removeStoredMark(type)
return true
}