Plugbox cleanup

This commit is contained in:
Zef Hemel
2022-03-04 11:21:11 +01:00
parent 24ceaea9d5
commit a97bff60d9
26 changed files with 1266 additions and 365 deletions
-78
View File
@@ -1,78 +0,0 @@
{
"commands": {
"Navigate To page": {
"invoke": "linkNavigate",
"key": "Ctrl-Enter",
"mac": "Cmd-Enter"
},
"Insert Current Date": {
"invoke": "insertToday",
"slashCommand": "/today"
},
"Toggle : Heading 1": {
"invoke": "toggle_h1",
"mac": "Cmd-1",
"key": "Ctrl-1"
},
"Toggle : Heading 2": {
"invoke": "toggle_h2",
"mac": "Cmd-2",
"key": "Ctrl-2"
},
"Page: Delete": {
"invoke": "deletePage"
},
"Page: Rename": {
"invoke": "renamePage"
},
"Pages: Reindex": {
"invoke": "reindexPages"
},
"Pages: Back Links": {
"invoke": "showBackLinks"
}
},
"events": {
"page:click": ["taskToggle", "clickNavigate"],
"editor:complete": ["pageComplete"],
"page:index": ["indexLinks"]
},
"functions": {
"indexLinks": {
"path": "./page.ts:indexLinks"
},
"deletePage": {
"path": "./page.ts:deletePage"
},
"showBackLinks": {
"path": "./page.ts:showBackLinks"
},
"renamePage": {
"path": "./page.ts:renamePage"
},
"reindexPages": {
"path": "./page.ts:reindex"
},
"pageComplete": {
"path": "./navigate.ts:pageComplete"
},
"linkNavigate": {
"path": "./navigate.ts:linkNavigate"
},
"clickNavigate": {
"path": "./navigate.ts:clickNavigate"
},
"taskToggle": {
"path": "./task.ts:taskToggle"
},
"insertToday": {
"path": "./dates.ts:insertToday"
},
"toggle_h1": {
"path": "./markup.ts:toggleH1"
},
"toggle_h2": {
"path": "./markup.ts:toggleH2"
}
}
}
-6
View File
@@ -1,6 +0,0 @@
import { syscall } from "./lib/syscall.ts";
export async function insertToday() {
let niceDate = new Date().toISOString().split("T")[0];
await syscall("editor.insertAtCursor", niceDate);
}
-9
View File
@@ -1,9 +0,0 @@
import {syscall} from "./syscall.ts";
export async function put(key: string, value: any) {
return await syscall("db.put", key, value);
}
export async function get(key: string) {
return await syscall("db.get", key);
}
-16
View File
@@ -1,16 +0,0 @@
export function syscall(name: string, ...args: any[]): any {
let reqId = Math.floor(Math.random() * 1000000);
// console.log("Syscall", name, reqId);
return new Promise((resolve, reject) => {
self.dispatchEvent(
new CustomEvent("syscall", {
detail: {
id: reqId,
name: name,
args: args,
callback: resolve,
},
})
);
});
}
-33
View File
@@ -1,33 +0,0 @@
import { syscall } from "./lib/syscall.ts";
export async function toggleH1() {
await togglePrefix("# ");
}
export async function toggleH2() {
await togglePrefix("## ");
}
function lookBack(s: string, pos: number, backString: string): boolean {
return s.substring(pos - backString.length, pos) === backString;
}
async function togglePrefix(prefix: string) {
let text = (await syscall("editor.getText")) as string;
let pos = (await syscall("editor.getCursor")) as number;
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, "");
return;
}
pos--;
}
if (pos) {
pos++;
}
await syscall("editor.insertAtPos", prefix, pos);
}
-49
View File
@@ -1,49 +0,0 @@
import { ClickEvent } from "../../webapp/src/app_event.ts";
import { syscall } from "./lib/syscall.ts";
async function navigate(syntaxNode: any) {
if (!syntaxNode) {
return;
}
console.log("Attempting to navigate based on syntax node", syntaxNode);
switch (syntaxNode.name) {
case "WikiLinkPage":
await syscall("editor.navigate", syntaxNode.text);
break;
case "URL":
await syscall("editor.openUrl", syntaxNode.text);
break;
case "Link":
// Markdown link: [bla](URLHERE) needs extraction
let match = /\[[^\\]+\]\(([^\)]+)\)/.exec(syntaxNode.text);
if (match) {
await syscall("editor.openUrl", match[1]);
}
}
}
export async function linkNavigate() {
navigate(await syscall("editor.getSyntaxNodeUnderCursor"));
}
export async function clickNavigate(event: ClickEvent) {
if (event.ctrlKey || event.metaKey) {
let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos);
navigate(syntaxNode);
}
}
export async function pageComplete() {
let prefix = await syscall("editor.matchBefore", "\\[\\[[\\w\\s]*");
if (!prefix) {
return null;
}
let allPages = await syscall("space.listPages");
return {
from: prefix.from + 2,
options: allPages.map((pageMeta: any) => ({
label: pageMeta.name,
type: "page",
})),
};
}
-108
View File
@@ -1,108 +0,0 @@
import { IndexEvent } from "../../webapp/src/app_event";
import { pageLinkRegex } from "../../webapp/src/constant";
import { syscall } from "./lib/syscall";
const wikilinkRegex = new RegExp(pageLinkRegex, "g");
export async function indexLinks({ name, text }: IndexEvent) {
let backLinks: { key: string; value: string }[] = [];
for (let match of text.matchAll(wikilinkRegex)) {
let toPage = match[1];
let pos = match.index!;
backLinks.push({
key: `pl:${toPage}:${pos}`,
value: name,
});
}
console.log("Found", backLinks.length, "wiki link(s)");
await syscall("indexer.batchSet", name, backLinks);
}
export async function deletePage() {
let pageMeta = await syscall("editor.getCurrentPage");
console.log("Navigating to start page");
await syscall("editor.navigate", "start");
console.log("Deleting page from space");
await syscall("space.deletePage", pageMeta.name);
console.log("Reloading page list");
await syscall("space.reloadPageList");
}
export async function renamePage() {
const pageMeta = await syscall("editor.getCurrentPage");
const oldName = pageMeta.name;
console.log("Old name is", oldName);
const newName = await syscall(
"editor.prompt",
`Rename ${oldName} to:`,
oldName
);
if (!newName) {
return;
}
console.log("New name", newName);
let pagesToUpdate = await getBackLinks(oldName);
console.log("All pages containing backlinks", pagesToUpdate);
let text = await syscall("editor.getText");
console.log("Writing new page to space");
await syscall("space.writePage", newName, text);
console.log("Deleting page from space");
await syscall("space.deletePage", oldName);
console.log("Reloading page list");
await syscall("space.reloadPageList");
console.log("Navigating to new page");
await syscall("editor.navigate", newName);
let pageToUpdateSet = new Set<string>();
for (let pageToUpdate of pagesToUpdate) {
pageToUpdateSet.add(pageToUpdate.page);
}
for (let pageToUpdate of pageToUpdateSet) {
console.log("Now going to update links in", pageToUpdate);
let { text } = await syscall("space.readPage", pageToUpdate);
if (!text) {
// Page likely does not exist, but at least we can skip it
continue;
}
let newText = text.replaceAll(`[[${oldName}]]`, `[[${newName}]]`);
if (text !== newText) {
console.log("Changes made, saving...");
await syscall("space.writePage", pageToUpdate, newText);
}
}
}
type BackLink = {
page: string;
pos: number;
};
async function getBackLinks(pageName: string): Promise<BackLink[]> {
let allBackLinks = await syscall(
"indexer.scanPrefixGlobal",
`pl:${pageName}:`
);
let pagesToUpdate: BackLink[] = [];
for (let { key, value } of allBackLinks) {
let keyParts = key.split(":");
pagesToUpdate.push({
page: value,
pos: +keyParts[keyParts.length - 1],
});
}
return pagesToUpdate;
}
export async function showBackLinks() {
const pageMeta = await syscall("editor.getCurrentPage");
let backLinks = await getBackLinks(pageMeta.name);
console.log("Backlinks", backLinks);
}
export async function reindex() {
await syscall("space.reindex");
}
-31
View File
@@ -1,31 +0,0 @@
import { ClickEvent } from "../../webapp/src/app_event.ts";
import { syscall } from "./lib/syscall.ts";
export async function taskToggle(event: ClickEvent) {
let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos);
if (syntaxNode && syntaxNode.name === "TaskMarker") {
if (syntaxNode.text === "[x]" || syntaxNode.text === "[X]") {
await syscall("editor.dispatch", {
changes: {
from: syntaxNode.from,
to: syntaxNode.to,
insert: "[ ]",
},
selection: {
anchor: event.pos,
},
});
} else {
await syscall("editor.dispatch", {
changes: {
from: syntaxNode.from,
to: syntaxNode.to,
insert: "[x]",
},
selection: {
anchor: event.pos,
},
});
}
}
}
-3
View File
@@ -1,3 +0,0 @@
export default function welcome() {
console.log("Hello world!");
}
-20
View File
@@ -1,20 +0,0 @@
function countWords(str: string): number {
var matches = str.match(/[\w\d\'\'-]+/gi);
return matches ? matches.length : 0;
}
function readingTime(wordCount: number): number {
// 225 is average word reading speed for adults
return Math.ceil(wordCount / 225);
}
import { syscall } from "./lib/syscall.ts";
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;
}
+2 -1
View File
@@ -6,10 +6,11 @@
"main": "dist/bundle.js",
"scripts": {
"build": "parcel build",
"core": "node dist/bundle.js --debug core/core.plugin.json ../webapp/src/generated/core.plugin.json"
"check": "tsc --noEmit"
},
"dependencies": {
"esbuild": "^0.14.24",
"idb": "^7.0.0",
"typescript": ">=3.0.0",
"vm2": "^3.9.9",
"yargs": "^17.3.1"
+3 -3
View File
@@ -6,7 +6,7 @@ import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { Manifest } from "../../webapp/src/plugins/types";
async function compile(filePath: string, sourceMap: string) {
async function compile(filePath: string, sourceMap: boolean) {
let tempFile = "out.js";
let js = await esbuild.build({
entryPoints: [filePath],
@@ -25,7 +25,7 @@ async function compile(filePath: string, sourceMap: string) {
return jsCode;
}
async function bundle(manifestPath, sourceMaps) {
async function bundle(manifestPath: string, sourceMaps: boolean) {
const rootPath = path.dirname(manifestPath);
const manifest = JSON.parse(
(await readFile(manifestPath)).toString()
@@ -53,7 +53,7 @@ async function run() {
})
.parse();
let generatedManifest = await bundle(args._[0], !!args.debug);
let generatedManifest = await bundle(args._[0] as string, !!args.debug);
writeFile(args._[1] as string, JSON.stringify(generatedManifest, null, 2));
}
+184
View File
@@ -0,0 +1,184 @@
import { Manifest } from "./types";
interface SysCallMapping {
// TODO: Better typing
[key: string]: any;
}
export class FunctionWorker {
private worker: Worker;
private inited: Promise<any>;
private initCallback: any;
private invokeResolve?: (result?: any) => void;
private invokeReject?: (reason?: any) => void;
private plugin: Plugin;
constructor(plugin: Plugin, pathPrefix: string, name: string) {
let worker = window.Worker;
this.worker = new worker("/function_worker.js");
// console.log("Starting worker", this.worker);
this.worker.onmessage = this.onmessage.bind(this);
this.worker.postMessage({
type: "boot",
prefix: pathPrefix,
name: name,
// @ts-ignore
userAgent: navigator.userAgent,
});
this.inited = new Promise((resolve) => {
this.initCallback = resolve;
});
this.plugin = plugin;
}
async onmessage(evt: MessageEvent) {
let data = evt.data;
if (!data) return;
switch (data.type) {
case "inited":
this.initCallback();
break;
case "syscall":
let result = await this.plugin.system.syscall(data.name, data.args);
this.worker.postMessage({
type: "syscall-response",
id: data.id,
data: result,
});
break;
case "result":
this.invokeResolve!(data.result);
break;
case "error":
this.invokeReject!(data.reason);
break;
default:
console.error("Unknown message type", data);
}
}
async invoke(args: Array<any>): Promise<any> {
await this.inited;
this.worker.postMessage({
type: "invoke",
args: args,
});
return new Promise((resolve, reject) => {
this.invokeResolve = resolve;
this.invokeReject = reject;
});
}
stop() {
this.worker.terminate();
}
}
export interface PluginLoader {
load(name: string, manifest: Manifest): Promise<void>;
}
export class Plugin {
pathPrefix: string;
system: System;
private runningFunctions: Map<string, FunctionWorker>;
public manifest?: Manifest;
private name: string;
constructor(system: System, pathPrefix: string, name: string) {
this.name = name;
this.pathPrefix = `${pathPrefix}/${name}`;
this.system = system;
this.runningFunctions = new Map<string, FunctionWorker>();
}
async load(manifest: Manifest) {
this.manifest = manifest;
await this.system.pluginLoader.load(this.name, manifest);
await this.dispatchEvent("load");
}
async invoke(name: string, args: Array<any>): Promise<any> {
if (!this.runningFunctions.has(name)) {
this.runningFunctions.set(
name,
new FunctionWorker(this, this.pathPrefix, name)
);
}
return await this.runningFunctions.get(name)!.invoke(args);
}
async dispatchEvent(name: string, data?: any): Promise<any[]> {
let functionsToSpawn = this.manifest!.events[name];
if (functionsToSpawn) {
return await Promise.all(
functionsToSpawn.map(
async (functionToSpawn: string) =>
await this.invoke(functionToSpawn, [data])
)
);
} else {
return [];
}
}
async stop() {
for (const [functionname, worker] of Object.entries(
this.runningFunctions
)) {
console.log(`Stopping ${functionname}`);
worker.stop();
}
this.runningFunctions = new Map<string, FunctionWorker>();
}
}
export class System {
protected plugins: Map<string, Plugin>;
protected pathPrefix: string;
registeredSyscalls: SysCallMapping;
pluginLoader: PluginLoader;
constructor(PluginLoader: PluginLoader, pathPrefix: string) {
this.pluginLoader = PluginLoader;
this.pathPrefix = pathPrefix;
this.plugins = new Map<string, Plugin>();
this.registeredSyscalls = {};
}
registerSyscalls(...registrationObjects: Array<SysCallMapping>) {
for (const registrationObject of registrationObjects) {
for (let p in registrationObject) {
this.registeredSyscalls[p] = registrationObject[p];
}
}
}
async syscall(name: string, args: Array<any>): Promise<any> {
const callback = this.registeredSyscalls[name];
if (!name) {
throw Error(`Unregistered syscall ${name}`);
}
if (!callback) {
throw Error(`Registered but not implemented syscall ${name}`);
}
return Promise.resolve(callback(...args));
}
async load(name: string, manifest: Manifest): Promise<Plugin> {
const plugin = new Plugin(this, this.pathPrefix, name);
await plugin.load(manifest);
this.plugins.set(name, plugin);
return plugin;
}
async stop(): Promise<void[]> {
return Promise.all(
Array.from(this.plugins.values()).map((plugin) => plugin.stop())
);
}
}
console.log("Starting");
+30
View File
@@ -0,0 +1,30 @@
export interface Manifest {
events: { [key: string]: string[] };
commands: {
[key: string]: CommandDef;
};
functions: {
[key: string]: FunctionDef;
};
}
export const slashCommandRegexp = /\/[\w\-]*/;
export interface CommandDef {
// Function name to invoke
invoke: string;
// Bind to keyboard shortcut
key?: string;
mac?: string;
// If to show in slash invoked menu and if so, with what label
// should match slashCommandRegexp
slashCommand?: string;
}
export interface FunctionDef {
path: string;
functionName?: string;
code?: string;
}
+27
View File
@@ -0,0 +1,27 @@
export function countWords(str: string): number {
var matches = str.match(/[\w\d\'\'-]+/gi);
return matches ? matches.length : 0;
}
export function readingTime(wordCount: number): number {
// 225 is average word reading speed for adults
return Math.ceil(wordCount / 225);
}
export function safeRun(fn: () => Promise<void>) {
fn().catch((e) => {
console.error(e);
});
}
export function sleep(ms: number): Promise<void> {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
}
export function isMacLike() {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
}
+11
View File
@@ -0,0 +1,11 @@
{
"include": ["src/**/*", "../webapp/src/plugbox_browser/browser_system.ts"],
"compilerOptions": {
"target": "esnext",
"strict": true,
"moduleResolution": "node",
"module": "ESNext",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
}
}
+220 -215
View File
File diff suppressed because it is too large Load Diff