diff --git a/src/public/app/services/frontend_script_api.ts b/src/public/app/services/frontend_script_api.ts index b320518fe..905226d2d 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 { /** *

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. api.showMessage(api.startNote.title);

- * - * @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 | 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 | null): Api +}; diff --git a/src/public/app/services/script_context.js b/src/public/app/services/script_context.ts similarity index 70% rename from src/public/app/services/script_context.js rename to src/public/app/services/script_context.ts index f47380704..821f9aec0 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 | null = null) { + const modules: Record = {}; 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.ts index 4addc1d1f..6e2937beb 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(array: T[], fn: (arg0: T) => [key: string, value: T]) { - const obj: Record = {}; +function toObject(array: T[], fn: (arg0: T) => [key: string, value: R]) { + const obj: Record = {}; for (const item of array) { const [key, value] = fn(item);