This commit is contained in:
Zef Hemel
2022-03-04 12:09:25 +01:00
parent a97bff60d9
commit e94d4d3253
14 changed files with 114 additions and 343 deletions
+7 -5
View File
@@ -23,6 +23,7 @@ import {
import React, { useEffect, useReducer } from "react";
import ReactDOM from "react-dom";
import coreManifest from "./generated/core.plug.json";
// @ts-ignore
window.coreManifest = coreManifest;
import { AppEvent, AppEventDispatcher, ClickEvent } from "./app_event";
@@ -38,7 +39,7 @@ import { IPageNavigator, PathPageNavigator } from "./navigator";
import customMarkDown from "./parser";
import { BrowserSystem } from "./plugbox_browser/browser_system";
import { Plugin } from "../../plugbox/src/runtime";
import { slashCommandRegexp } from "../../plugbox/src/types";
import { slashCommandRegexp } from "./types";
import reducer from "./reducer";
import { smartQuoteKeymap } from "./smart_quotes";
@@ -53,6 +54,7 @@ import {
AppCommand,
AppViewState,
initialViewState,
NuggetHook,
PageMeta,
} from "./types";
import { safeRun } from "./util";
@@ -78,7 +80,7 @@ export class Editor implements AppEventDispatcher {
openPages: Map<string, PageState>;
space: Space;
editorCommands: Map<string, AppCommand>;
plugins: Plugin[];
plugins: Plugin<NuggetHook>[];
indexer: Indexer;
navigationResolve?: (val: undefined) => void;
pageNavigator: IPageNavigator;
@@ -122,7 +124,7 @@ export class Editor implements AppEventDispatcher {
}
async loadPlugins() {
const system = new BrowserSystem("/plugin");
const system = new BrowserSystem<NuggetHook>("/plugin");
system.registerSyscalls(
dbSyscalls,
editorSyscalls(this),
@@ -144,8 +146,8 @@ export class Editor implements AppEventDispatcher {
});
}
private buildCommands(plugin: Plugin) {
const cmds = plugin.manifest!.commands;
private buildCommands(plugin: Plugin<NuggetHook>) {
const cmds = plugin.manifest!.hooks.commands;
for (let name in cmds) {
let cmd = cmds[name];
this.editorCommands.set(name, {
+3 -3
View File
@@ -2,14 +2,14 @@ import { PluginLoader, System } from "../../../plugbox/src/runtime";
import { Manifest } from "../../../plugbox/src/types";
import { sleep } from "../util";
export class BrowserLoader implements PluginLoader {
export class BrowserLoader<HookT> implements PluginLoader<HookT> {
readonly pathPrefix: string;
constructor(pathPrefix: string) {
this.pathPrefix = pathPrefix;
}
async load(name: string, manifest: Manifest): Promise<void> {
async load(name: string, manifest: Manifest<HookT>): Promise<void> {
await fetch(`${this.pathPrefix}/${name}`, {
method: "PUT",
body: JSON.stringify(manifest),
@@ -17,7 +17,7 @@ export class BrowserLoader implements PluginLoader {
}
}
export class BrowserSystem extends System {
export class BrowserSystem<HookT> extends System<HookT> {
constructor(pathPrefix: string) {
super(new BrowserLoader(pathPrefix), pathPrefix);
}
+1 -1
View File
@@ -1,4 +1,4 @@
import { Manifest } from "../../plugbox/src/types";
import { Manifest } from "./types";
import { openDB } from "idb";
+24 -1
View File
@@ -1,4 +1,12 @@
import { CommandDef } from "../../plugbox/src/types";
import * as plugbox from "../../plugbox/src/types";
export type NuggetHook = {
commands: {
[key: string]: CommandDef;
};
};
export type Manifest = plugbox.Manifest<NuggetHook>;
export type PageMeta = {
name: string;
@@ -12,6 +20,21 @@ export type AppCommand = {
run: (arg: any) => Promise<any>;
};
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 AppViewState = {
currentPage?: PageMeta;
isSaved: boolean;