fix some bugs

This commit is contained in:
Philipp Kühn 2020-11-06 00:13:18 +01:00
parent e4eed4ea09
commit dc62ac1326
5 changed files with 28 additions and 23 deletions

View File

@ -4,16 +4,16 @@ import { Command } from '../Editor'
import getMarkType from '../utils/getMarkType'
import markIsActive from '../utils/markIsActive'
export default (typeOrName: string | MarkType, attrs?: {}): Command => ({ state, dispatch, commands }) => {
export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ state, dispatch, commands }) => {
const type = getMarkType(typeOrName, state.schema)
const hasMarkWithDifferentAttributes = attrs
const hasMarkWithDifferentAttributes = attributes
&& markIsActive(state, type)
&& !markIsActive(state, type, attrs)
&& !markIsActive(state, type, attributes)
if (attrs && hasMarkWithDifferentAttributes) {
return commands.updateMarkAttributes(type, attrs)
if (attributes && hasMarkWithDifferentAttributes) {
return commands.updateMarkAttributes(type, attributes)
}
return toggleMark(type)(state, dispatch)
return toggleMark(type, attributes)(state, dispatch)
}

View File

@ -2,10 +2,11 @@ import { EditorState } from 'prosemirror-state'
import { Mark, MarkType } from 'prosemirror-model'
export default function getMarkAttrs(state: EditorState, type: MarkType) {
const { from, to } = state.selection
const { from, to, empty } = state.selection
let marks: Mark[] = []
state.doc.nodesBetween(from, to, node => {
// TODO: -1 only for inclusive marks?
state.doc.nodesBetween(empty ? from - 1 : from, to, node => {
marks = [...marks, ...node.marks]
})

View File

@ -2,12 +2,17 @@ import { EditorState } from 'prosemirror-state'
import { MarkType } from 'prosemirror-model'
import getMarkAttrs from './getMarkAttrs'
import { AnyObject } from '../types'
import isEmptyObject from './isEmptyObject'
export default function markHasAttributes(state: EditorState, type: MarkType, attributes: AnyObject) {
if (isEmptyObject(attributes)) {
return true
}
export default function markHasAttributes(state: EditorState, type: MarkType, attrs: AnyObject) {
const originalAttrs = getMarkAttrs(state, type)
return !!Object
.keys(attrs)
.filter(key => attrs[key] === originalAttrs[key])
.keys(attributes)
.filter(key => attributes[key] === originalAttrs[key])
.length
}

View File

@ -1,8 +1,9 @@
import { EditorState } from 'prosemirror-state'
import { MarkType } from 'prosemirror-model'
import markHasAttributes from './markHasAttributes'
import isEmptyObject from './isEmptyObject'
export default function markIsActive(state: EditorState, type: MarkType, attrs = {}) {
export default function markIsActive(state: EditorState, type: MarkType, attributes = {}) {
const {
from,
$from,
@ -10,11 +11,13 @@ export default function markIsActive(state: EditorState, type: MarkType, attrs =
empty,
} = state.selection
const hasAttributes = markHasAttributes(state, type, attrs)
const hasMark = empty
? !!(type.isInSet(state.storedMarks || $from.marks()))
: state.doc.rangeHasMark(from, to, type)
if (empty) {
return (type.isInSet(state.storedMarks || $from.marks()) && hasAttributes)
}
const hasAttributes = markHasAttributes(state, type, attributes)
return (state.doc.rangeHasMark(from, to, type) && hasAttributes)
return isEmptyObject(attributes)
? hasMark
: hasMark && hasAttributes
}

View File

@ -2,10 +2,6 @@ import {
Command, createMark, markInputRule, markPasteRule,
} from '@tiptap/core'
export interface HighlightOptions {
color: string,
}
export const inputRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))$/gm
export const pasteRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))/gm
@ -54,8 +50,8 @@ const Highlight = createMark({
addCommands() {
return {
highlight: (attrs?: HighlightOptions): Command => ({ commands }) => {
return commands.toggleMark('highlight', attrs)
highlight: (attributes?: { color: string }): Command => ({ commands }) => {
return commands.toggleMark('highlight', attributes)
},
}
},