2024-12-21 16:43:50 +02:00
|
|
|
import FrontendScriptApi, { Entity } from './frontend_script_api.js';
|
2018-03-25 11:09:17 -04:00
|
|
|
import utils from './utils.js';
|
2021-04-16 23:01:56 +02:00
|
|
|
import froca from './froca.js';
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2024-12-21 16:43:50 +02:00
|
|
|
async function ScriptContext(startNoteId: string, allNoteIds: string[], originEntity: Entity | null = null, $container: JQuery<HTMLElement> | null = null) {
|
2024-12-19 22:16:03 +02:00
|
|
|
const modules: Record<string, { exports: unknown }> = {};
|
2018-03-24 23:45:36 -04:00
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
await froca.initializedPromise;
|
2020-03-26 16:21:17 +01:00
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
const startNote = await froca.getNote(startNoteId);
|
|
|
|
const allNotes = await froca.getNotes(allNoteIds);
|
2019-01-13 12:16:05 +01:00
|
|
|
|
2024-12-19 22:16:03 +02:00
|
|
|
if (!startNote) {
|
|
|
|
throw new Error(`Could not find start note ${startNoteId}.`);
|
|
|
|
}
|
|
|
|
|
2018-03-24 23:45:36 -04:00
|
|
|
return {
|
|
|
|
modules: modules,
|
|
|
|
notes: utils.toObject(allNotes, note => [note.noteId, note]),
|
2020-02-25 19:19:10 +01:00
|
|
|
apis: utils.toObject(allNotes, note => [note.noteId, new FrontendScriptApi(startNote, note, originEntity, $container)]),
|
2024-12-19 22:16:03 +02:00
|
|
|
require: (moduleNoteIds: string) => {
|
|
|
|
return (moduleName: string) => {
|
2018-03-24 23:45:36 -04:00
|
|
|
const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
|
|
|
|
const note = candidates.find(c => c.title === moduleName);
|
|
|
|
|
|
|
|
if (!note) {
|
2022-12-21 15:19:05 +01:00
|
|
|
throw new Error(`Could not find module note ${moduleName}`);
|
2018-03-24 23:45:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return modules[note.noteId].exports;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ScriptContext;
|