1
0
silverbullet/plugs/tasks/task.ts

95 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-03-28 13:25:05 +00:00
import type { ClickEvent } from "../../webapp/app_event";
import { IndexEvent } from "../../webapp/app_event";
2022-03-29 15:02:28 +00:00
import { whiteOutQueries } from "../core/materialized_queries";
2022-04-01 15:32:03 +00:00
import { batchSet } from "plugos-silverbullet-syscall/index";
2022-04-01 15:07:08 +00:00
import { readPage, writePage } from "plugos-silverbullet-syscall/space";
import {
dispatch,
getLineUnderCursor,
getSyntaxNodeAtPos,
} from "plugos-silverbullet-syscall/editor";
2022-03-29 15:02:28 +00:00
const taskFullRe =
/(?<prefix>[\t ]*)[\-\*]\s*\[([ Xx])\]\s*([^\n]+)(\n\k<prefix>\s+[\-\*][^\n]+)*/g;
2022-04-01 15:32:03 +00:00
2022-03-28 13:25:05 +00:00
const extractPageLink = /[\-\*]\s*\[[ Xx]\]\s\[\[([^\]]+)@(\d+)\]\]\s*(.*)/;
2022-03-29 15:02:28 +00:00
type Task = {
task: string;
complete: boolean;
pos?: number;
children?: string[];
};
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);
for (let match of text.matchAll(taskFullRe)) {
let entire = match[0];
let complete = match[2] !== " ";
let task = match[3];
2022-03-28 13:25:05 +00:00
let pos = match.index!;
2022-03-29 15:02:28 +00:00
let lines = entire.split("\n");
let value: Task = {
task,
complete,
};
if (lines.length > 1) {
value.children = lines.slice(1);
}
2022-03-28 13:25:05 +00:00
tasks.push({
key: `task:${pos}`,
2022-03-29 15:02:28 +00:00
value,
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 syntaxNode = await getSyntaxNodeAtPos(pos);
2022-03-28 13:25:05 +00:00
if (syntaxNode && syntaxNode.name === "TaskMarker") {
let changeTo = "[x]";
if (syntaxNode.text === "[x]" || syntaxNode.text === "[X]") {
changeTo = "[ ]";
}
2022-04-01 15:07:08 +00:00
await dispatch({
2022-03-28 13:25:05 +00:00
changes: {
from: syntaxNode.from,
to: syntaxNode.to,
insert: changeTo,
},
selection: {
2022-04-01 15:32:03 +00:00
anchor: pos,
2022-03-28 13:25:05 +00:00
},
});
2022-04-01 15:32:03 +00:00
// In case there's a page reference with @ position in the task, let's propagate this change back to that page
// Example: * [ ] [[MyPage@123]] My task
let line = await getLineUnderCursor();
let match = line.match(extractPageLink);
if (match) {
console.log("Found a remote task reference, updating other page");
let [, page, posS] = match;
let pos = +posS;
let pageData = await readPage(page);
let text = pageData.text;
2022-03-28 13:25:05 +00:00
2022-04-01 15:32:03 +00:00
// Apply the toggle
text =
text.substring(0, pos) +
text
.substring(pos)
.replace(/^(\s*[\-\*]\s*)\[[ xX]\]/, "$1" + changeTo);
2022-03-28 13:25:05 +00:00
2022-04-01 15:32:03 +00:00
await writePage(page, text);
2022-03-28 13:25:05 +00:00
}
}
}