11import { VueRenderer } from '@tiptap/vue-2'
22import tippy from 'tippy.js'
3+ import debounce from 'debounce'
34import { domHref } from '../helpers/links.js'
45import LinkBubbleView from '../components/Link/LinkBubbleView.vue'
56
67import { getViewerVue } from '../ViewerVue.js'
78
8- const updateDelay = 250
9-
109class LinkBubblePluginView {
1110
1211 #component = null
13- #preventHide = false
1412 #hadUpdateFromClick = false
15- #updateDebounceTimer = undefined
1613
1714 constructor ( { editor, view } ) {
1815 this . editor = editor
@@ -33,18 +30,16 @@ class LinkBubblePluginView {
3330 this . view . dom . addEventListener ( 'dragstart' , this . dragOrScrollHandler )
3431 this . view . dom . addEventListener ( 'click' , this . clickHandler )
3532 document . addEventListener ( 'scroll' , this . dragOrScrollHandler , { capture : true } )
36- this . editor . on ( 'focus' , this . focusHandler )
37- this . editor . on ( 'blur' , this . blurHandler )
3833 }
3934
40- dragOrScrollHandler = ( ) => {
35+ dragOrScrollHandler = ( event ) => {
36+ // Cypress fires unexpected scroll events, which breaks testing the link bubble
37+ if ( window . Cypress ) {
38+ return
39+ }
4140 this . hide ( )
4241 }
4342
44- pointerdownHandler = ( ) => {
45- this . #preventHide = true
46- }
47-
4843 // Required for read-only mode on Firefox. For some reason, editor selection doesn't get
4944 // updated when clicking a link in read-only mode on Firefox.
5045 clickHandler = ( event ) => {
@@ -67,28 +62,6 @@ class LinkBubblePluginView {
6762 setTimeout ( ( ) => this . updateFromClick ( this . editor . view , clickedPos ) )
6863 }
6964
70- focusHandler = ( ) => {
71- // we use `setTimeout` to make sure `selection` is already updated
72- setTimeout ( ( ) => this . update ( this . editor . view ) )
73- }
74-
75- blurHandler = ( { event } ) => {
76- if ( this . #preventHide) {
77- this . #preventHide = false
78- return
79- }
80-
81- if ( event ?. relatedTarget && this . tippy ?. popper . firstChild . contains ( event . relatedTarget ) ) {
82- return
83- }
84-
85- this . hide ( )
86- }
87-
88- tippyBlurHandler = ( ) => {
89- this . hide ( )
90- }
91-
9265 keydownHandler = ( event ) => {
9366 if ( event . key === 'Escape' ) {
9467 event . preventDefault ( )
@@ -116,10 +89,6 @@ class LinkBubblePluginView {
11689 strategy : 'fixed' ,
11790 } ,
11891 } )
119-
120- this . tippy . popper . firstChild ?. addEventListener ( 'pointerdown' , this . pointerdownHandler , { capture : true } )
121- // Hide tippy on its own blur event as well
122- this . tippy . popper . firstChild ?. addEventListener ( 'blur' , this . tippyBlurHandler )
12392 }
12493
12594 update ( view , oldState ) {
@@ -132,16 +101,10 @@ class LinkBubblePluginView {
132101 return
133102 }
134103
135- if ( this . #updateDebounceTimer) {
136- clearTimeout ( this . #updateDebounceTimer)
137- }
138-
139- this . #updateDebounceTimer = window . setTimeout ( ( ) => {
140- this . updateFromSelection ( view )
141- } , updateDelay )
104+ this . updateFromSelection ( view )
142105 }
143106
144- updateFromSelection ( view ) {
107+ updateFromSelection = debounce ( ( view ) => {
145108 // Don't update directly after updateFromClick. Prevents race condition in read-only documents in Chrome.
146109 if ( this . #hadUpdateFromClick) {
147110 return
@@ -164,7 +127,7 @@ class LinkBubblePluginView {
164127 const shouldShow = ! ! linkNode && hasEditorFocus
165128
166129 this . updateTooltip ( view , shouldShow , linkNode , nodeStart )
167- }
130+ } , 250 )
168131
169132 updateFromClick ( view , clickedLinkPos ) {
170133 const nodeStart = clickedLinkPos . pos - clickedLinkPos . textOffset
@@ -174,11 +137,11 @@ class LinkBubblePluginView {
174137 this . #hadUpdateFromClick = true
175138 setTimeout ( ( ) => {
176139 this . #hadUpdateFromClick = false
177- } , 200 )
140+ } , 500 )
178141 this . updateTooltip ( this . editor . view , shouldShow , clickedNode , nodeStart )
179142 }
180143
181- updateTooltip = ( view , shouldShow , linkNode , nodeStart ) => {
144+ updateTooltip ( view , shouldShow , linkNode , nodeStart ) {
182145 this . createTooltip ( )
183146
184147 if ( ! shouldShow || ! linkNode ) {
@@ -216,17 +179,13 @@ class LinkBubblePluginView {
216179 }
217180
218181 destroy ( ) {
219- this . tippy ?. popper . firstChild ?. removeEventListener ( 'blur' , this . tippyBlurHandler )
220- this . tippy ?. popper . firstChild ?. removeEventListener ( 'pointerdown' , this . pointerdownHandler , { capture : true } )
221182 this . tippy ?. destroy ( )
222183 this . view . dom . removeEventListener ( 'dragstart' , this . dragOrScrollHandler )
223184 this . view . dom . removeEventListener ( 'click' , this . clickHandler )
224185 document . removeEventListener ( 'scroll' , this . dragOrScrollHandler , { capture : true } )
225- this . editor . off ( 'focus' , this . focusHandler )
226- this . editor . off ( 'blur' , this . blurHandler )
227186 }
228187
229- linkNodeFromSelection = ( view ) => {
188+ linkNodeFromSelection ( view ) {
230189 const { state } = view
231190 const { selection } = state
232191
0 commit comments