From 3cf84af89494cbd24721464c686716b97abe8cb9 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sat, 26 Feb 2022 12:59:16 +0100 Subject: [PATCH] Pre-rename --- plugins/core/core.plugin.json | 7 +++++- plugins/core/welcome.ts | 3 +++ server/server.ts | 2 +- webapp/src/editor.tsx | 41 +++++++++++++++++++++++++---------- webapp/src/function_worker.js | 2 -- webapp/src/types.ts | 2 ++ 6 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 plugins/core/welcome.ts diff --git a/plugins/core/core.plugin.json b/plugins/core/core.plugin.json index 9be62ec0..020b435c 100644 --- a/plugins/core/core.plugin.json +++ b/plugins/core/core.plugin.json @@ -27,8 +27,13 @@ "key": "Ctrl-2" } }, - "events": {}, + "events": { + "ready": ["welcome"] + }, "functions": { + "welcome": { + "path": "./welcome.ts" + }, "word_count_command": { "path": "./word_count_command.ts:wordCount" }, diff --git a/plugins/core/welcome.ts b/plugins/core/welcome.ts new file mode 100644 index 00000000..6ce38116 --- /dev/null +++ b/plugins/core/welcome.ts @@ -0,0 +1,3 @@ +export default function welcome() { + console.log("Hello world!"); +} diff --git a/server/server.ts b/server/server.ts index a1d24ac6..dcc9827e 100644 --- a/server/server.ts +++ b/server/server.ts @@ -12,7 +12,7 @@ type NuggetMeta = { }; const fsPrefix = "/fs"; -const nuggetsPath = "../nuggets"; +const nuggetsPath = "../pages"; const fsRouter = new Router(); diff --git a/webapp/src/editor.tsx b/webapp/src/editor.tsx index 6dde192a..244b225f 100644 --- a/webapp/src/editor.tsx +++ b/webapp/src/editor.tsx @@ -38,10 +38,12 @@ import { Manifest, slashCommandRegexp } from "./plugins/types"; import reducer from "./reducer"; import customMarkdownStyle from "./style"; import dbSyscalls from "./syscalls/db.localstorage"; +import { Plugin } from "./plugins/runtime"; import editorSyscalls from "./syscalls/editor.browser"; import { Action, AppCommand, + AppEvent, AppViewState, CommandContext, initialViewState, @@ -71,10 +73,12 @@ export class Editor { openNuggets: Map; fs: FileSystem; editorCommands: Map; + plugins: Plugin[]; constructor(fs: FileSystem, parent: Element) { this.editorCommands = new Map(); this.openNuggets = new Map(); + this.plugins = []; this.fs = fs; this.viewState = initialViewState; this.viewDispatch = () => {}; @@ -84,7 +88,7 @@ export class Editor { parent: document.getElementById("editor")!, }); this.addListeners(); - this.watch(); + // this.watch(); } async init() { @@ -92,6 +96,7 @@ export class Editor { await this.loadPlugins(); this.$hashChange!(); this.focus(); + await this.dispatchAppEvent("ready"); } async loadPlugins() { @@ -100,17 +105,11 @@ export class Editor { await system.bootServiceWorker(); console.log("Now loading core plugin"); - let mainPlugin = await system.load("core", coreManifest as Manifest); + let mainPlugin = await system.load("core", coreManifest); + this.plugins.push(mainPlugin); this.editorCommands = new Map(); - const cmds = mainPlugin.manifest!.commands; - for (let name in cmds) { - let cmd = cmds[name]; - this.editorCommands.set(name, { - command: cmd, - run: async (arg: CommandContext): Promise => { - return await mainPlugin.invoke(cmd.invoke, [arg]); - }, - }); + for (let plugin of this.plugins) { + this.buildCommands(plugin); } this.viewDispatch({ type: "update-commands", @@ -118,6 +117,26 @@ export class Editor { }); } + private buildCommands(plugin: Plugin) { + const cmds = plugin.manifest!.commands; + for (let name in cmds) { + let cmd = cmds[name]; + this.editorCommands.set(name, { + command: cmd, + run: async (arg: CommandContext): Promise => { + return await plugin.invoke(cmd.invoke, [arg]); + }, + }); + } + } + + // TODO: Parallelize? + async dispatchAppEvent(name: AppEvent, data?: any) { + for (let plugin of this.plugins) { + await plugin.dispatchEvent(name, data); + } + } + get currentNugget(): NuggetMeta | undefined { return this.viewState.currentNugget; } diff --git a/webapp/src/function_worker.js b/webapp/src/function_worker.js index c1b099af..cec71317 100644 --- a/webapp/src/function_worker.js +++ b/webapp/src/function_worker.js @@ -9,8 +9,6 @@ function safeRun(fn) { let func = null; let pendingRequests = {}; -console.log("hello world!"); - self.addEventListener("syscall", (event) => { let customEvent = event; let detail = customEvent.detail; diff --git a/webapp/src/types.ts b/webapp/src/types.ts index 38c4b364..cadc5ab1 100644 --- a/webapp/src/types.ts +++ b/webapp/src/types.ts @@ -42,3 +42,5 @@ export type Action = | { type: "update-commands"; commands: Map } | { type: "show-palette" } | { type: "hide-palette" }; + +export type AppEvent = "ready" | "change" | "switch" | "click";