60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import { Hook, Manifest } from "../../plugos/types.ts";
|
|
import { System } from "../../plugos/system.ts";
|
|
import { CodeWidgetCallback } from "$sb/types.ts";
|
|
|
|
export type PanelWidgetT = {
|
|
panelWidget?: "top" | "bottom";
|
|
};
|
|
|
|
export class PanelWidgetHook implements Hook<PanelWidgetT> {
|
|
callbacks = new Map<string, CodeWidgetCallback>();
|
|
|
|
constructor() {
|
|
}
|
|
|
|
collectAllPanelWidgets(system: System<PanelWidgetT>) {
|
|
this.callbacks.clear();
|
|
for (const plug of system.loadedPlugs.values()) {
|
|
for (
|
|
const [name, functionDef] of Object.entries(
|
|
plug.manifest!.functions,
|
|
)
|
|
) {
|
|
if (!functionDef.panelWidget) {
|
|
continue;
|
|
}
|
|
this.callbacks.set(
|
|
functionDef.panelWidget,
|
|
(bodyText, pageName) => {
|
|
return plug.invoke(name, [bodyText, pageName]);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
apply(system: System<PanelWidgetT>): void {
|
|
this.collectAllPanelWidgets(system);
|
|
system.on({
|
|
plugLoaded: () => {
|
|
this.collectAllPanelWidgets(system);
|
|
},
|
|
});
|
|
}
|
|
|
|
validateManifest(manifest: Manifest<PanelWidgetT>): string[] {
|
|
const errors = [];
|
|
for (const functionDef of Object.values(manifest.functions)) {
|
|
if (!functionDef.panelWidget) {
|
|
continue;
|
|
}
|
|
if (!["top", "bottom"].includes(functionDef.panelWidget)) {
|
|
errors.push(
|
|
`Panel widgets must be attached to either 'top' or 'bottom'.`,
|
|
);
|
|
}
|
|
}
|
|
return errors;
|
|
}
|
|
}
|