trilium/src/services/script_context.js

80 lines
2.9 KiB
JavaScript
Raw Normal View History

const log = require('./log');
const noteService = require('./notes');
2018-02-25 11:44:45 +08:00
const sql = require('./sql');
const utils = require('./utils');
2018-04-03 08:46:46 +08:00
const dateUtils = require('./date_utils');
const attributeService = require('./attributes');
const dateNoteService = require('./date_notes');
const treeService = require('./tree');
const config = require('./config');
2018-03-31 21:07:58 +08:00
const repository = require('./repository');
const axios = require('axios');
2018-08-12 01:45:55 +08:00
const cloningService = require('./cloning');
const messagingService = require('./messaging');
2018-08-10 19:30:20 +08:00
function ScriptContext(startNote, allNotes, originEntity = null) {
this.modules = {};
this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
2018-08-10 19:30:20 +08:00
this.apis = utils.toObject(allNotes, note => [note.noteId, new ScriptApi(startNote, note, originEntity)]);
this.require = moduleNoteIds => {
return moduleName => {
const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
const note = candidates.find(c => c.title === moduleName);
if (!note) {
throw new Error("Could not find module note " + moduleName);
}
return this.modules[note.noteId].exports;
}
};
}
2018-08-10 19:30:20 +08:00
function ScriptApi(startNote, currentNote, originEntity) {
this.startNote = startNote;
this.currentNote = currentNote;
2018-08-10 19:30:20 +08:00
this.originEntity = originEntity;
2018-03-04 23:32:53 +08:00
2018-03-05 01:06:35 +08:00
this.axios = axios;
this.utils = {
unescapeHtml: utils.unescapeHtml,
2018-04-03 08:46:46 +08:00
isoDateTimeStr: dateUtils.dateStr,
isoDateStr: date => dateUtils.dateStr(date).substr(0, 10)
};
this.getInstanceName = () => config.General ? config.General.instanceName : null;
2018-04-02 00:45:35 +08:00
this.getNote = repository.getNote;
this.getBranch = repository.getBranch;
this.getAttribute = repository.getAttribute;
2018-04-02 00:45:35 +08:00
this.getImage = repository.getImage;
this.getEntity = repository.getEntity;
this.getEntities = repository.getEntities;
this.createAttribute = attributeService.createAttribute;
this.getNotesWithLabel = attributeService.getNotesWithLabel;
this.getNoteWithLabel = attributeService.getNoteWithLabel;
2018-04-03 10:53:01 +08:00
2018-08-12 01:45:55 +08:00
this.ensureNoteIsPresentInParent = cloningService.ensureNoteIsPresentInParent;
this.ensureNoteIsAbsentFromParent = cloningService.ensureNoteIsAbsentFromParent;
this.toggleNoteInParent = cloningService.toggleNoteInParent;
2018-04-03 10:53:01 +08:00
this.createNote = noteService.createNote;
this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`);
this.getRootCalendarNote = dateNoteService.getRootCalendarNote;
this.getDateNote = dateNoteService.getDateNote;
2018-02-25 11:44:45 +08:00
this.sortNotesAlphabetically = treeService.sortNotesAlphabetically;
this.setNoteToParent = treeService.setNoteToParent;
this.transactional = sql.transactional;
2018-08-12 01:45:55 +08:00
this.refreshTree = () => messagingService.sendMessageToAllClients({ type: 'refresh-tree' });
}
module.exports = ScriptContext;