-
Notifications
You must be signed in to change notification settings - Fork 27
/
link.ts
68 lines (57 loc) · 1.94 KB
/
link.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { getMarkRange } from '@tiptap/core'
import type { LinkOptions as TiptapLinkOptions } from '@tiptap/extension-link'
import { Link as TiptapLink } from '@tiptap/extension-link'
import { Plugin, TextSelection } from '@tiptap/pm/state'
import type { EditorView } from '@tiptap/pm/view'
import LinkDialog from './components/link/LinkDialog.vue'
import LinkActionButton from './components/LinkActionButton.vue'
import type { GeneralOptions } from '@/type'
/**
* Represents the interface for link options, extending TiptapLinkOptions and GeneralOptions.
*/
export interface LinkOptions extends TiptapLinkOptions, GeneralOptions<LinkOptions> {
/** Component for the link dialog */
dialogComponent: any
}
export const Link = /* @__PURE__*/ TiptapLink.extend<LinkOptions>({
addOptions() {
return {
...this.parent?.(),
openOnClick: false,
dialogComponent: () => LinkDialog,
button: ({ editor, extension, t }) => {
const { dialogComponent } = extension.options
return {
component: LinkActionButton,
componentProps: {
isActive: () => editor.isActive('link') || false,
disabled: !editor.can().setLink({ href: '' }),
icon: 'link',
tooltip: t('editor.link.tooltip')
},
componentSlots: {
dialog: dialogComponent()
}
}
}
}
},
addProseMirrorPlugins() {
return [
new Plugin({
props: {
handleClick(view: EditorView, pos: number) {
const { schema, doc, tr } = view.state
const range = getMarkRange(doc.resolve(pos), schema.marks.link)
if (!range) return false
const $start = doc.resolve(range.from)
const $end = doc.resolve(range.to)
const transaction = tr.setSelection(new TextSelection($start, $end))
view.dispatch(transaction)
return true
}
}
})
]
}
})