mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	refactoring of getModules function
This commit is contained in:
		| @@ -8,12 +8,12 @@ async function executeNote(note) { | |||||||
|  |  | ||||||
|     const manualTransactionHandling = (await note.getAttributeMap()).manual_transaction_handling !== undefined; |     const manualTransactionHandling = (await note.getAttributeMap()).manual_transaction_handling !== undefined; | ||||||
|  |  | ||||||
|     const modules = await getModules([note], []); |     const bundle = await getScriptBundle(note); | ||||||
|  |  | ||||||
|     // last \r\n is necessary if script contains line comment on its last line |     // last \r\n is necessary if script contains line comment on its last line | ||||||
|     const script = "async function() {\r\n" + modules.script + "\r\n}"; |     const script = "async function() {\r\n" + bundle.script + "\r\n}"; | ||||||
|  |  | ||||||
|     const ctx = new ScriptContext(null, note, module.allModules); |     const ctx = new ScriptContext(null, note, bundle.allNotes); | ||||||
|  |  | ||||||
|     if (manualTransactionHandling) { |     if (manualTransactionHandling) { | ||||||
|         return await execute(ctx, script, ''); |         return await execute(ctx, script, ''); | ||||||
| @@ -31,8 +31,6 @@ async function executeScript(dataKey, script, params) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function execute(ctx, script, paramsStr) { | async function execute(ctx, script, paramsStr) { | ||||||
|     console.log(`const api = this; (${script})(${paramsStr})`); |  | ||||||
|  |  | ||||||
|     return await (function() { return eval(`const api = this;\r\n(${script})(${paramsStr})`); }.call(ctx)); |     return await (function() { return eval(`const api = this;\r\n(${script})(${paramsStr})`); }.call(ctx)); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -107,47 +105,45 @@ async function getNoteScript(note) { | |||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| /** | async function getScriptBundle(note, includedNoteIds = []) { | ||||||
|  * @param includedNoteIds - if multiple child note scripts reference same dependency (child note), |     if (!note.isJavaScript()) { | ||||||
|  *                          it will be included just once |         return; | ||||||
|  */ |     } | ||||||
| async function getModules(children, includedNoteIds) { |  | ||||||
|  |     const bundle = { | ||||||
|  |         note: note, | ||||||
|  |         script: '', | ||||||
|  |         allNotes: [note] | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     if (includedNoteIds.includes(note.noteId)) { | ||||||
|  |         return bundle; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     includedNoteIds.push(note.noteId); | ||||||
|  |  | ||||||
|     const modules = []; |     const modules = []; | ||||||
|     let allModules = []; |  | ||||||
|     let script = ''; |  | ||||||
|  |  | ||||||
|     for (const child of children) { |     for (const child of await note.getChildren()) { | ||||||
|         if (!child.isJavaScript()) { |         const childBundle = await getScriptBundle(child, includedNoteIds); | ||||||
|             continue; |  | ||||||
|  |         if (childBundle) { | ||||||
|  |             modules.push(childBundle.note); | ||||||
|  |             bundle.script += childBundle.script; | ||||||
|  |             bundle.allNotes = bundle.allNotes.concat(childBundle.allNotes); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|         modules.push(child); |     bundle.script += ` | ||||||
|  | api.__modules['${note.noteId}'] = {}; | ||||||
|         if (includedNoteIds.includes(child.noteId)) { | await (async function(module, api, startNote, currentNote` + (modules.length > 0 ? ', ' : '') + | ||||||
|             continue; |         modules.map(child => child.title).join(', ') + `) { | ||||||
|         } | ${note.content} | ||||||
|  | })(api.__modules['${note.noteId}'], api, api.__startNote, api.__notes['${note.noteId}']` + (modules.length > 0 ? ', ' : '') + | ||||||
|         includedNoteIds.push(child.noteId); |         modules.map(mod => `api.__modules['${mod.noteId}'].exports`).join(', ') + `); | ||||||
|  |  | ||||||
|         const children = await getModules(await child.getChildren(), includedNoteIds); |  | ||||||
|  |  | ||||||
|         allModules = allModules.concat(children.allModules); |  | ||||||
|  |  | ||||||
|         script += children.script; |  | ||||||
|  |  | ||||||
|         script += ` |  | ||||||
| api.__modules['${child.noteId}'] = {}; |  | ||||||
| await (async function(module, api, startNote, currentNote` + (children.modules.length > 0 ? ', ' : '') + |  | ||||||
|         children.modules.map(child => child.title).join(', ') + `) { |  | ||||||
| ${child.content} |  | ||||||
| })(api.__modules['${child.noteId}'], api, api.__startNote, api.__notes['${child.noteId}']` + (children.modules.length > 0 ? ', ' : '') + |  | ||||||
|         children.modules.map(child => `api.__modules['${child.noteId}'].exports`).join(', ') + `); |  | ||||||
| `; | `; | ||||||
|     } |  | ||||||
|  |  | ||||||
|     allModules = allModules.concat(modules); |     return bundle; | ||||||
|  |  | ||||||
|     return { script, modules, allModules }; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   | |||||||
| @@ -9,21 +9,16 @@ const config = require('./config'); | |||||||
| const Repository = require('./repository'); | const Repository = require('./repository'); | ||||||
| const axios = require('axios'); | const axios = require('axios'); | ||||||
|  |  | ||||||
| function ScriptContext(dataKey, note, allNotes) { | function ScriptContext(dataKey, startNote, allNotes) { | ||||||
|     dataKey = protected_session.getDataKey(dataKey); |     dataKey = protected_session.getDataKey(dataKey); | ||||||
|     const repository = new Repository(dataKey); |     const repository = new Repository(dataKey); | ||||||
|  |  | ||||||
|     this.axios = axios; |     this.__startNote = startNote; | ||||||
|  |     this.__notes = utils.toObject(allNotes, note => [note.noteId, note]); | ||||||
|     this.__startNote = note; |  | ||||||
|     this.__notes = {}; |  | ||||||
|  |  | ||||||
|     if (allNotes) { |  | ||||||
|         allNotes.forEach(note => this.__notes[note.noteId] = note); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     this.__modules = {}; |     this.__modules = {}; | ||||||
|  |  | ||||||
|  |     this.axios = axios; | ||||||
|  |  | ||||||
|     this.utils = { |     this.utils = { | ||||||
|         unescapeHtml: utils.unescapeHtml, |         unescapeHtml: utils.unescapeHtml, | ||||||
|         isoDateTimeStr: utils.dateStr |         isoDateTimeStr: utils.dateStr | ||||||
|   | |||||||
| @@ -134,6 +134,18 @@ function unescapeHtml(str) { | |||||||
|     return unescape(str); |     return unescape(str); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | function toObject(array, fn) { | ||||||
|  |     const obj = {}; | ||||||
|  |  | ||||||
|  |     for (const item of array) { | ||||||
|  |         const ret = fn(item); | ||||||
|  |  | ||||||
|  |         obj[ret[0]] = ret[1]; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return obj; | ||||||
|  | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     randomSecureToken, |     randomSecureToken, | ||||||
|     randomString, |     randomString, | ||||||
| @@ -159,5 +171,6 @@ module.exports = { | |||||||
|     sanitizeSql, |     sanitizeSql, | ||||||
|     assertArguments, |     assertArguments, | ||||||
|     stopWatch, |     stopWatch, | ||||||
|     unescapeHtml |     unescapeHtml, | ||||||
|  |     toObject | ||||||
| }; | }; | ||||||
		Reference in New Issue
	
	Block a user