refactoring
This commit is contained in:
@@ -30,7 +30,10 @@ export default function isMarkActive(
|
||||
const markRanges: MarkRange[] = []
|
||||
|
||||
state.doc.nodesBetween(from, to, (node, pos) => {
|
||||
if (node.isText || node.marks.length) {
|
||||
if (!node.marks.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const relativeFrom = Math.max(from, pos)
|
||||
const relativeTo = Math.min(to, pos + node.nodeSize)
|
||||
const range = relativeTo - relativeFrom
|
||||
@@ -42,7 +45,6 @@ export default function isMarkActive(
|
||||
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` doesn’t 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
|
||||
|
||||
@@ -17,7 +17,10 @@ export default function isNodeActive(
|
||||
const nodeRanges: NodeRange[] = []
|
||||
|
||||
state.doc.nodesBetween(from, to, (node, pos) => {
|
||||
if (!node.isText) {
|
||||
if (node.isText) {
|
||||
return
|
||||
}
|
||||
|
||||
const relativeFrom = Math.max(from, pos)
|
||||
const relativeTo = Math.min(to, pos + node.nodeSize)
|
||||
|
||||
@@ -26,24 +29,10 @@ export default function isNodeActive(
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user