2023-07-14 14:48:35 +00:00
|
|
|
import { PlugNamespaceHook } from "../common/hooks/plug_namespace.ts";
|
2023-07-14 11:44:30 +00:00
|
|
|
import { Manifest, SilverBulletHooks } from "../common/manifest.ts";
|
|
|
|
import buildMarkdown from "../common/markdown_parser/parser.ts";
|
|
|
|
import { CronHook } from "../plugos/hooks/cron.ts";
|
|
|
|
import { EventHook } from "../plugos/hooks/event.ts";
|
|
|
|
import { DexieKVStore } from "../plugos/lib/kv_store.dexie.ts";
|
|
|
|
import { createSandbox } from "../plugos/environments/webworker_sandbox.ts";
|
|
|
|
|
|
|
|
import assetSyscalls from "../plugos/syscalls/asset.ts";
|
|
|
|
import { eventSyscalls } from "../plugos/syscalls/event.ts";
|
2023-08-04 16:56:55 +00:00
|
|
|
import { storeSyscalls } from "../plugos/syscalls/store.ts";
|
2023-07-14 11:44:30 +00:00
|
|
|
import { SysCallMapping, System } from "../plugos/system.ts";
|
2023-07-14 14:56:20 +00:00
|
|
|
import type { Client } from "./client.ts";
|
2023-07-14 11:44:30 +00:00
|
|
|
import { CodeWidgetHook } from "./hooks/code_widget.ts";
|
|
|
|
import { CommandHook } from "./hooks/command.ts";
|
|
|
|
import { SlashCommandHook } from "./hooks/slash_command.ts";
|
|
|
|
import { clientStoreSyscalls } from "./syscalls/clientStore.ts";
|
|
|
|
import { debugSyscalls } from "./syscalls/debug.ts";
|
|
|
|
import { editorSyscalls } from "./syscalls/editor.ts";
|
|
|
|
import { sandboxFetchSyscalls } from "./syscalls/fetch.ts";
|
|
|
|
import { pageIndexSyscalls } from "./syscalls/index.ts";
|
|
|
|
import { markdownSyscalls } from "./syscalls/markdown.ts";
|
|
|
|
import { shellSyscalls } from "./syscalls/shell.ts";
|
|
|
|
import { spaceSyscalls } from "./syscalls/space.ts";
|
|
|
|
import { syncSyscalls } from "./syscalls/sync.ts";
|
|
|
|
import { systemSyscalls } from "./syscalls/system.ts";
|
|
|
|
import { yamlSyscalls } from "./syscalls/yaml.ts";
|
|
|
|
import { Space } from "./space.ts";
|
|
|
|
import {
|
|
|
|
loadMarkdownExtensions,
|
|
|
|
MDExt,
|
|
|
|
} from "../common/markdown_parser/markdown_ext.ts";
|
2023-08-10 16:32:41 +00:00
|
|
|
import { DexieMQ } from "../plugos/lib/mq.dexie.ts";
|
|
|
|
import { MQHook } from "../plugos/hooks/mq.ts";
|
|
|
|
import { mqSyscalls } from "../plugos/syscalls/mq.dexie.ts";
|
2023-07-14 11:44:30 +00:00
|
|
|
|
|
|
|
export class ClientSystem {
|
|
|
|
system: System<SilverBulletHooks> = new System("client");
|
|
|
|
commandHook: CommandHook;
|
|
|
|
slashCommandHook: SlashCommandHook;
|
2023-07-14 14:48:35 +00:00
|
|
|
namespaceHook: PlugNamespaceHook;
|
2023-07-14 11:44:30 +00:00
|
|
|
indexSyscalls: SysCallMapping;
|
|
|
|
codeWidgetHook: CodeWidgetHook;
|
|
|
|
plugsUpdated = false;
|
|
|
|
mdExtensions: MDExt[] = [];
|
|
|
|
|
|
|
|
constructor(
|
2023-07-14 14:56:20 +00:00
|
|
|
private editor: Client,
|
2023-07-14 11:44:30 +00:00
|
|
|
private kvStore: DexieKVStore,
|
2023-08-10 16:32:41 +00:00
|
|
|
private mq: DexieMQ,
|
2023-07-14 11:44:30 +00:00
|
|
|
private dbPrefix: string,
|
|
|
|
private eventHook: EventHook,
|
|
|
|
) {
|
|
|
|
this.system.addHook(this.eventHook);
|
|
|
|
|
2023-07-14 14:48:35 +00:00
|
|
|
// Plug page namespace hook
|
|
|
|
this.namespaceHook = new PlugNamespaceHook();
|
|
|
|
this.system.addHook(this.namespaceHook);
|
2023-07-14 11:44:30 +00:00
|
|
|
|
|
|
|
// Cron hook
|
|
|
|
const cronHook = new CronHook(this.system);
|
|
|
|
this.system.addHook(cronHook);
|
|
|
|
|
|
|
|
this.indexSyscalls = pageIndexSyscalls(
|
|
|
|
`${dbPrefix}_page_index`,
|
|
|
|
globalThis.indexedDB,
|
2023-08-11 18:37:13 +00:00
|
|
|
globalThis.IDBKeyRange,
|
2023-07-14 11:44:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Code widget hook
|
|
|
|
this.codeWidgetHook = new CodeWidgetHook();
|
|
|
|
this.system.addHook(this.codeWidgetHook);
|
|
|
|
|
2023-08-10 16:32:41 +00:00
|
|
|
// MQ hook
|
|
|
|
this.system.addHook(new MQHook(this.system, this.mq));
|
|
|
|
|
2023-07-14 11:44:30 +00:00
|
|
|
// Command hook
|
|
|
|
this.commandHook = new CommandHook();
|
|
|
|
this.commandHook.on({
|
|
|
|
commandsUpdated: (commandMap) => {
|
2023-07-14 12:22:26 +00:00
|
|
|
this.editor.ui.viewDispatch({
|
2023-07-14 11:44:30 +00:00
|
|
|
type: "update-commands",
|
|
|
|
commands: commandMap,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
this.system.addHook(this.commandHook);
|
|
|
|
|
|
|
|
// Slash command hook
|
|
|
|
this.slashCommandHook = new SlashCommandHook(this.editor);
|
|
|
|
this.system.addHook(this.slashCommandHook);
|
|
|
|
|
|
|
|
this.eventHook.addLocalListener("plug:changed", async (fileName) => {
|
|
|
|
console.log("Plug updated, reloading:", fileName);
|
|
|
|
this.system.unload(fileName);
|
|
|
|
const plug = await this.system.load(
|
|
|
|
new URL(`/${fileName}`, location.href),
|
|
|
|
createSandbox,
|
|
|
|
);
|
|
|
|
if ((plug.manifest! as Manifest).syntax) {
|
|
|
|
// If there are syntax extensions, rebuild the markdown parser immediately
|
|
|
|
this.updateMarkdownParser();
|
|
|
|
}
|
|
|
|
this.plugsUpdated = true;
|
|
|
|
});
|
|
|
|
this.registerSyscalls();
|
|
|
|
}
|
|
|
|
|
|
|
|
registerSyscalls() {
|
|
|
|
const storeCalls = storeSyscalls(this.kvStore);
|
|
|
|
|
|
|
|
// Slash command hook
|
|
|
|
this.slashCommandHook = new SlashCommandHook(this.editor);
|
|
|
|
this.system.addHook(this.slashCommandHook);
|
|
|
|
|
|
|
|
// Syscalls available to all plugs
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
[],
|
|
|
|
eventSyscalls(this.eventHook),
|
|
|
|
editorSyscalls(this.editor),
|
|
|
|
spaceSyscalls(this.editor),
|
|
|
|
systemSyscalls(this.editor, this.system),
|
|
|
|
markdownSyscalls(buildMarkdown(this.mdExtensions)),
|
|
|
|
assetSyscalls(this.system),
|
|
|
|
yamlSyscalls(),
|
2023-08-10 16:32:41 +00:00
|
|
|
mqSyscalls(this.mq),
|
2023-07-14 11:44:30 +00:00
|
|
|
storeCalls,
|
|
|
|
this.indexSyscalls,
|
|
|
|
debugSyscalls(),
|
|
|
|
syncSyscalls(this.editor),
|
|
|
|
// LEGACY
|
|
|
|
clientStoreSyscalls(storeCalls),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Syscalls that require some additional permissions
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
["fetch"],
|
2023-07-24 07:36:33 +00:00
|
|
|
sandboxFetchSyscalls(this.editor),
|
2023-07-14 11:44:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
["shell"],
|
2023-07-24 07:36:33 +00:00
|
|
|
shellSyscalls(this.editor),
|
2023-07-14 11:44:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async reloadPlugsFromSpace(space: Space) {
|
|
|
|
console.log("Loading plugs");
|
|
|
|
await space.updatePageList();
|
|
|
|
await this.system.unloadAll();
|
|
|
|
console.log("(Re)loading plugs");
|
|
|
|
await Promise.all((await space.listPlugs()).map(async (plugName) => {
|
|
|
|
try {
|
|
|
|
await this.system.load(
|
|
|
|
new URL(plugName, location.origin),
|
|
|
|
createSandbox,
|
|
|
|
);
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error("Could not load plug", plugName, "error:", e.message);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMarkdownParser() {
|
|
|
|
// Load all syntax extensions
|
|
|
|
this.mdExtensions = loadMarkdownExtensions(this.system);
|
|
|
|
// And reload the syscalls to use the new syntax extensions
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
[],
|
|
|
|
markdownSyscalls(buildMarkdown(this.mdExtensions)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
localSyscall(name: string, args: any[]) {
|
|
|
|
return this.system.localSyscall("[local]", name, args);
|
|
|
|
}
|
|
|
|
}
|