Task fixes

pull/3/head
Zef Hemel 2022-04-01 17:32:03 +02:00
parent 3aafa63073
commit a1a10a1d1f
5 changed files with 73 additions and 105 deletions

View File

@ -1,33 +1,33 @@
declare namespace window { // declare namespace window {
var ReactNativeWebView: any; // var ReactNativeWebView: any;
var receiveMessage: any; // var receiveMessage: any;
} // }
//
function safeRun(fn: () => Promise<void>) { // function safeRun(fn: () => Promise<void>) {
fn().catch((e) => { // fn().catch((e) => {
console.error(e); // console.error(e);
}); // });
} // }
//
window.receiveMessage = (msg: string) => { // window.receiveMessage = (msg: string) => {
console.log("Received message", msg); // console.log("Received message", msg);
}; // };
// @ts-ignore // // @ts-ignore
window.onerror = (msg, source, lineno, colno, error) => { // window.onerror = (msg, source, lineno, colno, error) => {
console.error("Error", msg, source, lineno, error); // console.error("Error", msg, source, lineno, error);
}; // };
//
console.log = (...args) => { // console.log = (...args) => {
window.ReactNativeWebView.postMessage( // window.ReactNativeWebView.postMessage(
JSON.stringify({ type: "console.log", args: args }) // JSON.stringify({ type: "console.log", args: args })
); // );
}; // };
//
console.error = (...args) => { // console.error = (...args) => {
window.ReactNativeWebView.postMessage( // window.ReactNativeWebView.postMessage(
JSON.stringify({ type: "console.error", args: args }) // JSON.stringify({ type: "console.error", args: args })
); // );
}; // };
try { try {
// let editor = new Editor( // let editor = new Editor(
// new Space(`http://192.168.2.22:3000/fs`, null), // new Space(`http://192.168.2.22:3000/fs`, null),

View File

@ -6,10 +6,11 @@ import {
navigate as navigateTo, navigate as navigateTo,
openUrl, openUrl,
} from "plugos-silverbullet-syscall/editor"; } from "plugos-silverbullet-syscall/editor";
import { taskToggleAtPos } from "../tasks/task";
const materializedQueryPrefix = /<!--\s*#query\s+/; const materializedQueryPrefix = /<!--\s*#query\s+/;
async function navigate(syntaxNode: any) { async function actionClickOrActionEnter(syntaxNode: any) {
if (!syntaxNode) { if (!syntaxNode) {
return; return;
} }
@ -38,16 +39,19 @@ async function navigate(syntaxNode: any) {
await openUrl(match[1]); await openUrl(match[1]);
} }
break; break;
case "TaskMarker":
await taskToggleAtPos(syntaxNode.from + 1);
break;
} }
} }
export async function linkNavigate() { export async function linkNavigate() {
await navigate(await getSyntaxNodeUnderCursor()); await actionClickOrActionEnter(await getSyntaxNodeUnderCursor());
} }
export async function clickNavigate(event: ClickEvent) { export async function clickNavigate(event: ClickEvent) {
if (event.ctrlKey || event.metaKey) { if (event.ctrlKey || event.metaKey) {
let syntaxNode = await getSyntaxNodeAtPos(event.pos); let syntaxNode = await getSyntaxNodeAtPos(event.pos);
await navigate(syntaxNode); await actionClickOrActionEnter(syntaxNode);
} }
} }

View File

@ -2,7 +2,7 @@ import type { ClickEvent } from "../../webapp/app_event";
import { IndexEvent } from "../../webapp/app_event"; import { IndexEvent } from "../../webapp/app_event";
import { whiteOutQueries } from "../core/materialized_queries"; import { whiteOutQueries } from "../core/materialized_queries";
import { batchSet, scanPrefixGlobal } from "plugos-silverbullet-syscall/index"; import { batchSet } from "plugos-silverbullet-syscall/index";
import { readPage, writePage } from "plugos-silverbullet-syscall/space"; import { readPage, writePage } from "plugos-silverbullet-syscall/space";
import { import {
dispatch, dispatch,
@ -10,10 +10,9 @@ import {
getSyntaxNodeAtPos, getSyntaxNodeAtPos,
} from "plugos-silverbullet-syscall/editor"; } from "plugos-silverbullet-syscall/editor";
const allTasksPageName = "ALL TASKS";
const taskRe = /[\-\*]\s*\[([ Xx])\]\s*(.*)/g;
const taskFullRe = const taskFullRe =
/(?<prefix>[\t ]*)[\-\*]\s*\[([ Xx])\]\s*([^\n]+)(\n\k<prefix>\s+[\-\*][^\n]+)*/g; /(?<prefix>[\t ]*)[\-\*]\s*\[([ Xx])\]\s*([^\n]+)(\n\k<prefix>\s+[\-\*][^\n]+)*/g;
const extractPageLink = /[\-\*]\s*\[[ Xx]\]\s\[\[([^\]]+)@(\d+)\]\]\s*(.*)/; const extractPageLink = /[\-\*]\s*\[[ Xx]\]\s\[\[([^\]]+)@(\d+)\]\]\s*(.*)/;
type Task = { type Task = {
@ -24,10 +23,6 @@ type Task = {
}; };
export async function indexTasks({ name, text }: IndexEvent) { export async function indexTasks({ name, text }: IndexEvent) {
if (name === allTasksPageName) {
return;
}
console.log("Indexing tasks"); console.log("Indexing tasks");
let tasks: { key: string; value: Task }[] = []; let tasks: { key: string; value: Task }[] = [];
text = whiteOutQueries(text); text = whiteOutQueries(text);
@ -54,41 +49,12 @@ export async function indexTasks({ name, text }: IndexEvent) {
await batchSet(name, tasks); await batchSet(name, tasks);
} }
export async function updateTaskPage() { export async function taskToggle(event: ClickEvent) {
let allTasks = await scanPrefixGlobal("task:"); return taskToggleAtPos(event.pos);
let pageTasks = new Map<string, Task[]>();
for (let {
key,
page,
value: { task, complete },
} of allTasks) {
if (complete) {
continue;
}
let [, pos] = key.split(":");
let tasks = pageTasks.get(page) || [];
tasks.push({ task, complete, pos: +pos });
pageTasks.set(page, tasks);
}
let mdPieces = [];
for (let pageName of [...pageTasks.keys()].sort()) {
mdPieces.push(`\n## ${pageName}\n`);
for (let task of pageTasks.get(pageName)!) {
mdPieces.push(
`* [${task.complete ? "x" : " "}] [[${pageName}@${task.pos}]] ${
task.task
}`
);
}
}
let taskMd = mdPieces.join("\n");
await writePage(allTasksPageName, taskMd);
} }
export async function taskToggle(event: ClickEvent) { export async function taskToggleAtPos(pos: number) {
let syntaxNode = await getSyntaxNodeAtPos(event.pos); let syntaxNode = await getSyntaxNodeAtPos(pos);
if (syntaxNode && syntaxNode.name === "TaskMarker") { if (syntaxNode && syntaxNode.name === "TaskMarker") {
let changeTo = "[x]"; let changeTo = "[x]";
if (syntaxNode.text === "[x]" || syntaxNode.text === "[X]") { if (syntaxNode.text === "[x]" || syntaxNode.text === "[X]") {
@ -101,26 +67,28 @@ export async function taskToggle(event: ClickEvent) {
insert: changeTo, insert: changeTo,
}, },
selection: { selection: {
anchor: event.pos, anchor: pos,
}, },
}); });
if (event.page === allTasksPageName) { // In case there's a page reference with @ position in the task, let's propagate this change back to that page
// Propagate back to the page in question // Example: * [ ] [[MyPage@123]] My task
let line = await getLineUnderCursor(); let line = await getLineUnderCursor();
let match = line.match(extractPageLink); let match = line.match(extractPageLink);
if (match) { if (match) {
let [, page, posS] = match; console.log("Found a remote task reference, updating other page");
let pos = +posS; let [, page, posS] = match;
let pageData = await readPage(page); let pos = +posS;
let text = pageData.text; let pageData = await readPage(page);
let text = pageData.text;
// Apply the toggle // Apply the toggle
text = text =
text.substring(0, pos) + text.substring(0, pos) +
text.substring(pos).replace(/^([\-\*]\s*)\[[ xX]\]/, "$1" + changeTo); text
.substring(pos)
.replace(/^(\s*[\-\*]\s*)\[[ xX]\]/, "$1" + changeTo);
await writePage(page, text); await writePage(page, text);
}
} }
} }
} }

View File

@ -3,10 +3,6 @@ functions:
path: "./task.ts:indexTasks" path: "./task.ts:indexTasks"
events: events:
- page:index - page:index
updateTaskPage:
path: "./task.ts:updateTaskPage"
command:
name: "Tasks: Update Page"
taskToggle: taskToggle:
path: "./task.ts:taskToggle" path: "./task.ts:taskToggle"
events: events:

View File

@ -3,22 +3,22 @@
import express from "express"; import express from "express";
import http from "http"; import http from "http";
import yargs from "yargs"; import yargs from "yargs";
import {hideBin} from "yargs/helpers"; import { hideBin } from "yargs/helpers";
import {SilverBulletHooks} from "../common/manifest"; import { SilverBulletHooks } from "../common/manifest";
import {ExpressServer} from "./api_server"; import { ExpressServer } from "./api_server";
import {DiskPlugLoader} from "../plugos/plug_loader"; import { DiskPlugLoader } from "../plugos/plug_loader";
import {System} from "../plugos/system"; import { System } from "../plugos/system";
let args = yargs(hideBin(process.argv)) let args = yargs(hideBin(process.argv))
.option("port", { .option("port", {
type: "number", type: "number",
default: 3000, default: 3000,
}) })
.parse(); .parse();
if (!args._.length) { if (!args._.length) {
console.error("Usage: silverbullet <path-to-pages>"); console.error("Usage: silverbullet <path-to-pages>");
process.exit(1); process.exit(1);
} }
const pagesPath = args._[0] as string; const pagesPath = args._[0] as string;