const log = Log();
function UI() {
- const undoLog: string[] = [];
+ const undoLog: string[][] = [];
+ const redoLog: string[][] = [];
function perform(forward: string, reverse: string) {
- undoLog.push(reverse);
+ undoLog.push([reverse, forward]);
return log.recordAndApply(`${clock.now()} ${forward}`);
}
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 perform(`State ${createTimestamp} ${newState}`, `State ${createTimestamp} ${oldState}`);
},
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]}`);
}
},
};
},
beginEdit: function (event: Event) {
- const task = document.activeElement;
+ var task = document.activeElement;
if (!task) return;
+ if (task.classList.contains("tag")) task = task.parentElement!;
const input = document.createElement("input");
const desc = task.getElementsByClassName("desc")[0];
const oldDescription = desc.textContent!;
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();
+ },
+
makeTopPriority: function (task: Element | null = null) {
if (!task) task = document.activeElement;
if (!task) return;
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,
VS,
}
var inputState = InputState.Root;
+var inputCount: number | null = null;
function handleKey(event: any) {
+ if (["Alt", "Control", "Meta", "Shift"].includes(event.key)) return;
if (event.target.tagName === "INPUT") {
if (event.target.id === "taskName") {
if (event.key == "Enter") return browserUI.addTask(event);
}
} else {
if (inputState === InputState.Root) {
- if (event.key == "j") return browserUI.moveCursor(1);
- if (event.key == "k") return browserUI.moveCursor(-1);
- if (event.key == "J") return browserUI.moveTask(1);
- if (event.key == "K") return browserUI.moveTask(-1);
- if (event.key == "T") return browserUI.makeTopPriority();
- if (event.key == "n") return browserUI.focusTaskNameInput(event);
- if (event.key == "c") return browserUI.setState("cancelled");
- if (event.key == "d") return browserUI.setState("done");
- if (event.key == "q") return browserUI.setState("todo");
- if (event.key == "s") return (inputState = InputState.S);
- if (event.key == "w") return browserUI.setState("waiting");
- 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 == "e") return browserUI.beginEdit(event);
- if (event.key == "t") return browserUI.beginTagEdit(event);
- if (event.key == "v") return (inputState = InputState.V);
+ if ("0" <= event.key && event.key <= "9") {
+ return (inputCount = (inputCount ?? 0) * 10 + parseInt(event.key));
+ }
+ try {
+ if (event.ctrlKey) {
+ if (event.key == "e") return window.scrollBy(0, (inputCount ?? 1) * scrollIncrement);
+ if (event.key == "y") return window.scrollBy(0, (inputCount ?? 1) * -scrollIncrement);
+ } else {
+ if (event.key == "j") return browserUI.moveCursor(inputCount ?? 1);
+ if (event.key == "k") return browserUI.moveCursor(-(inputCount ?? 1));
+ if (event.key == "J") return browserUI.moveTask(inputCount ?? 1);
+ if (event.key == "K") return browserUI.moveTask(-(inputCount ?? 1));
+ if (event.key == "G") return browserUI.jumpCursor(inputCount ?? MAX_SAFE_INTEGER);
+ if (event.key == "T") return browserUI.makeTopPriority();
+ if (event.key == "n") return browserUI.focusTaskNameInput(event);
+ if (event.key == "c") return browserUI.setState("cancelled");
+ if (event.key == "d") return browserUI.setState("done");
+ if (event.key == "q") return browserUI.setState("todo");
+ if (event.key == "s") return (inputState = InputState.S);
+ if (event.key == "w") return browserUI.setState("waiting");
+ 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);
+ }
+ } finally {
+ inputCount = null;
+ }
} else if (inputState === InputState.S) {
inputState = InputState.Root;
if (event.key == "m") return browserUI.setState("someday-maybe");