refactoring

This commit is contained in:
Philipp Kühn 2021-09-22 21:14:07 +02:00
parent f096209a77
commit c91b2f29be
2 changed files with 35 additions and 49 deletions

View File

@ -30,19 +30,21 @@ export default function isMarkActive(
const markRanges: MarkRange[] = []
state.doc.nodesBetween(from, to, (node, pos) => {
if (node.isText || node.marks.length) {
const relativeFrom = Math.max(from, pos)
const relativeTo = Math.min(to, pos + node.nodeSize)
const range = relativeTo - relativeFrom
selectionRange += range
markRanges.push(...node.marks.map(mark => ({
mark,
from: relativeFrom,
to: relativeTo,
})))
if (!node.marks.length) {
return
}
const relativeFrom = Math.max(from, pos)
const relativeTo = Math.min(to, pos + node.nodeSize)
const range = relativeTo - relativeFrom
selectionRange += range
markRanges.push(...node.marks.map(mark => ({
mark,
from: relativeFrom,
to: relativeTo,
})))
})
if (selectionRange === 0) {
@ -59,11 +61,7 @@ export default function isMarkActive(
return type.name === markRange.mark.type.name
})
.filter(markRange => objectIncludes(markRange.mark.attrs, attributes, { strict: false }))
.reduce((sum, markRange) => {
const size = markRange.to - markRange.from
return sum + size
}, 0)
.reduce((sum, markRange) => sum + markRange.to - markRange.from, 0)
// calculate range of marks that excludes the searched mark
// for example `code` doesnt allow any other marks
@ -76,11 +74,7 @@ export default function isMarkActive(
return markRange.mark.type !== type
&& markRange.mark.type.excludes(type)
})
.reduce((sum, markRange) => {
const size = markRange.to - markRange.from
return sum + size
}, 0)
.reduce((sum, markRange) => sum + markRange.to - markRange.from, 0)
// we only include the result of `excludedRange`
// if there is a match at all

View File

@ -17,33 +17,22 @@ export default function isNodeActive(
const nodeRanges: NodeRange[] = []
state.doc.nodesBetween(from, to, (node, pos) => {
if (!node.isText) {
const relativeFrom = Math.max(from, pos)
const relativeTo = Math.min(to, pos + node.nodeSize)
nodeRanges.push({
node,
from: relativeFrom,
to: relativeTo,
})
if (node.isText) {
return
}
const relativeFrom = Math.max(from, pos)
const relativeTo = Math.min(to, pos + node.nodeSize)
nodeRanges.push({
node,
from: relativeFrom,
to: relativeTo,
})
})
if (empty) {
return !!nodeRanges
.filter(nodeRange => {
if (!type) {
return true
}
return type.name === nodeRange.node.type.name
})
.find(nodeRange => objectIncludes(nodeRange.node.attrs, attributes, { strict: false }))
}
const selectionRange = to - from
const range = nodeRanges
const matchedNodeRanges = nodeRanges
.filter(nodeRange => {
if (!type) {
return true
@ -52,10 +41,13 @@ export default function isNodeActive(
return type.name === nodeRange.node.type.name
})
.filter(nodeRange => objectIncludes(nodeRange.node.attrs, attributes, { strict: false }))
.reduce((sum, nodeRange) => {
const size = nodeRange.to - nodeRange.from
return sum + size
}, 0)
if (empty) {
return !!matchedNodeRanges.length
}
const range = matchedNodeRanges
.reduce((sum, nodeRange) => sum + nodeRange.to - nodeRange.from, 0)
return range >= selectionRange
}