1
0
silverbullet/packages/plugs/tasks/task.ts

210 lines
5.4 KiB
TypeScript
Raw Normal View History

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,
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,
findNodeOfType,
2022-04-11 18:34:09 +00:00
nodeAtPos,
ParseTree,
2022-04-25 08:33:38 +00:00
renderToText,
} from "@silverbulletmd/common/tree";
import { removeQueries } from "../query/util";
import { applyQuery, QueryProviderEvent, renderQuery } from "../query/engine";
import { niceDate } from "../core/dates";
2022-03-28 13:25:05 +00:00
export type Task = {
name: string;
done: boolean;
2022-04-11 18:34:09 +00:00
deadline?: string;
2022-04-04 09:51:41 +00:00
nested?: string;
// 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
function getDeadline(deadlineNode: ParseTree): string {
return deadlineNode.children![0].text!.replace(/📅\s*/, "");
}
export async function indexTasks({ name, tree }: IndexTreeEvent) {
// console.log("Indexing tasks");
2022-03-28 13:25:05 +00:00
let tasks: { key: string; value: Task }[] = [];
removeQueries(tree);
collectNodesOfType(tree, "Task").forEach((n) => {
2022-04-11 18:34:09 +00:00
let task = n.children!.slice(1).map(renderToText).join("").trim();
2022-04-04 09:51:41 +00:00
let complete = n.children![0].children![0].text! !== "[ ]";
2022-03-29 15:02:28 +00:00
let value: Task = {
name: task,
done: complete,
2022-03-29 15:02:28 +00:00
};
2022-04-11 18:34:09 +00:00
let deadlineNode = findNodeOfType(n, "DeadlineDate");
if (deadlineNode) {
value.deadline = getDeadline(deadlineNode);
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-04-11 18:34:09 +00:00
value.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-03-29 15:02:28 +00:00
value,
2022-03-28 13:25:05 +00:00
});
2022-04-11 18:34:09 +00:00
// console.log("Task", value);
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
}
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") {
await toggleTaskMarker(node, pos);
}
}
2022-03-28 13:25:05 +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
}
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
}
export async function queryProvider({
query,
}: QueryProviderEvent): Promise<Task[]> {
let allTasks: Task[] = [];
for (let { key, page, value } of await queryPrefix("task:")) {
let [, pos] = key.split(":");
allTasks.push({
...value,
page: page,
pos: pos,
});
}
return applyQuery(query, allTasks);
}