1
0
silverbullet/web/hooks/code_widget.ts

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-12-22 15:20:05 +00:00
import { Hook, Manifest } from "../../plugos/types.ts";
import { System } from "../../plugos/system.ts";
2023-10-29 09:02:50 +00:00
import { CodeWidgetCallback } from "$sb/types.ts";
2022-12-22 15:20:05 +00:00
export type CodeWidgetT = {
codeWidget?: string;
};
export class CodeWidgetHook implements Hook<CodeWidgetT> {
codeWidgetCallbacks = new Map<string, CodeWidgetCallback>();
constructor() {
}
collectAllCodeWidgets(system: System<CodeWidgetT>) {
this.codeWidgetCallbacks.clear();
for (const plug of system.loadedPlugs.values()) {
for (
const [name, functionDef] of Object.entries(
plug.manifest!.functions,
)
) {
if (!functionDef.codeWidget) {
continue;
}
this.codeWidgetCallbacks.set(functionDef.codeWidget, (bodyText) => {
return plug.invoke(name, [bodyText]);
});
}
}
}
apply(system: System<CodeWidgetT>): void {
this.collectAllCodeWidgets(system);
system.on({
plugLoaded: () => {
this.collectAllCodeWidgets(system);
},
});
}
validateManifest(manifest: Manifest<CodeWidgetT>): string[] {
const errors = [];
for (const functionDef of Object.values(manifest.functions)) {
if (!functionDef.codeWidget) {
continue;
}
if (typeof functionDef.codeWidget !== "string") {
errors.push(`Codewidgets require a string name.`);
}
}
return errors;
}
}