1
0
silverbullet/plugs/tasks/task.ts

123 lines
3.6 KiB
TypeScript
Raw Normal View History

import type { ClickEvent, IndexEvent } from "../../webapp/app_event";
2022-03-28 13:25:05 +00:00
2022-04-05 15:02:17 +00:00
import { batchSet } from "plugos-silverbullet-syscall/index";
import { readPage, writePage } from "plugos-silverbullet-syscall/space";
import { parseMarkdown } from "plugos-silverbullet-syscall/markdown";
import { dispatch, getCurrentPage, getText } from "plugos-silverbullet-syscall/editor";
2022-04-11 18:34:09 +00:00
import {
addParentPointers,
collectNodesMatching,
collectNodesOfType,
nodeAtPos,
renderToText
} from "../../common/tree";
import { whiteOutQueries } from "../query/util";
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
export async function indexTasks({ name, text }: IndexEvent) {
// console.log("Indexing tasks");
2022-03-28 13:25:05 +00:00
let tasks: { key: string; value: Task }[] = [];
2022-03-29 15:02:28 +00:00
text = whiteOutQueries(text);
2022-04-04 09:51:41 +00:00
let mdTree = await parseMarkdown(text);
addParentPointers(mdTree);
2022-04-11 18:34:09 +00:00
collectNodesOfType(mdTree, "Task").forEach((n) => {
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 deadlineNodes = collectNodesOfType(n, "DeadlineDate");
if (deadlineNodes.length > 0) {
value.deadline = deadlineNodes[0].children![0].text!.replace(/📅\s*/, "");
}
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
}
2022-04-01 15:32:03 +00:00
export async function taskToggleAtPos(pos: number) {
let currentpage = await getCurrentPage();
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-03-28 13:25:05 +00:00
let changeTo = "[x]";
2022-04-04 09:51:41 +00:00
if (node.children![0].text === "[x]" || node.children![0].text === "[X]") {
2022-03-28 13:25:05 +00:00
changeTo = "[ ]";
}
2022-04-01 15:07:08 +00:00
await dispatch({
2022-03-28 13:25:05 +00:00
changes: {
2022-04-04 09:51:41 +00:00
from: node.from,
to: node.to,
2022-03-28 13:25:05 +00:00
insert: changeTo,
},
selection: {
2022-04-01 15:32:03 +00:00
anchor: pos,
2022-03-28 13:25:05 +00:00
},
});
2022-04-04 09:51:41 +00:00
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("@");
if (page !== currentpage) {
text = (await readPage(page)).text;
}
2022-04-04 09:51:41 +00:00
let referenceMdTree = await parseMarkdown(text);
// Adding +1 to immediately hit the task marker
let taskMarkerNode = nodeAtPos(referenceMdTree, +pos + 1);
2022-03-28 13:25:05 +00:00
2022-04-04 09:51:41 +00:00
if (!taskMarkerNode || taskMarkerNode.type !== "TaskMarker") {
console.error(
"Reference not a task marker, out of date?",
taskMarkerNode
);
return;
}
taskMarkerNode.children![0].text = changeTo;
2022-04-04 13:25:07 +00:00
console.log(
"This will be the new marker",
2022-04-11 18:34:09 +00:00
renderToText(taskMarkerNode)
2022-04-04 13:25:07 +00:00
);
2022-04-11 18:34:09 +00:00
text = renderToText(referenceMdTree);
2022-04-04 09:51:41 +00:00
console.log("Updated reference paged text", text);
await writePage(page, text);
}
2022-03-28 13:25:05 +00:00
}
}
}