]> git.scottworley.com Git - vopamoi/blobdiff - vopamoi.ts
Redo
[vopamoi] / vopamoi.ts
index 24b8774a4225036243e3fac4aaf81e9e5c791bc0..284363b047d24fb71bfff0ae2bb4a10c1bc7efe5 100644 (file)
@@ -215,36 +215,45 @@ function Log(prefix: string = "vp-") {
 const log = Log();
 
 function UI() {
 const log = Log();
 
 function UI() {
-  const undoLog: string[] = [];
+  const undoLog: string[][] = [];
+  const redoLog: string[][] = [];
+  function perform(forward: string, reverse: string) {
+    undoLog.push([reverse, forward]);
+    return log.recordAndApply(`${clock.now()} ${forward}`);
+  }
   return {
     addTask: function (description: string): Element {
       const now = clock.now();
   return {
     addTask: function (description: string): Element {
       const now = clock.now();
-      undoLog.push(`State ${now} deleted`);
+      undoLog.push([`State ${now} deleted`, `State ${now} todo`]);
       return <Element>log.recordAndApply(`${now} Create ${description}`);
     },
     addTag: function (createTimestamp: string, tag: string) {
       return <Element>log.recordAndApply(`${now} Create ${description}`);
     },
     addTag: function (createTimestamp: string, tag: string) {
-      undoLog.push(`Untag ${createTimestamp} ${tag}`);
-      return log.recordAndApply(`${clock.now()} Tag ${createTimestamp} ${tag}`);
+      return perform(`Tag ${createTimestamp} ${tag}`, `Untag ${createTimestamp} ${tag}`);
     },
     edit: function (createTimestamp: string, newDescription: string, oldDescription: string) {
     },
     edit: function (createTimestamp: string, newDescription: string, oldDescription: string) {
-      undoLog.push(`Edit ${createTimestamp} ${oldDescription}`);
-      return log.recordAndApply(`${clock.now()} Edit ${createTimestamp} ${newDescription}`);
+      return perform(`Edit ${createTimestamp} ${newDescription}`, `Edit ${createTimestamp} ${oldDescription}`);
     },
     removeTag: function (createTimestamp: string, tag: string) {
     },
     removeTag: function (createTimestamp: string, tag: string) {
-      undoLog.push(`Tag ${createTimestamp} ${tag}`);
-      return log.recordAndApply(`${clock.now()} Untag ${createTimestamp} ${tag}`);
+      return perform(`Untag ${createTimestamp} ${tag}`, `Tag ${createTimestamp} ${tag}`);
     },
     setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) {
     },
     setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) {
-      undoLog.push(`Priority ${createTimestamp} ${oldPriority}`);
-      return log.recordAndApply(`${clock.now()} Priority ${createTimestamp} ${newPriority}`);
+      return perform(`Priority ${createTimestamp} ${newPriority}`, `Priority ${createTimestamp} ${oldPriority}`);
     },
     setState: function (createTimestamp: string, newState: string, oldState: string) {
     },
     setState: function (createTimestamp: string, newState: string, oldState: string) {
-      undoLog.push(`State ${createTimestamp} ${oldState}`);
-      return log.recordAndApply(`${clock.now()} State ${createTimestamp} ${newState}`);
+      return perform(`State ${createTimestamp} ${newState}`, `State ${createTimestamp} ${oldState}`);
     },
     undo: function () {
     },
     undo: function () {
-      if (undoLog.length > 0) {
-        return log.recordAndApply(`${clock.now()} ${undoLog.pop()}`);
+      const entry = undoLog.pop();
+      if (entry) {
+        redoLog.push(entry);
+        return log.recordAndApply(`${clock.now()} ${entry[0]}`);
+      }
+    },
+    redo: function () {
+      const entry = redoLog.pop();
+      if (entry) {
+        undoLog.push(entry);
+        return log.recordAndApply(`${clock.now()} ${entry[1]}`);
       }
     },
   };
       }
     },
   };
@@ -465,6 +474,10 @@ function BrowserUI() {
       const ret = ui.undo();
       if (ret && ret instanceof HTMLElement) ret.focus();
     },
       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 browserUI = BrowserUI();
@@ -505,6 +518,7 @@ function handleKey(event: any) {
       if (event.key == "X") return browserUI.setState("deleted");
       if (event.key == "x") return browserUI.removeTag();
       if (event.key == "u") return browserUI.undo();
       if (event.key == "X") return browserUI.setState("deleted");
       if (event.key == "x") return browserUI.removeTag();
       if (event.key == "u") return browserUI.undo();
+      if (event.key == "r") return browserUI.redo();
       if (event.key == "e") return browserUI.beginEdit(event);
       if (event.key == "t") return browserUI.beginTagEdit(event);
       if (event.key == "v") return (inputState = InputState.V);
       if (event.key == "e") return browserUI.beginEdit(event);
       if (event.key == "t") return browserUI.beginTagEdit(event);
       if (event.key == "v") return (inputState = InputState.V);