2022-04-21 11:57:45 +00:00
|
|
|
import type { ClickEvent, IndexTreeEvent } from "@silverbulletmd/web/app_event";
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
batchSet,
|
2022-05-17 13:54:55 +00:00
|
|
|
queryPrefix,
|
2022-04-25 09:24:13 +00:00
|
|
|
} from "@silverbulletmd/plugos-silverbullet-syscall/index";
|
|
|
|
import {
|
|
|
|
readPage,
|
|
|
|
writePage,
|
|
|
|
} from "@silverbulletmd/plugos-silverbullet-syscall/space";
|
|
|
|
import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown";
|
2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
dispatch,
|
|
|
|
filterBox,
|
|
|
|
getCursor,
|
|
|
|
getText,
|
2022-04-25 09:24:13 +00:00
|
|
|
} from "@silverbulletmd/plugos-silverbullet-syscall/editor";
|
2022-04-11 18:34:09 +00:00
|
|
|
import {
|
|
|
|
addParentPointers,
|
|
|
|
collectNodesMatching,
|
|
|
|
collectNodesOfType,
|
2022-04-21 09:46:33 +00:00
|
|
|
findNodeOfType,
|
2022-04-11 18:34:09 +00:00
|
|
|
nodeAtPos,
|
2022-04-21 09:46:33 +00:00
|
|
|
ParseTree,
|
2022-04-25 08:33:38 +00:00
|
|
|
renderToText,
|
2022-07-04 09:30:30 +00:00
|
|
|
replaceNodesMatching,
|
2022-04-21 11:57:45 +00:00
|
|
|
} from "@silverbulletmd/common/tree";
|
2022-04-20 08:56:43 +00:00
|
|
|
import { removeQueries } from "../query/util";
|
2022-07-04 09:38:05 +00:00
|
|
|
import { applyQuery, QueryProviderEvent } from "../query/engine";
|
2022-04-21 09:46:33 +00:00
|
|
|
import { niceDate } from "../core/dates";
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2022-04-12 11:33:07 +00:00
|
|
|
export type Task = {
|
|
|
|
name: string;
|
|
|
|
done: boolean;
|
2022-04-11 18:34:09 +00:00
|
|
|
deadline?: string;
|
2022-07-04 09:30:30 +00:00
|
|
|
tags?: string[];
|
2022-04-04 09:51:41 +00:00
|
|
|
nested?: string;
|
2022-04-12 11:33:07 +00:00
|
|
|
// Not saved in DB, just added when pulled out (from key)
|
|
|
|
pos?: number;
|
|
|
|
page?: string;
|
2022-03-29 15:02:28 +00:00
|
|
|
};
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2022-04-21 09:46:33 +00:00
|
|
|
function getDeadline(deadlineNode: ParseTree): string {
|
|
|
|
return deadlineNode.children![0].text!.replace(/📅\s*/, "");
|
|
|
|
}
|
|
|
|
|
2022-04-20 08:56:43 +00:00
|
|
|
export async function indexTasks({ name, tree }: IndexTreeEvent) {
|
2022-04-13 12:46:52 +00:00
|
|
|
// console.log("Indexing tasks");
|
2022-03-28 13:25:05 +00:00
|
|
|
let tasks: { key: string; value: Task }[] = [];
|
2022-04-20 08:56:43 +00:00
|
|
|
removeQueries(tree);
|
|
|
|
collectNodesOfType(tree, "Task").forEach((n) => {
|
2022-04-04 09:51:41 +00:00
|
|
|
let complete = n.children![0].children![0].text! !== "[ ]";
|
2022-07-04 09:30:30 +00:00
|
|
|
let task: Task = {
|
|
|
|
name: "",
|
2022-04-12 11:33:07 +00:00
|
|
|
done: complete,
|
2022-03-29 15:02:28 +00:00
|
|
|
};
|
2022-04-11 18:34:09 +00:00
|
|
|
|
2022-07-04 09:30:30 +00:00
|
|
|
replaceNodesMatching(n, (tree) => {
|
|
|
|
if (tree.type === "DeadlineDate") {
|
|
|
|
task.deadline = getDeadline(tree);
|
|
|
|
// Remove this node from the tree
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (tree.type === "Hashtag") {
|
|
|
|
if (!task.tags) {
|
|
|
|
task.tags = [];
|
|
|
|
}
|
|
|
|
task.tags.push(tree.children![0].text!);
|
|
|
|
// Remove this node from the tree
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
task.name = n.children!.slice(1).map(renderToText).join("").trim();
|
2022-04-11 18:34:09 +00:00
|
|
|
|
2022-04-04 09:51:41 +00:00
|
|
|
let taskIndex = n.parent!.children!.indexOf(n);
|
|
|
|
let nestedItems = n.parent!.children!.slice(taskIndex + 1);
|
|
|
|
if (nestedItems.length > 0) {
|
2022-07-04 09:30:30 +00:00
|
|
|
task.nested = nestedItems.map(renderToText).join("").trim();
|
2022-03-29 15:02:28 +00:00
|
|
|
}
|
2022-03-28 13:25:05 +00:00
|
|
|
tasks.push({
|
2022-04-04 09:51:41 +00:00
|
|
|
key: `task:${n.from}`,
|
2022-07-04 09:30:30 +00:00
|
|
|
value: task,
|
2022-03-28 13:25:05 +00:00
|
|
|
});
|
2022-07-04 09:30:30 +00:00
|
|
|
console.log("Task", task);
|
2022-04-04 09:51:41 +00:00
|
|
|
});
|
|
|
|
|
2022-03-28 13:25:05 +00:00
|
|
|
console.log("Found", tasks.length, "task(s)");
|
2022-04-01 15:07:08 +00:00
|
|
|
await batchSet(name, tasks);
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 15:32:03 +00:00
|
|
|
export async function taskToggle(event: ClickEvent) {
|
|
|
|
return taskToggleAtPos(event.pos);
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 09:46:33 +00:00
|
|
|
async function toggleTaskMarker(node: ParseTree, moveToPos: number) {
|
|
|
|
let changeTo = "[x]";
|
|
|
|
if (node.children![0].text === "[x]" || node.children![0].text === "[X]") {
|
|
|
|
changeTo = "[ ]";
|
|
|
|
}
|
|
|
|
await dispatch({
|
|
|
|
changes: {
|
|
|
|
from: node.from,
|
|
|
|
to: node.to,
|
|
|
|
insert: changeTo,
|
|
|
|
},
|
|
|
|
selection: {
|
|
|
|
anchor: moveToPos,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let parentWikiLinks = collectNodesMatching(
|
|
|
|
node.parent!,
|
|
|
|
(n) => n.type === "WikiLinkPage"
|
|
|
|
);
|
|
|
|
for (let wikiLink of parentWikiLinks) {
|
|
|
|
let ref = wikiLink.children![0].text!;
|
|
|
|
if (ref.includes("@")) {
|
|
|
|
let [page, pos] = ref.split("@");
|
|
|
|
let text = (await readPage(page)).text;
|
|
|
|
|
|
|
|
let referenceMdTree = await parseMarkdown(text);
|
|
|
|
// Adding +1 to immediately hit the task marker
|
|
|
|
let taskMarkerNode = nodeAtPos(referenceMdTree, +pos + 1);
|
|
|
|
|
|
|
|
if (!taskMarkerNode || taskMarkerNode.type !== "TaskMarker") {
|
|
|
|
console.error(
|
|
|
|
"Reference not a task marker, out of date?",
|
|
|
|
taskMarkerNode
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
taskMarkerNode.children![0].text = changeTo;
|
|
|
|
text = renderToText(referenceMdTree);
|
|
|
|
console.log("Updated reference paged text", text);
|
|
|
|
await writePage(page, text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 15:32:03 +00:00
|
|
|
export async function taskToggleAtPos(pos: number) {
|
2022-04-04 09:51:41 +00:00
|
|
|
let text = await getText();
|
|
|
|
let mdTree = await parseMarkdown(text);
|
|
|
|
addParentPointers(mdTree);
|
|
|
|
|
|
|
|
let node = nodeAtPos(mdTree, pos);
|
|
|
|
if (node && node.type === "TaskMarker") {
|
2022-04-21 09:46:33 +00:00
|
|
|
await toggleTaskMarker(node, pos);
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2022-04-21 09:46:33 +00:00
|
|
|
export async function taskToggleCommand() {
|
|
|
|
let text = await getText();
|
|
|
|
let pos = await getCursor();
|
|
|
|
let tree = await parseMarkdown(text);
|
|
|
|
addParentPointers(tree);
|
|
|
|
|
|
|
|
let node = nodeAtPos(tree, pos);
|
|
|
|
// We kwow node.type === Task (due to the task context)
|
|
|
|
let taskMarker = findNodeOfType(node!, "TaskMarker");
|
|
|
|
await toggleTaskMarker(taskMarker!, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function postponeCommand() {
|
|
|
|
let text = await getText();
|
|
|
|
let pos = await getCursor();
|
|
|
|
let tree = await parseMarkdown(text);
|
|
|
|
addParentPointers(tree);
|
|
|
|
|
|
|
|
let node = nodeAtPos(tree, pos)!;
|
|
|
|
// We kwow node.type === DeadlineDate (due to the task context)
|
|
|
|
let date = getDeadline(node);
|
|
|
|
let option = await filterBox(
|
|
|
|
"Postpone for...",
|
|
|
|
[
|
|
|
|
{ name: "a day", orderId: 1 },
|
|
|
|
{ name: "a week", orderId: 2 },
|
|
|
|
{ name: "following Monday", orderId: 3 },
|
|
|
|
],
|
|
|
|
"Select the desired time span to delay this task"
|
|
|
|
);
|
|
|
|
if (!option) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let d = new Date(date);
|
|
|
|
switch (option.name) {
|
|
|
|
case "a day":
|
|
|
|
d.setDate(d.getDate() + 1);
|
|
|
|
break;
|
|
|
|
case "a week":
|
|
|
|
d.setDate(d.getDate() + 7);
|
|
|
|
break;
|
|
|
|
case "following Monday":
|
|
|
|
d.setDate(d.getDate() + ((7 - d.getDay() + 1) % 7 || 7));
|
|
|
|
break;
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
await dispatch({
|
|
|
|
changes: {
|
|
|
|
from: node.from,
|
|
|
|
to: node.to,
|
|
|
|
insert: `📅 ${niceDate(d)}`,
|
|
|
|
},
|
|
|
|
selection: {
|
|
|
|
anchor: pos,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
// await toggleTaskMarker(taskMarker!, pos);
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|
2022-04-19 14:54:47 +00:00
|
|
|
|
|
|
|
export async function queryProvider({
|
|
|
|
query,
|
2022-04-28 09:55:38 +00:00
|
|
|
}: QueryProviderEvent): Promise<Task[]> {
|
2022-04-19 14:54:47 +00:00
|
|
|
let allTasks: Task[] = [];
|
2022-05-17 13:54:55 +00:00
|
|
|
for (let { key, page, value } of await queryPrefix("task:")) {
|
2022-04-19 14:54:47 +00:00
|
|
|
let [, pos] = key.split(":");
|
|
|
|
allTasks.push({
|
|
|
|
...value,
|
|
|
|
page: page,
|
|
|
|
pos: pos,
|
|
|
|
});
|
|
|
|
}
|
2022-04-28 09:55:38 +00:00
|
|
|
return applyQuery(query, allTasks);
|
2022-04-19 14:54:47 +00:00
|
|
|
}
|