1- import { useState , useEffect , useRef } from "react"
1+ import { useState , useEffect , useRef , useCallback } from "react"
22import {
33 MOUSE ,
44 KEYBOARD ,
@@ -56,27 +56,31 @@ export function useDetectLinkCommand(): DetectLinkCommandReturn {
5656 const command = commands ?. find ( isLinkCommand ) as Command
5757 const enabled =
5858 pageRule == null ||
59- pageRule . linkCommandEnabled == undefined ||
60- pageRule . linkCommandEnabled === LINK_COMMAND_ENABLED . INHERIT
59+ pageRule . linkCommandEnabled == undefined ||
60+ pageRule . linkCommandEnabled === LINK_COMMAND_ENABLED . INHERIT
6161 ? settings . linkCommand ?. enabled === LINK_COMMAND_ENABLED . ENABLE
6262 : pageRule . linkCommandEnabled === LINK_COMMAND_ENABLED . ENABLE
6363
64- const onChangeState = ( state : ExecState , message ?: string ) => {
65- console . debug ( { state , message } )
66- }
64+ const onDetect = useCallback (
65+ ( position : Point , target : Element ) => {
66+ if ( command == null ) return
6767
68- const onDetect = ( position : Point , target : Element ) => {
69- if ( command == null ) return
70- LinkPreview . execute ( {
71- selectionText : "" ,
72- command,
73- position,
74- useSecondary : false ,
75- changeState : onChangeState ,
76- target,
77- } )
78- sendEvent ( ANALYTICS_EVENTS . LINK_COMMAND , { id : "link_preview" } )
79- }
68+ const onChangeState = ( state : ExecState , message ?: string ) => {
69+ console . debug ( { state, message } )
70+ }
71+
72+ LinkPreview . execute ( {
73+ selectionText : "" ,
74+ command,
75+ position,
76+ useSecondary : false ,
77+ changeState : onChangeState ,
78+ target,
79+ } )
80+ sendEvent ( ANALYTICS_EVENTS . LINK_COMMAND , { id : "link_preview" } )
81+ } ,
82+ [ command ] ,
83+ )
8084
8185 return {
8286 showIndicator : showIndicator ?? false ,
@@ -106,7 +110,7 @@ function useDetectDrag(
106110 const dragEnabled =
107111 enabled &&
108112 settings . linkCommand ?. startupMethod ?. method ===
109- LINK_COMMAND_STARTUP_METHOD . DRAG
113+ LINK_COMMAND_STARTUP_METHOD . DRAG
110114
111115 const threshold =
112116 settings . linkCommand ?. startupMethod ?. threshold ??
@@ -133,7 +137,7 @@ function useDetectDrag(
133137 const current = { x : e . clientX , y : e . clientY }
134138 const distance = Math . sqrt (
135139 Math . pow ( current . x - startPosition . x , 2 ) +
136- Math . pow ( current . y - startPosition . y , 2 ) ,
140+ Math . pow ( current . y - startPosition . y , 2 ) ,
137141 )
138142 setMousePosition ( current )
139143 setInProgress ( distance > playPixel )
@@ -221,7 +225,7 @@ function useDetectKeyboard(
221225 const keyboardEnabled =
222226 enabled &&
223227 settings . linkCommand ?. startupMethod ?. method ===
224- LINK_COMMAND_STARTUP_METHOD . KEYBOARD
228+ LINK_COMMAND_STARTUP_METHOD . KEYBOARD
225229 const key = settings . linkCommand ?. startupMethod ?. keyboardParam
226230 const popupOption = command ?. popupOption ?? PopupOption
227231 const [ target , setTarget ] = useState < Element | null > ( null )
@@ -268,11 +272,11 @@ function useDetectKeyboard(
268272
269273 return keyboardEnabled
270274 ? {
271- progress : 0 ,
272- mousePosition,
273- inProgress : mousePress ,
274- preventLinkClick : true ,
275- }
275+ progress : 0 ,
276+ mousePosition,
277+ inProgress : mousePress ,
278+ preventLinkClick : true ,
279+ }
276280 : { }
277281}
278282
@@ -285,12 +289,12 @@ function useDetectClickHold(
285289 const clickHoldEnabled =
286290 enabled &&
287291 settings . linkCommand ?. startupMethod ?. method ===
288- LINK_COMMAND_STARTUP_METHOD . LEFT_CLICK_HOLD
292+ LINK_COMMAND_STARTUP_METHOD . LEFT_CLICK_HOLD
289293 const duration =
290294 settings . linkCommand ?. startupMethod ?. leftClickHoldParam ?? 200
291295 const detectLinkRef = useRef ( false )
292296 const [ forceClear , setForceClear ] = useState ( false )
293- const playPixel = 20
297+ const playProgress = 40 // percentage to start progress.
294298
295299 const { detectHoldLink, position, progress, linkElement } = useLeftClickHold ( {
296300 enable : clickHoldEnabled && ! forceClear ,
@@ -339,10 +343,10 @@ function useDetectClickHold(
339343
340344 return clickHoldEnabled && detectLinkRef . current
341345 ? {
342- mousePosition : position ,
343- inProgress : progress > playPixel ,
344- progress : progress ,
345- preventLinkClick : detectHoldLink ,
346- }
346+ mousePosition : position ,
347+ inProgress : progress > playProgress ,
348+ progress : progress ,
349+ preventLinkClick : detectHoldLink ,
350+ }
347351 : { }
348352}
0 commit comments