Notes/src/public/app/services/script_context.js

32 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-08-23 12:55:45 +02:00
import FrontendScriptApi from './frontend_script_api.js';
import utils from './utils.js';
2021-04-16 23:01:56 +02:00
import froca from './froca.js';
2020-02-25 19:19:10 +01:00
async function ScriptContext(startNoteId, allNoteIds, originEntity = null, $container = null) {
const modules = {};
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);
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)]),
require: moduleNoteIds => {
return moduleName => {
const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
const note = candidates.find(c => c.title === moduleName);
if (!note) {
throw new Error("Could not find module note " + moduleName);
}
return modules[note.noteId].exports;
}
}
};
}
export default ScriptContext;