frontpage/components/REPL/REPLInput.tsx

162 lines
6.3 KiB
TypeScript
Raw Normal View History

2021-12-14 23:08:18 +01:00
import type { NextPage } from "next";
import useSWR from "swr";
import { MutableRefObject, useState, createRef, useEffect } from "react";
2022-01-14 14:27:19 +01:00
import { CommandInterface } from "../../lib/commands";
2021-12-16 00:21:14 +01:00
import styles from "../../styles/REPL/REPLInput.module.css";
import { Project } from "../../lib/projects/types";
2021-12-14 23:08:18 +01:00
2021-12-16 01:15:43 +01:00
interface REPLInputParams {
2021-12-26 14:17:11 +01:00
historyCallback: CallableFunction;
historyClear: CallableFunction;
2022-02-05 00:47:36 +01:00
inputRef: MutableRefObject<HTMLInputElement|null>;
2022-01-14 14:27:19 +01:00
modalManipulation: {
setModalVisible: CallableFunction;
setModalProject: CallableFunction;
}
2021-12-16 01:15:43 +01:00
}
async function fetchProjects(endpoint: string): Promise<Project[]> {
const res = await fetch(endpoint);
return res.json();
}
2022-01-14 14:27:19 +01:00
const REPLInput: NextPage<REPLInputParams> = ({historyCallback, historyClear, inputRef, modalManipulation}) => {
2022-01-14 17:44:52 +01:00
const typed = createRef<HTMLSpanElement>();
const completion = createRef<HTMLSpanElement>();
const [currentCmd, setCurrentCmd] = useState<string[]>([]);
const [justTabbed, setJustTabbed] = useState<number>(0);
const [inCmdHistory, setInCmdHistory] = useState<number>(-1);
const [cmdHistory, setCmdHistory] = useState<string[]>([]);
2022-01-16 16:11:48 +01:00
const [usrInputTmp, setUsrInputTmp] = useState<string>("");
const [cmdIf, setCmdIf] = useState<CommandInterface>(new CommandInterface(modalManipulation, []));
2022-02-04 19:21:26 +01:00
const { data: projects, error: projectsError } = useSWR("/api/projects?swr=1", fetchProjects);
2021-12-14 23:08:18 +01:00
2022-01-16 16:11:48 +01:00
const setInput = (inputRef: HTMLInputElement, input: string) => {
const nativeSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value")?.set;
if (!nativeSetter) return;
nativeSetter.call(inputRef, input);
//if(typed.current) typed.current.innerHTML = input;
//if(completion.current) completion.current.innerHTML = "";
const event = new Event("input", { bubbles: true });
inputRef.dispatchEvent(event);
};
2021-12-26 14:17:11 +01:00
const clearInput = (inputRef: HTMLInputElement) => {
2022-01-16 16:11:48 +01:00
setInput(inputRef, "");
2022-01-05 17:52:20 +01:00
};
2021-12-26 14:17:11 +01:00
2021-12-14 23:08:18 +01:00
const replinOnChange = (e: React.FormEvent<HTMLInputElement>) => {
2021-12-17 18:55:00 +01:00
const input = (e.target as HTMLInputElement);
const currentInput = input.value.toLowerCase();
// Force lowercase
input.value = currentInput;
if (currentInput.trim() === "") input.value = "";
2021-12-17 18:55:00 +01:00
if (currentInput.includes(" ")) {
// Command already typed
input.maxLength = 524288; // Default value
if (typed.current) typed.current.innerHTML = "";
if (completion.current) completion.current.innerHTML = "";
setCurrentCmd([]);
return;
} else {
input.maxLength = 20;
// Get completion hint
2022-01-14 14:27:19 +01:00
const suggest = CommandInterface.commandCompletion(currentInput);
2021-12-17 18:55:00 +01:00
setCurrentCmd(suggest);
if (suggest.length === 0) suggest.push("");
if (typed.current) typed.current.innerHTML = currentInput;
if (completion.current) completion.current.innerHTML = suggest[0].substring(currentInput.length);
}
2021-12-14 23:45:00 +01:00
};
2021-12-14 23:08:18 +01:00
2021-12-26 14:07:46 +01:00
const keyEvent = (e: React.KeyboardEvent<HTMLInputElement>) => {
2021-12-17 18:55:00 +01:00
const input = (e.target as HTMLInputElement);
2022-01-14 17:44:52 +01:00
if (e.key === "Tab") {
2021-12-14 23:08:18 +01:00
e.preventDefault();
2022-01-14 17:44:52 +01:00
}
if (e.key === "Tab" && currentCmd.length !== 0) {
2022-01-16 16:13:38 +01:00
const cmd = `${currentCmd[justTabbed % currentCmd.length]}${currentCmd.length === 1 ? " " : ""}`;
setInput(input, cmd);
2021-12-15 18:56:32 +01:00
setJustTabbed(justTabbed + 1);
2021-12-26 16:21:08 +01:00
return false;
2021-12-16 00:21:14 +01:00
} else setJustTabbed(0);
if (e.key === "Enter") {
2021-12-26 14:07:46 +01:00
e.preventDefault();
const command = (e.target as HTMLInputElement).value.trim();
if (cmdHistory.at(-1) !== command) setCmdHistory(cmdHistory.concat([command]).splice(0, 100));
2022-01-14 17:44:52 +01:00
clearInput(input);
setInCmdHistory(-1);
setCurrentCmd([]);
2021-12-26 14:17:11 +01:00
if (command === "clear") {
historyClear();
return false;
}
2022-01-14 14:27:19 +01:00
const result = cmdIf.executeCommand(command);
2021-12-16 00:21:14 +01:00
historyCallback(result);
2021-12-26 16:21:08 +01:00
return false;
2021-12-14 23:08:18 +01:00
}
2021-12-16 00:21:14 +01:00
2021-12-26 14:07:46 +01:00
if (e.key === "d" && e.ctrlKey) {
e.preventDefault();
2022-01-14 14:27:19 +01:00
const result = cmdIf.executeCommand("exit");
2021-12-26 14:17:11 +01:00
clearInput(input);
2021-12-26 14:07:46 +01:00
historyCallback(result);
2021-12-26 16:21:08 +01:00
return false;
2021-12-26 14:07:46 +01:00
}
2021-12-26 14:17:11 +01:00
if (e.key === "l" && e.ctrlKey) {
e.preventDefault();
clearInput(input);
historyClear();
2021-12-26 16:21:08 +01:00
return false;
}
if ((e.key === "c" || e.key === "u") && e.ctrlKey) {
e.preventDefault();
clearInput(input);
return false;
2021-12-26 14:17:11 +01:00
}
2022-01-14 17:44:52 +01:00
if (e.key === "ArrowUp") {
e.preventDefault();
const idx = inCmdHistory + 1;
if (idx < cmdHistory.length) {
2022-01-16 16:11:48 +01:00
if (inCmdHistory === -1) setUsrInputTmp(input.value);
2022-01-14 17:44:52 +01:00
const cmd = cmdHistory[cmdHistory.length - idx - 1];
2022-01-16 16:11:48 +01:00
setInput(input, cmd);
2022-01-14 17:44:52 +01:00
setInCmdHistory(idx);
}
}
if (e.key === "ArrowDown") {
e.preventDefault();
const idx = inCmdHistory - 1;
if (0 <= idx) {
const cmd = cmdHistory[cmdHistory.length - idx - 1];
2022-01-16 16:11:48 +01:00
setInput(input, cmd);
2022-01-14 17:44:52 +01:00
setInCmdHistory(idx);
2022-01-16 16:11:48 +01:00
} else if (idx === -1) {
setInput(input, usrInputTmp);
setInCmdHistory(-1);
2022-01-14 17:44:52 +01:00
}
}
2021-12-14 23:45:00 +01:00
};
2021-12-14 23:08:18 +01:00
useEffect(() => {
if (!projectsError && projects) setCmdIf(new CommandInterface(modalManipulation, projects));
// In any other case we just don't create another interface.
}, [projects, projectsError, modalManipulation]);
2021-12-16 01:03:54 +01:00
return <div className={styles.wrapperwrapper}>
<span className={styles.inputstart}>$&nbsp;</span>
<div className={styles.wrapper}>
2021-12-26 14:07:46 +01:00
<input ref={inputRef as MutableRefObject<HTMLInputElement>} className={styles.in} type={"text"} onChange={replinOnChange} onKeyDown={keyEvent} spellCheck={"false"} autoFocus maxLength={20} />
2021-12-16 01:03:54 +01:00
<span className={styles.completionWrapper}><span ref={typed} className={styles.typed}></span><span ref={completion} className={styles.completion}></span></span>
</div>
2021-12-14 23:45:00 +01:00
</div>;
};
2021-12-14 23:08:18 +01:00
export default REPLInput;