2022-04-05 15:02:17 +00:00
|
|
|
import type { ClickEvent } from "../../webapp/app_event";
|
|
|
|
import { IndexEvent } from "../../webapp/app_event";
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2022-04-05 15:02:17 +00:00
|
|
|
import { whiteOutQueries } from "../core/materialized_queries";
|
|
|
|
import { batchSet } from "plugos-silverbullet-syscall/index";
|
|
|
|
import { readPage, writePage } from "plugos-silverbullet-syscall/space";
|
|
|
|
import { parseMarkdown } from "plugos-silverbullet-syscall/markdown";
|
|
|
|
import { dispatch, getText } from "plugos-silverbullet-syscall/editor";
|
|
|
|
import { addParentPointers, collectNodesMatching, nodeAtPos, renderMarkdown } from "../lib/tree";
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2022-03-29 15:02:28 +00:00
|
|
|
type Task = {
|
|
|
|
task: string;
|
|
|
|
complete: boolean;
|
|
|
|
pos?: number;
|
2022-04-04 09:51:41 +00:00
|
|
|
nested?: 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");
|
|
|
|
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);
|
|
|
|
collectNodesMatching(mdTree, (n) => n.type === "Task").forEach((n) => {
|
2022-04-04 13:25:07 +00:00
|
|
|
let task = n.children!.slice(1).map(renderMarkdown).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 = {
|
|
|
|
task,
|
|
|
|
complete,
|
|
|
|
};
|
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-04 13:25:07 +00:00
|
|
|
value.nested = nestedItems.map(renderMarkdown).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-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-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("@");
|
|
|
|
let pageData = await readPage(page);
|
|
|
|
let text = pageData.text;
|
|
|
|
|
|
|
|
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",
|
|
|
|
renderMarkdown(taskMarkerNode)
|
|
|
|
);
|
|
|
|
text = renderMarkdown(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|