mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 21:11:30 +08:00 
			
		
		
		
	server-ts: Convert services/script_context
This commit is contained in:
		
							parent
							
								
									15dee4b952
								
							
						
					
					
						commit
						884b6618fb
					
				| @ -33,6 +33,7 @@ import BOption = require('../becca/entities/boption'); | |||||||
| import { AttributeRow, AttributeType, NoteType } from '../becca/entities/rows'; | import { AttributeRow, AttributeType, NoteType } from '../becca/entities/rows'; | ||||||
| import Becca from '../becca/becca-interface'; | import Becca from '../becca/becca-interface'; | ||||||
| import { NoteParams } from './note-interface'; | import { NoteParams } from './note-interface'; | ||||||
|  | import { ApiParams } from './backend_script_api_interface'; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -46,11 +47,6 @@ import { NoteParams } from './note-interface'; | |||||||
|  * @var {BackendScriptApi} api |  * @var {BackendScriptApi} api | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| interface ApiParams { |  | ||||||
|     startNote: BNote; |  | ||||||
|     originEntity: AbstractBeccaEntity<any>; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| interface SearchParams { | interface SearchParams { | ||||||
|     includeArchivedNotes?: boolean; |     includeArchivedNotes?: boolean; | ||||||
|     ignoreHoistedNote?: boolean; |     ignoreHoistedNote?: boolean; | ||||||
| @ -384,6 +380,7 @@ interface Api { | |||||||
|     }; |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // TODO: Convert to class.
 | ||||||
| /** | /** | ||||||
|  * <p>This is the main backend API interface for scripts. All the properties and methods are published in the "api" object |  * <p>This is the main backend API interface for scripts. All the properties and methods are published in the "api" object | ||||||
|  * available in the JS backend notes. You can use e.g. <code>api.log(api.startNote.title);</code></p> |  * available in the JS backend notes. You can use e.g. <code>api.log(api.startNote.title);</code></p> | ||||||
| @ -654,4 +651,6 @@ function BackendScriptApi(this: Api, currentNote: BNote, apiParams: ApiParams) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export = BackendScriptApi; | export = BackendScriptApi as any as { | ||||||
|  |     new (currentNote: BNote, apiParams: ApiParams): Api | ||||||
|  | }; | ||||||
|  | |||||||
							
								
								
									
										7
									
								
								src/services/backend_script_api_interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/services/backend_script_api_interface.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | |||||||
|  | import AbstractBeccaEntity = require("../becca/entities/abstract_becca_entity"); | ||||||
|  | import BNote = require("../becca/entities/bnote"); | ||||||
|  | 
 | ||||||
|  | export interface ApiParams { | ||||||
|  |     startNote: BNote; | ||||||
|  |     originEntity: AbstractBeccaEntity<any>; | ||||||
|  | } | ||||||
| @ -1,4 +1,4 @@ | |||||||
| const ScriptContext = require('./script_context.js'); | const ScriptContext = require('./script_context'); | ||||||
| const cls = require('./cls'); | const cls = require('./cls'); | ||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
| const becca = require('../becca/becca'); | const becca = require('../becca/becca'); | ||||||
| @ -76,7 +76,7 @@ function executeScript(script, params, startNoteId, currentNoteId, originEntityN | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function execute(ctx, script) { | function execute(ctx, script) { | ||||||
|     return function() { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx); |     return function () { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function getParams(params) { | function getParams(params) { | ||||||
|  | |||||||
| @ -1,22 +0,0 @@ | |||||||
| const utils = require('./utils'); |  | ||||||
| const BackendScriptApi = require('./backend_script_api'); |  | ||||||
| 
 |  | ||||||
| function ScriptContext(allNotes, apiParams = {}) { |  | ||||||
|     this.modules = {}; |  | ||||||
|     this.notes = utils.toObject(allNotes, note => [note.noteId, note]); |  | ||||||
|     this.apis = utils.toObject(allNotes, note => [note.noteId, new BackendScriptApi(note, apiParams)]); |  | ||||||
|     this.require = moduleNoteIds => { |  | ||||||
|         return moduleName => { |  | ||||||
|             const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId)); |  | ||||||
|             const note = candidates.find(c => c.title === moduleName); |  | ||||||
| 
 |  | ||||||
|             if (!note) { |  | ||||||
|                 return require(moduleName); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             return this.modules[note.noteId].exports; |  | ||||||
|         } |  | ||||||
|     }; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| module.exports = ScriptContext; |  | ||||||
							
								
								
									
										37
									
								
								src/services/script_context.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/services/script_context.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,37 @@ | |||||||
|  | import utils = require('./utils'); | ||||||
|  | import BackendScriptApi = require('./backend_script_api'); | ||||||
|  | import BNote = require('../becca/entities/bnote'); | ||||||
|  | import { ApiParams } from './backend_script_api_interface'; | ||||||
|  | 
 | ||||||
|  | type Module = { | ||||||
|  |     exports: any[]; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | class ScriptContext { | ||||||
|  |     modules: Record<string, Module>; | ||||||
|  |     notes: {}; | ||||||
|  |     apis: {}; | ||||||
|  |     allNotes: BNote[]; | ||||||
|  |      | ||||||
|  |     constructor(allNotes: BNote[], apiParams: ApiParams) { | ||||||
|  |         this.allNotes = allNotes; | ||||||
|  |         this.modules = {}; | ||||||
|  |         this.notes = utils.toObject(allNotes, note => [note.noteId, note]); | ||||||
|  |         this.apis = utils.toObject(allNotes, note => [note.noteId, new BackendScriptApi(note, apiParams)]); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     require(moduleNoteIds: string[]) { | ||||||
|  |         return (moduleName: string) => { | ||||||
|  |             const candidates = this.allNotes.filter(note => moduleNoteIds.includes(note.noteId)); | ||||||
|  |             const note = candidates.find(c => c.title === moduleName); | ||||||
|  | 
 | ||||||
|  |             if (!note) { | ||||||
|  |                 return require(moduleName); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             return this.modules[note.noteId].exports; | ||||||
|  |         } | ||||||
|  |     }; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export = ScriptContext; | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran