From 9b04fe403437ab614f99a5314791cab5e1a6c0c4 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sun, 15 Mar 2026 14:02:33 +0100 Subject: [PATCH] Removed useless hooks --- src/hooks/useComposition.ts | 81 ------------------------------------- src/hooks/usePersistFn.ts | 20 --------- 2 files changed, 101 deletions(-) delete mode 100644 src/hooks/useComposition.ts delete mode 100644 src/hooks/usePersistFn.ts diff --git a/src/hooks/useComposition.ts b/src/hooks/useComposition.ts deleted file mode 100644 index 8405ad0..0000000 --- a/src/hooks/useComposition.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { useRef } from "react"; -import { usePersistFn } from "./usePersistFn"; - -export interface UseCompositionReturn< - T extends HTMLInputElement | HTMLTextAreaElement, -> { - onCompositionStart: React.CompositionEventHandler; - onCompositionEnd: React.CompositionEventHandler; - onKeyDown: React.KeyboardEventHandler; - isComposing: () => boolean; -} - -export interface UseCompositionOptions< - T extends HTMLInputElement | HTMLTextAreaElement, -> { - onKeyDown?: React.KeyboardEventHandler; - onCompositionStart?: React.CompositionEventHandler; - onCompositionEnd?: React.CompositionEventHandler; -} - -type TimerResponse = ReturnType; - -export function useComposition< - T extends HTMLInputElement | HTMLTextAreaElement = HTMLInputElement, ->(options: UseCompositionOptions = {}): UseCompositionReturn { - const { - onKeyDown: originalOnKeyDown, - onCompositionStart: originalOnCompositionStart, - onCompositionEnd: originalOnCompositionEnd, - } = options; - - const c = useRef(false); - const timer = useRef(null); - const timer2 = useRef(null); - - const onCompositionStart = usePersistFn((e: React.CompositionEvent) => { - if (timer.current) { - clearTimeout(timer.current); - timer.current = null; - } - if (timer2.current) { - clearTimeout(timer2.current); - timer2.current = null; - } - c.current = true; - originalOnCompositionStart?.(e); - }); - - const onCompositionEnd = usePersistFn((e: React.CompositionEvent) => { - // 使用两层 setTimeout 来处理 Safari 浏览器中 compositionEnd 先于 onKeyDown 触发的问题 - timer.current = setTimeout(() => { - timer2.current = setTimeout(() => { - c.current = false; - }); - }); - originalOnCompositionEnd?.(e); - }); - - const onKeyDown = usePersistFn((e: React.KeyboardEvent) => { - // 在 composition 状态下,阻止 ESC 和 Enter(非 shift+Enter)事件的冒泡 - if ( - c.current && - (e.key === "Escape" || (e.key === "Enter" && !e.shiftKey)) - ) { - e.stopPropagation(); - return; - } - originalOnKeyDown?.(e); - }); - - const isComposing = usePersistFn(() => { - return c.current; - }); - - return { - onCompositionStart, - onCompositionEnd, - onKeyDown, - isComposing, - }; -} diff --git a/src/hooks/usePersistFn.ts b/src/hooks/usePersistFn.ts deleted file mode 100644 index 6c4c024..0000000 --- a/src/hooks/usePersistFn.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { useRef } from "react"; - -type noop = (...args: any[]) => any; - -/** - * usePersistFn instead of useCallback to reduce cognitive load - */ -export function usePersistFn(fn: T) { - const fnRef = useRef(fn); - fnRef.current = fn; - - const persistFn = useRef(null); - if (!persistFn.current) { - persistFn.current = function (this: unknown, ...args) { - return fnRef.current!.apply(this, args); - } as T; - } - - return persistFn.current!; -}