improve link handling

This commit is contained in:
Philipp Kühn
2018-11-14 15:55:13 +01:00
parent df05a1d6ca
commit 21c8ad852a
4 changed files with 66 additions and 2 deletions

View File

@@ -1,3 +1,4 @@
export { default as getMarkAttrs } from './utils/getMarkAttrs'
export { default as getMarkRange } from './utils/getMarkRange'
export { default as markIsActive } from './utils/markIsActive'
export { default as nodeIsActive } from './utils/nodeIsActive'

View File

@@ -0,0 +1,34 @@
export default function ($pos = null, type = null) {
if (!$pos || !type) {
return false
}
const start = $pos.parent.childAfter($pos.parentOffset)
if (!start.node) {
return false
}
const link = start.node.marks.find(mark => mark.type === type)
if (!link) {
return false
}
let startIndex = $pos.index()
let startPos = $pos.start() + start.offset
while (startIndex > 0 && link.isInSet($pos.parent.child(startIndex - 1).marks)) {
startIndex -= 1
startPos -= $pos.parent.child(startIndex).nodeSize
}
let endIndex = $pos.indexAfter()
let endPos = startPos + start.node.nodeSize
while (endIndex < $pos.parent.childCount && link.isInSet($pos.parent.child(endIndex).marks)) {
endPos += $pos.parent.child(endIndex).nodeSize
endIndex += 1
}
return { from: startPos, to: endPos }
}