getMarkRange() will not return the actual end position of the current mark. might reintroduce bug from #156 (i cannot reproduce #156 so i cannot check) fixes in this commit: A) work around a possible bug in indexAfter()?: $pos.indexAfter() seems to return the same index as $pos.index() at some point -> fixed by increasing startIndex by 1 instead of using indexAfter() B) endPos needs to be initialized with initial startPos + nodeSize and not with resulting startPos: -> moved initialization right after setting startPos
36 lines
838 B
JavaScript
36 lines
838 B
JavaScript
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
|
|
let endIndex = startIndex + 1
|
|
let endPos = startPos + start.node.nodeSize
|
|
|
|
while (startIndex > 0 && link.isInSet($pos.parent.child(startIndex - 1).marks)) {
|
|
startIndex -= 1
|
|
startPos -= $pos.parent.child(startIndex).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 }
|
|
|
|
}
|