trilium/src/services/script_context.js

66 lines
2.1 KiB
JavaScript
Raw Normal View History

const log = require('./log');
const protected_session = require('./protected_session');
const notes = require('./notes');
2018-02-25 11:44:45 +08:00
const sql = require('./sql');
const utils = require('./utils');
const attributes = require('./attributes');
const date_notes = require('./date_notes');
const config = require('./config');
const Repository = require('./repository');
const axios = require('axios');
2018-03-05 01:06:35 +08:00
function ScriptContext(dataKey, startNote, allNotes) {
dataKey = protected_session.getDataKey(dataKey);
this.modules = {};
this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
this.apis = utils.toObject(allNotes, note => [note.noteId, new ScriptApi(dataKey, startNote, note)]);
}
function ScriptApi(dataKey, startNote, currentNote) {
const repository = new Repository(dataKey);
this.startNote = startNote;
this.currentNote = currentNote;
2018-03-04 23:32:53 +08:00
2018-03-05 01:06:35 +08:00
this.axios = axios;
this.utils = {
unescapeHtml: utils.unescapeHtml,
isoDateTimeStr: utils.dateStr
};
this.getInstanceName = () => config.General ? config.General.instanceName : null;
this.getNoteById = async function(noteId) {
return repository.getNote(noteId);
};
2018-01-29 01:08:57 +08:00
this.getNotesWithAttribute = async function (attrName, attrValue) {
2018-03-05 03:21:11 +08:00
return await attributes.getNotesWithAttribute(repository, attrName, attrValue);
2018-01-29 01:08:57 +08:00
};
this.getNoteWithAttribute = async function (attrName, attrValue) {
2018-01-31 10:25:47 +08:00
const notes = await this.getNotesWithAttribute(attrName, attrValue);
2018-01-29 01:08:57 +08:00
return notes.length > 0 ? notes[0] : null;
};
this.createNote = async function(parentNoteId, title, content = "", extraOptions = {}) {
extraOptions.dataKey = dataKey;
return await notes.createNote(parentNoteId, title, content, extraOptions);
};
this.createAttribute = attributes.createAttribute;
this.updateEntity = repository.updateEntity;
this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`);
this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId;
this.getDateNoteId = date_notes.getDateNoteId;
2018-02-25 11:44:45 +08:00
this.transaction = sql.doInTransaction;
}
module.exports = ScriptContext;