mirror of https://github.com/TriliumNext/Notes
server-ts: Convert services/script_context
parent
15dee4b952
commit
884b6618fb
@ -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,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;
|
||||
@ -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…
Reference in New Issue