Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(client/ts): port services/script_context
Elian Doran7c2002c589fb
diff --git a/src/public/app/services/frontend_script_api.ts b/src/public/app/services/frontend_script_api.tsindex b320518fee..905226d2d1 100644--- a/src/public/app/services/frontend_script_api.ts+++ b/src/public/app/services/frontend_script_api.ts@@ -445,10 +445,8 @@ interface Api {/*** <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>- *- * @constructor*/-function FrontendScriptApi (this: Api, startNote: FNote, currentNote: FNote, originEntity: Entity | null = null, $container = null) {+function FrontendScriptApi(this: Api, startNote: FNote, currentNote: FNote, originEntity: Entity | null = null, $container: JQuery<HTMLElement> | null = null) {this.$container = $container;this.startNote = startNote;@@ -671,4 +669,6 @@ function FrontendScriptApi (this: Api, startNote: FNote, currentNote: FNote, ori};}-export default FrontendScriptApi;+export default FrontendScriptApi as any as {+ new (startNote: FNote, currentNote: FNote, originEntity: Entity | null, $container: JQuery<HTMLElement> | null): Api+};diff --git a/src/public/app/services/script_context.js b/src/public/app/services/script_context.tssimilarity index 68%rename from src/public/app/services/script_context.jsrename to src/public/app/services/script_context.tsindex f473807044..821f9aec04 100644--- a/src/public/app/services/script_context.js+++ b/src/public/app/services/script_context.ts@@ -2,20 +2,24 @@ import FrontendScriptApi from './frontend_script_api.js';import utils from './utils.js';import froca from './froca.js';-async function ScriptContext(startNoteId, allNoteIds, originEntity = null, $container = null) {- const modules = {};+async function ScriptContext(startNoteId: string, allNoteIds: string[], originEntity = null, $container: JQuery<HTMLElement> | null = null) {+ const modules: Record<string, { exports: unknown }> = {};await froca.initializedPromise;const startNote = await froca.getNote(startNoteId);const allNotes = await froca.getNotes(allNoteIds);+ if (!startNote) {+ throw new Error(`Could not find start note ${startNoteId}.`);+ }+return {modules: modules,notes: utils.toObject(allNotes, note => [note.noteId, note]),apis: utils.toObject(allNotes, note => [note.noteId, new FrontendScriptApi(startNote, note, originEntity, $container)]),- require: moduleNoteIds => {- return moduleName => {+ require: (moduleNoteIds: string) => {+ return (moduleName: string) => {const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));const note = candidates.find(c => c.title === moduleName);diff --git a/src/public/app/services/utils.ts b/src/public/app/services/utils.tsindex 4addc1d1f6..6e2937beb9 100644--- a/src/public/app/services/utils.ts+++ b/src/public/app/services/utils.ts@@ -138,8 +138,8 @@ function formatSize(size: number) {}}-function toObject<T>(array: T[], fn: (arg0: T) => [key: string, value: T]) {- const obj: Record<string, T> = {};+function toObject<T, R>(array: T[], fn: (arg0: T) => [key: string, value: R]) {+ const obj: Record<string, R> = {};for (const item of array) {const [key, value] = fn(item);