2020-09-25 04:26:23 +08:00
|
|
|
import {
|
|
|
|
Command, Mark, markPasteRule, getMarkAttrs,
|
|
|
|
} from '@tiptap/core'
|
|
|
|
import { Plugin, PluginKey } from 'prosemirror-state'
|
|
|
|
|
|
|
|
export interface LinkOptions {
|
|
|
|
openOnClick: boolean,
|
|
|
|
target: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
export type LinkCommand = () => Command
|
|
|
|
|
|
|
|
declare module '@tiptap/core/src/Editor' {
|
|
|
|
interface Commands {
|
|
|
|
link: LinkCommand,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const pasteRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/gi
|
|
|
|
|
|
|
|
export default new Mark<LinkOptions>()
|
|
|
|
.name('link')
|
|
|
|
.defaults({
|
|
|
|
openOnClick: true,
|
|
|
|
target: '',
|
|
|
|
})
|
|
|
|
.schema(({ options }) => ({
|
|
|
|
attrs: {
|
|
|
|
href: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
target: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inclusive: false,
|
|
|
|
parseDOM: [
|
|
|
|
{
|
|
|
|
tag: 'a[href]',
|
|
|
|
getAttrs: node => ({
|
|
|
|
href: (node as HTMLElement).getAttribute('href'),
|
|
|
|
target: (node as HTMLElement).getAttribute('target'),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
toDOM: node => ['a', {
|
|
|
|
...node.attrs,
|
|
|
|
rel: 'noopener noreferrer nofollow',
|
|
|
|
target: options.target,
|
|
|
|
}, 0],
|
|
|
|
}))
|
|
|
|
.commands(({ name }) => ({
|
|
|
|
link: () => ({ commands }) => {
|
|
|
|
return commands.toggleMark(name)
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
.pasteRules(({ type }) => [
|
2020-09-25 04:46:29 +08:00
|
|
|
markPasteRule(pasteRegex, type),
|
2020-09-25 04:26:23 +08:00
|
|
|
])
|
|
|
|
.plugins(({ options }) => {
|
|
|
|
if (!options.openOnClick) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
new Plugin({
|
|
|
|
key: new PluginKey('handleClick'),
|
|
|
|
props: {
|
|
|
|
handleClick: (view, pos, event) => {
|
|
|
|
const { schema } = view.state
|
|
|
|
const attrs = getMarkAttrs(view.state, schema.marks.link)
|
|
|
|
|
|
|
|
if (attrs.href && event.target instanceof HTMLAnchorElement) {
|
|
|
|
window.open(attrs.href, attrs.target)
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
})
|
|
|
|
.create()
|