experiments-d3/ideaweb/modal.mjs

66 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-10-14 06:10:17 +00:00
export default class ModalEditor {
constructor(graph) {
this.graph = graph;
2024-10-14 06:47:07 +00:00
this.activeMode = this.commandMode;
2024-10-14 06:10:17 +00:00
this.word = "";
2024-10-14 06:47:07 +00:00
this.secondarySelection = [];
2024-10-14 06:10:17 +00:00
}
handleKeyup(event) {
console.log(event);
2024-10-14 06:47:07 +00:00
this.activeMode(event);
2024-10-14 06:10:17 +00:00
}
2024-10-14 06:47:07 +00:00
commandMode(event) {
2024-10-14 06:10:17 +00:00
switch (event.key) {
case "i":
2024-10-14 06:47:07 +00:00
this.activeMode = this.insertMode;
break;
case "s":
this.activeMode = this.searchMode;
2024-10-14 06:10:17 +00:00
break;
}
}
2024-10-14 06:47:07 +00:00
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
2024-10-14 06:10:17 +00:00
if (event.key === "Enter") {
let newId = this.graph.insert(this.word, "unknown");
this.graph.addLink(newId, this.graph.InboxId);
this.word = "";
2024-10-14 06:47:07 +00:00
this.activeMode = this.commandMode;
2024-10-14 06:10:17 +00:00
} else {
2024-10-14 06:47:07 +00:00
this.gatherText(event);
2024-10-14 06:10:17 +00:00
}
}
2024-10-14 06:47:07 +00:00
searchMode(event) {
this.gatherText(event);
this.secondarySelection = Array.from(this.graph.search(this.word));
console.log(this.word, this.secondarySelection);
}
2024-10-14 06:10:17 +00:00
display() {
2024-10-14 06:47:07 +00:00
switch (this.activeMode) {
case this.insertMode:
2024-10-14 06:10:17 +00:00
return "insert> " + this.word;
2024-10-14 06:47:07 +00:00
case this.searchMode:
return "search/ " + this.word;
default:
return "(i)nsert (s)earch";
2024-10-14 06:10:17 +00:00
}
}
}