trilium/src/services/script_context.js

70 lines
2.3 KiB
JavaScript
Raw Normal View History

const log = require('./log');
const protected_session = require('./protected_session');
const notes = require('./notes');
const attributes = require('./attributes');
const date_notes = require('./date_notes');
const config = require('./config');
const Repository = require('./repository');
function ScriptContext(noteId, dataKey) {
dataKey = protected_session.getDataKey(dataKey);
const repository = new Repository(dataKey);
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 = {}) {
const note = {
title: title,
content: extraOptions.json ? JSON.stringify(content, null, '\t') : content,
target: 'into',
2018-01-29 08:30:14 +08:00
isProtected: extraOptions.isProtected !== undefined ? extraOptions.isProtected : false,
type: extraOptions.type,
mime: extraOptions.mime
};
if (extraOptions.json) {
note.type = "code";
note.mime = "application/json";
}
if (!note.type) {
note.type = "text";
2018-01-29 09:52:05 +08:00
note.mime = "text/html";
}
const noteId = (await notes.createNewNote(parentNoteId, note, dataKey)).noteId;
if (extraOptions.attributes) {
for (const attrName in extraOptions.attributes) {
await attributes.createAttribute(noteId, attrName, extraOptions.attributes[attrName]);
}
}
return noteId;
};
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;
}
module.exports = ScriptContext;