Cleanup and progress

This commit is contained in:
Zef Hemel
2022-03-21 15:21:34 +01:00
parent 7e591c6f44
commit a916088215
31 changed files with 707 additions and 143 deletions
+1 -1
View File
@@ -17,5 +17,5 @@ window.editor = editor;
navigator.serviceWorker
.register(new URL("service_worker.ts", import.meta.url), { type: "module" })
.then((r) => {
console.log("Service worker registered", r);
// console.log("Service worker registered", r);
});
+1 -1
View File
@@ -17,7 +17,7 @@ import {
} from "@codemirror/view";
import { throttle } from "./util";
import { Cursor, cursorEffect } from "./cursorEffect";
import { EventEmitter } from "./event";
import { EventEmitter } from "../common/event";
const throttleInterval = 250;
+46 -34
View File
@@ -19,7 +19,6 @@ import {
KeyBinding,
keymap,
} from "@codemirror/view";
// import { debounce } from "lodash";
import React, { useEffect, useReducer } from "react";
import ReactDOM from "react-dom";
import { Plug, System } from "../plugbox/runtime";
@@ -31,7 +30,6 @@ import { CommandPalette } from "./components/command_palette";
import { PageNavigator } from "./components/page_navigator";
import { TopBar } from "./components/top_bar";
import { Cursor } from "./cursorEffect";
import coreManifest from "./generated/core.plug.json";
import { lineWrapper } from "./line_wrapper";
import { markdown } from "./markdown";
import { IPageNavigator, PathPageNavigator } from "./navigator";
@@ -49,9 +47,9 @@ import {
AppCommand,
AppViewState,
initialViewState,
NuggetHook,
slashCommandRegexp,
} from "./types";
import { SilverBulletHooks } from "../common/manifest";
import { safeRun } from "./util";
class PageState {
@@ -65,20 +63,19 @@ class PageState {
}
export class Editor implements AppEventDispatcher {
private system = new System<SilverBulletHooks>();
editorView?: EditorView;
viewState: AppViewState;
viewDispatch: React.Dispatch<Action>;
openPages: Map<string, PageState>;
space: Space;
editorCommands: Map<string, AppCommand>;
plugs: Plug<NuggetHook>[];
navigationResolve?: (val: undefined) => void;
pageNavigator: IPageNavigator;
constructor(space: Space, parent: Element) {
this.editorCommands = new Map();
this.openPages = new Map();
this.plugs = [];
this.space = space;
this.viewState = initialViewState;
this.viewDispatch = () => {};
@@ -91,6 +88,13 @@ export class Editor implements AppEventDispatcher {
parent: document.getElementById("editor")!,
});
this.pageNavigator = new PathPageNavigator();
this.system.registerSyscalls(
dbSyscalls,
editorSyscalls(this),
spaceSyscalls(this),
indexerSyscalls(this.space)
);
}
async init() {
@@ -128,6 +132,39 @@ export class Editor implements AppEventDispatcher {
pages: pages,
});
},
loadSystem: (systemJSON) => {
safeRun(async () => {
console.log("Received SYSTEM", systemJSON);
await this.system.replaceAllFromJSON(systemJSON, () =>
createIFrameSandbox(this.system)
);
console.log("Loaded plugs, now updating editor comands");
this.editorCommands = new Map<string, AppCommand>();
for (let plug of this.system.loadedPlugs.values()) {
this.buildCommands(plug);
}
this.viewDispatch({
type: "update-commands",
commands: this.editorCommands,
});
});
},
plugUpdated: (plugName, plug) => {
safeRun(async () => {
console.log("Plug updated", plugName);
await this.system.load(
plugName,
plug,
createIFrameSandbox(this.system)
);
});
},
plugRemoved: (plugName) => {
safeRun(async () => {
console.log("Plug removed", plugName);
await this.system.unload(plugName);
});
},
});
if (this.pageNavigator.getCurrentPage() === "") {
@@ -154,32 +191,16 @@ export class Editor implements AppEventDispatcher {
}
async loadPlugs() {
const system = new System<NuggetHook>();
const system = new System<SilverBulletHooks>();
system.registerSyscalls(
dbSyscalls,
editorSyscalls(this),
spaceSyscalls(this),
indexerSyscalls(this.space)
);
console.log("Now loading core plug");
let mainPlug = await system.load(
"core",
coreManifest,
createIFrameSandbox(system)
);
this.plugs.push(mainPlug);
this.editorCommands = new Map<string, AppCommand>();
for (let plug of this.plugs) {
this.buildCommands(plug);
}
this.viewDispatch({
type: "update-commands",
commands: this.editorCommands,
});
}
private buildCommands(plug: Plug<NuggetHook>) {
private buildCommands(plug: Plug<SilverBulletHooks>) {
const cmds = plug.manifest!.hooks.commands;
for (let name in cmds) {
let cmd = cmds[name];
@@ -192,18 +213,8 @@ export class Editor implements AppEventDispatcher {
}
}
// TODO: Parallelize?
async dispatchAppEvent(name: AppEvent, data?: any): Promise<any[]> {
let results: any[] = [];
for (let plug of this.plugs) {
let plugResults = await plug.dispatchEvent(name, data);
if (plugResults) {
for (let result of plugResults) {
results.push(result);
}
}
}
return results;
return this.system.dispatchEvent(name, data);
}
get currentPage(): string | undefined {
@@ -338,6 +349,7 @@ export class Editor implements AppEventDispatcher {
async plugCompleter(): Promise<CompletionResult | null> {
let allCompletionResults = await this.dispatchAppEvent("editor:complete");
console.log("Completion results", allCompletionResults);
if (allCompletionResults.length === 1) {
return allCompletionResults[0];
} else if (allCompletionResults.length > 1) {
-20
View File
@@ -1,20 +0,0 @@
export abstract class EventEmitter<HandlerT> {
private handlers: Partial<HandlerT>[] = [];
on(handlers: Partial<HandlerT>) {
this.handlers.push(handlers);
}
off(handlers: Partial<HandlerT>) {
this.handlers = this.handlers.filter((h) => h !== handlers);
}
emit(eventName: keyof HandlerT, ...args: any[]) {
for (let handler of this.handlers) {
let fn: any = handler[eventName];
if (fn) {
fn(...args);
}
}
}
}
+10 -2
View File
@@ -5,7 +5,9 @@ import { ChangeSet, Text, Transaction } from "@codemirror/state";
import { CollabDocument, CollabEvents } from "./collab";
import { cursorEffect } from "./cursorEffect";
import { EventEmitter } from "./event";
import { EventEmitter } from "../common/event";
import { SystemJSON } from "../plugbox/runtime";
import { Manifest } from "../common/manifest";
export type SpaceEvents = {
connect: () => void;
@@ -13,6 +15,9 @@ export type SpaceEvents = {
pageChanged: (meta: PageMeta) => void;
pageDeleted: (name: string) => void;
pageListUpdated: (pages: Set<PageMeta>) => void;
loadSystem: (systemJSON: SystemJSON<any>) => void;
plugUpdated: (plugName: string, plug: Manifest) => void;
plugRemoved: (plugName: string) => void;
} & CollabEvents;
export type KV = {
@@ -35,6 +40,9 @@ export class Space extends EventEmitter<SpaceEvents> {
"pageCreated",
"pageChanged",
"pageDeleted",
"loadSystem",
"plugUpdated",
"plugRemoved",
].forEach((eventName) => {
socket.on(eventName, (...args) => {
this.emit(eventName as keyof SpaceEvents, ...args);
@@ -54,7 +62,7 @@ export class Space extends EventEmitter<SpaceEvents> {
break;
}
}
if(!found) {
if (!found) {
this.allPages.add(meta);
console.log("New page created", meta);
this.emit("pageListUpdated", this.allPages);
+1 -22
View File
@@ -1,12 +1,4 @@
import * as plugbox from "../plugbox/types";
export type NuggetHook = {
commands: {
[key: string]: CommandDef;
};
};
export type Manifest = plugbox.Manifest<NuggetHook>;
import { CommandDef } from "../common/manifest";
export type PageMeta = {
name: string;
@@ -22,19 +14,6 @@ export type AppCommand = {
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 type Notification = {
id: number;
message: string;