From c0e9684f73f976a395e87593a2ab825c1b5e5203 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 21 Dec 2024 16:43:50 +0200 Subject: [PATCH] chore(client/ts): port services/bundle --- .../app/services/{bundle.js => bundle.ts} | 40 +++++++++++++------ .../app/services/frontend_script_api.ts | 2 +- src/public/app/services/script_context.ts | 4 +- src/public/app/types.d.ts | 2 +- 4 files changed, 31 insertions(+), 17 deletions(-) rename src/public/app/services/{bundle.js => bundle.ts} (72%) diff --git a/src/public/app/services/bundle.js b/src/public/app/services/bundle.ts similarity index 72% rename from src/public/app/services/bundle.js rename to src/public/app/services/bundle.ts index e0a81eee4..e3ba2c21a 100644 --- a/src/public/app/services/bundle.js +++ b/src/public/app/services/bundle.ts @@ -4,9 +4,21 @@ import toastService from "./toast.js"; import froca from "./froca.js"; import utils from "./utils.js"; import { t } from "./i18n.js"; +import { Entity } from "./frontend_script_api.js"; -async function getAndExecuteBundle(noteId, originEntity = null, script = null, params = null) { - const bundle = await server.post(`script/bundle/${noteId}`, { +// TODO: Deduplicate with server. +interface Bundle { + script: string; + noteId: string; + allNoteIds: string[]; +} + +interface Widget { + parentWidget?: string; +} + +async function getAndExecuteBundle(noteId: string, originEntity = null, script = null, params = null) { + const bundle = await server.post(`script/bundle/${noteId}`, { script, params }); @@ -14,24 +26,23 @@ async function getAndExecuteBundle(noteId, originEntity = null, script = null, p return await executeBundle(bundle, originEntity); } -async function executeBundle(bundle, originEntity, $container) { +async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery) { const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container); try { return await (function () { return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`); }.call(apiContext)); - } - catch (e) { + } catch (e: any) { const note = await froca.getNote(bundle.noteId); - toastService.showAndLogError(`Execution of JS note "${note.title}" with ID ${bundle.noteId} failed with error: ${e.message}`); + toastService.showAndLogError(`Execution of JS note "${note?.title}" with ID ${bundle.noteId} failed with error: ${e?.message}`); } } async function executeStartupBundles() { const isMobile = utils.isMobile(); - const scriptBundles = await server.get("script/startup" + (isMobile ? "?mobile=true" : "")); + const scriptBundles = await server.get("script/startup" + (isMobile ? "?mobile=true" : "")); for (const bundle of scriptBundles) { await executeBundle(bundle); @@ -39,11 +50,14 @@ async function executeStartupBundles() { } class WidgetsByParent { + + private byParent: Record; + constructor() { this.byParent = {}; } - add(widget) { + add(widget: Widget) { if (!widget.parentWidget) { console.log(`Custom widget does not have mandatory 'parentWidget' property defined`); return; @@ -53,7 +67,7 @@ class WidgetsByParent { this.byParent[widget.parentWidget].push(widget); } - get(parentName) { + get(parentName: string) { if (!this.byParent[parentName]) { return []; } @@ -62,12 +76,12 @@ 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 => w.prototype ? new w() : w); + .map((w: any) => w.prototype ? new w() : w); } } async function getWidgetBundlesByParent() { - const scriptBundles = await server.get("script/widgets"); + const scriptBundles = await server.get("script/widgets"); const widgetsByParent = new WidgetsByParent(); @@ -80,7 +94,7 @@ async function getWidgetBundlesByParent() { widget._noteId = bundle.noteId; widgetsByParent.add(widget); } - } catch (e) { + } catch (e: any) { const noteId = bundle.noteId; const note = await froca.getNote(noteId); toastService.showPersistent({ @@ -88,7 +102,7 @@ async function getWidgetBundlesByParent() { icon: "alert", message: t("toast.bundle-error.message", { id: noteId, - title: note.title, + title: note?.title, message: e.message }) }); diff --git a/src/public/app/services/frontend_script_api.ts b/src/public/app/services/frontend_script_api.ts index 905226d2d..6a3c61c49 100644 --- a/src/public/app/services/frontend_script_api.ts +++ b/src/public/app/services/frontend_script_api.ts @@ -53,7 +53,7 @@ interface ExecResult { error?: string; } -interface Entity { +export interface Entity { noteId: string; } diff --git a/src/public/app/services/script_context.ts b/src/public/app/services/script_context.ts index 821f9aec0..7d76c12a7 100644 --- a/src/public/app/services/script_context.ts +++ b/src/public/app/services/script_context.ts @@ -1,8 +1,8 @@ -import FrontendScriptApi from './frontend_script_api.js'; +import FrontendScriptApi, { Entity } from './frontend_script_api.js'; import utils from './utils.js'; import froca from './froca.js'; -async function ScriptContext(startNoteId: string, allNoteIds: string[], originEntity = null, $container: JQuery | null = null) { +async function ScriptContext(startNoteId: string, allNoteIds: string[], originEntity: Entity | null = null, $container: JQuery | null = null) { const modules: Record = {}; await froca.initializedPromise; diff --git a/src/public/app/types.d.ts b/src/public/app/types.d.ts index 13c506091..14cc2c19a 100644 --- a/src/public/app/types.d.ts +++ b/src/public/app/types.d.ts @@ -82,7 +82,7 @@ declare global { setNote(noteId: string); } - var logError: (message: string) => void; + var logError: (message: string, e?: Error) => void; var logInfo: (message: string) => void; var glob: CustomGlobals; var require: RequireMethod;