chore(client/ts): port services/script_context

This commit is contained in:
Elian Doran 2024-12-19 22:16:03 +02:00
parent 650a116193
commit 7c2002c589
No known key found for this signature in database
3 changed files with 14 additions and 10 deletions

View File

@ -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
};

View File

@ -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);

View File

@ -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);