2018-08-23 18:55:45 +08:00
|
|
|
import FrontendScriptApi from './frontend_script_api.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
import utils from './utils.js';
|
2019-01-13 19:16:05 +08:00
|
|
|
import treeCache from './tree_cache.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2019-10-05 16:55:29 +08:00
|
|
|
async function ScriptContext(startNoteId, allNoteIds, originEntity = null, tabContext = null, $container = null) {
|
2018-03-25 11:45:36 +08:00
|
|
|
const modules = {};
|
|
|
|
|
2019-01-13 19:16:05 +08:00
|
|
|
const startNote = await treeCache.getNote(startNoteId);
|
|
|
|
const allNotes = await treeCache.getNotes(allNoteIds);
|
|
|
|
|
2018-03-25 11:45:36 +08:00
|
|
|
return {
|
|
|
|
modules: modules,
|
|
|
|
notes: utils.toObject(allNotes, note => [note.noteId, note]),
|
2019-10-05 16:55:29 +08:00
|
|
|
apis: utils.toObject(allNotes, note => [note.noteId, new FrontendScriptApi(startNote, note, originEntity, tabContext, $container)]),
|
2018-03-25 11:45:36 +08:00
|
|
|
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;
|