From 5350da9f7576c4af01c7359db78e4d3f658d98dc Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Wed, 26 Jan 2022 17:51:51 -0800 Subject: [PATCH] Tasks have one state. Enables undo for state changes --- vopamoi.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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 () { -- 2.44.1