Add input history
parent
8d74237a6d
commit
9a6ce1f832
|
@ -20,6 +20,10 @@ export default class ItemBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
withType(type) {
|
||||||
|
this.item.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
withState(key, value) {
|
withState(key, value) {
|
||||||
this.item.setState(key, value);
|
this.item.setState(key, value);
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -4,6 +4,8 @@ export default class Input {
|
||||||
this.output = outputHandler;
|
this.output = outputHandler;
|
||||||
this.echoInput = true;
|
this.echoInput = true;
|
||||||
this.inputField = document.getElementById("input-area");
|
this.inputField = document.getElementById("input-area");
|
||||||
|
this.history = [];
|
||||||
|
this.historyCursor = 0;
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +15,18 @@ export default class Input {
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
this.inputField.addEventListener("keydown", (e) => {
|
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;
|
const val = this.inputField.value;
|
||||||
this.inputField.value = "";
|
this.inputField.value = "";
|
||||||
if (this.echoInput) this.output.say(`> ${val}`);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -5,6 +5,7 @@ export default class Item {
|
||||||
this.id = "item";
|
this.id = "item";
|
||||||
this.name = "An item";
|
this.name = "An item";
|
||||||
this.description = "You see nothing special about this item";
|
this.description = "You see nothing special about this item";
|
||||||
|
this.type = "item";
|
||||||
this.state = new State();
|
this.state = new State();
|
||||||
this.usable = true;
|
this.usable = true;
|
||||||
this.takeable = true;
|
this.takeable = true;
|
||||||
|
|
Loading…
Reference in New Issue