fix some bugs
This commit is contained in:
@@ -4,16 +4,16 @@ import { Command } from '../Editor'
|
|||||||
import getMarkType from '../utils/getMarkType'
|
import getMarkType from '../utils/getMarkType'
|
||||||
import markIsActive from '../utils/markIsActive'
|
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 type = getMarkType(typeOrName, state.schema)
|
||||||
|
|
||||||
const hasMarkWithDifferentAttributes = attrs
|
const hasMarkWithDifferentAttributes = attributes
|
||||||
&& markIsActive(state, type)
|
&& markIsActive(state, type)
|
||||||
&& !markIsActive(state, type, attrs)
|
&& !markIsActive(state, type, attributes)
|
||||||
|
|
||||||
if (attrs && hasMarkWithDifferentAttributes) {
|
if (attributes && hasMarkWithDifferentAttributes) {
|
||||||
return commands.updateMarkAttributes(type, attrs)
|
return commands.updateMarkAttributes(type, attributes)
|
||||||
}
|
}
|
||||||
|
|
||||||
return toggleMark(type)(state, dispatch)
|
return toggleMark(type, attributes)(state, dispatch)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ import { EditorState } from 'prosemirror-state'
|
|||||||
import { Mark, MarkType } from 'prosemirror-model'
|
import { Mark, MarkType } from 'prosemirror-model'
|
||||||
|
|
||||||
export default function getMarkAttrs(state: EditorState, type: MarkType) {
|
export default function getMarkAttrs(state: EditorState, type: MarkType) {
|
||||||
const { from, to } = state.selection
|
const { from, to, empty } = state.selection
|
||||||
let marks: Mark[] = []
|
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]
|
marks = [...marks, ...node.marks]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,17 @@ import { EditorState } from 'prosemirror-state'
|
|||||||
import { MarkType } from 'prosemirror-model'
|
import { MarkType } from 'prosemirror-model'
|
||||||
import getMarkAttrs from './getMarkAttrs'
|
import getMarkAttrs from './getMarkAttrs'
|
||||||
import { AnyObject } from '../types'
|
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)
|
const originalAttrs = getMarkAttrs(state, type)
|
||||||
|
|
||||||
return !!Object
|
return !!Object
|
||||||
.keys(attrs)
|
.keys(attributes)
|
||||||
.filter(key => attrs[key] === originalAttrs[key])
|
.filter(key => attributes[key] === originalAttrs[key])
|
||||||
.length
|
.length
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { EditorState } from 'prosemirror-state'
|
import { EditorState } from 'prosemirror-state'
|
||||||
import { MarkType } from 'prosemirror-model'
|
import { MarkType } from 'prosemirror-model'
|
||||||
import markHasAttributes from './markHasAttributes'
|
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 {
|
const {
|
||||||
from,
|
from,
|
||||||
$from,
|
$from,
|
||||||
@@ -10,11 +11,13 @@ export default function markIsActive(state: EditorState, type: MarkType, attrs =
|
|||||||
empty,
|
empty,
|
||||||
} = state.selection
|
} = 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) {
|
const hasAttributes = markHasAttributes(state, type, attributes)
|
||||||
return (type.isInSet(state.storedMarks || $from.marks()) && hasAttributes)
|
|
||||||
}
|
|
||||||
|
|
||||||
return (state.doc.rangeHasMark(from, to, type) && hasAttributes)
|
return isEmptyObject(attributes)
|
||||||
|
? hasMark
|
||||||
|
: hasMark && hasAttributes
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,6 @@ import {
|
|||||||
Command, createMark, markInputRule, markPasteRule,
|
Command, createMark, markInputRule, markPasteRule,
|
||||||
} from '@tiptap/core'
|
} from '@tiptap/core'
|
||||||
|
|
||||||
export interface HighlightOptions {
|
|
||||||
color: string,
|
|
||||||
}
|
|
||||||
|
|
||||||
export const inputRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))$/gm
|
export const inputRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))$/gm
|
||||||
export const pasteRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))/gm
|
export const pasteRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))/gm
|
||||||
|
|
||||||
@@ -54,8 +50,8 @@ const Highlight = createMark({
|
|||||||
|
|
||||||
addCommands() {
|
addCommands() {
|
||||||
return {
|
return {
|
||||||
highlight: (attrs?: HighlightOptions): Command => ({ commands }) => {
|
highlight: (attributes?: { color: string }): Command => ({ commands }) => {
|
||||||
return commands.toggleMark('highlight', attrs)
|
return commands.toggleMark('highlight', attributes)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user