experiments-d3/ideaweb/modal.mjs

48 lines
919 B
JavaScript
Raw Normal View History

2024-10-14 06:10:17 +00:00
export default class ModalEditor {
constructor(graph) {
this.graph = graph;
this.mode = "command";
this.word = "";
}
handleKeyup(event) {
console.log(event);
switch (this.mode) {
case "command":
this.commandModeKey(event);
break;
case "insert":
this.insertModeKey(event);
break;
}
}
commandModeKey(event) {
switch (event.key) {
case "i":
this.mode = "insert";
break;
}
}
insertModeKey(event) {
if (event.key === "Enter") {
let newId = this.graph.insert(this.word, "unknown");
this.graph.addLink(newId, this.graph.InboxId);
this.word = "";
this.mode = "command";
} else {
this.word += event.key;
}
}
display() {
switch (this.mode) {
case "insert":
return "insert> " + this.word;
case "command":
return "(i)nsert";
}
}
}