Skip to content

Build your personal knowledge base with Trilium Notes

chore(script/jsx): get widgets to be interpreted

Elian Doranfe8f0334095e
diff --git a/apps/client/src/services/bundle.ts b/apps/client/src/services/bundle.ts
index 146ed47201..926f217bc1 100644
--- a/apps/client/src/services/bundle.ts
+++ b/apps/client/src/services/bundle.ts
@@ -70,11 +70,12 @@ export class WidgetsByParent {
}
add(widget: Widget) {
- if ("parent" in widget) {
+ if ("type" in widget && widget.type === "react-widget") {
// React-based script.
- this.byParent[widget.parent] = this.byParent[widget.parent] || [];
- this.byParent[widget.parent].push(widget);
- } else if (widget.parentWidget) {
+ const reactWidget = widget as WidgetDefinition;
+ this.byParent[reactWidget.parent] = this.byParent[reactWidget.parent] || [];
+ this.byParent[reactWidget.parent].push(widget);
+ } else if ("parentWidget" in widget && widget.parentWidget) {
this.byParent[widget.parentWidget] = this.byParent[widget.parentWidget] || [];
this.byParent[widget.parentWidget].push(widget);
} else {
@@ -92,7 +93,13 @@ export class WidgetsByParent {
// previously, custom widgets were provided as a single instance, but that has the disadvantage
// for splits where we actually need multiple instaces and thus having a class to instantiate is better
// https://github.com/zadam/trilium/issues/4274
- .map((w: any) => (w.prototype ? new w() : w))
+ .map((w: any) => {
+ if ("type" in w && w.type === "react-widget") {
+ return w.render();
+ }
+
+ return (w.prototype ? new w() : w);
+ })
);
}
}
diff --git a/apps/client/src/services/frontend_script_api.ts b/apps/client/src/services/frontend_script_api.ts
index f8dee4ab32..ac11d43e02 100644
--- a/apps/client/src/services/frontend_script_api.ts
+++ b/apps/client/src/services/frontend_script_api.ts
@@ -480,6 +480,10 @@ export interface WidgetDefinition {
render: () => VNode
}
+export interface WidgetDefinitionWithType extends WidgetDefinition {
+ type: "react-widget"
+}
+
/**
* <p>This is the main frontend API interface for scripts. All the properties and methods are published in the "api" object
* available in the JS frontend notes. You can use e.g. <code>api.showMessage(api.startNote.title);</code></p>
@@ -736,7 +740,10 @@ function FrontendScriptApi(this: Api, startNote: FNote, currentNote: FNote, orig
this.logSpacedUpdates[noteId].scheduleUpdate();
};
this.defineWidget = (definition) => {
- return definition;
+ return {
+ type: "react-widget",
+ ...definition
+ };
};
}