-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: context menu prevent default and shortcut handler
Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
4 changed files
with
120 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/renderer/src/modules/entry-column/EntryColumnShortcutHandler.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { useMainContainerElement } from "@renderer/atoms/dom" | ||
import { shortcuts } from "@renderer/constants/shortcuts" | ||
import { useNavigateEntry } from "@renderer/hooks/biz/useNavigateEntry" | ||
import { useRouteEntryId } from "@renderer/hooks/biz/useRouteParams" | ||
import { useRefValue } from "@renderer/hooks/common" | ||
import type { FC } from "react" | ||
import { memo, useLayoutEffect, useState } from "react" | ||
import { useHotkeys } from "react-hotkeys-hook" | ||
import type { VirtuosoHandle } from "react-virtuoso" | ||
|
||
export const EntryColumnShortcutHandler: FC<{ | ||
refetch: () => void | ||
data: readonly string[] | ||
virtuosoRef: React.RefObject<VirtuosoHandle> | ||
}> = memo(({ data, refetch, virtuosoRef }) => { | ||
const dataRef = useRefValue(data!) | ||
|
||
useHotkeys( | ||
shortcuts.entries.refetch.key, | ||
() => { | ||
refetch() | ||
}, | ||
{ scopes: ["home"] }, | ||
) | ||
const currentEntryIdRef = useRefValue(useRouteEntryId()) | ||
|
||
const navigate = useNavigateEntry() | ||
|
||
const $mainContainer = useMainContainerElement() | ||
const [enabledArrowKey, setEnabledArrowKey] = useState(false) | ||
|
||
// Enable arrow key navigation shortcuts only when focus is on entryContent or entryList, | ||
// entryList shortcuts should not be triggered in the feed col | ||
useLayoutEffect(() => { | ||
if (!$mainContainer) return | ||
const handler = () => { | ||
const target = document.activeElement | ||
const isFocusIn = | ||
$mainContainer.contains(target) || $mainContainer === target | ||
|
||
setEnabledArrowKey(isFocusIn) | ||
} | ||
|
||
handler() | ||
// NOTE: focusin event will bubble to the document | ||
document.addEventListener("focusin", handler) | ||
return () => { | ||
document.removeEventListener("focusin", handler) | ||
} | ||
}, [$mainContainer]) | ||
|
||
useHotkeys( | ||
shortcuts.entries.next.key, | ||
() => { | ||
const data = dataRef.current | ||
const currentActiveEntryIndex = data.indexOf( | ||
currentEntryIdRef.current || "", | ||
) | ||
|
||
const nextIndex = Math.min(currentActiveEntryIndex + 1, data.length - 1) | ||
|
||
virtuosoRef.current?.scrollIntoView?.({ | ||
index: nextIndex, | ||
}) | ||
const nextId = data![nextIndex] | ||
|
||
navigate({ | ||
entryId: nextId, | ||
}) | ||
}, | ||
{ scopes: ["home"], enabled: enabledArrowKey }, | ||
) | ||
useHotkeys( | ||
shortcuts.entries.previous.key, | ||
() => { | ||
const data = dataRef.current | ||
const currentActiveEntryIndex = data.indexOf( | ||
currentEntryIdRef.current || "", | ||
) | ||
|
||
const nextIndex = | ||
currentActiveEntryIndex === -1 ? | ||
data.length - 1 : | ||
Math.max(0, currentActiveEntryIndex - 1) | ||
|
||
virtuosoRef.current?.scrollIntoView?.({ | ||
index: nextIndex, | ||
}) | ||
const nextId = data![nextIndex] | ||
|
||
navigate({ | ||
entryId: nextId, | ||
}) | ||
}, | ||
{ scopes: ["home"], enabled: enabledArrowKey }, | ||
) | ||
return null | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters