2020-04-22 20:22:31 +08:00
|
|
|
import { MarkType, ResolvedPos } from 'prosemirror-model'
|
2021-01-28 16:50:17 +08:00
|
|
|
import { Range } from '../types'
|
2020-04-22 20:22:31 +08:00
|
|
|
|
|
|
|
export default function getMarkRange($pos: ResolvedPos, type: MarkType): Range | void {
|
|
|
|
if (!$pos || !type) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const start = $pos.parent.childAfter($pos.parentOffset)
|
|
|
|
|
|
|
|
if (!start.node) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const link = start.node.marks.find(mark => mark.type === type)
|
2020-11-06 20:38:48 +08:00
|
|
|
|
2020-04-22 20:22:31 +08:00
|
|
|
if (!link) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-06 20:38:48 +08:00
|
|
|
return {
|
|
|
|
from: startPos,
|
|
|
|
to: endPos,
|
|
|
|
}
|
2020-04-22 20:22:31 +08:00
|
|
|
}
|