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

@@ -1,7 +1,9 @@
<template> <template>
<div v-if="editor"> <div v-if="editor">
<button <button
@click="editor.chain().focus().highlight().run()" @click="editor.chain().focus().highlight({
color: ''
}).run()"
:class="{ 'is-active': editor.isActive('highlight', { :class="{ 'is-active': editor.isActive('highlight', {
color: '' color: ''
}) }" }) }"

View File

@@ -2,6 +2,7 @@ import { toggleMark as originalToggleMark } from 'prosemirror-commands'
import { MarkType } from 'prosemirror-model' import { MarkType } from 'prosemirror-model'
import { Command } from '../Editor' import { Command } from '../Editor'
import getMarkType from '../utils/getMarkType' import getMarkType from '../utils/getMarkType'
import markIsActive from '../utils/markIsActive'
type ToggleMarkCommand = (typeOrName: string | MarkType, attrs?: {}) => Command 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) 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) return originalToggleMark(type, attrs)(state, dispatch)
} }

View File

@@ -3,7 +3,7 @@ import { MarkType } from 'prosemirror-model'
import getMarkAttrs from './getMarkAttrs' import getMarkAttrs from './getMarkAttrs'
export default function markHasAttributes(state: EditorState, type: MarkType, attrs?: Object) { 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), getMarkAttrs(state, type),
) )
} }