Skip to content

Commit 74fe094

Browse files
authored
Merge pull request #233 from ujiro99/fix/link-preview
Fix/link preview
2 parents f5d6403 + a0a738c commit 74fe094

4 files changed

Lines changed: 56 additions & 39 deletions

File tree

packages/extension/src/action/helper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export const openPopupAndClick = (
6868
incrementCommandExecutionCount().then(async () => {
6969
try {
7070
const { tabId } = await openPopupWindow(param)
71+
if (!tabId) {
72+
console.error("Tab ID is not available after opening popup")
73+
response(false)
74+
return
75+
}
7176
await Ipc.sendQueue(tabId, TabCommand.clickElement, {
7277
selector: (param as { selector: string }).selector,
7378
})

packages/extension/src/hooks/useDetectLinkCommand.ts

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect, useRef } from "react"
1+
import { useState, useEffect, useRef, useCallback } from "react"
22
import {
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
}

packages/extension/src/services/chrome.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ type ReadClipboardResult = {
131131
}
132132

133133
type OpenResult = {
134-
tabId: number
134+
tabId: number | undefined
135135
clipboardText: string
136136
}
137137

@@ -316,11 +316,11 @@ const readClipboardContent = async (
316316
): Promise<ClipboardResult> => {
317317
try {
318318
const result = await new Promise<ClipboardResult>((resolve) => {
319-
chrome.runtime.onConnect.addListener(function (port) {
319+
chrome.runtime.onConnect.addListener(function(port) {
320320
if (port.sender?.tab?.id !== tabId) {
321321
return
322322
}
323-
port.onMessage.addListener(function (msg) {
323+
port.onMessage.addListener(function(msg) {
324324
if (msg.command === BgCommand.setClipboard) {
325325
resolve(msg.data)
326326
}
@@ -431,10 +431,12 @@ export const openPopupWindow = async (
431431
}
432432

433433
await updateBackgroundData([window], param.commandId, current.id, type)
434-
updateRules([window.tabs?.[0].id as number])
434+
if (window.tabs?.[0]?.id) {
435+
updateRules([window.tabs[0].id])
436+
}
435437

436438
return {
437-
tabId: window.tabs?.[0].id as number,
439+
tabId: window.tabs?.[0]?.id,
438440
clipboardText,
439441
}
440442
}

packages/extension/src/services/pageAction/background.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export const openAndRun = (
272272
response: (res: unknown) => void,
273273
): boolean => {
274274
const open = async () => {
275-
let tabId: number
275+
let tabId: number | undefined
276276
let selectedText = param.selectedText
277277
let clipboardText: string
278278

@@ -295,6 +295,12 @@ export const openAndRun = (
295295
clipboardText = ret.clipboardText
296296
}
297297

298+
if (tabId == null) {
299+
console.error("Failed to open popup or tab")
300+
response(false)
301+
return
302+
}
303+
298304
// Use clipboard text if selected text is empty and useClipboard is true.
299305
// This is for the case for shortcut key.
300306
if (

0 commit comments

Comments
 (0)