experiments-d3/ideaweb/modal.mjs
2024-10-14 01:47:07 -05:00

66 lines
1.5 KiB
JavaScript

export default class ModalEditor {
constructor(graph) {
this.graph = graph;
this.activeMode = this.commandMode;
this.word = "";
this.secondarySelection = [];
}
handleKeyup(event) {
console.log(event);
this.activeMode(event);
}
commandMode(event) {
switch (event.key) {
case "i":
this.activeMode = this.insertMode;
break;
case "s":
this.activeMode = this.searchMode;
break;
}
}
gatherText(event) {
if (event.key === "Escape" || event.key == "Enter") {
this.activeMode = this.commandMode;
this.word = "";
} else if (event.key == "Backspace") {
this.word = this.word.substring(0, this.word.length - 1);
event.stopPropagation();
} else {
this.word += event.key;
}
}
insertMode(event) {
// override enter handler
if (event.key === "Enter") {
let newId = this.graph.insert(this.word, "unknown");
this.graph.addLink(newId, this.graph.InboxId);
this.word = "";
this.activeMode = this.commandMode;
} else {
this.gatherText(event);
}
}
searchMode(event) {
this.gatherText(event);
this.secondarySelection = Array.from(this.graph.search(this.word));
console.log(this.word, this.secondarySelection);
}
display() {
switch (this.activeMode) {
case this.insertMode:
return "insert> " + this.word;
case this.searchMode:
return "search/ " + this.word;
default:
return "(i)nsert (s)earch";
}
}
}