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

View File

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

View File

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

View File

@ -184,21 +184,21 @@ const exitCmd: Command = {
execute: () => {
if (typeof window !== undefined) {
window.opener = null;
window.open('', '_self');
window.open("", "_self");
window.close();
}
return [
"If you can read this, closing the window did not work.",
"This is most likely because of a restriction in JavaScript.",
"#{Read more here|https://developer.mozilla.org/en-US/docs/Web/API/Window/close}."
]
];
}
}
};
const clear: Command = {
name: "clear",
desc: "Clears the output on screen.",
execute: () => []
}
};
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 inputRef = useRef<HTMLInputElement>();
const focusInput = () => {
console.log("Focus")
if (inputRef.current) inputRef.current.focus();};
const focusInput = () => {if (inputRef.current) inputRef.current.focus();};
return (<>
<Head>