+ focusTaskNameInput: function (event: Event) {
+ if (document.activeElement instanceof HTMLElement) {
+ taskFocusedBeforeJumpingToInput = document.activeElement;
+ }
+ document.getElementById("taskName")!.focus();
+ window.scroll(0, 0);
+ event.preventDefault();
+ },
+
+ visibleTaskAtOffset(task: Element, offset: number): Element {
+ var cursor: Element | null = task;
+ var valid_cursor = cursor;
+ const increment = offset / Math.abs(offset);
+ while (true) {
+ cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling;
+ if (!cursor || !(cursor instanceof HTMLElement)) break;
+ const state = cursor.getAttribute("data-state")!;
+ if (state === currentViewState || (currentViewState === "all" && state !== "deleted")) {
+ offset -= increment;
+ valid_cursor = cursor;
+ }
+ if (Math.abs(offset) < 0.5) break;
+ }
+ return valid_cursor;
+ },
+
+ jumpCursor: function (position: number) {
+ const first = this.firstVisibleTask();
+ if (!first) return;
+ const dest = this.visibleTaskAtOffset(first, position - 1);
+ if (dest instanceof HTMLElement) dest.focus();
+ },
+
+ makeBottomPriority: function (task: Element | null = null) {
+ if (!task) task = document.activeElement;
+ if (!task) return;
+ this.setPriority(task, document.getElementById("tasks")!.lastElementChild, null);
+ },
+
+ makeTopPriority: function (task: Element | null = null) {
+ if (!task) task = document.activeElement;
+ if (!task) return;
+ ui.setPriority(task.getAttribute("data-created")!, clock.now(), Model.getPriority(task));
+ task instanceof HTMLElement && task.focus();
+ },
+
+ moveCursor: function (offset: number): boolean {
+ const active = document.activeElement;
+ if (!active) return false;
+ const dest = this.visibleTaskAtOffset(active, offset);
+ if (dest !== active && dest instanceof HTMLElement) {
+ dest.focus();
+ return true;
+ }
+ return false;
+ },
+
+ moveTask: function (offset: number) {
+ const active = document.activeElement;
+ if (!active) return;
+ const dest = this.visibleTaskAtOffset(active, offset);
+ if (dest === active) return; // Already extremal
+ var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset));
+ if (onePastDest == dest) onePastDest = null; // Will become extremal
+ if (offset > 0) {
+ this.setPriority(active, dest, onePastDest);
+ } else {
+ this.setPriority(active, onePastDest, dest);
+ }
+ },
+
+ removeTag: function () {
+ const target = this.currentTag();
+ if (!target) return;
+ ui.removeTag(target.parentElement!.getAttribute("data-created")!, target.textContent!);
+ },
+
+ resetView: function () {
+ this.setView("todo");
+ },
+
+ returnFocusAfterInput: function (): boolean {
+ if (taskFocusedBeforeJumpingToInput) {
+ taskFocusedBeforeJumpingToInput.focus();
+ return true;
+ }
+ return false;
+ },
+
+ // Change task's priority to be between other tasks a and b.
+ setPriority: function (task: Element, a: Element | null, b: Element | null) {
+ const aPriority = a === null ? clock.now() : Model.getPriority(a);
+ const bPriority = b === null ? 0 : Model.getPriority(b);
+ console.assert(aPriority > bPriority, aPriority, ">", bPriority);
+ const span = aPriority - bPriority;
+ const newPriority = bPriority + 0.1 * span + 0.8 * span * Math.random();
+ console.assert(aPriority > newPriority && newPriority > bPriority, aPriority, ">", newPriority, ">", bPriority);
+ const newPriorityRounded = Math.round(newPriority);
+ const okToRound = aPriority > newPriorityRounded && newPriorityRounded > bPriority;
+ ui.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority, Model.getPriority(task));
+ task instanceof HTMLElement && task.focus();
+ },
+
+ setState: function (newState: string) {
+ const task = document.activeElement;
+ if (!task) return;
+ const oldState = task.getAttribute("data-state")!;
+ if (newState === oldState) return;
+ const createTimestamp = task.getAttribute("data-created")!;
+ if (currentViewState !== "all" || newState == "deleted") {
+ this.moveCursor(1) || this.moveCursor(-1);
+ }
+ return ui.setState(createTimestamp, newState, oldState);
+ },
+
+ setView: function (state: string) {
+ const sheet = (document.getElementById("viewStyle") as HTMLStyleElement).sheet!;
+ if (state === "all") {
+ sheet.insertRule(`.task[data-state=deleted] { display: none }`);
+ } else {
+ sheet.insertRule(`.task:not([data-state=${state}]) { display: none }`);
+ }
+ sheet.insertRule(`:root { --view-state-indicator-color: ${viewColors[state]}; }`);
+ sheet.removeRule(2);
+ sheet.removeRule(2);
+ currentViewState = state;
+ if (document.activeElement?.getAttribute("data-state") !== state) {
+ this.firstVisibleTask()?.focus();
+ }
+ },
+
+ undo: function () {
+ const ret = ui.undo();
+ if (ret && ret instanceof HTMLElement) ret.focus();
+ },
+ redo: function () {
+ const ret = ui.redo();
+ if (ret && ret instanceof HTMLElement) ret.focus();
+ },
+ };
+}
+const browserUI = BrowserUI();
+
+const scrollIncrement = 60;
+enum InputState {
+ Root,
+ S,
+ V,
+ VS,
+}
+var inputState = InputState.Root;
+var inputCount: number | null = null;