Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
startNote/currentNote is now accessible on frontend as well
azivner61c2456cf628
diff --git a/src/public/javascripts/api.js b/src/public/javascripts/api.jsindex 818f18f015..9d5844232a 100644--- a/src/public/javascripts/api.js+++ b/src/public/javascripts/api.js@@ -1,4 +1,4 @@-function Api() {+function Api(startNote, allNotes) {const $pluginButtons = $("#plugin-buttons");async function activateNote(notePath) {@@ -33,14 +33,19 @@ function Api() {script = script.toString();}- const ret = await server.post('script/exec', { script: script, params: prepareParams(params) });+ const ret = await server.post('script/exec', {+ script: script,+ params: prepareParams(params),+ startNoteId: startNote.noteId+ });return ret.executionResult;}return {+ __startNote: startNote,+ __notes: toObject(allNotes, note => [note.noteId, note]),__modules: {},- __notes: {},addButtonToToolbar,activateNote,getInstanceName: noteTree.getInstanceName,diff --git a/src/public/javascripts/init.js b/src/public/javascripts/init.jsindex dcef0f84d4..a9f445c272 100644--- a/src/public/javascripts/init.js+++ b/src/public/javascripts/init.js@@ -203,7 +203,7 @@ $("#logout-button").toggle(!isElectron());$(document).ready(() => {server.get("script/startup").then(scripts => {for (const script of scripts) {- executeScript(script);+ executeBundle(script);}});});diff --git a/src/public/javascripts/note_editor.js b/src/public/javascripts/note_editor.jsindex 57a93eb2eb..a9abb9d900 100644--- a/src/public/javascripts/note_editor.js+++ b/src/public/javascripts/note_editor.js@@ -298,9 +298,9 @@ const noteEditor = (function() {// make sure note is saved so we load latest changesawait saveNoteIfChanged();- const script = await server.get('script/subtree/' + getCurrentNoteId());+ const bundle = await server.get('script/subtree/' + getCurrentNoteId());- executeScript(script);+ executeBundle(bundle);}}diff --git a/src/public/javascripts/utils.js b/src/public/javascripts/utils.jsindex 63b3a15f7c..254f309f4a 100644--- a/src/public/javascripts/utils.js+++ b/src/public/javascripts/utils.js@@ -115,15 +115,10 @@ async function stopWatch(what, func) {return ret;}-function executeScript(script) {- const completeScript = `-(async function() {- const api = Api();-- ${script}-})();`;+async function executeBundle(bundle) {+ const api = Api(bundle.note, bundle.allNotes);- eval(completeScript);+ return await (function() { return eval(`const api = this; (async function() { ${bundle.script}\r\n})()`); }.call(api));}function formatValueWithWhitespace(val) {@@ -212,4 +207,16 @@ function download(url) {else {window.location.href = url;}+}++function toObject(array, fn) {+ const obj = {};++ for (const item of array) {+ const ret = fn(item);++ obj[ret[0]] = ret[1];+ }++ return obj;}\ No newline at end of filediff --git a/src/routes/api/script.js b/src/routes/api/script.jsindex 488789db12..15344fb3f0 100644--- a/src/routes/api/script.js+++ b/src/routes/api/script.js@@ -9,7 +9,7 @@ const script = require('../../services/script');const Repository = require('../../services/repository');router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => {- const ret = await script.executeScript(req, req.body.script, req.body.params);+ const ret = await script.executeScript(req, req.body.script, req.body.params, req.body.startNoteId);res.send({executionResult: ret@@ -25,7 +25,7 @@ router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => {for (const note of notes) {const bundle = await script.getScriptBundle(note);- scripts.push(bundle.script);+ scripts.push(bundle);}res.send(scripts);@@ -36,14 +36,15 @@ router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) =>const note = await repository.getNote(req.params.noteId);const bundle = await script.getScriptBundle(note);- res.send(bundle.script);+ res.send(bundle);}));router.get('/render/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {const repository = new Repository(req);const note = await repository.getNote(req.params.noteId);+ const bundle = await script.getRenderScript(note);- res.send(await script.getRenderScript(note));+ res.send(bundle);}));module.exports = router;\ No newline at end of filediff --git a/src/services/script.js b/src/services/script.jsindex 415c9454b0..495330dd80 100644--- a/src/services/script.js+++ b/src/services/script.js@@ -1,5 +1,6 @@const sql = require('./sql');const ScriptContext = require('./script_context');+const Repository = require('./repository');async function executeNote(note) {if (note.isProtected || !note.isJavaScript()) {@@ -23,15 +24,18 @@ async function executeNote(note) {}}-async function executeScript(dataKey, script, params) {- const ctx = new ScriptContext(dataKey, null, []);+async function executeScript(dataKey, script, params, startNoteId) {+ const repository = new Repository(dataKey);+ const startNote = await repository.getNote(startNoteId);++ const ctx = new ScriptContext(dataKey, startNote, []);const paramsStr = getParams(params);return await sql.doInTransaction(async () => execute(ctx, script, paramsStr));}async function execute(ctx, script, paramsStr) {- return await (function() { return eval(`const api = this;\r\n(${script})(${paramsStr})`); }.call(ctx));+ return await (function() { return eval(`const api = this;const startNote = api.__startNote;\r\n(${script}\r\n)(${paramsStr})`); }.call(ctx));}function getParams(params) {