Add input history

master
Kevin Weispfennig 2022-10-26 16:45:32 +02:00
parent 8d74237a6d
commit 9a6ce1f832
4 changed files with 65 additions and 42 deletions

View File

@ -20,6 +20,10 @@ export default class ItemBuilder {
return this;
}
withType(type) {
this.item.type = type;
}
withState(key, value) {
this.item.setState(key, value);
return this;

View File

@ -4,6 +4,8 @@ export default class Input {
this.output = outputHandler;
this.echoInput = true;
this.inputField = document.getElementById("input-area");
this.history = [];
this.historyCursor = 0;
this.init();
}
@ -13,7 +15,18 @@ export default class Input {
init() {
this.inputField.addEventListener("keydown", (e) => {
if (e.which == 13) {
if (e.key === "ArrowUp") {
if (this.historyCursor > 0) {
this.historyCursor--;
this.inputField.value = this.history[this.historyCursor];
}
} else if (e.key === "ArrowDown") {
if (this.historyCursor < this.history.length) {
this.historyCursor++;
this.inputField.value = this.history[this.historyCursor] ?? "";
}
} else if (e.key === "Enter") {
this.addToInputHistory(this.inputField.value);
const val = this.inputField.value;
this.inputField.value = "";
if (this.echoInput) this.output.say(`> ${val}`);
@ -21,4 +34,9 @@ export default class Input {
}
})
}
addToInputHistory(str) {
this.history.push(str);
this.historyCursor = this.history.length;
}
}

View File

@ -5,6 +5,7 @@ export default class Item {
this.id = "item";
this.name = "An item";
this.description = "You see nothing special about this item";
this.type = "item";
this.state = new State();
this.usable = true;
this.takeable = true;