fix: Input issues fixed #535,#536

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-09-20 21:26:25 +08:00
parent 75cfd554ca
commit 3d486379b9
5 changed files with 75 additions and 36 deletions
@@ -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: <UilReact />,
@@ -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<HTMLInputElement>,
@@ -16,15 +16,19 @@ export const useInputComposition = <E = HTMLInputElement>(
: E extends HTMLTextAreaElement
? TextareaElementAttributes
: never,
"onKeyDown" | "onCompositionEnd" | "onCompositionStart"
"onKeyDown" | "onCompositionEnd" | "onCompositionStart" | "onKeyDownCapture"
>,
) => {
const { onKeyDown, onCompositionStart, onCompositionEnd } = props
const isCompositionRef = useRef(false)
const currentInputTargetRef = useRef<E | null>(null)
const handleCompositionStart: CompositionEventHandler<E> = useCallback(
(e) => {
currentInputTargetRef.current = e.target as E
isCompositionRef.current = true
onCompositionStart?.(e as any)
},
@@ -33,6 +37,7 @@ export const useInputComposition = <E = HTMLInputElement>(
const handleCompositionEnd: CompositionEventHandler<E> = useCallback(
(e) => {
currentInputTargetRef.current = null
isCompositionRef.current = false
onCompositionEnd?.(e as any)
},
@@ -52,12 +57,30 @@ export const useInputComposition = <E = HTMLInputElement>(
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,
@@ -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 = ({
) : (
<Input
{...formRegister}
onBlur={(e) => {
// Fix #535
nextFrame(() => {
formRegister.onBlur(e)
})
}}
placeholder={
(parameters?.default ?? formPlaceholder[keyItem.name])
? `e.g. ${formPlaceholder[keyItem.name]}`
+9 -3
View File
@@ -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<HTMLInputElement>({})
const handleKeyDownToFocusInput: React.EventHandler<React.KeyboardEvent> = 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}
/>
<div className={cn(styles["status-bar"], isPending && styles["loading"])} />
+3
View File
@@ -86,6 +86,9 @@ export default ({ mode }) => {
},
server: {
port: 2233,
watch: {
ignored: ["**/dist/**", "**/out/**", "**/public/**", ".git/**"],
},
},
plugins: [
...((viteRenderBaseConfig.plugins ?? []) as any),