mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	refactored targetNote to workNote in the ScriptContext which was very confusing with relation's targetNote
This commit is contained in:
		| @@ -1,14 +1,14 @@ | |||||||
| import ScriptContext from "./script_context.js"; | import ScriptContext from "./script_context.js"; | ||||||
| import server from "./server.js"; | import server from "./server.js"; | ||||||
|  |  | ||||||
| async function getAndExecuteBundle(noteId, targetNote = null) { | async function getAndExecuteBundle(noteId, workNote = null) { | ||||||
|     const bundle = await server.get('script/bundle/' + noteId); |     const bundle = await server.get('script/bundle/' + noteId); | ||||||
|  |  | ||||||
|     await executeBundle(bundle, targetNote); |     await executeBundle(bundle, workNote); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function executeBundle(bundle, targetNote) { | async function executeBundle(bundle, workNote) { | ||||||
|     const apiContext = ScriptContext(bundle.note, bundle.allNotes, targetNote); |     const apiContext = ScriptContext(bundle.note, bundle.allNotes, workNote); | ||||||
|  |  | ||||||
|     return await (function () { |     return await (function () { | ||||||
|         return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`); |         return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`); | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ import utils from './utils.js'; | |||||||
| import infoService from './info.js'; | import infoService from './info.js'; | ||||||
| import linkService from './link.js'; | import linkService from './link.js'; | ||||||
|  |  | ||||||
| function ScriptApi(startNote, currentNote, targetNote = null) { | function ScriptApi(startNote, currentNote, workNote = null) { | ||||||
|     const $pluginButtons = $("#plugin-buttons"); |     const $pluginButtons = $("#plugin-buttons"); | ||||||
|  |  | ||||||
|     async function activateNote(notePath) { |     async function activateNote(notePath) { | ||||||
| @@ -44,7 +44,7 @@ function ScriptApi(startNote, currentNote, targetNote = null) { | |||||||
|             params: prepareParams(params), |             params: prepareParams(params), | ||||||
|             startNoteId: startNote.noteId, |             startNoteId: startNote.noteId, | ||||||
|             currentNoteId: currentNote.noteId, |             currentNoteId: currentNote.noteId, | ||||||
|             targetNoteId: targetNote ? targetNote.noteId : null |             workNoteId: workNote ? workNote.noteId : null | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         return ret.executionResult; |         return ret.executionResult; | ||||||
| @@ -53,7 +53,7 @@ function ScriptApi(startNote, currentNote, targetNote = null) { | |||||||
|     return { |     return { | ||||||
|         startNote: startNote, |         startNote: startNote, | ||||||
|         currentNote: currentNote, |         currentNote: currentNote, | ||||||
|         targetNote: targetNote, |         workNote: workNote, | ||||||
|         addButtonToToolbar, |         addButtonToToolbar, | ||||||
|         activateNote, |         activateNote, | ||||||
|         getInstanceName: () => window.glob.instanceName, |         getInstanceName: () => window.glob.instanceName, | ||||||
|   | |||||||
| @@ -1,13 +1,13 @@ | |||||||
| import ScriptApi from './script_api.js'; | import ScriptApi from './script_api.js'; | ||||||
| import utils from './utils.js'; | import utils from './utils.js'; | ||||||
|  |  | ||||||
| function ScriptContext(startNote, allNotes, targetNote = null) { | function ScriptContext(startNote, allNotes, workNote = null) { | ||||||
|     const modules = {}; |     const modules = {}; | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
|         modules: modules, |         modules: modules, | ||||||
|         notes: utils.toObject(allNotes, note => [note.noteId, note]), |         notes: utils.toObject(allNotes, note => [note.noteId, note]), | ||||||
|         apis: utils.toObject(allNotes, note => [note.noteId, ScriptApi(startNote, note, targetNote)]), |         apis: utils.toObject(allNotes, note => [note.noteId, ScriptApi(startNote, note, workNote)]), | ||||||
|         require: moduleNoteIds => { |         require: moduleNoteIds => { | ||||||
|             return moduleName => { |             return moduleName => { | ||||||
|                 const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId)); |                 const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId)); | ||||||
|   | |||||||
| @@ -7,7 +7,7 @@ const repository = require('../../services/repository'); | |||||||
|  |  | ||||||
| async function exec(req) { | async function exec(req) { | ||||||
|     const result = await scriptService.executeScript(req.body.script, req.body.params, req.body.startNoteId, |     const result = await scriptService.executeScript(req.body.script, req.body.params, req.body.startNoteId, | ||||||
|         req.body.currentNoteId, req.body.targetNoteId); |         req.body.currentNoteId, req.body.workNoteId); | ||||||
|  |  | ||||||
|     return { executionResult: result }; |     return { executionResult: result }; | ||||||
| } | } | ||||||
|   | |||||||
										
											Binary file not shown.
										
									
								
							| @@ -14,7 +14,7 @@ async function executeNote(note, targetNote) { | |||||||
|     await executeBundle(bundle, note, targetNote); |     await executeBundle(bundle, note, targetNote); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function executeBundle(bundle, startNote, targetNote = null) { | async function executeBundle(bundle, startNote, workNote = null) { | ||||||
|     if (!startNote) { |     if (!startNote) { | ||||||
|         // this is the default case, the only exception is when we want to preserve frontend startNote |         // this is the default case, the only exception is when we want to preserve frontend startNote | ||||||
|         startNote = bundle.note; |         startNote = bundle.note; | ||||||
| @@ -23,7 +23,7 @@ async function executeBundle(bundle, startNote, targetNote = null) { | |||||||
|     // 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" + bundle.script + "\r\n}"; |     const script = "async function() {\r\n" + bundle.script + "\r\n}"; | ||||||
|  |  | ||||||
|     const ctx = new ScriptContext(startNote, bundle.allNotes, targetNote); |     const ctx = new ScriptContext(startNote, bundle.allNotes, workNote); | ||||||
|  |  | ||||||
|     if (await bundle.note.hasLabel('manualTransactionHandling')) { |     if (await bundle.note.hasLabel('manualTransactionHandling')) { | ||||||
|         return await execute(ctx, script, ''); |         return await execute(ctx, script, ''); | ||||||
|   | |||||||
| @@ -10,10 +10,10 @@ const config = require('./config'); | |||||||
| const repository = require('./repository'); | const repository = require('./repository'); | ||||||
| const axios = require('axios'); | const axios = require('axios'); | ||||||
|  |  | ||||||
| function ScriptContext(startNote, allNotes, targetNote = null) { | function ScriptContext(startNote, allNotes, workNote = null) { | ||||||
|     this.modules = {}; |     this.modules = {}; | ||||||
|     this.notes = utils.toObject(allNotes, note => [note.noteId, note]); |     this.notes = utils.toObject(allNotes, note => [note.noteId, note]); | ||||||
|     this.apis = utils.toObject(allNotes, note => [note.noteId, new ScriptApi(startNote, note, targetNote)]); |     this.apis = utils.toObject(allNotes, note => [note.noteId, new ScriptApi(startNote, note, workNote)]); | ||||||
|     this.require = moduleNoteIds => { |     this.require = moduleNoteIds => { | ||||||
|         return moduleName => { |         return moduleName => { | ||||||
|             const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId)); |             const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId)); | ||||||
| @@ -28,10 +28,10 @@ function ScriptContext(startNote, allNotes, targetNote = null) { | |||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  |  | ||||||
| function ScriptApi(startNote, currentNote, targetNote) { | function ScriptApi(startNote, currentNote, workNote) { | ||||||
|     this.startNote = startNote; |     this.startNote = startNote; | ||||||
|     this.currentNote = currentNote; |     this.currentNote = currentNote; | ||||||
|     this.targetNote = targetNote; |     this.workNote = workNote; | ||||||
|  |  | ||||||
|     this.axios = axios; |     this.axios = axios; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user