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) { async function executeBundle(bundle, originEntity) {
const apiContext = ScriptContext(bundle.note, bundle.allNotes, originEntity); const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity);
try { try {
return await (function () { return await (function () {

View File

@ -1,9 +1,13 @@
import FrontendScriptApi from './frontend_script_api.js'; import FrontendScriptApi from './frontend_script_api.js';
import utils from './utils.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 modules = {};
const startNote = await treeCache.getNote(startNoteId);
const allNotes = await treeCache.getNotes(allNoteIds);
return { return {
modules: modules, modules: modules,
notes: utils.toObject(allNotes, note => [note.noteId, note]), notes: utils.toObject(allNotes, note => [note.noteId, note]),

View File

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

View File

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

View File

@ -79,6 +79,19 @@ function getParams(params) {
}).join(","); }).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 = []) { async function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = []) {
if (!note.isContentAvailable) { if (!note.isContentAvailable) {
return; return;
@ -156,5 +169,5 @@ function sanitizeVariableName(str) {
module.exports = { module.exports = {
executeNote, executeNote,
executeScript, executeScript,
getScriptBundle getScriptBundleForFrontend
}; };