From 3d486379b91dff13bfbcc90532efd3e186405a13 Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 20 Sep 2024 21:26:25 +0800 Subject: [PATCH] fix: Input issues fixed #535,#536 Signed-off-by: Innei --- .../ui/code-highlighter/constants/index.tsx | 60 +++++++++---------- .../src/hooks/common/useInputComposition.ts | 29 ++++++++- .../src/modules/discover/DiscoverFeedForm.tsx | 7 +++ apps/renderer/src/modules/panel/cmdk.tsx | 12 +++- vite.config.ts | 3 + 5 files changed, 75 insertions(+), 36 deletions(-) diff --git a/apps/renderer/src/components/ui/code-highlighter/constants/index.tsx b/apps/renderer/src/components/ui/code-highlighter/constants/index.tsx index a55db8215..905644c71 100644 --- a/apps/renderer/src/components/ui/code-highlighter/constants/index.tsx +++ b/apps/renderer/src/components/ui/code-highlighter/constants/index.tsx @@ -16,42 +16,42 @@ const LanguageAlias = { md: "markdown", } const LanguageToColorMap = { - typescript: "#2b7489", - javascript: "#f1e05a", - javascriptreact: "#f1e05a", - typescriptreact: "#2b7489", - html: "#e34c26", - java: "#b07219", - go: "#00add8", - vue: "#2c3e50", - css: "#563d7c", - yaml: "#cb171e", - json: "#292929", - markdown: "#083fa1", - csharp: "#178600", "c#": "#178600", - c: "#555555", - cpp: "#f34b7d", "c++": "#f34b7d", - python: "#3572a5", - lua: "#000080", - vimscript: "#199f4b", - shell: "#89e051", - dockerfile: "#384d54", - ruby: "#701516", - php: "#4f5d95", - lisp: "#3fb68b", - kotlin: "#F18E33", - rust: "#dea584", - dart: "#00B4AB", - swift: "#ffac45", "objective-c": "#438eff", "objective-c++": "#6866fb", - r: "#198ce7", + c: "#555555", + cpp: "#f34b7d", + csharp: "#178600", + css: "#563d7c", + dart: "#00B4AB", + dockerfile: "#384d54", + go: "#00add8", + html: "#e34c26", + java: "#b07219", + javascript: "#f1e05a", + javascriptreact: "#f1e05a", + json: "#292929", + kotlin: "#F18E33", + lisp: "#3fb68b", + lua: "#000080", + markdown: "#083fa1", matlab: "#e16737", - scala: "#c22d40", - sql: "#e38c00", perl: "#0298c3", + php: "#4f5d95", + python: "#3572a5", + r: "#198ce7", + ruby: "#701516", + rust: "#dea584", + scala: "#c22d40", + shell: "#89e051", + sql: "#e38c00", + swift: "#ffac45", + typescript: "#2b7489", + typescriptreact: "#2b7489", + vimscript: "#199f4b", + vue: "#2c3e50", + yaml: "#cb171e", } const languageToIconMap = { javascriptreact: , diff --git a/apps/renderer/src/hooks/common/useInputComposition.ts b/apps/renderer/src/hooks/common/useInputComposition.ts index 59505f3e1..3479951c6 100644 --- a/apps/renderer/src/hooks/common/useInputComposition.ts +++ b/apps/renderer/src/hooks/common/useInputComposition.ts @@ -1,5 +1,5 @@ import type { CompositionEventHandler } from "react" -import { useCallback, useRef } from "react" +import { useCallback, useEffect, useRef } from "react" type InputElementAttributes = React.DetailedHTMLProps< React.InputHTMLAttributes, @@ -16,15 +16,19 @@ export const useInputComposition = ( : E extends HTMLTextAreaElement ? TextareaElementAttributes : never, - "onKeyDown" | "onCompositionEnd" | "onCompositionStart" + "onKeyDown" | "onCompositionEnd" | "onCompositionStart" | "onKeyDownCapture" >, ) => { const { onKeyDown, onCompositionStart, onCompositionEnd } = props const isCompositionRef = useRef(false) + const currentInputTargetRef = useRef(null) + const handleCompositionStart: CompositionEventHandler = useCallback( (e) => { + currentInputTargetRef.current = e.target as E + isCompositionRef.current = true onCompositionStart?.(e as any) }, @@ -33,6 +37,7 @@ export const useInputComposition = ( const handleCompositionEnd: CompositionEventHandler = useCallback( (e) => { + currentInputTargetRef.current = null isCompositionRef.current = false onCompositionEnd?.(e as any) }, @@ -52,12 +57,30 @@ export const useInputComposition = ( e.preventDefault() e.stopPropagation() - e.currentTarget.blur() + if (!isCompositionRef.current) { + e.currentTarget.blur() + } } }, [onKeyDown], ) + // Register a global capture keydown listener to prevent the radix `useEscapeKeydown` from working + useEffect(() => { + const handleGlobalKeyDown = (e: KeyboardEvent) => { + if (e.key === "Escape" && currentInputTargetRef.current) { + e.stopPropagation() + e.preventDefault() + } + } + + document.addEventListener("keydown", handleGlobalKeyDown, { capture: true }) + + return () => { + document.removeEventListener("keydown", handleGlobalKeyDown, { capture: true }) + } + }, []) + const ret = { onCompositionEnd: handleCompositionEnd, onCompositionStart: handleCompositionStart, diff --git a/apps/renderer/src/modules/discover/DiscoverFeedForm.tsx b/apps/renderer/src/modules/discover/DiscoverFeedForm.tsx index 806f7826b..aae1a9016 100644 --- a/apps/renderer/src/modules/discover/DiscoverFeedForm.tsx +++ b/apps/renderer/src/modules/discover/DiscoverFeedForm.tsx @@ -22,6 +22,7 @@ import { SelectTrigger, SelectValue, } from "~/components/ui/select" +import { nextFrame } from "~/lib/dom" import type { FeedViewType } from "~/lib/enum" import { MissingOptionalParamError, @@ -237,6 +238,12 @@ export const DiscoverFeedForm = ({ ) : ( { + // Fix #535 + nextFrame(() => { + formRegister.onBlur(e) + }) + }} placeholder={ (parameters?.default ?? formPlaceholder[keyItem.name]) ? `e.g. ${formPlaceholder[keyItem.name]}` diff --git a/apps/renderer/src/modules/panel/cmdk.tsx b/apps/renderer/src/modules/panel/cmdk.tsx index 2de3d6120..48498c4db 100644 --- a/apps/renderer/src/modules/panel/cmdk.tsx +++ b/apps/renderer/src/modules/panel/cmdk.tsx @@ -22,7 +22,7 @@ import { import { Tooltip, TooltipContent, TooltipTrigger } from "~/components/ui/tooltip" import { ROUTE_ENTRY_PENDING } from "~/constants" import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry" -import { useI18n } from "~/hooks/common" +import { useI18n, useInputComposition } from "~/hooks/common" import type { FeedViewType } from "~/lib/enum" import { cn } from "~/lib/utils" import { getFeedById } from "~/store/feed" @@ -64,10 +64,14 @@ export const SearchCmdK: React.FC = () => { $input.focus() } }, [open]) + + const { onCompositionEnd, onCompositionStart, isCompositionRef } = + useInputComposition({}) const handleKeyDownToFocusInput: React.EventHandler = React.useCallback( (e) => { const $input = inputRef.current - if (e.key === "Escape") { + + if (e.key === "Escape" && !isCompositionRef.current) { setAppSearchOpen(false) return } @@ -78,7 +82,7 @@ export const SearchCmdK: React.FC = () => { $input?.focus() } }, - [], + [isCompositionRef], ) const [isPending, startTransition] = React.useTransition() const handleSearch = React.useCallback( @@ -140,6 +144,8 @@ export const SearchCmdK: React.FC = () => { ref={inputRef} placeholder={searchActions.getCurrentKeyword() || t("search.placeholder")} onValueChange={handleSearch} + onCompositionStart={onCompositionStart} + onCompositionEnd={onCompositionEnd} />
diff --git a/vite.config.ts b/vite.config.ts index c8dfa02b6..559d8fe52 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -86,6 +86,9 @@ export default ({ mode }) => { }, server: { port: 2233, + watch: { + ignored: ["**/dist/**", "**/out/**", "**/public/**", ".git/**"], + }, }, plugins: [ ...((viteRenderBaseConfig.plugins ?? []) as any),