From: Scott Worley Date: Thu, 27 Jan 2022 08:25:01 +0000 (-0800) Subject: Always return to command mode (leave input box) after creating a task X-Git-Url: http://git.scottworley.com/vopamoi/commitdiff_plain/a59fbe41a148be79ea3623cd9a07cecf11b67d6c Always return to command mode (leave input box) after creating a task Even in other views, where the just-created task cannot accept focus. --- diff --git a/vopamoi.ts b/vopamoi.ts index a3b7824..fdc4723 100644 --- a/vopamoi.ts +++ b/vopamoi.ts @@ -149,7 +149,6 @@ function Log(prefix: string = "vp-") { } const log = Log(); - function UI() { const undoLog: string[] = []; return { @@ -181,12 +180,18 @@ const ui = UI(); function BrowserUI() { var currentViewState = "todo"; + var taskFocusedBeforeJumpingToInput: HTMLElement | null = null; return { addTask: function (event: KeyboardEvent) { const input = document.getElementById("taskName"); if (input.value) { const task = ui.addTask(input.value); - if (task && task instanceof HTMLElement) task.focus(); + if (currentViewState === "todo") { + task instanceof HTMLElement && task.focus(); + } else if (this.returnFocusAfterInput()) { + } else { + this.firstVisibleTask()?.focus(); + } input.value = ""; if (event.getModifierState("Control")) { this.setPriority(task, null, document.getElementsByClassName("task")[0]); @@ -234,6 +239,9 @@ function BrowserUI() { }, focusTaskNameInput: function (event: Event) { + if (document.activeElement instanceof HTMLElement) { + taskFocusedBeforeJumpingToInput = document.activeElement; + } document.getElementById("taskName")!.focus(); event.preventDefault(); }, @@ -279,6 +287,14 @@ function BrowserUI() { } }, + 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 ? 0 : Model.getPriority(a); @@ -331,6 +347,7 @@ function handleKey(event: any) { if (event.target.tagName === "INPUT") { if (event.target.id === "taskName") { if (event.key == "Enter") return browserUI.addTask(event); + if (event.key == "Escape") return browserUI.returnFocusAfterInput(); } else { if (event.key == "Enter") return browserUI.completeEdit(event); }