Code widget refactor

This commit is contained in:
Zef Hemel
2023-10-31 10:33:38 +01:00
parent 4ef857acf4
commit d8318c4ad7
13 changed files with 64 additions and 32 deletions
+1 -1
View File
@@ -403,7 +403,7 @@ export class Client {
this.eventHook.addLocalListener("file:listed", (fileList: FileMeta[]) => {
this.ui.viewDispatch({
type: "pages-listed",
pages: fileList.filter((f) => f.name.endsWith(".md")).map(
pages: fileList.filter(this.space.isListedPage).map(
fileMetaToPageMeta,
),
});
+2 -2
View File
@@ -36,7 +36,7 @@ import { DataStore } from "../plugos/lib/datastore.ts";
import { MessageQueue } from "../plugos/lib/mq.ts";
import { languageSyscalls } from "../common/syscalls/language.ts";
import { handlebarsSyscalls } from "../common/syscalls/handlebars.ts";
import { widgetSyscalls } from "./syscalls/widget.ts";
import { codeWidgetSyscalls } from "./syscalls/code_widget.ts";
export class ClientSystem {
commandHook: CommandHook;
@@ -143,7 +143,7 @@ export class ClientSystem {
assetSyscalls(this.system),
yamlSyscalls(),
handlebarsSyscalls(),
widgetSyscalls(this.client),
codeWidgetSyscalls(this.codeWidgetHook),
languageSyscalls(),
this.client.syncMode
// In sync mode handle locally
+13 -11
View File
@@ -27,7 +27,7 @@ class IFrameWidget extends WidgetType {
const iframe = createWidgetSandboxIFrame(
this.editor,
this.bodyText,
this.codeWidgetCallback(this.bodyText),
this.codeWidgetCallback(this.bodyText, this.editor.currentPage!),
(message) => {
switch (message.type) {
case "blur":
@@ -38,16 +38,18 @@ class IFrameWidget extends WidgetType {
break;
case "reload":
this.codeWidgetCallback(this.bodyText).then(
(widgetContent: WidgetContent) => {
iframe.contentWindow!.postMessage({
type: "html",
html: widgetContent.html,
script: widgetContent.script,
theme: document.getElementsByTagName("html")[0].dataset.theme,
});
},
);
this.codeWidgetCallback(this.bodyText, this.editor.currentPage!)
.then(
(widgetContent: WidgetContent) => {
iframe.contentWindow!.postMessage({
type: "html",
html: widgetContent.html,
script: widgetContent.script,
theme:
document.getElementsByTagName("html")[0].dataset.theme,
});
},
);
break;
}
},
+6 -3
View File
@@ -23,9 +23,12 @@ export class CodeWidgetHook implements Hook<CodeWidgetT> {
if (!functionDef.codeWidget) {
continue;
}
this.codeWidgetCallbacks.set(functionDef.codeWidget, (bodyText) => {
return plug.invoke(name, [bodyText]);
});
this.codeWidgetCallbacks.set(
functionDef.codeWidget,
(bodyText, pageName) => {
return plug.invoke(name, [bodyText, pageName]);
},
);
}
}
}
@@ -1,23 +1,24 @@
import { CodeWidgetContent } from "$sb/types.ts";
import { SysCallMapping } from "../../plugos/system.ts";
import { Client } from "../client.ts";
import { CodeWidgetHook } from "../hooks/code_widget.ts";
export function widgetSyscalls(
client: Client,
export function codeWidgetSyscalls(
codeWidgetHook: CodeWidgetHook,
): SysCallMapping {
return {
"widget.render": (
"codeWidget.render": (
_ctx,
lang: string,
body: string,
pageName: string,
): Promise<CodeWidgetContent> => {
const langCallback = client.system.codeWidgetHook.codeWidgetCallbacks.get(
const langCallback = codeWidgetHook.codeWidgetCallbacks.get(
lang,
);
if (!langCallback) {
throw new Error(`Code widget ${lang} not found`);
}
return langCallback(body);
return langCallback(body, pageName);
},
};
}