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,5 +1,6 @@
import { Mark } from 'tiptap'
import { Mark, Plugin, TextSelection } from 'tiptap'
import { updateMark, removeMark } from 'tiptap-commands'
import { getMarkRange } from 'tiptap-utils'
export default class Link extends Mark {
@@ -40,4 +41,27 @@ export default class Link extends Mark {
}
}
get plugins() {
return [
new Plugin({
props: {
handleClick(view, pos) {
const { schema, doc, tr } = view.state
const range = getMarkRange(doc.resolve(pos), schema.marks.link)
if (!range) {
return
}
const $start = doc.resolve(range.from)
const $end = doc.resolve(range.to)
const transaction = tr.setSelection(new TextSelection($start, $end))
view.dispatch(transaction)
},
},
}),
]
}
}

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 }
}

View File

@@ -4,4 +4,9 @@ export { default as EditorContent } from './Components/EditorContent'
export { default as EditorMenuBar } from './Components/EditorMenuBar'
export { default as EditorMenuBubble } from './Components/EditorMenuBubble'
export { default as EditorFloatingMenu } from './Components/EditorFloatingMenu'
export { Plugin, PluginKey } from 'prosemirror-state'
export {
Plugin,
PluginKey,
TextSelection,
NodeSelection,
} from 'prosemirror-state'