2023-10-03 12:16:33 +00:00
|
|
|
import type { ClickEvent, IndexTreeEvent } from "$sb/app_event.ts";
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
import { editor, markdown, space, sync } from "$sb/syscalls.ts";
|
2022-10-14 13:11:33 +00:00
|
|
|
|
2022-04-11 18:34:09 +00:00
|
|
|
import {
|
|
|
|
addParentPointers,
|
|
|
|
collectNodesMatching,
|
2022-04-21 09:46:33 +00:00
|
|
|
findNodeOfType,
|
2023-09-01 14:57:29 +00:00
|
|
|
findParentMatching,
|
2022-04-11 18:34:09 +00:00
|
|
|
nodeAtPos,
|
2022-04-21 09:46:33 +00:00
|
|
|
ParseTree,
|
2022-04-25 08:33:38 +00:00
|
|
|
renderToText,
|
2022-07-04 09:30:30 +00:00
|
|
|
replaceNodesMatching,
|
2023-07-26 15:12:56 +00:00
|
|
|
traverseTreeAsync,
|
2022-10-14 13:11:33 +00:00
|
|
|
} from "$sb/lib/tree.ts";
|
|
|
|
import { niceDate } from "$sb/lib/dates.ts";
|
2023-07-24 17:54:31 +00:00
|
|
|
import { extractAttributes } from "$sb/lib/attribute.ts";
|
2023-07-30 17:31:04 +00:00
|
|
|
import { rewritePageRefs } from "$sb/lib/resolve.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { ObjectValue } from "$sb/types.ts";
|
|
|
|
import { indexObjects, queryObjects } from "../index/plug_api.ts";
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2023-10-13 15:10:57 +00:00
|
|
|
export type TaskObject = ObjectValue<
|
|
|
|
{
|
|
|
|
page: string;
|
|
|
|
pos: number;
|
|
|
|
name: string;
|
|
|
|
done: boolean;
|
|
|
|
state: string;
|
|
|
|
deadline?: string;
|
|
|
|
} & Record<string, any>
|
|
|
|
>;
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2023-10-13 15:10:57 +00:00
|
|
|
export type TaskStateObject = ObjectValue<{
|
2023-10-03 12:16:33 +00:00
|
|
|
state: string;
|
|
|
|
count: number;
|
|
|
|
page: string;
|
2023-10-13 15:10:57 +00:00
|
|
|
}>;
|
2023-10-03 12:16:33 +00:00
|
|
|
|
2022-04-21 09:46:33 +00:00
|
|
|
function getDeadline(deadlineNode: ParseTree): string {
|
|
|
|
return deadlineNode.children![0].text!.replace(/📅\s*/, "");
|
|
|
|
}
|
|
|
|
|
2023-09-01 14:57:29 +00:00
|
|
|
const completeStates = ["x", "X"];
|
|
|
|
const incompleteStates = [" "];
|
|
|
|
|
2022-04-20 08:56:43 +00:00
|
|
|
export async function indexTasks({ name, tree }: IndexTreeEvent) {
|
2023-10-03 12:16:33 +00:00
|
|
|
const tasks: ObjectValue<TaskObject>[] = [];
|
|
|
|
const taskStates = new Map<string, { count: number; firstPos: number }>();
|
2022-12-14 19:04:20 +00:00
|
|
|
addParentPointers(tree);
|
2023-10-03 12:16:33 +00:00
|
|
|
// const allAttributes: AttributeObject[] = [];
|
|
|
|
// const allTags = new Set<string>();
|
2023-07-26 15:12:56 +00:00
|
|
|
await traverseTreeAsync(tree, async (n) => {
|
|
|
|
if (n.type !== "Task") {
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-01 14:57:29 +00:00
|
|
|
const state = n.children![0].children![1].text!;
|
|
|
|
if (!incompleteStates.includes(state) && !completeStates.includes(state)) {
|
2023-10-03 12:16:33 +00:00
|
|
|
let currentState = taskStates.get(state);
|
|
|
|
if (!currentState) {
|
|
|
|
currentState = { count: 0, firstPos: n.from! };
|
2023-10-13 15:03:20 +00:00
|
|
|
taskStates.set(state, currentState);
|
2023-09-01 14:57:29 +00:00
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
currentState.count++;
|
2023-09-01 14:57:29 +00:00
|
|
|
}
|
|
|
|
const complete = completeStates.includes(state);
|
2023-10-03 12:16:33 +00:00
|
|
|
const task: TaskObject = {
|
|
|
|
ref: `${name}@${n.from}`,
|
|
|
|
tags: [],
|
2022-07-04 09:30:30 +00:00
|
|
|
name: "",
|
2022-04-12 11:33:07 +00:00
|
|
|
done: complete,
|
2023-10-03 12:16:33 +00:00
|
|
|
page: name,
|
|
|
|
pos: n.from!,
|
2023-09-01 14:57:29 +00:00
|
|
|
state,
|
2022-03-29 15:02:28 +00:00
|
|
|
};
|
2022-04-11 18:34:09 +00:00
|
|
|
|
2023-07-30 17:31:04 +00:00
|
|
|
rewritePageRefs(n, name);
|
|
|
|
|
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") {
|
2022-11-20 09:24:24 +00:00
|
|
|
// Push the tag to the list, removing the initial #
|
2023-10-03 12:16:33 +00:00
|
|
|
const tagName = tree.children![0].text!.substring(1);
|
|
|
|
task.tags.push(tagName);
|
2022-07-04 09:30:30 +00:00
|
|
|
}
|
|
|
|
});
|
2023-10-03 12:16:33 +00:00
|
|
|
task.tags = ["task", ...task.tags];
|
2022-07-04 09:30:30 +00:00
|
|
|
|
2023-07-24 17:54:31 +00:00
|
|
|
// Extract attributes and remove from tree
|
2023-07-26 15:12:56 +00:00
|
|
|
const extractedAttributes = await extractAttributes(n, true);
|
2023-07-24 17:54:31 +00:00
|
|
|
for (const [key, value] of Object.entries(extractedAttributes)) {
|
|
|
|
task[key] = value;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
tasks.push(task);
|
2023-07-26 15:12:56 +00:00
|
|
|
return true;
|
2022-04-04 09:51:41 +00:00
|
|
|
});
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
// Index task states
|
|
|
|
if (taskStates.size > 0) {
|
|
|
|
await indexObjects<TaskStateObject>(
|
|
|
|
name,
|
|
|
|
Array.from(taskStates.entries()).map(([state, { firstPos, count }]) => ({
|
|
|
|
ref: `${name}@${firstPos}`,
|
|
|
|
tags: ["taskstate"],
|
|
|
|
state,
|
|
|
|
count,
|
|
|
|
page: name,
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index tasks themselves
|
|
|
|
if (tasks.length > 0) {
|
|
|
|
await indexObjects(name, tasks);
|
|
|
|
}
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
export function taskToggle(event: ClickEvent) {
|
2023-09-01 14:57:29 +00:00
|
|
|
if (event.altKey) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
return taskCycleAtPos(event.pos);
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
export function previewTaskToggle(eventString: string) {
|
2022-11-01 14:01:28 +00:00
|
|
|
const [eventName, pos] = JSON.parse(eventString);
|
|
|
|
if (eventName === "task") {
|
2023-10-03 12:16:33 +00:00
|
|
|
return taskCycleAtPos(+pos);
|
2022-11-01 14:01:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 14:57:29 +00:00
|
|
|
async function cycleTaskState(
|
2023-07-02 09:25:32 +00:00
|
|
|
node: ParseTree,
|
|
|
|
) {
|
2023-09-01 14:57:29 +00:00
|
|
|
const stateText = node.children![1].text!;
|
|
|
|
let changeTo: string | undefined;
|
|
|
|
if (completeStates.includes(stateText)) {
|
|
|
|
changeTo = " ";
|
|
|
|
} else if (incompleteStates.includes(stateText)) {
|
|
|
|
changeTo = "x";
|
|
|
|
} else {
|
|
|
|
// Not a checkbox, but a custom state
|
2023-10-03 12:16:33 +00:00
|
|
|
const allStates = await queryObjects<TaskStateObject>("taskstate", {});
|
|
|
|
const states = [...new Set(allStates.map((s) => s.state))];
|
2023-09-01 14:57:29 +00:00
|
|
|
states.sort();
|
|
|
|
// Select a next state
|
|
|
|
const currentStateIndex = states.indexOf(stateText);
|
|
|
|
if (currentStateIndex === -1) {
|
|
|
|
console.error("Unknown state", stateText);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const nextStateIndex = (currentStateIndex + 1) % states.length;
|
|
|
|
changeTo = states[nextStateIndex];
|
|
|
|
// console.log("All possible states", states);
|
|
|
|
// return;
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.dispatch({
|
2022-04-21 09:46:33 +00:00
|
|
|
changes: {
|
2023-09-01 14:57:29 +00:00
|
|
|
from: node.children![1].from,
|
|
|
|
to: node.children![1].to,
|
2022-04-21 09:46:33 +00:00
|
|
|
insert: changeTo,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
const parentWikiLinks = collectNodesMatching(
|
2022-04-21 09:46:33 +00:00
|
|
|
node.parent!,
|
2022-10-10 12:50:21 +00:00
|
|
|
(n) => n.type === "WikiLinkPage",
|
2022-04-21 09:46:33 +00:00
|
|
|
);
|
2022-10-14 13:11:33 +00:00
|
|
|
for (const wikiLink of parentWikiLinks) {
|
|
|
|
const ref = wikiLink.children![0].text!;
|
2022-04-21 09:46:33 +00:00
|
|
|
if (ref.includes("@")) {
|
2023-10-03 12:16:33 +00:00
|
|
|
await updateTaskState(ref, stateText, changeTo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-28 11:54:31 +00:00
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
export async function updateTaskState(
|
|
|
|
ref: string,
|
|
|
|
oldState: string,
|
|
|
|
newState: string,
|
|
|
|
) {
|
|
|
|
const currentPage = await editor.getCurrentPage();
|
|
|
|
const [page, posS] = ref.split("@");
|
|
|
|
const pos = +posS;
|
|
|
|
if (page === currentPage) {
|
|
|
|
// In current page, just update the task marker with dispatch
|
|
|
|
const editorText = await editor.getText();
|
|
|
|
// Check if the task state marker is still there
|
|
|
|
const targetText = editorText.substring(
|
|
|
|
pos + 1,
|
|
|
|
pos + 1 + oldState.length,
|
|
|
|
);
|
|
|
|
if (targetText !== oldState) {
|
|
|
|
console.error(
|
|
|
|
"Reference not a task marker, out of date?",
|
|
|
|
targetText,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await editor.dispatch({
|
|
|
|
changes: {
|
|
|
|
from: pos + 1,
|
|
|
|
to: pos + 1 + oldState.length,
|
|
|
|
insert: newState,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
let text = await space.readPage(page);
|
|
|
|
|
|
|
|
const referenceMdTree = await markdown.parseMarkdown(text);
|
|
|
|
// Adding +1 to immediately hit the task state node
|
|
|
|
const taskStateNode = nodeAtPos(referenceMdTree, pos + 1);
|
|
|
|
if (!taskStateNode || taskStateNode.type !== "TaskState") {
|
|
|
|
console.error(
|
|
|
|
"Reference not a task marker, out of date?",
|
|
|
|
taskStateNode,
|
|
|
|
);
|
|
|
|
return;
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
taskStateNode.children![1].text = newState;
|
|
|
|
text = renderToText(referenceMdTree);
|
|
|
|
await space.writePage(page, text);
|
|
|
|
sync.scheduleFileSync(`${page}.md`);
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
export async function taskCycleAtPos(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);
|
|
|
|
|
2023-09-01 14:57:29 +00:00
|
|
|
let node = nodeAtPos(mdTree, pos);
|
|
|
|
if (node) {
|
2023-10-03 12:16:33 +00:00
|
|
|
if (node.type === "TaskMark") {
|
2023-09-01 14:57:29 +00:00
|
|
|
node = node.parent!;
|
|
|
|
}
|
|
|
|
if (node.type === "TaskState") {
|
2023-10-03 12:16:33 +00:00
|
|
|
await cycleTaskState(node);
|
2023-09-01 14:57:29 +00:00
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2023-09-01 14:57:29 +00:00
|
|
|
export async function taskCycleCommand() {
|
2022-10-14 13:11:33 +00:00
|
|
|
const text = await editor.getText();
|
|
|
|
const pos = await editor.getCursor();
|
|
|
|
const tree = await markdown.parseMarkdown(text);
|
2022-04-21 09:46:33 +00:00
|
|
|
addParentPointers(tree);
|
|
|
|
|
2023-09-01 14:57:29 +00:00
|
|
|
let node = nodeAtPos(tree, pos);
|
|
|
|
if (!node) {
|
|
|
|
await editor.flashNotification("No task at cursor");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (["BulletList", "Document"].includes(node.type!)) {
|
|
|
|
// Likely at the end of the line, let's back up a position
|
|
|
|
node = nodeAtPos(tree, pos - 1);
|
|
|
|
}
|
|
|
|
if (!node) {
|
|
|
|
await editor.flashNotification("No task at cursor");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log("Node", node);
|
|
|
|
const taskNode = node.type === "Task"
|
|
|
|
? node
|
|
|
|
: findParentMatching(node!, (n) => n.type === "Task");
|
|
|
|
if (!taskNode) {
|
|
|
|
await editor.flashNotification("No task at cursor");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const taskState = findNodeOfType(taskNode!, "TaskState");
|
|
|
|
if (taskState) {
|
2023-10-03 12:16:33 +00:00
|
|
|
await cycleTaskState(taskState);
|
2023-09-01 14:57:29 +00:00
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-04-21 09:46:33 +00:00
|
|
|
addParentPointers(tree);
|
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
const node = nodeAtPos(tree, pos)!;
|
2022-04-21 09:46:33 +00:00
|
|
|
// 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(
|
2022-04-21 09:46:33 +00:00
|
|
|
"Postpone for...",
|
|
|
|
[
|
|
|
|
{ name: "a day", orderId: 1 },
|
|
|
|
{ name: "a week", orderId: 2 },
|
|
|
|
{ name: "following Monday", orderId: 3 },
|
|
|
|
],
|
2022-10-10 12:50:21 +00:00
|
|
|
"Select the desired time span to delay this task",
|
2022-04-21 09:46:33 +00:00
|
|
|
);
|
|
|
|
if (!option) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-22 09:54:25 +00:00
|
|
|
// Parse "naive" due date
|
2023-09-01 14:57:29 +00:00
|
|
|
const [yyyy, mm, dd] = date.split("-").map(Number);
|
2023-05-22 09:54:25 +00:00
|
|
|
// Create new naive Date object.
|
|
|
|
// `monthIndex` parameter is zero-based, so subtract 1 from parsed month.
|
|
|
|
const d = new Date(yyyy, mm - 1, dd);
|
2022-04-21 09:46:33 +00:00
|
|
|
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
|
|
|
}
|
2023-05-22 09:54:25 +00:00
|
|
|
// console.log("New date", niceDate(d));
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.dispatch({
|
2022-04-21 09:46:33 +00:00
|
|
|
changes: {
|
|
|
|
from: node.from,
|
|
|
|
to: node.to,
|
|
|
|
insert: `📅 ${niceDate(d)}`,
|
|
|
|
},
|
|
|
|
selection: {
|
|
|
|
anchor: pos,
|
|
|
|
},
|
|
|
|
});
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|