Extracted syscall packages
This commit is contained in:
+2
-3
@@ -1,7 +1,6 @@
|
||||
import { syscall } from "../lib/syscall";
|
||||
import { insertAtCursor } from "plugos-silverbullet-syscall/editor";
|
||||
|
||||
export async function insertToday() {
|
||||
console.log("Inserting date!");
|
||||
let niceDate = new Date().toISOString().split("T")[0];
|
||||
await syscall("editor.insertAtCursor", niceDate);
|
||||
await insertAtCursor(niceDate);
|
||||
}
|
||||
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
import { IndexEvent } from "../../webapp/app_event";
|
||||
import { whiteOutQueries } from "./materialized_queries";
|
||||
import { syscall } from "../lib/syscall";
|
||||
|
||||
import { batchSet } from "plugos-silverbullet-syscall/index";
|
||||
|
||||
type Item = {
|
||||
item: string;
|
||||
@@ -35,5 +36,5 @@ export async function indexItems({ name, text }: IndexEvent) {
|
||||
});
|
||||
}
|
||||
console.log("Found", items.length, "item(s)");
|
||||
await syscall("index.batchSet", name, items);
|
||||
await batchSet(name, items);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { syscall } from "../lib/syscall";
|
||||
import mdParser from "../../webapp/parser";
|
||||
import { getText } from "plugos-silverbullet-syscall/editor";
|
||||
|
||||
export async function renderMD() {
|
||||
let text = await syscall("editor.getText");
|
||||
let text = await getText();
|
||||
let tree = mdParser.parser.parse(text);
|
||||
let slicesToRemove: [number, number][] = [];
|
||||
|
||||
|
||||
+10
-5
@@ -1,4 +1,9 @@
|
||||
import { syscall } from "../lib/syscall";
|
||||
import {
|
||||
getCursor,
|
||||
getText,
|
||||
insertAtPos,
|
||||
replaceRange,
|
||||
} from "plugos-silverbullet-syscall/editor";
|
||||
|
||||
export async function toggleH1() {
|
||||
await togglePrefix("# ");
|
||||
@@ -13,15 +18,15 @@ function lookBack(s: string, pos: number, backString: string): boolean {
|
||||
}
|
||||
|
||||
async function togglePrefix(prefix: string) {
|
||||
let text = (await syscall("editor.getText")) as string;
|
||||
let pos = (await syscall("editor.getCursor")) as number;
|
||||
let text = await getText();
|
||||
let pos = await getCursor();
|
||||
if (text[pos] === "\n") {
|
||||
pos--;
|
||||
}
|
||||
while (pos > 0 && text[pos] !== "\n") {
|
||||
if (lookBack(text, pos, prefix)) {
|
||||
// Already has this prefix, let's flip it
|
||||
await syscall("editor.replaceRange", pos - prefix.length, pos, "");
|
||||
await replaceRange(pos - prefix.length, pos, "");
|
||||
return;
|
||||
}
|
||||
pos--;
|
||||
@@ -29,5 +34,5 @@ async function togglePrefix(prefix: string) {
|
||||
if (pos) {
|
||||
pos++;
|
||||
}
|
||||
await syscall("editor.insertAtPos", prefix, pos);
|
||||
await insertAtPos(prefix, pos);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
import { syscall } from "../lib/syscall";
|
||||
import {
|
||||
flashNotification,
|
||||
getCurrentPage,
|
||||
reloadPage,
|
||||
save,
|
||||
} from "plugos-silverbullet-syscall/editor";
|
||||
|
||||
import { readPage, writePage } from "plugos-silverbullet-syscall/space";
|
||||
import { invokeFunctionOnServer } from "plugos-silverbullet-syscall/system";
|
||||
import { scanPrefixGlobal } from "plugos-silverbullet-syscall";
|
||||
|
||||
export const queryRegex =
|
||||
/(<!--\s*#query\s+(?<table>\w+)\s*(filter\s+["'“”‘’](?<filter>[^"'“”‘’]+)["'“”‘’])?\s*(group by\s+(?<groupBy>\w+))?\s*-->)(.+?)(<!--\s*#end\s*-->)/gs;
|
||||
/(<!--\s*#query\s+(?<table>\w+)\s*(filter\s+["'“”‘’](?<filter>[^"'“”‘’]+)["'“”‘’])?\s*-->)(.+?)(<!--\s*#end\s*-->)/gs;
|
||||
|
||||
export function whiteOutQueries(text: string): string {
|
||||
return text.replaceAll(queryRegex, (match) =>
|
||||
@@ -25,18 +34,16 @@ async function replaceAsync(
|
||||
}
|
||||
|
||||
export async function updateMaterializedQueriesCommand() {
|
||||
await syscall(
|
||||
"system.invokeFunctionOnServer",
|
||||
"updateMaterializedQueriesOnPage",
|
||||
await syscall("editor.getCurrentPage")
|
||||
);
|
||||
await syscall("editor.reloadPage");
|
||||
await syscall("editor.flashNotification", "Updated materialized queries");
|
||||
const currentPage = await getCurrentPage();
|
||||
await save();
|
||||
await invokeFunctionOnServer("updateMaterializedQueriesOnPage", currentPage);
|
||||
await reloadPage();
|
||||
await flashNotification("Updated materialized queries");
|
||||
}
|
||||
|
||||
// Called from client, running on server
|
||||
export async function updateMaterializedQueriesOnPage(pageName: string) {
|
||||
let { text } = await syscall("space.readPage", pageName);
|
||||
let { text } = await readPage(pageName);
|
||||
text = await replaceAsync(text, queryRegex, async (match, ...args) => {
|
||||
let { table, filter, groupBy } = args[args.length - 1];
|
||||
const startQuery = args[0];
|
||||
@@ -48,23 +55,20 @@ export async function updateMaterializedQueriesOnPage(pageName: string) {
|
||||
key,
|
||||
page,
|
||||
value: { task, complete, children },
|
||||
} of await syscall("index.scanPrefixGlobal", "task:")) {
|
||||
} of await scanPrefixGlobal("task:")) {
|
||||
let [, pos] = key.split(":");
|
||||
if (!filter || (filter && task.includes(filter))) {
|
||||
results.push(
|
||||
`* [${complete ? "x" : " "}] [[${page}@${pos}]] ${task}`
|
||||
`* [${complete ? "x" : " "}] [[${page}@${pos}]] ${task}` +
|
||||
(children ? "\n" + children.join("\n") : "")
|
||||
);
|
||||
if (children) {
|
||||
results.push(children.join("\n"));
|
||||
}
|
||||
}
|
||||
}
|
||||
return `${startQuery}\n${results.join("\n")}\n${endQuery}`;
|
||||
return `${startQuery}\n${results.sort().join("\n")}\n${endQuery}`;
|
||||
case "link":
|
||||
let uniqueLinks = new Set<string>();
|
||||
for (let {key, page, value: name} of await syscall(
|
||||
"index.scanPrefixGlobal",
|
||||
`pl:${pageName}:`
|
||||
for (let { key, page, value: name } of await scanPrefixGlobal(
|
||||
`pl:${pageName}:`
|
||||
)) {
|
||||
let [, pos] = key.split(":");
|
||||
if (!filter || (filter && name.includes(filter))) {
|
||||
@@ -80,20 +84,20 @@ export async function updateMaterializedQueriesOnPage(pageName: string) {
|
||||
key,
|
||||
page,
|
||||
value: { item, children },
|
||||
}; of await syscall("index.scanPrefixGlobal", "it:")) {
|
||||
} of await scanPrefixGlobal("it:")) {
|
||||
let [, pos] = key.split(":");
|
||||
if (!filter || (filter && item.includes(filter))) {
|
||||
results.push(`* [[${page}@${pos}]] ${item}`);
|
||||
if (children) {
|
||||
results.push(children.join("\n"));
|
||||
}
|
||||
results.push(
|
||||
`* [[${page}@${pos}]] ${item}` +
|
||||
(children ? "\n" + children.join("\n") : "")
|
||||
);
|
||||
}
|
||||
}
|
||||
return `${startQuery}\n${results.join("\n")}\n${endQuery}`;
|
||||
return `${startQuery}\n${results.sort().join("\n")}\n${endQuery}`;
|
||||
default:
|
||||
return match;
|
||||
}
|
||||
});
|
||||
// console.log("New text", text);
|
||||
await syscall("space.writePage", pageName, text);
|
||||
await writePage(pageName, text);
|
||||
}
|
||||
|
||||
+11
-6
@@ -1,6 +1,11 @@
|
||||
import { ClickEvent } from "../../webapp/app_event";
|
||||
import { syscall } from "../lib/syscall";
|
||||
import { updateMaterializedQueriesCommand } from "./materialized_queries";
|
||||
import {
|
||||
getSyntaxNodeAtPos,
|
||||
getSyntaxNodeUnderCursor,
|
||||
navigate as navigateTo,
|
||||
openUrl,
|
||||
} from "plugos-silverbullet-syscall/editor";
|
||||
|
||||
const materializedQueryPrefix = /<!--\s*#query\s+/;
|
||||
|
||||
@@ -16,10 +21,10 @@ async function navigate(syntaxNode: any) {
|
||||
if (pageLink.includes("@")) {
|
||||
[pageLink, pos] = syntaxNode.text.split("@");
|
||||
}
|
||||
await syscall("editor.navigate", pageLink, +pos);
|
||||
await navigateTo(pageLink, +pos);
|
||||
break;
|
||||
case "URL":
|
||||
await syscall("editor.openUrl", syntaxNode.text);
|
||||
await openUrl(syntaxNode.text);
|
||||
break;
|
||||
case "CommentBlock":
|
||||
if (syntaxNode.text.match(materializedQueryPrefix)) {
|
||||
@@ -30,19 +35,19 @@ async function navigate(syntaxNode: any) {
|
||||
// Markdown link: [bla](URLHERE) needs extraction
|
||||
let match = /\[[^\\]+\]\(([^\)]+)\)/.exec(syntaxNode.text);
|
||||
if (match) {
|
||||
await syscall("editor.openUrl", match[1]);
|
||||
await openUrl(match[1]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
export async function linkNavigate() {
|
||||
await navigate(await syscall("editor.getSyntaxNodeUnderCursor"));
|
||||
await navigate(await getSyntaxNodeUnderCursor());
|
||||
}
|
||||
|
||||
export async function clickNavigate(event: ClickEvent) {
|
||||
if (event.ctrlKey || event.metaKey) {
|
||||
let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos);
|
||||
let syntaxNode = await getSyntaxNodeAtPos(event.pos);
|
||||
await navigate(syntaxNode);
|
||||
}
|
||||
}
|
||||
|
||||
+47
-30
@@ -1,6 +1,27 @@
|
||||
import { IndexEvent } from "../../webapp/app_event";
|
||||
import { pageLinkRegex } from "../../webapp/constant";
|
||||
import { syscall } from "../lib/syscall";
|
||||
import {
|
||||
batchSet,
|
||||
clearPageIndex as clearPageIndexSyscall,
|
||||
clearPageIndexForPage,
|
||||
scanPrefixGlobal,
|
||||
} from "plugos-silverbullet-syscall/index";
|
||||
import {
|
||||
flashNotification,
|
||||
getCurrentPage,
|
||||
getText,
|
||||
matchBefore,
|
||||
navigate,
|
||||
} from "plugos-silverbullet-syscall/editor";
|
||||
|
||||
import { dispatch } from "plugos-syscall/event";
|
||||
import {
|
||||
deletePage as deletePageSyscall,
|
||||
listPages,
|
||||
readPage,
|
||||
writePage,
|
||||
} from "plugos-silverbullet-syscall/space";
|
||||
import { invokeFunctionOnServer } from "plugos-silverbullet-syscall/system";
|
||||
|
||||
const wikilinkRegex = new RegExp(pageLinkRegex, "g");
|
||||
|
||||
@@ -20,25 +41,21 @@ export async function indexLinks({ name, text }: IndexEvent) {
|
||||
});
|
||||
}
|
||||
console.log("Found", backLinks.length, "wiki link(s)");
|
||||
await syscall("index.batchSet", name, backLinks);
|
||||
await batchSet(name, backLinks);
|
||||
}
|
||||
|
||||
export async function deletePage() {
|
||||
let pageName = await syscall("editor.getCurrentPage");
|
||||
let pageName = await getCurrentPage();
|
||||
console.log("Navigating to start page");
|
||||
await syscall("editor.navigate", "start");
|
||||
await navigate("start");
|
||||
console.log("Deleting page from space");
|
||||
await syscall("space.deletePage", pageName);
|
||||
await deletePageSyscall(pageName);
|
||||
}
|
||||
|
||||
export async function renamePage() {
|
||||
const oldName = await syscall("editor.getCurrentPage");
|
||||
const oldName = await getCurrentPage();
|
||||
console.log("Old name is", oldName);
|
||||
const newName = await syscall(
|
||||
"editor.prompt",
|
||||
`Rename ${oldName} to:`,
|
||||
oldName
|
||||
);
|
||||
const newName = await prompt(`Rename ${oldName} to:`, oldName);
|
||||
if (!newName) {
|
||||
return;
|
||||
}
|
||||
@@ -47,13 +64,13 @@ export async function renamePage() {
|
||||
let pagesToUpdate = await getBackLinks(oldName);
|
||||
console.log("All pages containing backlinks", pagesToUpdate);
|
||||
|
||||
let text = await syscall("editor.getText");
|
||||
let text = await getText();
|
||||
console.log("Writing new page to space");
|
||||
await syscall("space.writePage", newName, text);
|
||||
await writePage(newName, text);
|
||||
console.log("Navigating to new page");
|
||||
await syscall("editor.navigate", newName);
|
||||
await navigate(newName);
|
||||
console.log("Deleting page from space");
|
||||
await syscall("space.deletePage", oldName);
|
||||
await deletePageSyscall(oldName);
|
||||
|
||||
let pageToUpdateSet = new Set<string>();
|
||||
for (let pageToUpdate of pagesToUpdate) {
|
||||
@@ -62,7 +79,7 @@ export async function renamePage() {
|
||||
|
||||
for (let pageToUpdate of pageToUpdateSet) {
|
||||
console.log("Now going to update links in", pageToUpdate);
|
||||
let { text } = await syscall("space.readPage", pageToUpdate);
|
||||
let { text } = await readPage(pageToUpdate);
|
||||
console.log("Received text", text);
|
||||
if (!text) {
|
||||
// Page likely does not exist, but at least we can skip it
|
||||
@@ -71,7 +88,7 @@ export async function renamePage() {
|
||||
let newText = text.replaceAll(`[[${oldName}]]`, `[[${newName}]]`);
|
||||
if (text !== newText) {
|
||||
console.log("Changes made, saving...");
|
||||
await syscall("space.writePage", pageToUpdate, newText);
|
||||
await writePage(pageToUpdate, newText);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,7 +99,7 @@ type BackLink = {
|
||||
};
|
||||
|
||||
async function getBackLinks(pageName: string): Promise<BackLink[]> {
|
||||
let allBackLinks = await syscall("index.scanPrefixGlobal", `pl:${pageName}:`);
|
||||
let allBackLinks = await scanPrefixGlobal(`pl:${pageName}:`);
|
||||
let pagesToUpdate: BackLink[] = [];
|
||||
for (let { key, value } of allBackLinks) {
|
||||
let keyParts = key.split(":");
|
||||
@@ -95,28 +112,28 @@ async function getBackLinks(pageName: string): Promise<BackLink[]> {
|
||||
}
|
||||
|
||||
export async function showBackLinks() {
|
||||
const pageName = await syscall("editor.getCurrentPage");
|
||||
const pageName = await getCurrentPage();
|
||||
let backLinks = await getBackLinks(pageName);
|
||||
|
||||
console.log("Backlinks", backLinks);
|
||||
}
|
||||
|
||||
export async function reindexCommand() {
|
||||
await syscall("editor.flashNotification", "Reindexing...");
|
||||
await syscall("system.invokeFunctionOnServer", "reindexSpace");
|
||||
await syscall("editor.flashNotification", "Reindexing done");
|
||||
await flashNotification("Reindexing...");
|
||||
await invokeFunctionOnServer("reindexSpace");
|
||||
await flashNotification("Reindexing done");
|
||||
}
|
||||
|
||||
// Completion
|
||||
export async function pageComplete() {
|
||||
let prefix = await syscall("editor.matchBefore", "\\[\\[[\\w\\s]*");
|
||||
let prefix = await matchBefore("\\[\\[[\\w\\s]*");
|
||||
if (!prefix) {
|
||||
return null;
|
||||
}
|
||||
let allPages = await syscall("space.listPages");
|
||||
let allPages = await listPages();
|
||||
return {
|
||||
from: prefix.from + 2,
|
||||
options: allPages.map((pageMeta: any) => ({
|
||||
options: allPages.map((pageMeta) => ({
|
||||
label: pageMeta.name,
|
||||
type: "page",
|
||||
})),
|
||||
@@ -126,13 +143,13 @@ export async function pageComplete() {
|
||||
// Server functions
|
||||
export async function reindexSpace() {
|
||||
console.log("Clearing page index...");
|
||||
await syscall("index.clearPageIndex");
|
||||
await clearPageIndexSyscall();
|
||||
console.log("Listing all pages");
|
||||
let pages = await syscall("space.listPages");
|
||||
let pages = await listPages();
|
||||
for (let { name } of pages) {
|
||||
console.log("Indexing", name);
|
||||
const pageObj = await syscall("space.readPage", name);
|
||||
await syscall("event.dispatch", "page:index", {
|
||||
const pageObj = await readPage(name);
|
||||
await dispatch("page:index", {
|
||||
name,
|
||||
text: pageObj.text,
|
||||
});
|
||||
@@ -141,5 +158,5 @@ export async function reindexSpace() {
|
||||
|
||||
export async function clearPageIndex(page: string) {
|
||||
console.log("Clearing page index for page", page);
|
||||
await syscall("index.clearPageIndexForPage", page);
|
||||
await clearPageIndexForPage(page);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { syscall } from "../lib/syscall";
|
||||
|
||||
function countWords(str: string): number {
|
||||
const matches = str.match(/[\w\d\'-]+/gi);
|
||||
return matches ? matches.length : 0;
|
||||
@@ -9,12 +7,3 @@ function readingTime(wordCount: number): number {
|
||||
// 225 is average word reading speed for adults
|
||||
return Math.ceil(wordCount / 225);
|
||||
}
|
||||
|
||||
export async function wordCount({ text }: { text: string }) {
|
||||
let sysCallText = (await syscall("editor.getText")) as string;
|
||||
const count = countWords(sysCallText);
|
||||
console.log("Word count", count);
|
||||
let syntaxNode = await syscall("editor.getSyntaxNodeUnderCursor");
|
||||
console.log("Syntax node", syntaxNode);
|
||||
return count;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user