1
0
silverbullet/plugs/tasks/task.ts

222 lines
5.7 KiB
TypeScript
Raw Normal View History

2022-10-14 13:11:33 +00:00
import type {
ClickEvent,
IndexTreeEvent,
QueryProviderEvent,
} from "$sb/app_event.ts";
2022-03-28 13:25:05 +00:00
2022-04-25 08:33:38 +00:00
import {
2022-10-14 13:11:33 +00:00
editor,
index,
markdown,
space,
} from "$sb/silverbullet-syscall/mod.ts";
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,
2022-07-04 09:30:30 +00:00
replaceNodesMatching,
2022-10-14 13:11:33 +00:00
} from "$sb/lib/tree.ts";
import { applyQuery, removeQueries } from "$sb/lib/query.ts";
import { niceDate } from "$sb/lib/dates.ts";
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-07-04 09:30:30 +00:00
tags?: 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) {
2022-10-14 13:11:33 +00:00
const tasks: { key: string; value: Task }[] = [];
removeQueries(tree);
addParentPointers(tree);
collectNodesOfType(tree, "Task").forEach((n) => {
2022-10-14 13:11:33 +00:00
const complete = n.children![0].children![0].text! !== "[ ]";
const task: Task = {
2022-07-04 09:30:30 +00:00
name: "",
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 = [];
}
2022-11-20 09:24:24 +00:00
// Push the tag to the list, removing the initial #
task.tags.push(tree.children![0].text!.substring(1));
2022-07-04 09:30:30 +00:00
// Remove this node from the tree
2022-11-30 14:59:28 +00:00
// return null;
2022-07-04 09:30:30 +00:00
}
});
task.name = n.children!.slice(1).map(renderToText).join("").trim();
2022-04-11 18:34:09 +00:00
2022-10-14 13:11:33 +00:00
const taskIndex = n.parent!.children!.indexOf(n);
const nestedItems = n.parent!.children!.slice(taskIndex + 1);
2022-04-04 09:51:41 +00:00
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-04-04 09:51:41 +00:00
});
// console.log("Found", tasks.length, "task(s)");
2022-10-14 13:11:33 +00:00
await index.batchSet(name, tasks);
2022-03-28 13:25:05 +00:00
}
2022-10-14 13:11:33 +00:00
export function taskToggle(event: ClickEvent) {
2022-04-01 15:32:03 +00:00
return taskToggleAtPos(event.pos);
2022-03-28 13:25:05 +00:00
}
export function previewTaskToggle(eventString: string) {
const [eventName, pos] = JSON.parse(eventString);
if (eventName === "task") {
return taskToggleAtPos(+pos);
}
}
async function toggleTaskMarker(node: ParseTree, moveToPos: number) {
let changeTo = "[x]";
if (node.children![0].text === "[x]" || node.children![0].text === "[X]") {
changeTo = "[ ]";
}
2022-10-14 13:11:33 +00:00
await editor.dispatch({
changes: {
from: node.from,
to: node.to,
insert: changeTo,
},
});
2022-10-14 13:11:33 +00:00
const parentWikiLinks = collectNodesMatching(
node.parent!,
(n) => n.type === "WikiLinkPage",
);
2022-10-14 13:11:33 +00:00
for (const wikiLink of parentWikiLinks) {
const ref = wikiLink.children![0].text!;
if (ref.includes("@")) {
2022-10-14 13:11:33 +00:00
const [page, pos] = ref.split("@");
2023-01-22 17:53:14 +00:00
let text = await space.readPage(page);
2022-10-14 13:11:33 +00:00
const referenceMdTree = await markdown.parseMarkdown(text);
// Adding +1 to immediately hit the task marker
2022-10-14 13:11:33 +00:00
const 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);
2022-10-14 13:11:33 +00:00
await space.writePage(page, text);
}
}
}
2022-04-01 15:32:03 +00:00
export async function taskToggleAtPos(pos: number) {
2022-10-14 13:11:33 +00:00
const text = await editor.getText();
const mdTree = await markdown.parseMarkdown(text);
2022-04-04 09:51:41 +00:00
addParentPointers(mdTree);
2022-10-14 13:11:33 +00:00
const node = nodeAtPos(mdTree, pos);
2022-04-04 09:51:41 +00:00
if (node && node.type === "TaskMarker") {
await toggleTaskMarker(node, pos);
}
}
2022-03-28 13:25:05 +00:00
export async function taskToggleCommand() {
2022-10-14 13:11:33 +00:00
const text = await editor.getText();
const pos = await editor.getCursor();
const tree = await markdown.parseMarkdown(text);
addParentPointers(tree);
2022-10-14 13:11:33 +00:00
const node = nodeAtPos(tree, pos);
// We kwow node.type === Task (due to the task context)
2022-10-14 13:11:33 +00:00
const taskMarker = findNodeOfType(node!, "TaskMarker");
await toggleTaskMarker(taskMarker!, pos);
}
export async function postponeCommand() {
2022-10-14 13:11:33 +00:00
const text = await editor.getText();
const pos = await editor.getCursor();
const tree = await markdown.parseMarkdown(text);
addParentPointers(tree);
2022-10-14 13:11:33 +00:00
const node = nodeAtPos(tree, pos)!;
// We kwow node.type === DeadlineDate (due to the task context)
2022-10-14 13:11:33 +00:00
const date = getDeadline(node);
const option = await editor.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;
}
2022-10-15 17:02:56 +00:00
const 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-10-14 13:11:33 +00:00
await editor.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[]> {
2022-10-14 13:11:33 +00:00
const allTasks: Task[] = [];
2022-10-15 17:02:56 +00:00
for (const { key, page, value } of await index.queryPrefix("task:")) {
const pos = key.split(":")[1];
allTasks.push({
...value,
page: page,
pos: pos,
});
}
return applyQuery(query, allTasks);
}