@docs.plus/extension-hyperlink 1.5.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,97 +0,0 @@
1
- import { Editor } from "@tiptap/core";
2
- import Tooltip, { TippyInitOptions } from "../helpers/tippyHelper";
3
- import { find } from "linkifyjs";
4
-
5
- export type TSetHyperlinkModalOptions = {
6
- editor: Editor;
7
- validate?: (url: string) => boolean;
8
- extentionName: string;
9
- attributes?: { href: string; target?: string | null };
10
- Tooltip: new (options: TippyInitOptions) => Tooltip;
11
- roundArrow: string;
12
- };
13
-
14
- let tooltip: Tooltip | undefined = undefined;
15
-
16
- export function setHyperlinkModal(options: TSetHyperlinkModalOptions): void {
17
- // Create the tooltip instance
18
- if (!tooltip) tooltip = new options.Tooltip(options);
19
-
20
- // Initialize the tooltip
21
- let { tippyModal } = tooltip.init();
22
-
23
- const hyperlinkModal = document.createElement("div");
24
- const buttonsWrapper = document.createElement("div");
25
- const inputsWrapper = document.createElement("div");
26
-
27
- hyperlinkModal.classList.add("hyperlink-set-modal");
28
-
29
- buttonsWrapper.classList.add("hyperlink-set-modal__buttons-wrapper");
30
- inputsWrapper.classList.add("hyperlink-set-modal__inputs-wrapper");
31
-
32
- // create a form that contain url input and a button for submit
33
- const form = document.createElement("form");
34
- const input = document.createElement("input");
35
- const button = document.createElement("button");
36
-
37
- input.setAttribute("type", "text");
38
- input.setAttribute("placeholder", "https://example.com");
39
- button.setAttribute("type", "submit");
40
- button.innerText = "Submit";
41
-
42
- inputsWrapper.append(input);
43
- buttonsWrapper.append(button);
44
- form.append(inputsWrapper, buttonsWrapper);
45
-
46
- hyperlinkModal.append(form);
47
-
48
- tippyModal.innerHTML = "";
49
- tippyModal.append(hyperlinkModal);
50
- tooltip.update(options.editor.view, {
51
- arrow: options.roundArrow,
52
- });
53
-
54
- // make sure
55
- setTimeout(() => {
56
- input.focus();
57
- input.style.outlineColor = " #dadce0";
58
- }, 100);
59
-
60
- input.addEventListener("keydown", () => {
61
- input.style.outlineColor = " #dadce0";
62
- });
63
-
64
- // event listenr for submit button
65
- form.addEventListener("submit", (event) => {
66
- event.preventDefault();
67
- const url = input.value;
68
-
69
- if (!url) {
70
- input.style.outlineColor = "red";
71
- return;
72
- }
73
-
74
- const sanitizeURL = find(url)
75
- .filter((link) => link.isLink)
76
- .filter((link) => {
77
- if (options.validate) {
78
- return options.validate(link.value);
79
- }
80
- return true;
81
- })
82
- .at(0);
83
-
84
- if (!sanitizeURL?.href) {
85
- input.style.outlineColor = "red";
86
- return;
87
- }
88
-
89
- tooltip?.hide();
90
-
91
- return options.editor
92
- .chain()
93
- .setMark(options.extentionName, { href: sanitizeURL.href })
94
- .setMeta("preventautohyperlink", true)
95
- .run();
96
- });
97
- }