From 3dea76b80f47a4bd6d91366b75c9165baff36a71 Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Sun, 26 Dec 2021 14:07:46 +0100 Subject: [PATCH] Add exit command (fix #3) --- components/REPL/REPLInput.tsx | 14 ++++++++++++-- lib/commands/definitions.ts | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/components/REPL/REPLInput.tsx b/components/REPL/REPLInput.tsx index 7366081..2f175fb 100644 --- a/components/REPL/REPLInput.tsx +++ b/components/REPL/REPLInput.tsx @@ -38,7 +38,7 @@ const REPLInput: NextPage = ({historyCallback, inputRef}) => { } }; - const tabComplete = (e: React.KeyboardEvent) => { + const keyEvent = (e: React.KeyboardEvent) => { const input = (e.target as HTMLInputElement); if (e.key === "Tab" && currentCmd.length !== 0) { e.preventDefault(); @@ -49,6 +49,7 @@ const REPLInput: NextPage = ({historyCallback, inputRef}) => { } else setJustTabbed(0); if (e.key === "Enter") { + e.preventDefault(); const result = executeCommand((e.target as HTMLInputElement).value); input.value = ""; if(typed.current) typed.current.innerHTML = ""; @@ -56,13 +57,22 @@ const REPLInput: NextPage = ({historyCallback, inputRef}) => { historyCallback(result); } + if (e.key === "d" && e.ctrlKey) { + e.preventDefault(); + const result = executeCommand("exit"); + input.value = ""; + if(typed.current) typed.current.innerHTML = ""; + if(completion.current) completion.current.innerHTML = ""; + historyCallback(result); + } + return false; }; return
- } className={styles.in} type={"text"} onChange={replinOnChange} onKeyDown={tabComplete} spellCheck={"false"} autoFocus maxLength={20} /> + } className={styles.in} type={"text"} onChange={replinOnChange} onKeyDown={keyEvent} spellCheck={"false"} autoFocus maxLength={20} />
; diff --git a/lib/commands/definitions.ts b/lib/commands/definitions.ts index 479689e..3347834 100644 --- a/lib/commands/definitions.ts +++ b/lib/commands/definitions.ts @@ -178,4 +178,21 @@ const project: Command = { } }; -export const commandList = [about, help, man, project]; \ No newline at end of file +const exitCmd: Command = { + name: "exit", + desc: "Tries to close this tab. Mostly fails because of restrictions.", + execute: () => { + if (typeof window !== undefined) { + window.opener = null; + window.open('', '_self'); + window.close(); + } + return [ + "If you can read this, closing the window did not work.", + "This is because of restriction in JavaScript.", + "Read more here: https://developer.mozilla.org/en-US/docs/Web/API/Window/close" + ] + } +} + +export const commandList = [about, help, man, project, exitCmd]; \ No newline at end of file