improve link handling
This commit is contained in:
@@ -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'
|
||||
|
||||
34
packages/tiptap-utils/src/utils/getMarkRange.js
Normal file
34
packages/tiptap-utils/src/utils/getMarkRange.js
Normal 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 }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user