Merge pull request #1298 from danielyxie/hotfix

fix terminal not saving state
This commit is contained in:
hydroflame 2021-09-18 02:36:05 -04:00 committed by GitHub
commit 48cfa14366
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 11 deletions

File diff suppressed because one or more lines are too long

@ -43,15 +43,23 @@ interface IProps {
player: IPlayer; player: IPlayer;
} }
// Save command in case we de-load this screen.
let command = "";
export function TerminalInput({ terminal, engine, player }: IProps): React.ReactElement { export function TerminalInput({ terminal, engine, player }: IProps): React.ReactElement {
const terminalInput = useRef<HTMLInputElement>(null); const terminalInput = useRef<HTMLInputElement>(null);
const [value, setValue] = useState(""); const [value, setValue] = useState(command);
const [possibilities, setPossibilities] = useState<string[]>([]); const [possibilities, setPossibilities] = useState<string[]>([]);
const classes = useStyles(); const classes = useStyles();
function saveValue(value: string): void {
command = value;
setValue(value);
}
function handleValueChange(event: React.ChangeEvent<HTMLInputElement>): void { function handleValueChange(event: React.ChangeEvent<HTMLInputElement>): void {
setValue(event.target.value); saveValue(event.target.value);
setPossibilities([]); setPossibilities([]);
} }
@ -66,13 +74,13 @@ export function TerminalInput({ terminal, engine, player }: IProps): React.React
switch (mod.toLowerCase()) { switch (mod.toLowerCase()) {
case "backspace": case "backspace":
if (start > 0 && start <= inputLength + 1) { if (start > 0 && start <= inputLength + 1) {
setValue(inputText.substr(0, start - 1) + inputText.substr(start)); saveValue(inputText.substr(0, start - 1) + inputText.substr(start));
} }
break; break;
case "deletewordbefore": // Delete rest of word before the cursor case "deletewordbefore": // Delete rest of word before the cursor
for (let delStart = start - 1; delStart > 0; --delStart) { for (let delStart = start - 1; delStart > 0; --delStart) {
if (inputText.charAt(delStart) === " ") { if (inputText.charAt(delStart) === " ") {
setValue(inputText.substr(0, delStart) + inputText.substr(start)); saveValue(inputText.substr(0, delStart) + inputText.substr(start));
return; return;
} }
} }
@ -80,7 +88,7 @@ export function TerminalInput({ terminal, engine, player }: IProps): React.React
case "deletewordafter": // Delete rest of word after the cursor case "deletewordafter": // Delete rest of word after the cursor
for (let delStart = start + 1; delStart <= value.length + 1; ++delStart) { for (let delStart = start + 1; delStart <= value.length + 1; ++delStart) {
if (inputText.charAt(delStart) === " ") { if (inputText.charAt(delStart) === " ") {
setValue(inputText.substr(0, start) + inputText.substr(delStart)); saveValue(inputText.substr(0, start) + inputText.substr(delStart));
return; return;
} }
} }
@ -160,7 +168,7 @@ export function TerminalInput({ terminal, engine, player }: IProps): React.React
event.preventDefault(); event.preventDefault();
terminal.print(`[${player.getCurrentServer().hostname} ~${terminal.cwd()}]> ${value}`); terminal.print(`[${player.getCurrentServer().hostname} ~${terminal.cwd()}]> ${value}`);
terminal.executeCommands(engine, player, value); terminal.executeCommands(engine, player, value);
setValue(""); saveValue("");
return; return;
} }
@ -207,7 +215,7 @@ export function TerminalInput({ terminal, engine, player }: IProps): React.React
const newValue = tabCompletion(command, arg, allPos, value); const newValue = tabCompletion(command, arg, allPos, value);
if (typeof newValue === "string" && newValue !== "") { if (typeof newValue === "string" && newValue !== "") {
setValue(newValue); saveValue(newValue);
} }
if (Array.isArray(newValue)) { if (Array.isArray(newValue)) {
setPossibilities(newValue); setPossibilities(newValue);
@ -242,7 +250,7 @@ export function TerminalInput({ terminal, engine, player }: IProps): React.React
--terminal.commandHistoryIndex; --terminal.commandHistoryIndex;
} }
const prevCommand = terminal.commandHistory[terminal.commandHistoryIndex]; const prevCommand = terminal.commandHistory[terminal.commandHistoryIndex];
setValue(prevCommand); saveValue(prevCommand);
const ref = terminalInput.current; const ref = terminalInput.current;
if (ref) { if (ref) {
setTimeout(function () { setTimeout(function () {
@ -272,11 +280,11 @@ export function TerminalInput({ terminal, engine, player }: IProps): React.React
// Latest command, put nothing // Latest command, put nothing
if (i == len || i == len - 1) { if (i == len || i == len - 1) {
terminal.commandHistoryIndex = len; terminal.commandHistoryIndex = len;
setValue(""); saveValue("");
} else { } else {
++terminal.commandHistoryIndex; ++terminal.commandHistoryIndex;
const prevCommand = terminal.commandHistory[terminal.commandHistoryIndex]; const prevCommand = terminal.commandHistory[terminal.commandHistoryIndex];
setValue(prevCommand); saveValue(prevCommand);
} }
} }