You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1019 B
45 lines
1019 B
|
3 years ago
|
import { Editor } from '@tiptap/core'
|
||
|
|
import { find } from 'linkifyjs'
|
||
|
|
import { MarkType } from 'prosemirror-model'
|
||
|
|
import { Plugin, PluginKey } from 'prosemirror-state'
|
||
|
|
|
||
|
|
type PasteHandlerOptions = {
|
||
|
|
editor: Editor,
|
||
|
|
type: MarkType,
|
||
|
|
}
|
||
|
|
|
||
|
|
export function pasteHandler(options: PasteHandlerOptions): Plugin {
|
||
|
|
return new Plugin({
|
||
|
|
key: new PluginKey('handlePasteLink'),
|
||
|
|
props: {
|
||
|
|
handlePaste: (view, event, slice) => {
|
||
|
|
const { state } = view
|
||
|
|
const { selection } = state
|
||
|
|
const { empty } = selection
|
||
|
|
|
||
|
|
if (empty) {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
let textContent = ''
|
||
|
|
|
||
|
|
slice.content.forEach(node => {
|
||
|
|
textContent += node.textContent
|
||
|
|
})
|
||
|
|
|
||
|
|
const link = find(textContent).find(item => item.isLink && item.value === textContent)
|
||
|
|
|
||
|
|
if (!textContent || !link) {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
options.editor.commands.setMark(options.type, {
|
||
|
|
href: link.href,
|
||
|
|
})
|
||
|
|
|
||
|
|
return true
|
||
|
|
},
|
||
|
|
},
|
||
|
|
})
|
||
|
|
}
|