trilium/src/public/javascripts/services/bundle.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-03-26 09:29:35 +08:00
import ScriptContext from "./script_context.js";
import server from "./server.js";
2018-08-10 02:08:00 +08:00
async function getAndExecuteBundle(noteId, workEntity = null) {
const bundle = await server.get('script/bundle/' + noteId);
2018-08-10 02:08:00 +08:00
await executeBundle(bundle, workEntity);
}
2018-08-10 02:08:00 +08:00
async function executeBundle(bundle, workEntity) {
const apiContext = ScriptContext(bundle.note, bundle.allNotes, workEntity);
return await (function () {
return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);
}.call(apiContext));
}
async function executeStartupBundles() {
const scriptBundles = await server.get("script/startup");
for (const bundle of scriptBundles) {
await executeBundle(bundle);
}
}
async function executeRelationBundles(note, relationName) {
const bundlesToRun = await server.get("script/relation/" + note.noteId + "/" + relationName);
for (const bundle of bundlesToRun) {
await executeBundle(bundle, note);
}
}
export default {
executeBundle,
getAndExecuteBundle,
executeStartupBundles,
executeRelationBundles
}