diff --git a/packages/core/src/helpers/isMarkActive.ts b/packages/core/src/helpers/isMarkActive.ts index bfd96a29..87e74772 100644 --- a/packages/core/src/helpers/isMarkActive.ts +++ b/packages/core/src/helpers/isMarkActive.ts @@ -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` 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 diff --git a/packages/core/src/helpers/isNodeActive.ts b/packages/core/src/helpers/isNodeActive.ts index 78cee635..a5e3882f 100644 --- a/packages/core/src/helpers/isNodeActive.ts +++ b/packages/core/src/helpers/isNodeActive.ts @@ -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 }