mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	#125, implementation of inheritable relations
This commit is contained in:
		| @@ -23,8 +23,17 @@ async function executeStartupBundles() { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | async function executeRelationBundles(note, relationName) { | ||||||
|  |     const bundlesToRun = await server.get("script/relation/" + note.noteId + "/" + relationName); | ||||||
|  |  | ||||||
|  |     for (const bundle of bundlesToRun) { | ||||||
|  |         await executeBundle(bundle, note); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|     executeBundle, |     executeBundle, | ||||||
|     getAndExecuteBundle, |     getAndExecuteBundle, | ||||||
|     executeStartupBundles |     executeStartupBundles, | ||||||
|  |     executeRelationBundles | ||||||
| } | } | ||||||
| @@ -190,12 +190,9 @@ async function loadNoteDetail(noteId) { | |||||||
|     const hideChildrenOverview = labels.some(label => label.name === 'hideChildrenOverview'); |     const hideChildrenOverview = labels.some(label => label.name === 'hideChildrenOverview'); | ||||||
|     await showChildrenOverview(hideChildrenOverview); |     await showChildrenOverview(hideChildrenOverview); | ||||||
|  |  | ||||||
|     const relations = await loadRelationList(); |     await loadRelationList(); | ||||||
|     const relationsToRun = relations.filter(relation => relation.name === 'runOnNoteView'); |  | ||||||
|  |  | ||||||
|     for (const relationToRun of relationsToRun) { |     await bundleService.executeRelationBundles(getCurrentNote(), 'runOnNoteView'); | ||||||
|         await bundleService.getAndExecuteBundle(relationToRun.targetNoteId, getCurrentNote()); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| async function showChildrenOverview(hideChildrenOverview) { | async function showChildrenOverview(hideChildrenOverview) { | ||||||
|   | |||||||
| @@ -75,9 +75,7 @@ async function executeCurrentNote() { | |||||||
|     const currentNote = noteDetailService.getCurrentNote(); |     const currentNote = noteDetailService.getCurrentNote(); | ||||||
|  |  | ||||||
|     if (currentNote.mime.endsWith("env=frontend")) { |     if (currentNote.mime.endsWith("env=frontend")) { | ||||||
|         const bundle = await server.get('script/bundle/' + noteDetailService.getCurrentNoteId()); |         await bundleService.getAndExecuteBundle(noteDetailService.getCurrentNoteId()); | ||||||
|  |  | ||||||
|         bundleService.executeBundle(bundle); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (currentNote.mime.endsWith("env=backend")) { |     if (currentNote.mime.endsWith("env=backend")) { | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ function setupTooltip() { | |||||||
|                 notePath = $(this).attr("note-path"); |                 notePath = $(this).attr("note-path"); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             if (notePath !== null) { |             if (notePath) { | ||||||
|                 const noteId = treeUtils.getNoteIdFromNotePath(notePath); |                 const noteId = treeUtils.getNoteIdFromNotePath(notePath); | ||||||
|  |  | ||||||
|                 noteDetailService.loadNote(noteId).then(note => callback(note.content)); |                 noteDetailService.loadNote(noteId).then(note => callback(note.content)); | ||||||
|   | |||||||
| @@ -2,6 +2,7 @@ | |||||||
|  |  | ||||||
| const labelService = require('../../services/labels'); | const labelService = require('../../services/labels'); | ||||||
| const scriptService = require('../../services/script'); | const scriptService = require('../../services/script'); | ||||||
|  | const relationService = require('../../services/relations'); | ||||||
| const repository = require('../../services/repository'); | const repository = require('../../services/repository'); | ||||||
|  |  | ||||||
| async function exec(req) { | async function exec(req) { | ||||||
| @@ -35,14 +36,32 @@ async function getStartupBundles() { | |||||||
|     return bundles; |     return bundles; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | async function getRelationBundles(req) { | ||||||
|  |     const noteId = req.params.noteId; | ||||||
|  |     const relationName = req.params.relationName; | ||||||
|  |  | ||||||
|  |     const relations = await relationService.getEffectiveRelations(noteId); | ||||||
|  |     const filtered = relations.filter(relation => relation.name === relationName); | ||||||
|  |     const targetNoteIds = filtered.map(relation => relation.targetNoteId); | ||||||
|  |     const uniqueNoteIds = Array.from(new Set(targetNoteIds)); | ||||||
|  |  | ||||||
|  |     const bundles = []; | ||||||
|  |  | ||||||
|  |     for (const noteId of uniqueNoteIds) { | ||||||
|  |         bundles.push(await scriptService.getScriptBundleForNoteId(noteId)); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return bundles; | ||||||
|  | } | ||||||
|  |  | ||||||
| async function getBundle(req) { | async function getBundle(req) { | ||||||
|     const note = await repository.getNote(req.params.noteId); |     return await scriptService.getScriptBundleForNoteId(req.params.noteId); | ||||||
|     return await scriptService.getScriptBundle(note); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     exec, |     exec, | ||||||
|     run, |     run, | ||||||
|     getStartupBundles, |     getStartupBundles, | ||||||
|  |     getRelationBundles, | ||||||
|     getBundle |     getBundle | ||||||
| }; | }; | ||||||
| @@ -185,6 +185,7 @@ function register(app) { | |||||||
|     apiRoute(POST, '/api/script/run/:noteId', scriptRoute.run); |     apiRoute(POST, '/api/script/run/:noteId', scriptRoute.run); | ||||||
|     apiRoute(GET, '/api/script/startup', scriptRoute.getStartupBundles); |     apiRoute(GET, '/api/script/startup', scriptRoute.getStartupBundles); | ||||||
|     apiRoute(GET, '/api/script/bundle/:noteId', scriptRoute.getBundle); |     apiRoute(GET, '/api/script/bundle/:noteId', scriptRoute.getBundle); | ||||||
|  |     apiRoute(GET, '/api/script/relation/:noteId/:relationName', scriptRoute.getRelationBundles); | ||||||
|  |  | ||||||
|     route(POST, '/api/sender/login', [], senderRoute.login, apiResultHandler); |     route(POST, '/api/sender/login', [], senderRoute.login, apiResultHandler); | ||||||
|     route(POST, '/api/sender/image', [auth.checkSenderToken], senderRoute.uploadImage, apiResultHandler); |     route(POST, '/api/sender/image', [auth.checkSenderToken], senderRoute.uploadImage, apiResultHandler); | ||||||
|   | |||||||
| @@ -36,9 +36,23 @@ async function createRelation(sourceNoteId, name, targetNoteId) { | |||||||
|     }).save(); |     }).save(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | async function getEffectiveRelations(noteId) { | ||||||
|  |     return await repository.getEntities(` | ||||||
|  |         WITH RECURSIVE tree(noteId) AS ( | ||||||
|  |         SELECT ? | ||||||
|  |             UNION | ||||||
|  |             SELECT branches.parentNoteId FROM branches | ||||||
|  |             JOIN tree ON branches.noteId = tree.noteId | ||||||
|  |             JOIN notes ON notes.noteId = branches.parentNoteId | ||||||
|  |             WHERE notes.isDeleted = 0 AND branches.isDeleted = 0 | ||||||
|  |         ) | ||||||
|  |         SELECT relations.* FROM relations JOIN tree ON relations.sourceNoteId = tree.noteId WHERE relations.isDeleted = 0 AND relations.name IN ('runOnNoteView')`, [noteId]); | ||||||
|  | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|  |     BUILTIN_RELATIONS, | ||||||
|     getNotesWithRelation, |     getNotesWithRelation, | ||||||
|     getNoteWithRelation, |     getNoteWithRelation, | ||||||
|     createRelation, |     createRelation, | ||||||
|     BUILTIN_RELATIONS |     getEffectiveRelations | ||||||
| }; | }; | ||||||
| @@ -139,8 +139,14 @@ function sanitizeVariableName(str) { | |||||||
|     return str.replace(/[^a-z0-9_]/gim, ""); |     return str.replace(/[^a-z0-9_]/gim, ""); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | async function getScriptBundleForNoteId(noteId) { | ||||||
|  |     const note = await repository.getNote(noteId); | ||||||
|  |     return await getScriptBundle(note); | ||||||
|  | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     executeNote, |     executeNote, | ||||||
|     executeScript, |     executeScript, | ||||||
|     getScriptBundle |     getScriptBundle, | ||||||
|  |     getScriptBundleForNoteId | ||||||
| }; | }; | ||||||
		Reference in New Issue
	
	Block a user