Skip to content

Build your personal knowledge base with Trilium Notes

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

azivner9268f88bc33a
diff --git a/src/public/javascripts/services/bundle.js b/src/public/javascripts/services/bundle.js
index 60a9fe992e..2f6f6bd871 100644
--- a/src/public/javascripts/services/bundle.js
+++ b/src/public/javascripts/services/bundle.js
@@ -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 () {
diff --git a/src/public/javascripts/services/script_context.js b/src/public/javascripts/services/script_context.js
index 2cea8c9cfd..e6ebbd8113 100644
--- a/src/public/javascripts/services/script_context.js
+++ b/src/public/javascripts/services/script_context.js
@@ -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]),
diff --git a/src/routes/api/script.js b/src/routes/api/script.js
index 1479b24b9e..87927e612b 100644
--- a/src/routes/api/script.js
+++ b/src/routes/api/script.js
@@ -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 = {
diff --git a/src/services/repository.js b/src/services/repository.js
index 44f8dbe060..2bb660c617 100644
--- a/src/services/repository.js
+++ b/src/services/repository.js
@@ -117,7 +117,6 @@ module.exports = {
getEntity,
getNote,
getBranch,
- getImage,
getAttribute,
getOption,
updateEntity,
diff --git a/src/services/script.js b/src/services/script.js
index 7e2209d907..2c3a7855ef 100644
--- a/src/services/script.js
+++ b/src/services/script.js
@@ -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
};
\ No newline at end of file