# Conflicts: # docs/src/docPages/api/extensions.md # docs/src/links.yaml # packages/core/src/extensions/toggleMark.ts
20 lines
655 B
TypeScript
20 lines
655 B
TypeScript
import { toggleMark } from 'prosemirror-commands'
|
|
import { MarkType } from 'prosemirror-model'
|
|
import { Command } from '../Editor'
|
|
import getMarkType from '../utils/getMarkType'
|
|
import markIsActive from '../utils/markIsActive'
|
|
|
|
export default (typeOrName: string | MarkType, attrs?: {}): Command => ({ state, dispatch, commands }) => {
|
|
const type = getMarkType(typeOrName, state.schema)
|
|
|
|
const hasMarkWithDifferentAttributes = attrs
|
|
&& markIsActive(state, type)
|
|
&& !markIsActive(state, type, attrs)
|
|
|
|
if (attrs && hasMarkWithDifferentAttributes) {
|
|
return commands.updateMark(type, attrs)
|
|
}
|
|
|
|
return toggleMark(type)(state, dispatch)
|
|
}
|