This commit is contained in:
Daniel Kluge 2022-01-05 17:52:20 +01:00
parent 25340429ee
commit 79753de78f
5 changed files with 21 additions and 23 deletions

View File

@ -23,7 +23,7 @@ const REPLHistory: NextPage<REPLHistoryParams> = ({history, inputRef}) => {
})); }));
} }
return true; return true;
} };
const makeLinks = (line: string) => { const makeLinks = (line: string) => {
let idxStart = line.indexOf("#{"); let idxStart = line.indexOf("#{");
@ -31,7 +31,7 @@ const REPLHistory: NextPage<REPLHistoryParams> = ({history, inputRef}) => {
let idxEnd = line.indexOf("}", idxSep); let idxEnd = line.indexOf("}", idxSep);
if (idxStart === -1 || idxSep === -1 || idxEnd === -1) return [line]; if (idxStart === -1 || idxSep === -1 || idxEnd === -1) return [line];
const result = [] const result = [];
while (idxStart !== -1 && idxSep !== -1 && idxEnd !== -1) { while (idxStart !== -1 && idxSep !== -1 && idxEnd !== -1) {
const linkText = line.substring(idxStart+2, idxSep); const linkText = line.substring(idxStart+2, idxSep);
@ -40,14 +40,14 @@ const REPLHistory: NextPage<REPLHistoryParams> = ({history, inputRef}) => {
result.push(line.substring(0, idxStart)); result.push(line.substring(0, idxStart));
result.push(<Link href={linkHref}><a className={styles.link}>{linkText}</a></Link>); result.push(<Link href={linkHref}><a className={styles.link}>{linkText}</a></Link>);
line = line.substring(idxEnd+1) line = line.substring(idxEnd+1);
idxStart = line.indexOf("#{"); idxStart = line.indexOf("#{");
idxSep = line.indexOf("|", idxStart); idxSep = line.indexOf("|", idxStart);
idxEnd = line.indexOf("}", idxSep); idxEnd = line.indexOf("}", idxSep);
} }
result.push(line.substring(idxEnd+1)) result.push(line.substring(idxEnd+1));
return result; return result;
} };
const makeCommands = (line: string|JSX.Element, indexKey: number) => { const makeCommands = (line: string|JSX.Element, indexKey: number) => {
if (typeof line !== "string") return line; if (typeof line !== "string") return line;
@ -56,7 +56,7 @@ const REPLHistory: NextPage<REPLHistoryParams> = ({history, inputRef}) => {
let idxEnd = line.indexOf("}", idxStart); let idxEnd = line.indexOf("}", idxStart);
if (idxStart === -1 || idxEnd === -1) return line; if (idxStart === -1 || idxEnd === -1) return line;
const result = [] const result = [];
while (idxStart !== -1 && idxEnd !== -1) { while (idxStart !== -1 && idxEnd !== -1) {
const cmdText = line.substring(idxStart+2, idxEnd); const cmdText = line.substring(idxStart+2, idxEnd);
@ -65,27 +65,27 @@ const REPLHistory: NextPage<REPLHistoryParams> = ({history, inputRef}) => {
result.push(<span className={styles.cmd} onClick={forceInput} key={`${indexKey}${line.length}${cmdText}`}>{cmdText}</span>); result.push(<span className={styles.cmd} onClick={forceInput} key={`${indexKey}${line.length}${cmdText}`}>{cmdText}</span>);
line = line.substring(idxEnd+1) line = line.substring(idxEnd+1);
idxStart = line.indexOf("%{"); idxStart = line.indexOf("%{");
idxEnd = line.indexOf("}", idxStart); idxEnd = line.indexOf("}", idxStart);
} }
result.push(line.substring(idxEnd+1)) result.push(line.substring(idxEnd+1));
return result; return result;
} };
const parseLine = (line: string) => { const parseLine = (line: string) => {
if (line === "") return "\u00A0"; if (line === "") return "\u00A0";
const resultLinks = makeLinks(line); const resultLinks = makeLinks(line);
const resultAll = resultLinks.map(makeCommands) const resultAll = resultLinks.map(makeCommands);
return resultAll.flat() return resultAll.flat();
} };
return <div className={styles.container} onClick={focusInput}> return <div className={styles.container} onClick={focusInput}>
{ history.map((value, idx) => { { history.map((value, idx) => {
return <div className={styles.line} key={idx}> return <div className={styles.line} key={idx}>
{parseLine(value)} {parseLine(value)}
</div>} </div>;}
)} )}
</div>; </div>;
}; };

View File

@ -19,7 +19,7 @@ const REPLInput: NextPage<REPLInputParams> = ({historyCallback, historyClear, in
inputRef.value = ""; inputRef.value = "";
if(typed.current) typed.current.innerHTML = ""; if(typed.current) typed.current.innerHTML = "";
if(completion.current) completion.current.innerHTML = ""; if(completion.current) completion.current.innerHTML = "";
} };
const replinOnChange = (e: React.FormEvent<HTMLInputElement>) => { const replinOnChange = (e: React.FormEvent<HTMLInputElement>) => {
const input = (e.target as HTMLInputElement); const input = (e.target as HTMLInputElement);
@ -58,7 +58,7 @@ const REPLInput: NextPage<REPLInputParams> = ({historyCallback, historyClear, in
if (e.key === "Enter") { if (e.key === "Enter") {
e.preventDefault(); e.preventDefault();
const command = (e.target as HTMLInputElement).value const command = (e.target as HTMLInputElement).value;
if (command === "clear") { if (command === "clear") {
clearInput(input); clearInput(input);
historyClear(); historyClear();

View File

@ -11,7 +11,7 @@ const REPL: NextPage<{inputRef: MutableRefObject<HTMLInputElement|undefined>}> =
const focusInput = () => { const focusInput = () => {
if (inputRef.current) inputRef.current.focus(); if (inputRef.current) inputRef.current.focus();
} };
return (<div className={styles.container}> return (<div className={styles.container}>
<REPLHistory history={history} inputRef={inputRef} /> <REPLHistory history={history} inputRef={inputRef} />

View File

@ -184,21 +184,21 @@ const exitCmd: Command = {
execute: () => { execute: () => {
if (typeof window !== undefined) { if (typeof window !== undefined) {
window.opener = null; window.opener = null;
window.open('', '_self'); window.open("", "_self");
window.close(); window.close();
} }
return [ return [
"If you can read this, closing the window did not work.", "If you can read this, closing the window did not work.",
"This is most likely because of a restriction in JavaScript.", "This is most likely because of a restriction in JavaScript.",
"#{Read more here|https://developer.mozilla.org/en-US/docs/Web/API/Window/close}." "#{Read more here|https://developer.mozilla.org/en-US/docs/Web/API/Window/close}."
] ];
} }
} };
const clear: Command = { const clear: Command = {
name: "clear", name: "clear",
desc: "Clears the output on screen.", desc: "Clears the output on screen.",
execute: () => [] execute: () => []
} };
export const commandList = [about, help, man, project, exitCmd, clear]; export const commandList = [about, help, man, project, exitCmd, clear];

View File

@ -9,9 +9,7 @@ import styles from "../styles/Home.module.css";
const Home: NextPage = () => { const Home: NextPage = () => {
const inputRef = useRef<HTMLInputElement>(); const inputRef = useRef<HTMLInputElement>();
const focusInput = () => { const focusInput = () => {if (inputRef.current) inputRef.current.focus();};
console.log("Focus")
if (inputRef.current) inputRef.current.focus();};
return (<> return (<>
<Head> <Head>