2018-03-25 23:09:17 +08:00
|
|
|
import ScriptApi from './script_api.js';
|
|
|
|
import utils from './utils.js';
|
|
|
|
|
2018-03-25 11:45:36 +08:00
|
|
|
function ScriptContext(startNote, allNotes) {
|
|
|
|
const modules = {};
|
|
|
|
|
|
|
|
return {
|
|
|
|
modules: modules,
|
|
|
|
notes: utils.toObject(allNotes, note => [note.noteId, note]),
|
|
|
|
apis: utils.toObject(allNotes, note => [note.noteId, ScriptApi(startNote, note)]),
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ScriptContext;
|