-
Notifications
You must be signed in to change notification settings - Fork 13
/
link.ts
72 lines (65 loc) · 2.17 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
69
70
71
72
// https://tiptap.dev/api/marks/link
import Link from "@tiptap/extension-link";
import customMessages from "../i18n/custom-messages";
import DialogLink from "../components/DialogLink.vue";
import type { Ref } from "vue";
import type { Editor } from "@tiptap/core";
import type { Tool, LinkAttributes, Dialog } from "../types";
const add: Tool = {
// https://tiptap.dev/api/marks/link
key: "link",
name: customMessages.tools.link,
icon: "link",
extension: [linkExtenstionConfig],
action: (editor: Editor, { dialog }: { dialog: Ref<Dialog> }) => {
dialog.value = {
component: DialogLink,
get: () => editor.getAttributes("link"),
set: (attrs: LinkAttributes) =>
editor
.chain()
.focus()
.extendMarkRange("link")
.setLink(attrs)
.run(),
unset: () =>
editor
.chain()
.focus()
.extendMarkRange("link")
.unsetLink()
.run(),
};
},
disabled: (editor: Editor) =>
!editor.can().chain().focus().toggleLink({ href: "" }).run(),
active: (editor: Editor) => editor.isActive("link"),
};
const remove: Tool = {
// https://tiptap.dev/api/marks/link
key: "removeLink",
name: customMessages.tools.unlink,
icon: "link_off",
extension: [linkExtenstionConfig],
action: (editor: Editor) => editor.chain().focus().unsetLink().run(),
// keep toggleLink for `disabled`
disabled: (editor: Editor) =>
!editor.can().chain().focus().toggleLink({ href: "" }).run(),
active: () => false,
};
const auto: Tool = {
// If you want to use autolink without a link button
key: "autolink",
name: customMessages.tools.autolink,
excludeFromToolbar: true,
extension: [linkExtenstionConfig],
};
function linkExtenstionConfig(selection: string[]) {
const autolink = selection.indexOf("autolink") >= 0;
return Link.configure({
autolink,
linkOnPaste: true,
openOnClick: false,
});
}
export default { add, remove, auto };