]> git.scottworley.com Git - vopamoi/blob - vopamoi.ts
j/k movement keys
[vopamoi] / vopamoi.ts
1 function createTask(description: string) {
2 const task = document.createElement("div");
3 task.appendChild(document.createTextNode(description));
4 task.setAttribute("tabindex", "0");
5 return task;
6 }
7
8 function addTask(description: string) {
9 document.body.appendChild(createTask(description));
10 }
11
12 function moveCursor(offset: number) {
13 var active = document.activeElement;
14 if (offset === 1 && active) {
15 active = active.nextElementSibling;
16 }
17 if (offset === -1 && active) {
18 active = active.previousElementSibling;
19 }
20 if (active && active instanceof HTMLElement) active.focus();
21 }
22
23 function handleKey(event: any) {
24 if (event.target.tagName !== "INPUT") {
25 if (event.key == "j") moveCursor(1);
26 if (event.key == "k") moveCursor(-1);
27 }
28 }
29
30 function browserCreateTask(form: any) {
31 addTask(form.taskName.value);
32 form.taskName.value = "";
33 return false;
34 }
35
36 function browserInit() {
37 document.body.addEventListener("keydown", handleKey, { capture: false });
38 }