@docs.plus/extension-hyperlink 1.1.0 → 1.2.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.
@@ -3,124 +3,136 @@ 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
+ console.log("onClickOutside", { instance, event });
91
+ this.hide();
92
+ },
93
+ onAfterUpdate: (instance, partialProps) => {
94
+ console.log("onAfterUpdate", { instance, partialProps });
95
+ this.show();
96
+ },
97
+ });
75
98
 
76
- if (tippyInstance || !editorIsAttached) {
77
- return;
99
+ if (this.tippyInstance.popper.firstChild) {
100
+ (this.tippyInstance.popper.firstChild as HTMLElement).addEventListener(
101
+ "blur",
102
+ this.tippyBlurHandler
103
+ );
104
+ }
78
105
  }
79
106
 
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
- };
107
+ update(view: EditorView, option: any = {}) {
108
+ this.createTooltip();
94
109
 
95
- export const update = (view: EditorView, option: any = {}) => {
96
- createTooltip();
110
+ option.arrow = option?.arrow ?? false;
97
111
 
98
- // Ensure 'arrow' is false when not provided
99
- option.arrow = option?.arrow ?? false;
112
+ if (this.tippyInstance) {
113
+ this.tippyInstance.setProps({
114
+ ...option,
115
+ getReferenceClientRect: () => {
116
+ const pos = view.state.selection.from;
117
+ return posToDOMRect(view, pos, pos);
118
+ },
119
+ });
120
+ }
100
121
 
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();
122
+ return {};
110
123
  }
111
124
 
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);
125
+ destroyTooltip() {
126
+ if (this.tippyInstance) {
127
+ this.tippyInstance.destroy();
128
+ this.tippyInstance = undefined;
129
+ this.tippyWrapper.removeEventListener("mousedown", this.mousedownHandler, {
130
+ capture: true,
131
+ });
132
+ this.view.dom.removeEventListener("dragstart", this.dragstartHandler);
133
+ this.editor.off("blur", this.blurHandler);
134
+ }
125
135
  }
126
- };
136
+ }
137
+
138
+ export default Tooltip;
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Hyperlink } from "./hyperlink";
2
2
 
3
- export * as tippy from "./helpers/tippyHelper";
3
+ export * as Tooltip from "./helpers/tippyHelper";
4
+
4
5
  export * from "./hyperlink";
5
6
 
6
7
  export default Hyperlink;