mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	startNote/currentNote is now accessible on frontend as well
This commit is contained in:
		| @@ -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, | ||||
|   | ||||
| @@ -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); | ||||
|         } | ||||
|     }); | ||||
| }); | ||||
|   | ||||
| @@ -298,9 +298,9 @@ const noteEditor = (function() { | ||||
|             // make sure note is saved so we load latest changes | ||||
|             await saveNoteIfChanged(); | ||||
|  | ||||
|             const script = await server.get('script/subtree/' + getCurrentNoteId()); | ||||
|             const bundle = await server.get('script/subtree/' + getCurrentNoteId()); | ||||
|  | ||||
|             executeScript(script); | ||||
|             executeBundle(bundle); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -115,15 +115,10 @@ async function stopWatch(what, func) { | ||||
|     return ret; | ||||
| } | ||||
|  | ||||
| function executeScript(script) { | ||||
|     const completeScript = ` | ||||
| (async function() { | ||||
|     const api = Api(); | ||||
| async function executeBundle(bundle) { | ||||
|     const api = Api(bundle.note, bundle.allNotes); | ||||
|  | ||||
|     ${script} | ||||
| })();`; | ||||
|  | ||||
|     eval(completeScript); | ||||
|     return await (function() { return eval(`const api = this; (async function() { ${bundle.script}\r\n})()`); }.call(api)); | ||||
| } | ||||
|  | ||||
| function formatValueWithWhitespace(val) { | ||||
| @@ -213,3 +208,15 @@ function download(url) { | ||||
|         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; | ||||
| } | ||||
| @@ -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; | ||||
| @@ -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) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user