trilium/src/services/script_context.js

61 lines
1.9 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);
const repository = new Repository(dataKey);
2018-03-05 01:06:35 +08:00
this.__startNote = startNote;
this.__notes = utils.toObject(allNotes, note => [note.noteId, note]);
2018-03-04 23:32:53 +08:00
this.__modules = {};
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) {
return await attributes.getNotesWithAttribute(dataKey, 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: ${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;