From 95ad958a0946cf1a07b0fa74ac0b537ca5a5032d Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sat, 20 Jul 2024 15:43:44 +0800 Subject: [PATCH] feat: disable hotkeys when modal opened --- .../src/components/ui/button/index.tsx | 5 ++- .../src/components/ui/image/preview-image.tsx | 7 ++- .../src/components/ui/modal/stacked/modal.tsx | 12 +++++ .../(main)/(layer)/feeds/[feedId]/layout.tsx | 45 +++++++++---------- src/renderer/src/providers/root-providers.tsx | 21 +++++---- 5 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/renderer/src/components/ui/button/index.tsx b/src/renderer/src/components/ui/button/index.tsx index 60724f348..9c664b4cc 100644 --- a/src/renderer/src/components/ui/button/index.tsx +++ b/src/renderer/src/components/ui/button/index.tsx @@ -143,7 +143,10 @@ const HotKeyTrigger = ({ fn: () => void options?: OptionsOrDependencyArray }) => { - useHotkeys(shortcut, fn, options) + useHotkeys(shortcut, fn, { + scopes: ["home"], + ...options, + }) return null } export const MotionButtonBase = React.forwardRef< diff --git a/src/renderer/src/components/ui/image/preview-image.tsx b/src/renderer/src/components/ui/image/preview-image.tsx index acee7e6c7..977856e38 100644 --- a/src/renderer/src/components/ui/image/preview-image.tsx +++ b/src/renderer/src/components/ui/image/preview-image.tsx @@ -4,7 +4,7 @@ import { stopPropagation } from "@renderer/lib/dom" import { showNativeMenu } from "@renderer/lib/native-menu" import type { FC } from "react" import { useCallback, useState } from "react" -import { Mousewheel, Scrollbar, Virtual } from "swiper/modules" +import { Keyboard, Mousewheel, Scrollbar, Virtual } from "swiper/modules" import { Swiper, SwiperSlide } from "swiper/react" import { ActionButton } from "../button" @@ -119,11 +119,14 @@ export const PreviewImageContent: FC<{ mousewheel={{ forceToAxis: true, }} + keyboard={{ + enabled: true, + }} virtual onSlideChange={({ realIndex }) => { setCurrentSrc(images[realIndex]) }} - modules={[Scrollbar, Mousewheel, Virtual]} + modules={[Scrollbar, Mousewheel, Virtual, Keyboard]} className="size-full" > {images.map((image, index) => ( diff --git a/src/renderer/src/components/ui/modal/stacked/modal.tsx b/src/renderer/src/components/ui/modal/stacked/modal.tsx index cf25d290b..8076ed2f4 100644 --- a/src/renderer/src/components/ui/modal/stacked/modal.tsx +++ b/src/renderer/src/components/ui/modal/stacked/modal.tsx @@ -20,6 +20,7 @@ import { useRef, useState, } from "react" +import { useHotkeysContext } from "react-hotkeys-hook" import { useEventCallback } from "usehooks-ts" import { Divider } from "../../divider" @@ -185,6 +186,17 @@ export const ModalInternal: Component<{ const edgeElementRef = useRef(null) + const { enableScope, disableScope } = useHotkeysContext() + + useEffect(() => { + enableScope("modal") + disableScope("home") + return () => { + enableScope("home") + disableScope("modal") + } + }, []) + if (CustomModalComponent) { return ( diff --git a/src/renderer/src/pages/(main)/(layer)/feeds/[feedId]/layout.tsx b/src/renderer/src/pages/(main)/(layer)/feeds/[feedId]/layout.tsx index d4b7c1f46..24aa49e69 100644 --- a/src/renderer/src/pages/(main)/(layer)/feeds/[feedId]/layout.tsx +++ b/src/renderer/src/pages/(main)/(layer)/feeds/[feedId]/layout.tsx @@ -4,7 +4,6 @@ import { views } from "@renderer/lib/constants" import { cn } from "@renderer/lib/utils" import { EntryColumn } from "@renderer/modules/entry-column" import { useMemo, useRef } from "react" -import { HotkeysProvider } from "react-hotkeys-hook" import { useResizable } from "react-resizable-layout" import { Outlet } from "react-router-dom" @@ -28,30 +27,28 @@ export function Component() { }) return ( - -
-
- -
- {!inWideMode && ( -
+
+
+ style={{ + width: position, + }} + > +
- + {!inWideMode && ( +
+ )} + +
) } diff --git a/src/renderer/src/providers/root-providers.tsx b/src/renderer/src/providers/root-providers.tsx index 601c1a4cf..d13fa251b 100644 --- a/src/renderer/src/providers/root-providers.tsx +++ b/src/renderer/src/providers/root-providers.tsx @@ -8,6 +8,7 @@ import { PersistQueryClientProvider } from "@tanstack/react-query-persist-client import { LazyMotion, MotionConfig } from "framer-motion" import { Provider } from "jotai" import type { FC, PropsWithChildren } from "react" +import { HotkeysProvider } from "react-hotkeys-hook" import { ContextMenuProvider } from "./context-menu-provider" import { StableRouterProvider } from "./stable-router-provider" @@ -30,15 +31,17 @@ export const RootProviders: FC = ({ children }) => ( client={queryClient} > - - - - - - - {import.meta.env.DEV && } - {children} - + + + + + + + + {import.meta.env.DEV && } + {children} + +