frontend scripts now have startNote, currentNote and targetNote as NoteShort entities which e.g. provides easy access to relations/labels

This commit is contained in:
azivner 2019-01-13 12:16:05 +01:00
parent f7f0560a9f
commit 9268f88bc3
5 changed files with 23 additions and 7 deletions

View file

@ -9,7 +9,7 @@ async function getAndExecuteBundle(noteId, originEntity = null) {
}
async function executeBundle(bundle, originEntity) {
const apiContext = ScriptContext(bundle.note, bundle.allNotes, originEntity);
const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity);
try {
return await (function () {

View file

@ -1,9 +1,13 @@
import FrontendScriptApi from './frontend_script_api.js';
import utils from './utils.js';
import treeCache from './tree_cache.js';
function ScriptContext(startNote, allNotes, originEntity = null) {
async function ScriptContext(startNoteId, allNoteIds, originEntity = null) {
const modules = {};
const startNote = await treeCache.getNote(startNoteId);
const allNotes = await treeCache.getNotes(allNoteIds);
return {
modules: modules,
notes: utils.toObject(allNotes, note => [note.noteId, note]),

View file

@ -30,7 +30,7 @@ async function getStartupBundles() {
const bundles = [];
for (const note of notes) {
const bundle = await scriptService.getScriptBundle(note);
const bundle = await scriptService.getScriptBundleForFrontend(note);
if (bundle) {
bundles.push(bundle);
@ -54,7 +54,7 @@ async function getRelationBundles(req) {
for (const noteId of uniqueNoteIds) {
const note = await repository.getNote(noteId);
const bundle = await scriptService.getScriptBundle(note);
const bundle = await scriptService.getScriptBundleForFrontend(note);
if (bundle) {
bundles.push(bundle);
@ -67,7 +67,7 @@ async function getRelationBundles(req) {
async function getBundle(req) {
const note = await repository.getNote(req.params.noteId);
return await scriptService.getScriptBundle(note);
return await scriptService.getScriptBundleForFrontend(note);
}
module.exports = {

View file

@ -117,7 +117,6 @@ module.exports = {
getEntity,
getNote,
getBranch,
getImage,
getAttribute,
getOption,
updateEntity,

View file

@ -79,6 +79,19 @@ function getParams(params) {
}).join(",");
}
async function getScriptBundleForFrontend(note) {
const bundle = await getScriptBundle(note);
// for frontend we return just noteIds because frontend needs to use its own entity instances
bundle.noteId = bundle.note.noteId;
delete bundle.note;
bundle.allNoteIds = bundle.allNotes.map(note => note.noteId);
delete bundle.allNotes;
return bundle;
}
async function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = []) {
if (!note.isContentAvailable) {
return;
@ -156,5 +169,5 @@ function sanitizeVariableName(str) {
module.exports = {
executeNote,
executeScript,
getScriptBundle
getScriptBundleForFrontend
};