Pre-rename

pull/3/head
Zef Hemel 2022-02-26 12:59:16 +01:00
parent 2986c2c231
commit 3cf84af894
6 changed files with 42 additions and 15 deletions

View File

@ -27,8 +27,13 @@
"key": "Ctrl-2" "key": "Ctrl-2"
} }
}, },
"events": {}, "events": {
"ready": ["welcome"]
},
"functions": { "functions": {
"welcome": {
"path": "./welcome.ts"
},
"word_count_command": { "word_count_command": {
"path": "./word_count_command.ts:wordCount" "path": "./word_count_command.ts:wordCount"
}, },

3
plugins/core/welcome.ts Normal file
View File

@ -0,0 +1,3 @@
export default function welcome() {
console.log("Hello world!");
}

View File

@ -12,7 +12,7 @@ type NuggetMeta = {
}; };
const fsPrefix = "/fs"; const fsPrefix = "/fs";
const nuggetsPath = "../nuggets"; const nuggetsPath = "../pages";
const fsRouter = new Router(); const fsRouter = new Router();

View File

@ -38,10 +38,12 @@ import { Manifest, slashCommandRegexp } from "./plugins/types";
import reducer from "./reducer"; import reducer from "./reducer";
import customMarkdownStyle from "./style"; import customMarkdownStyle from "./style";
import dbSyscalls from "./syscalls/db.localstorage"; import dbSyscalls from "./syscalls/db.localstorage";
import { Plugin } from "./plugins/runtime";
import editorSyscalls from "./syscalls/editor.browser"; import editorSyscalls from "./syscalls/editor.browser";
import { import {
Action, Action,
AppCommand, AppCommand,
AppEvent,
AppViewState, AppViewState,
CommandContext, CommandContext,
initialViewState, initialViewState,
@ -71,10 +73,12 @@ export class Editor {
openNuggets: Map<string, NuggetState>; openNuggets: Map<string, NuggetState>;
fs: FileSystem; fs: FileSystem;
editorCommands: Map<string, AppCommand>; editorCommands: Map<string, AppCommand>;
plugins: Plugin[];
constructor(fs: FileSystem, parent: Element) { constructor(fs: FileSystem, parent: Element) {
this.editorCommands = new Map(); this.editorCommands = new Map();
this.openNuggets = new Map(); this.openNuggets = new Map();
this.plugins = [];
this.fs = fs; this.fs = fs;
this.viewState = initialViewState; this.viewState = initialViewState;
this.viewDispatch = () => {}; this.viewDispatch = () => {};
@ -84,7 +88,7 @@ export class Editor {
parent: document.getElementById("editor")!, parent: document.getElementById("editor")!,
}); });
this.addListeners(); this.addListeners();
this.watch(); // this.watch();
} }
async init() { async init() {
@ -92,6 +96,7 @@ export class Editor {
await this.loadPlugins(); await this.loadPlugins();
this.$hashChange!(); this.$hashChange!();
this.focus(); this.focus();
await this.dispatchAppEvent("ready");
} }
async loadPlugins() { async loadPlugins() {
@ -100,17 +105,11 @@ export class Editor {
await system.bootServiceWorker(); await system.bootServiceWorker();
console.log("Now loading core plugin"); 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<string, AppCommand>(); this.editorCommands = new Map<string, AppCommand>();
const cmds = mainPlugin.manifest!.commands; for (let plugin of this.plugins) {
for (let name in cmds) { this.buildCommands(plugin);
let cmd = cmds[name];
this.editorCommands.set(name, {
command: cmd,
run: async (arg: CommandContext): Promise<any> => {
return await mainPlugin.invoke(cmd.invoke, [arg]);
},
});
} }
this.viewDispatch({ this.viewDispatch({
type: "update-commands", 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<any> => {
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 { get currentNugget(): NuggetMeta | undefined {
return this.viewState.currentNugget; return this.viewState.currentNugget;
} }

View File

@ -9,8 +9,6 @@ function safeRun(fn) {
let func = null; let func = null;
let pendingRequests = {}; let pendingRequests = {};
console.log("hello world!");
self.addEventListener("syscall", (event) => { self.addEventListener("syscall", (event) => {
let customEvent = event; let customEvent = event;
let detail = customEvent.detail; let detail = customEvent.detail;

View File

@ -42,3 +42,5 @@ export type Action =
| { type: "update-commands"; commands: Map<string, AppCommand> } | { type: "update-commands"; commands: Map<string, AppCommand> }
| { type: "show-palette" } | { type: "show-palette" }
| { type: "hide-palette" }; | { type: "hide-palette" };
export type AppEvent = "ready" | "change" | "switch" | "click";