Connect, collaborate, and grow with a community of TypeScript developers
Add Editor Modal and Build a custom hook to capture keypress (#497)
pizzaintheoven221f6a918429
diff --git a/apps/web/src/app/challenge/[id]/wrapper.tsx b/apps/web/src/app/challenge/[id]/wrapper.tsxindex 02890af9..a5e538a9 100644--- a/apps/web/src/app/challenge/[id]/wrapper.tsx+++ b/apps/web/src/app/challenge/[id]/wrapper.tsx@@ -8,6 +8,7 @@ import { SubmissionOverview } from './submissions/_components/overview';import { saveSubmission } from './submissions/save-submission.action';import { FullscreenButton } from '../_components/fullscreen';import { ResetEditorButton } from '../_components/reset-editor-button';+import { EditorShortcutsButton } from '../_components/editor-shortcuts/editor-shortcuts-button';export function Wrapper({ challenge }: { challenge: ChallengeRouteData }) {const segments = useSelectedLayoutSegments();@@ -37,6 +38,7 @@ function SettingsElements() {<ResetEditorButton /><FullscreenButton /><SettingsButton />+ <EditorShortcutsButton /></>);}diff --git a/apps/web/src/app/challenge/_components/editor-shortcuts/editor-shortcuts-button.tsx b/apps/web/src/app/challenge/_components/editor-shortcuts/editor-shortcuts-button.tsxnew file mode 100644index 00000000..7f88520f--- /dev/null+++ b/apps/web/src/app/challenge/_components/editor-shortcuts/editor-shortcuts-button.tsx@@ -0,0 +1,35 @@+import {+ Dialog,+ DialogContent,+ DialogHeader,+ DialogTitle,+ DialogTrigger,+ Tooltip,+ TooltipContent,+ TooltipTrigger,+} from '@repo/ui';+import { Command } from '@repo/ui/icons';+import { EditorShortcuts } from './editor-shortcuts-form';++export function EditorShortcutsButton() {+ return (+ <Dialog>+ <DialogTrigger>+ <Tooltip>+ <TooltipTrigger asChild>+ <Command className="stroke-zinc-500 stroke-1 hover:stroke-zinc-400" size={20} />+ </TooltipTrigger>+ <TooltipContent className="px-2 py-1">Settings</TooltipContent>+ </Tooltip>+ </DialogTrigger>+ <DialogContent className="w-[200px]">+ <DialogHeader>+ <DialogTitle>Settings</DialogTitle>+ </DialogHeader>+ <div className="pt-4">+ <EditorShortcuts />+ </div>+ </DialogContent>+ </Dialog>+ );+}diff --git a/apps/web/src/app/challenge/_components/editor-shortcuts/editor-shortcuts-form.tsx b/apps/web/src/app/challenge/_components/editor-shortcuts/editor-shortcuts-form.tsxnew file mode 100644index 00000000..9330951c--- /dev/null+++ b/apps/web/src/app/challenge/_components/editor-shortcuts/editor-shortcuts-form.tsx@@ -0,0 +1,101 @@+'use client';++interface Props {+ title: string;+ description: React.ReactNode;+}+function EditorShortcut(props: Props) {+ return (+ <div className="grid grid-cols-2 gap-2">+ <h1>{props.title}</h1>+ <>{props.description}</>+ </div>+ );+}++export function EditorShortcuts() {+ return (+ <div>+ <EditorShortcut+ title="Run Code"+ description={+ <p>+ <kbd>Ctrl</kbd> + <kbd>'</kbd>+ </p>+ }+ />+ <EditorShortcut+ title="Submit"+ description={+ <p>+ <kbd>Ctrl</kbd> + <kbd>Enter</kbd>+ </p>+ }+ />+ <EditorShortcut+ title="Debug Code"+ description={+ <p>+ <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>'</kbd>+ </p>+ }+ />+ <EditorShortcut+ title="To indent one level"+ description={+ <p>+ <kbd>Tab</kbd> + <kbd>Ctrl</kbd>+ </p>+ }+ />+ <EditorShortcut+ title="To indent one fewer levels"+ description={+ <p>+ <kbd>Shift</kbd> + <kbd>Tab</kbd>+ </p>+ }+ />+ <EditorShortcut+ title="To move lines up/down"+ description={+ <p>+ <kbd>Alt</kbd> + <kbd>Up/Down</kbd>+ </p>+ }+ />+ <EditorShortcut+ title="Delete line and copy to buffer"+ description={+ <p>+ <kbd>Ctrl</kbd> + <kbd>X</kbd>+ </p>+ }+ />+ <EditorShortcut+ title="Comment/uncomment current selection"+ description={+ <p>+ <kbd>Ctrl</kbd> + <kbd>/</kbd>+ </p>+ }+ />+ <EditorShortcut+ title="Undo action"+ description={+ <p>+ <kbd>Ctrl</kbd> + <kbd>Z</kbd>+ </p>+ }+ />+ <EditorShortcut+ title="Redo action"+ description={+ <p>+ <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Z</kbd>+ </p>+ }+ />+ </div>+ );+}diff --git a/packages/ui/src/components/use-keypress.tsx b/packages/ui/src/components/use-keypress.tsxnew file mode 100644index 00000000..51df229a--- /dev/null+++ b/packages/ui/src/components/use-keypress.tsx@@ -0,0 +1,25 @@+import { useState, useEffect } from 'react';++export function useKeyPress() {+ const [keyPresses, setKeyPresses] = useState<string>('');++ useEffect(() => {+ const handleKeyDown = (event: KeyboardEvent) => {+ setKeyPresses((prevKeyPresses) => prevKeyPresses + event.key);+ };++ const handleKeyUp = () => {+ setKeyPresses('');+ };++ window.addEventListener('keydown', handleKeyDown);+ window.addEventListener('keyup', handleKeyUp);++ return () => {+ window.removeEventListener('keydown', handleKeyDown);+ window.removeEventListener('keyup', handleKeyUp);+ };+ }, []);++ return Array.from(new Set(keyPresses)).join('');+}diff --git a/packages/ui/src/icons.ts b/packages/ui/src/icons.tsindex 0e22ffb2..053c9e0c 100644--- a/packages/ui/src/icons.ts+++ b/packages/ui/src/icons.ts@@ -10,6 +10,7 @@ export {ChevronLeft,ChevronRight,ChevronUp,+ Command,Circle,Clipboard,Copy,