2020-11-16 18:19:43 +08:00
|
|
|
import { Command, Mark, markPasteRule } from '@tiptap/core'
|
2020-09-25 04:26:23 +08:00
|
|
|
import { Plugin, PluginKey } from 'prosemirror-state'
|
|
|
|
|
|
|
|
export interface LinkOptions {
|
|
|
|
openOnClick: boolean,
|
2020-11-13 23:44:22 +08:00
|
|
|
HTMLAttributes: {
|
|
|
|
[key: string]: any
|
|
|
|
},
|
2020-09-25 04:26:23 +08:00
|
|
|
}
|
|
|
|
|
2020-11-13 16:07:51 +08:00
|
|
|
export const pasteRegex = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%._+~#=?!&/()]*)/gi
|
2020-09-25 04:26:23 +08:00
|
|
|
|
2020-11-16 18:19:43 +08:00
|
|
|
const Link = Mark.create({
|
2020-10-22 18:34:49 +08:00
|
|
|
name: 'link',
|
|
|
|
|
|
|
|
inclusive: false,
|
|
|
|
|
|
|
|
defaultOptions: <LinkOptions>{
|
2020-09-25 04:26:23 +08:00
|
|
|
openOnClick: true,
|
2020-11-13 23:44:22 +08:00
|
|
|
HTMLAttributes: {
|
|
|
|
target: '_blank',
|
|
|
|
rel: 'noopener noreferrer nofollow',
|
|
|
|
},
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
addAttributes() {
|
|
|
|
return {
|
2020-09-25 04:26:23 +08:00
|
|
|
href: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
target: {
|
2020-11-13 23:44:22 +08:00
|
|
|
default: this.options.HTMLAttributes.target,
|
2020-09-25 04:26:23 +08:00
|
|
|
},
|
2020-10-22 18:34:49 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
parseHTML() {
|
2020-10-27 19:59:18 +08:00
|
|
|
return [
|
|
|
|
{ tag: 'a[href]' },
|
|
|
|
]
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
|
2020-11-13 23:07:20 +08:00
|
|
|
renderHTML({ HTMLAttributes }) {
|
2020-11-15 00:27:59 +08:00
|
|
|
return ['a', HTMLAttributes, 0]
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2020-11-13 22:08:30 +08:00
|
|
|
/**
|
|
|
|
* Toggle or update a link mark
|
|
|
|
*/
|
2020-10-23 17:41:24 +08:00
|
|
|
link: (options: { href?: string, target?: string } = {}): Command => ({ commands }) => {
|
2020-10-23 04:40:40 +08:00
|
|
|
if (!options.href) {
|
2020-10-22 18:34:49 +08:00
|
|
|
return commands.removeMark('link')
|
|
|
|
}
|
|
|
|
|
2020-11-18 18:05:19 +08:00
|
|
|
return commands.addMark('link', options)
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addPasteRules() {
|
|
|
|
return [
|
|
|
|
markPasteRule(pasteRegex, this.type, (url: string) => ({ href: url })),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
addProseMirrorPlugins() {
|
|
|
|
if (!this.options.openOnClick) {
|
2020-09-25 04:26:23 +08:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
new Plugin({
|
|
|
|
key: new PluginKey('handleClick'),
|
|
|
|
props: {
|
|
|
|
handleClick: (view, pos, event) => {
|
2020-11-18 04:10:08 +08:00
|
|
|
const attrs = this.editor.getMarkAttributes('link')
|
2020-09-25 04:26:23 +08:00
|
|
|
|
|
|
|
if (attrs.href && event.target instanceof HTMLAnchorElement) {
|
|
|
|
window.open(attrs.href, attrs.target)
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
]
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
})
|
2020-10-23 04:40:40 +08:00
|
|
|
|
|
|
|
export default Link
|
|
|
|
|
2020-11-16 23:58:30 +08:00
|
|
|
declare module '@tiptap/core' {
|
|
|
|
interface AllExtensions {
|
|
|
|
Link: typeof Link,
|
2020-10-23 04:40:40 +08:00
|
|
|
}
|
|
|
|
}
|