invert markIsActive
This commit is contained in:
@@ -3,23 +3,20 @@ import { Mark, MarkType } from 'prosemirror-model'
|
|||||||
import objectIncludes from '../utilities/objectIncludes'
|
import objectIncludes from '../utilities/objectIncludes'
|
||||||
import getMarkType from '../helpers/getMarkType'
|
import getMarkType from '../helpers/getMarkType'
|
||||||
|
|
||||||
|
type MarkRange = {
|
||||||
|
mark: Mark,
|
||||||
|
from: number,
|
||||||
|
to: number,
|
||||||
|
}
|
||||||
|
|
||||||
export default function markIsActive(state: EditorState, typeOrName: MarkType | string | null, attributes = {}) {
|
export default function markIsActive(state: EditorState, typeOrName: MarkType | string | null, attributes = {}) {
|
||||||
const { from, to, empty } = state.selection
|
const { from, to, empty } = state.selection
|
||||||
const type = typeOrName
|
const type = typeOrName
|
||||||
? getMarkType(typeOrName, state.schema)
|
? getMarkType(typeOrName, state.schema)
|
||||||
: null
|
: null
|
||||||
|
|
||||||
let marks: Mark[] = []
|
|
||||||
|
|
||||||
if (empty) {
|
if (empty) {
|
||||||
marks = state.selection.$head.marks()
|
return !!state.selection.$head.marks()
|
||||||
} else {
|
|
||||||
state.doc.nodesBetween(from, to, node => {
|
|
||||||
marks = [...marks, ...node.marks]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const markWithAttributes = marks
|
|
||||||
.filter(mark => {
|
.filter(mark => {
|
||||||
if (!type) {
|
if (!type) {
|
||||||
return true
|
return true
|
||||||
@@ -28,6 +25,40 @@ export default function markIsActive(state: EditorState, typeOrName: MarkType |
|
|||||||
return type.name === mark.type.name
|
return type.name === mark.type.name
|
||||||
})
|
})
|
||||||
.find(mark => objectIncludes(mark.attrs, attributes))
|
.find(mark => objectIncludes(mark.attrs, attributes))
|
||||||
|
}
|
||||||
return !!markWithAttributes
|
|
||||||
|
let selectionRange = 0
|
||||||
|
let markRanges: MarkRange[] = []
|
||||||
|
|
||||||
|
state.doc.nodesBetween(from, to, (node, pos) => {
|
||||||
|
if (node.isInline) {
|
||||||
|
const relativeFrom = Math.max(from, pos)
|
||||||
|
const relativeTo = Math.min(to, pos + node.nodeSize)
|
||||||
|
const range = relativeTo - relativeFrom
|
||||||
|
|
||||||
|
selectionRange += range
|
||||||
|
|
||||||
|
markRanges = [...markRanges, ...node.marks.map(mark => ({
|
||||||
|
mark,
|
||||||
|
from: relativeFrom,
|
||||||
|
to: relativeTo,
|
||||||
|
}))]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const range = markRanges
|
||||||
|
.filter(markRange => {
|
||||||
|
if (!type) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return type.name === markRange.mark.type.name
|
||||||
|
})
|
||||||
|
.filter(markRange => objectIncludes(markRange.mark.attrs, attributes))
|
||||||
|
.reduce((sum, markRange) => {
|
||||||
|
const size = markRange.to - markRange.from
|
||||||
|
return sum + size
|
||||||
|
}, 0)
|
||||||
|
|
||||||
|
return selectionRange <= range
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user