@docs.plus/extension-hyperlink 1.1.0 → 1.2.1

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.
@@ -3,124 +3,134 @@ import { Editor } from "@tiptap/core";
3
3
  import { EditorView } from "@tiptap/pm/view";
4
4
  import { getAttributes, posToDOMRect } from "@tiptap/core";
5
5
 
6
- let tippyInstance: Instance | undefined;
7
- let preventHide: boolean = false;
8
- let tippyWrapper: HTMLDivElement;
9
- let editor: Editor;
10
- let view: EditorView;
11
-
12
- type TippyInitOptions = {
6
+ interface TippyInitOptions {
13
7
  editor: Editor;
14
8
  validate?: (url: string) => boolean;
15
9
  view: EditorView;
16
- };
17
-
18
- export const init = (options: TippyInitOptions) => {
19
- editor = options.editor;
20
- view = options.view;
21
- if (!tippyInstance) {
22
- tippyWrapper = document.createElement("div");
23
- tippyWrapper.addEventListener("mousedown", mousedownHandler, {
10
+ }
11
+
12
+ class Tooltip {
13
+ private tippyInstance?: Instance;
14
+ private preventHide: boolean = false;
15
+ private tippyWrapper: HTMLDivElement;
16
+ private editor: Editor;
17
+ private view: EditorView;
18
+
19
+ constructor(options: TippyInitOptions) {
20
+ this.editor = options.editor;
21
+ this.view = options.view;
22
+ this.tippyWrapper = document.createElement("div");
23
+ this.tippyWrapper.addEventListener("mousedown", this.mousedownHandler, {
24
24
  capture: true,
25
25
  });
26
- view.dom.addEventListener("dragstart", dragstartHandler);
27
- editor.on("focus", focusHandler);
28
- editor.on("blur", blurHandler);
26
+ this.view.dom.addEventListener("dragstart", this.dragstartHandler);
27
+ this.editor.on("blur", this.blurHandler);
29
28
  }
30
29
 
31
- return { tippyModal: tippyWrapper, tippyInstance: tippyInstance };
32
- };
33
-
34
- export const show = () => {
35
- tippyInstance?.show();
36
- return true;
37
- };
38
-
39
- export const hide = () => {
40
- setTimeout(() => tippyInstance?.hide());
41
- return false;
42
- };
43
-
44
- export const mousedownHandler = () => {
45
- preventHide = true;
46
- };
47
-
48
- export const dragstartHandler = () => {
49
- hide();
50
- };
30
+ init() {
31
+ this.tippyWrapper.innerHTML = "";
51
32
 
52
- export const focusHandler = () => {
53
- setTimeout(() => update(editor.view));
54
- };
55
-
56
- const blurHandler = ({ event }: { event: FocusEvent }) => {
57
- if (preventHide) {
58
- preventHide = false;
59
- return;
33
+ return { tippyModal: this.tippyWrapper, tippyInstance: this.tippyInstance };
60
34
  }
61
- if (event?.relatedTarget && tippyWrapper.parentNode?.contains(event.relatedTarget as Node)) {
62
- return;
35
+
36
+ show() {
37
+ this.tippyInstance?.show();
38
+ return true;
63
39
  }
64
- hide();
65
- };
66
40
 
67
- const tippyBlurHandler = (event: FocusEvent) => {
68
- blurHandler({ event });
69
- };
41
+ hide() {
42
+ setTimeout(() => this.tippyInstance?.hide());
43
+ return false;
44
+ }
70
45
 
71
- const createTooltip = () => {
72
- if (!editor || !editor.options) return;
73
- const { element: editorElement } = editor.options;
74
- const editorIsAttached = !!editorElement.parentElement;
46
+ private mousedownHandler = () => {
47
+ this.preventHide = true;
48
+ };
49
+
50
+ private dragstartHandler = () => {
51
+ this.hide();
52
+ };
53
+
54
+ private blurHandler = ({ event }: { event: FocusEvent }) => {
55
+ if (this.preventHide) {
56
+ this.preventHide = false;
57
+ return;
58
+ }
59
+ if (
60
+ event?.relatedTarget &&
61
+ this.tippyWrapper.parentNode?.contains(event.relatedTarget as Node)
62
+ ) {
63
+ return;
64
+ }
65
+ this.hide();
66
+ };
67
+
68
+ private tippyBlurHandler = (event: FocusEvent) => {
69
+ this.blurHandler({ event });
70
+ };
71
+
72
+ private createTooltip() {
73
+ if (!this.editor || !this.editor.options) return;
74
+ const { element: editorElement } = this.editor.options;
75
+ const editorIsAttached = !!editorElement.parentElement;
76
+
77
+ if (this.tippyInstance || !editorIsAttached) {
78
+ return;
79
+ }
80
+
81
+ this.tippyInstance = tippy(editorElement, {
82
+ duration: 0,
83
+ getReferenceClientRect: null,
84
+ content: this.tippyWrapper,
85
+ interactive: true,
86
+ trigger: "manual",
87
+ placement: "bottom",
88
+ hideOnClick: "toggle",
89
+ onClickOutside: (instance, event) => {
90
+ this.hide();
91
+ },
92
+ onAfterUpdate: (instance, partialProps) => {
93
+ this.show();
94
+ },
95
+ });
75
96
 
76
- if (tippyInstance || !editorIsAttached) {
77
- return;
97
+ if (this.tippyInstance.popper.firstChild) {
98
+ (this.tippyInstance.popper.firstChild as HTMLElement).addEventListener(
99
+ "blur",
100
+ this.tippyBlurHandler
101
+ );
102
+ }
78
103
  }
79
104
 
80
- tippyInstance = tippy(editorElement, {
81
- duration: 0,
82
- getReferenceClientRect: null,
83
- content: tippyWrapper,
84
- interactive: true,
85
- trigger: "manual",
86
- placement: "bottom",
87
- hideOnClick: "toggle",
88
- });
89
-
90
- if (tippyInstance.popper.firstChild) {
91
- (tippyInstance.popper.firstChild as HTMLElement).addEventListener("blur", tippyBlurHandler);
92
- }
93
- };
105
+ update(view: EditorView, option: any = {}) {
106
+ this.createTooltip();
94
107
 
95
- export const update = (view: EditorView, option: any = {}) => {
96
- createTooltip();
108
+ option.arrow = option?.arrow ?? false;
97
109
 
98
- // Ensure 'arrow' is false when not provided
99
- option.arrow = option?.arrow ?? false;
110
+ if (this.tippyInstance) {
111
+ this.tippyInstance.setProps({
112
+ ...option,
113
+ getReferenceClientRect: () => {
114
+ const pos = view.state.selection.from;
115
+ return posToDOMRect(view, pos, pos);
116
+ },
117
+ });
118
+ }
100
119
 
101
- if (tippyInstance) {
102
- tippyInstance.setProps({
103
- ...option,
104
- getReferenceClientRect: () => {
105
- const pos = view.state.selection.from;
106
- return posToDOMRect(view, pos, pos);
107
- },
108
- });
109
- show();
120
+ return {};
110
121
  }
111
122
 
112
- return {};
113
- };
114
-
115
- export const destroyTooltip = () => {
116
- if (tippyInstance) {
117
- tippyInstance.destroy();
118
- tippyInstance = undefined;
119
- tippyWrapper.removeEventListener("mousedown", mousedownHandler, {
120
- capture: true,
121
- });
122
- view.dom.removeEventListener("dragstart", dragstartHandler);
123
- editor.off("focus", focusHandler);
124
- editor.off("blur", blurHandler);
123
+ destroyTooltip() {
124
+ if (this.tippyInstance) {
125
+ this.tippyInstance.destroy();
126
+ this.tippyInstance = undefined;
127
+ this.tippyWrapper.removeEventListener("mousedown", this.mousedownHandler, {
128
+ capture: true,
129
+ });
130
+ this.view.dom.removeEventListener("dragstart", this.dragstartHandler);
131
+ this.editor.off("blur", this.blurHandler);
132
+ }
125
133
  }
126
- };
134
+ }
135
+
136
+ export default Tooltip;
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Hyperlink } from "./hyperlink";
2
+ import Tooltip from "./helpers/tippyHelper";
2
3
 
3
- export * as tippy from "./helpers/tippyHelper";
4
+ export { Tooltip };
4
5
  export * from "./hyperlink";
5
6
 
6
7
  export default Hyperlink;