Files
tiptap/packages/core/src/commands/toggleMark.ts
Dominik 8c6751f0c6 add precommit hook for linting and automatic eslint fixes + update eslint packages (#2862)
* chore: add precommit hook for eslint fixes, fix linting issues
* chore: add eslint import sort plugin
2022-06-08 14:10:25 +02:00

38 lines
1.1 KiB
TypeScript

import { MarkType } from 'prosemirror-model'
import { getMarkType } from '../helpers/getMarkType'
import { isMarkActive } from '../helpers/isMarkActive'
import { RawCommands } from '../types'
declare module '@tiptap/core' {
interface Commands<ReturnType> {
toggleMark: {
/**
* Toggle a mark on and off.
*/
toggleMark: (
typeOrName: string | MarkType,
attributes?: Record<string, any>,
options?: {
/**
* Removes the mark even across the current selection. Defaults to `false`.
*/
extendEmptyMarkRange?: boolean,
},
) => ReturnType,
}
}
}
export const toggleMark: RawCommands['toggleMark'] = (typeOrName, attributes = {}, options = {}) => ({ state, commands }) => {
const { extendEmptyMarkRange = false } = options
const type = getMarkType(typeOrName, state.schema)
const isActive = isMarkActive(state, type, attributes)
if (isActive) {
return commands.unsetMark(type, { extendEmptyMarkRange })
}
return commands.setMark(type, attributes)
}