mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	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:
		| @@ -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 () { | ||||
|   | ||||
| @@ -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]), | ||||
|   | ||||
| @@ -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 = { | ||||
|   | ||||
| @@ -117,7 +117,6 @@ module.exports = { | ||||
|     getEntity, | ||||
|     getNote, | ||||
|     getBranch, | ||||
|     getImage, | ||||
|     getAttribute, | ||||
|     getOption, | ||||
|     updateEntity, | ||||
|   | ||||
| @@ -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 | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user