trilium/src/services/script_context.js
2018-08-23 10:10:04 +02:00

22 lines
829 B
JavaScript

const utils = require('./utils');
const BackendScriptApi = require('./backend_script_api');
function ScriptContext(startNote, allNotes, originEntity = null) {
this.modules = {};
this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
this.apis = utils.toObject(allNotes, note => [note.noteId, new BackendScriptApi(startNote, note, originEntity)]);
this.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 this.modules[note.noteId].exports;
}
};
}
module.exports = ScriptContext;