check if mark has attributes

This commit is contained in:
Hans Pagel
2020-10-05 17:12:57 +02:00
parent 021d911ad6
commit 1d5d1f7c6e
4 changed files with 23 additions and 6 deletions

View File

@@ -351,7 +351,7 @@ export class Editor extends EventEmitter {
if (schemaType === 'node') {
return nodeIsActive(this.state, this.schema.nodes[name], attrs)
} if (schemaType === 'mark') {
return markIsActive(this.state, this.schema.marks[name])
return markIsActive(this.state, this.schema.marks[name], attrs)
}
return false

View File

@@ -0,0 +1,9 @@
import { EditorState } from 'prosemirror-state'
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(
getMarkAttrs(state, type),
)
}

View File

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