48 lines
919 B
JavaScript
48 lines
919 B
JavaScript
|
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";
|
||
|
}
|
||
|
}
|
||
|
}
|