diff --git a/packages/plugos-silverbullet-syscall/system.ts b/packages/plugos-silverbullet-syscall/system.ts index 6072fa68..3b12969a 100644 --- a/packages/plugos-silverbullet-syscall/system.ts +++ b/packages/plugos-silverbullet-syscall/system.ts @@ -8,6 +8,14 @@ export async function invokeFunction( return syscall("system.invokeFunction", env, name, ...args); } +export async function invokeCommand(name: string): Promise { + return syscall("system.invokeCommand", name); +} + +export async function getVersion(): Promise { + return syscall("system.getVersion"); +} + export async function reloadPlugs() { syscall("system.reloadPlugs"); } diff --git a/packages/plugos/system.ts b/packages/plugos/system.ts index 57ea72c0..9de99355 100644 --- a/packages/plugos/system.ts +++ b/packages/plugos/system.ts @@ -135,7 +135,7 @@ export class System extends EventEmitter> { ) { await this.unloadAll(); for (let manifest of json) { - console.log("Loading plug", manifest.name); + // console.log("Loading plug", manifest.name); await this.load(manifest, sandboxFactory); } } diff --git a/packages/plugs/core/core.plug.yaml b/packages/plugs/core/core.plug.yaml index 83ff40ee..6aefa2fb 100644 --- a/packages/plugs/core/core.plug.yaml +++ b/packages/plugs/core/core.plug.yaml @@ -41,6 +41,12 @@ functions: command: name: "Page: Delete" + editorInit: + path: "./editor.ts:editorInit" + env: client + events: + - plugs:loaded + # Backlinks indexLinks: path: "./page.ts:indexLinks" diff --git a/packages/plugs/core/editor.ts b/packages/plugs/core/editor.ts new file mode 100644 index 00000000..8d79a812 --- /dev/null +++ b/packages/plugs/core/editor.ts @@ -0,0 +1,29 @@ +import { get, set } from "@silverbulletmd/plugos-silverbullet-syscall"; +import { flashNotification } from "@silverbulletmd/plugos-silverbullet-syscall/editor"; +import { + getVersion, + invokeFunction, +} from "@silverbulletmd/plugos-silverbullet-syscall/system"; + +export async function editorInit() { + let currentVersion = await getVersion(); + console.log("Running version check", currentVersion); + let lastVersion = await get("index", "$silverBulletVersion"); + console.log("Last version", lastVersion); + if (lastVersion !== currentVersion) { + await flashNotification( + "Version update detected, going to reload plugs..." + ); + await set("index", "$spaceIndexed", false); + await set("index", "$silverBulletVersion", currentVersion); + invokeFunction("client", "updatePlugsCommand"); + } else { + let spaceIndexed = await get("index", "$spaceIndexed"); + console.log("Space indexed", spaceIndexed); + if (!spaceIndexed) { + await invokeFunction("client", "reindexSpaceCommand"); + // Resetting this, because part of the reindex will be to wipe this too + await set("index", "$silverBulletVersion", currentVersion); + } + } +} diff --git a/packages/plugs/core/page.ts b/packages/plugs/core/page.ts index 6b8df181..9e353ade 100644 --- a/packages/plugs/core/page.ts +++ b/packages/plugs/core/page.ts @@ -189,6 +189,7 @@ async function getBackLinks(pageName: string): Promise { export async function reindexCommand() { await flashNotification("Reindexing..."); await invokeFunction("server", "reindexSpace"); + await set("index", "$spaceIndexed", true); await flashNotification("Reindexing done"); } diff --git a/packages/server/express_server.ts b/packages/server/express_server.ts index 512f8f9f..9327c498 100644 --- a/packages/server/express_server.ts +++ b/packages/server/express_server.ts @@ -383,7 +383,8 @@ export class ExpressServer { args ); res.status(200); - res.send(result); + res.header("Content-Type", "application/json"); + res.send(JSON.stringify(result)); } catch (e: any) { res.status(500); return res.send(e.message); @@ -406,7 +407,8 @@ export class ExpressServer { try { const result = await plug.invoke(name, args); res.status(200); - res.send(result); + res.header("Content-Type", "application/json"); + res.send(JSON.stringify(result)); } catch (e: any) { res.status(500); // console.log("Error invoking function", e); diff --git a/packages/web/app_event.ts b/packages/web/app_event.ts index e5709d41..12ed039e 100644 --- a/packages/web/app_event.ts +++ b/packages/web/app_event.ts @@ -1,6 +1,11 @@ import type { ParseTree } from "@silverbulletmd/common/tree"; -export type AppEvent = "page:click" | "page:complete" | "page:load"; +export type AppEvent = + | "page:click" + | "page:complete" + | "page:load" + | "editor:init" + | "plugs:loaded"; export type ClickEvent = { page: string; diff --git a/packages/web/editor.tsx b/packages/web/editor.tsx index 9053e24d..f5fc1c8f 100644 --- a/packages/web/editor.tsx +++ b/packages/web/editor.tsx @@ -234,6 +234,8 @@ export class Editor { await this.pageNavigator.navigate("index"); } await this.reloadPlugs(); + + await this.dispatchAppEvent("editor:init"); } async save(immediate: boolean = false): Promise { @@ -459,11 +461,12 @@ export class Editor { await this.system.unloadAll(); console.log("(Re)loading plugs"); for (let pageInfo of this.space.listPlugs()) { - console.log("Loading plug", pageInfo.name); + // console.log("Loading plug", pageInfo.name); let { text } = await this.space.readPage(pageInfo.name); await this.system.load(JSON.parse(text), createIFrameSandbox); } this.rebuildEditorState(); + await this.dispatchAppEvent("plugs:loaded"); } rebuildEditorState() { @@ -725,6 +728,15 @@ export class Editor { ); } + async runCommandByName(name: string) { + const cmd = this.viewState.commands.get(name); + if (cmd) { + await cmd.run(); + } else { + throw new Error(`Command ${name} not found`); + } + } + render(container: ReactDOM.Container) { const ViewComponent = this.ViewComponent.bind(this); ReactDOM.render(, container); diff --git a/packages/web/syscalls/system.ts b/packages/web/syscalls/system.ts index 5e948fcb..70274add 100644 --- a/packages/web/syscalls/system.ts +++ b/packages/web/syscalls/system.ts @@ -1,5 +1,6 @@ import { SysCallMapping } from "@plugos/plugos/system"; import type { Editor } from "../editor"; +import { version } from "../package.json"; export function systemSyscalls(editor: Editor): SysCallMapping { return { @@ -19,6 +20,12 @@ export function systemSyscalls(editor: Editor): SysCallMapping { return editor.space.invokeFunction(ctx.plug, env, name, args); }, + "system.invokeCommand": async (ctx, name: string) => { + return editor.runCommandByName(name); + }, + "system.getVersion": async () => { + return version; + }, "system.reloadPlugs": async () => { return editor.reloadPlugs(); },