tiptap/packages/extension-link/src/link.ts

116 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-11-25 16:50:54 +08:00
import {
Command,
Mark,
markPasteRule,
mergeAttributes,
} 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
}
2021-02-10 16:59:35 +08:00
declare module '@tiptap/core' {
interface Commands {
2021-02-11 01:05:02 +08:00
/**
* Set a link mark
*/
2021-02-10 16:59:35 +08:00
setLink: (attributes: { href: string, target?: string }) => Command,
2021-02-11 01:05:02 +08:00
/**
* Toggle a link mark
*/
2021-02-10 16:59:35 +08:00
toggleLink: (attributes: { href: string, target?: string }) => Command,
2021-02-11 01:05:02 +08:00
/**
* Unset a link mark
*/
2021-02-10 16:59:35 +08:00
unsetLink: () => Command,
}
}
2020-11-25 00:29:43 +08:00
export const pasteRegex = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)/gi
export const pasteRegexWithBrackets = /(?:\()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-12-08 04:32:50 +08:00
export 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-25 16:50:54 +08:00
return ['a', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
2020-10-22 18:34:49 +08:00
},
addCommands() {
return {
2021-02-10 16:59:35 +08:00
setLink: attributes => ({ commands }) => {
2020-11-19 00:36:00 +08:00
return commands.setMark('link', attributes)
2020-11-18 18:10:06 +08:00
},
2021-02-10 16:59:35 +08:00
toggleLink: attributes => ({ commands }) => {
2020-11-18 18:10:06 +08:00
return commands.toggleMark('link', attributes)
},
2021-02-10 16:59:35 +08:00
unsetLink: () => ({ commands }) => {
2020-11-19 00:38:16 +08:00
return commands.unsetMark('link')
2020-10-22 18:34:49 +08:00
},
}
},
addPasteRules() {
return [
markPasteRule(pasteRegex, this.type, (url: string) => ({ href: url })),
2020-11-25 00:26:47 +08:00
markPasteRule(pasteRegexWithBrackets, this.type, (url: string) => ({ href: url })),
2020-10-22 18:34:49 +08:00
]
},
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
},
})