From 23bc97528882e293883fff896b4ac1c5d8030371 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sun, 31 Mar 2024 19:04:44 +0100 Subject: feature(web): Add ⌘ + E shortcut for quickly focusing on the editor card. Fixes #58 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/dashboard/bookmarks/EditorCard.tsx | 97 +++++++++++++++------- 1 file changed, 67 insertions(+), 30 deletions(-) (limited to 'apps/web/components/dashboard/bookmarks/EditorCard.tsx') diff --git a/apps/web/components/dashboard/bookmarks/EditorCard.tsx b/apps/web/components/dashboard/bookmarks/EditorCard.tsx index adada927..28686e6c 100644 --- a/apps/web/components/dashboard/bookmarks/EditorCard.tsx +++ b/apps/web/components/dashboard/bookmarks/EditorCard.tsx @@ -1,17 +1,44 @@ import type { SubmitErrorHandler, SubmitHandler } from "react-hook-form"; +import { useEffect, useImperativeHandle, useRef } from "react"; import { ActionButton } from "@/components/ui/action-button"; -import { Form, FormControl, FormField, FormItem } from "@/components/ui/form"; +import { Form, FormControl, FormItem } from "@/components/ui/form"; import { Separator } from "@/components/ui/separator"; import { Textarea } from "@/components/ui/textarea"; +import { + Tooltip, + TooltipContent, + TooltipProvider, + TooltipTrigger, +} from "@/components/ui/tooltip"; import { toast } from "@/components/ui/use-toast"; import { useClientConfig } from "@/lib/clientConfig"; import { api } from "@/lib/trpc"; import { cn } from "@/lib/utils"; import { zodResolver } from "@hookform/resolvers/zod"; +import { Info } from "lucide-react"; import { useForm } from "react-hook-form"; import { z } from "zod"; +function useFocusOnKeyPress(inputRef: React.RefObject) { + useEffect(() => { + function handleKeyPress(e: KeyboardEvent) { + if (!inputRef.current) { + return; + } + if ((e.metaKey || e.ctrlKey) && e.code === "KeyE") { + inputRef.current.focus(); + } + } + document.addEventListener("keydown", handleKeyPress); + return () => { + document.removeEventListener("keydown", handleKeyPress); + }; + }, [inputRef]); +} + export default function EditorCard({ className }: { className?: string }) { + const inputRef = useRef(null); + const demoMode = !!useClientConfig().demoMode; const formSchema = z.object({ text: z.string(), @@ -22,6 +49,9 @@ export default function EditorCard({ className }: { className?: string }) { text: "", }, }); + const { ref, ...textFieldProps } = form.register("text"); + useImperativeHandle(ref, () => inputRef.current); + useFocusOnKeyPress(inputRef); const invalidateBookmarksCache = api.useUtils().bookmarks.invalidate; const { mutate, isPending } = api.bookmarks.createBookmark.useMutation({ @@ -62,36 +92,43 @@ export default function EditorCard({ className }: { className?: string }) { )} onSubmit={form.handleSubmit(onSubmit, onError)} > -

NEW ITEM

+
+

NEW ITEM

+ + + + + + +

+ You can quickly focus on this field by pressing ⌘ + E +

+
+
+
+
- { - return ( - - -