2022-04-13 12:46:52 +00:00
|
|
|
import type { ClickEvent, IndexEvent } from "../../webapp/app_event";
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2022-04-19 14:54:47 +00:00
|
|
|
import { batchSet, scanPrefixGlobal } from "plugos-silverbullet-syscall/index";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { readPage, writePage } from "plugos-silverbullet-syscall/space";
|
|
|
|
import { parseMarkdown } from "plugos-silverbullet-syscall/markdown";
|
2022-04-12 11:33:07 +00:00
|
|
|
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";
|
2022-04-13 12:46:52 +00:00
|
|
|
import { whiteOutQueries } from "../query/util";
|
2022-04-19 14:54:47 +00:00
|
|
|
import { applyQuery, QueryProviderEvent } from "../query/engine";
|
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-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
|
|
|
|
|
|
|
export async function indexTasks({ name, text }: IndexEvent) {
|
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-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 = {
|
2022-04-12 11:33:07 +00:00
|
|
|
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) {
|
2022-04-12 11:33:07 +00:00
|
|
|
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("@");
|
2022-04-12 11:33:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 14:54:47 +00:00
|
|
|
|
|
|
|
export async function queryProvider({
|
|
|
|
query,
|
|
|
|
}: QueryProviderEvent): Promise<string> {
|
|
|
|
let allTasks: Task[] = [];
|
|
|
|
for (let { key, page, value } of await scanPrefixGlobal("task:")) {
|
|
|
|
let [, pos] = key.split(":");
|
|
|
|
allTasks.push({
|
|
|
|
...value,
|
|
|
|
page: page,
|
|
|
|
pos: pos,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let markdownTasks = applyQuery(query, allTasks).map(
|
|
|
|
(t) =>
|
|
|
|
`* [${t.done ? "x" : " "}] [[${t.page}@${t.pos}]] ${t.name}` +
|
|
|
|
(t.nested ? "\n " + t.nested : "")
|
|
|
|
);
|
|
|
|
return markdownTasks.join("\n");
|
|
|
|
}
|