improve highlight toggling

This commit is contained in:
Hans Pagel
2020-10-05 17:26:34 +02:00
parent 1d5d1f7c6e
commit 3b01b5b219
3 changed files with 11 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ import { toggleMark as originalToggleMark } from 'prosemirror-commands'
import { MarkType } from 'prosemirror-model'
import { Command } from '../Editor'
import getMarkType from '../utils/getMarkType'
import markIsActive from '../utils/markIsActive'
type ToggleMarkCommand = (typeOrName: string | MarkType, attrs?: {}) => Command
@@ -11,8 +12,13 @@ declare module '../Editor' {
}
}
export const toggleMark: ToggleMarkCommand = (typeOrName, attrs) => ({ state, dispatch }) => {
export const toggleMark: ToggleMarkCommand = (typeOrName, attrs) => ({ state, dispatch, commands }) => {
const type = getMarkType(typeOrName, state.schema)
if (markIsActive(state, type) && !markIsActive(state, type, attrs)) {
// @ts-ignore
return commands.updateMark(type, attrs)
}
return originalToggleMark(type, attrs)(state, dispatch)
}

View File

@@ -3,7 +3,7 @@ import { MarkType } from 'prosemirror-model'
import getMarkAttrs from './getMarkAttrs'
export default function markHasAttributes(state: EditorState, type: MarkType, attrs?: Object) {
return attrs === null || JSON.stringify(attrs) === JSON.stringify(
return attrs === undefined || JSON.stringify(attrs) === JSON.stringify(
getMarkAttrs(state, type),
)
}