From: Scott Worley Date: Thu, 27 Jan 2022 01:51:51 +0000 (-0800) Subject: Tasks have one state. Enables undo for state changes X-Git-Url: http://git.scottworley.com/vopamoi/commitdiff_plain/5350da9f7576c4af01c7359db78e4d3f658d98dc?hp=43f3cc0cb1e4b64b0560ca8d722754cf6ebb1b8d Tasks have one state. Enables undo for state changes --- diff --git a/vopamoi.ts b/vopamoi.ts index a88408f..298e513 100644 --- a/vopamoi.ts +++ b/vopamoi.ts @@ -32,6 +32,10 @@ const Model = { return parseFloat(task.getAttribute("data-created")!); }, + getState: function (task: Element): string { + return task.getAttribute("data-state") ?? "todo"; + }, + getTask: function (createTimestamp: string) { for (const task of document.getElementsByClassName("task")) { if (task.getAttribute("data-created") === createTimestamp) { @@ -59,9 +63,9 @@ const Model = { setState: function (stateTimestamp: string, createTimestamp: string, state: string) { const task = this.getTask(createTimestamp); if (task) { - task.setAttribute(`data-${state}`, stateTimestamp); + task.setAttribute("data-state", state); if (task instanceof HTMLElement) { - task.style.display = "none"; // Until view filtering + task.style.display = state == "todo" ? "block" : "none"; // Until view filtering } } }, @@ -120,8 +124,9 @@ const UI = { undoLog.push(`Priority ${createTimestamp} ${oldPriority}`); return log.recordAndApply(`${Date.now()} Priority ${createTimestamp} ${newPriority}`); }, - setState: function (createTimestamp: string, state: string) { - return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${state}`); + setState: function (createTimestamp: string, newState: string, oldState: string) { + undoLog.push(`State ${createTimestamp} ${oldState}`); + return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${newState}`); }, undo: function () { if (undoLog.length > 0) { @@ -210,9 +215,11 @@ const BrowserUI = { }, setState: function (state: string) { - const createTimestamp = document.activeElement?.getAttribute("data-created"); + const task = document.activeElement; + if (!task) return; + const createTimestamp = task.getAttribute("data-created")!; this.moveCursor(1) || this.moveCursor(-1); - return UI.setState(createTimestamp!, state); + return UI.setState(createTimestamp, state, Model.getState(task)); }, undo: function () {