mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
common JS module system prototype
This commit is contained in:
parent
83d6c2970f
commit
7a865a9081
@ -217,7 +217,7 @@ const noteEditor = (function() {
|
|||||||
if (currentNote.detail.type === 'render') {
|
if (currentNote.detail.type === 'render') {
|
||||||
$noteDetailRender.show();
|
$noteDetailRender.show();
|
||||||
|
|
||||||
const subTree = await server.get('script/subtree/' + getCurrentNoteId());
|
const subTree = await server.get('script/render/' + getCurrentNoteId());
|
||||||
|
|
||||||
$noteDetailRender.html(subTree);
|
$noteDetailRender.html(subTree);
|
||||||
}
|
}
|
||||||
|
@ -44,4 +44,11 @@ router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) =>
|
|||||||
res.send(await script.getNoteScript(note, repository));
|
res.send(await script.getNoteScript(note, repository));
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
router.get('/render/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
||||||
|
const repository = new Repository(req);
|
||||||
|
const note = await repository.getNote(req.params.noteId);
|
||||||
|
|
||||||
|
res.send(await script.getRenderScript(note, repository));
|
||||||
|
}));
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
@ -19,7 +19,7 @@ async function runNotesWithAttribute(runAttrValue) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(() => runNotesWithAttribute('backend_startup'), 10 * 1000);
|
setTimeout(() => runNotesWithAttribute('backend_startup'), 1 * 1000);
|
||||||
|
|
||||||
setInterval(() => runNotesWithAttribute('hourly'), 3600 * 1000);
|
setInterval(() => runNotesWithAttribute('hourly'), 3600 * 1000);
|
||||||
|
|
||||||
|
@ -7,9 +7,20 @@ async function executeNote(note) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const manualTransactionHandling = (await note.getAttributeMap()).manual_transaction_handling !== undefined;
|
const manualTransactionHandling = (await note.getAttributeMap()).manual_transaction_handling !== undefined;
|
||||||
const noteScript = await getNoteScript(note);
|
|
||||||
|
|
||||||
return await executeJob(noteScript, [], manualTransactionHandling);
|
const modules = await getModules([note], []);
|
||||||
|
|
||||||
|
// last \r\n is necessary if script contains line comment on its last line
|
||||||
|
const script = "async function() {\r\n" + modules.script + "\r\n}";
|
||||||
|
|
||||||
|
const ctx = new ScriptContext(null, note, module.allModules);
|
||||||
|
|
||||||
|
if (manualTransactionHandling) {
|
||||||
|
return await execute(ctx, script, '');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return await sql.doInTransaction(async () => execute(ctx, script, ''));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function executeScript(dataKey, script, params) {
|
async function executeScript(dataKey, script, params) {
|
||||||
@ -20,7 +31,9 @@ async function executeScript(dataKey, script, params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function execute(ctx, script, paramsStr) {
|
async function execute(ctx, script, paramsStr) {
|
||||||
return await (function() { return eval(`const api = this; (${script})(${paramsStr})`); }.call(ctx));
|
console.log(`const api = this; (${script})(${paramsStr})`);
|
||||||
|
|
||||||
|
return await (function() { return eval(`const api = this;\r\n(${script})(${paramsStr})`); }.call(ctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
const timeouts = {};
|
const timeouts = {};
|
||||||
@ -83,38 +96,64 @@ function getParams(params) {
|
|||||||
}).join(",");
|
}).join(",");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getNoteScript(note) {
|
async function getRenderScript(note) {
|
||||||
const subTreeScripts = await getSubTreeScripts(note, [note.noteId]);
|
const subTreeScripts = await getModules(note, [note.noteId]);
|
||||||
|
|
||||||
// last \r\n is necessary if script contains line comment on its last line
|
// last \r\n is necessary if script contains line comment on its last line
|
||||||
return "async function() {" + subTreeScripts + note.content + "\r\n}";
|
return "async function() {" + subTreeScripts + note.content + "\r\n}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getNoteScript(note) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param includedNoteIds - if multiple child note scripts reference same dependency (child note),
|
* @param includedNoteIds - if multiple child note scripts reference same dependency (child note),
|
||||||
* it will be included just once
|
* it will be included just once
|
||||||
*/
|
*/
|
||||||
async function getSubTreeScripts(parent, includedNoteIds) {
|
async function getModules(children, includedNoteIds) {
|
||||||
let script = "\r\n";
|
const modules = [];
|
||||||
|
let allModules = [];
|
||||||
|
let script = '';
|
||||||
|
|
||||||
for (const child of await parent.getChildren()) {
|
for (const child of children) {
|
||||||
if (!child.isJavaScript() || includedNoteIds.includes(child.noteId)) {
|
if (!child.isJavaScript()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
modules.push(child);
|
||||||
|
|
||||||
|
if (includedNoteIds.includes(child.noteId)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
includedNoteIds.push(child.noteId);
|
includedNoteIds.push(child.noteId);
|
||||||
|
|
||||||
script += await getSubTreeScripts(child.noteId, includedNoteIds);
|
const children = await getModules(await child.getChildren(), includedNoteIds);
|
||||||
|
|
||||||
script += child.content + "\r\n";
|
allModules = allModules.concat(children.allModules);
|
||||||
|
|
||||||
|
script += children.script;
|
||||||
|
|
||||||
|
script += `
|
||||||
|
api.__modules['${child.noteId}'] = {};
|
||||||
|
await (async function(module, api, startNote, currentNote` + (children.modules.length > 0 ? ', ' : '') +
|
||||||
|
children.modules.map(child => child.title).join(', ') + `) {
|
||||||
|
${child.content}
|
||||||
|
})(api.__modules['${child.noteId}'], api, api.__startNote, api.__notes['${child.noteId}']` + (children.modules.length > 0 ? ', ' : '') +
|
||||||
|
children.modules.map(child => `api.__modules['${child.noteId}'].exports`).join(', ') + `);
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return script;
|
allModules = allModules.concat(modules);
|
||||||
|
|
||||||
|
return { script, modules, allModules };
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
executeNote,
|
executeNote,
|
||||||
executeScript,
|
executeScript,
|
||||||
setJob,
|
setJob,
|
||||||
getNoteScript
|
getNoteScript,
|
||||||
|
getRenderScript
|
||||||
};
|
};
|
@ -9,12 +9,21 @@ const config = require('./config');
|
|||||||
const Repository = require('./repository');
|
const Repository = require('./repository');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
|
|
||||||
function ScriptContext(dataKey) {
|
function ScriptContext(dataKey, note, allNotes) {
|
||||||
dataKey = protected_session.getDataKey(dataKey);
|
dataKey = protected_session.getDataKey(dataKey);
|
||||||
const repository = new Repository(dataKey);
|
const repository = new Repository(dataKey);
|
||||||
|
|
||||||
this.axios = axios;
|
this.axios = axios;
|
||||||
|
|
||||||
|
this.__startNote = note;
|
||||||
|
this.__notes = {};
|
||||||
|
|
||||||
|
if (allNotes) {
|
||||||
|
allNotes.forEach(note => this.__notes[note.noteId] = note);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.__modules = {};
|
||||||
|
|
||||||
this.utils = {
|
this.utils = {
|
||||||
unescapeHtml: utils.unescapeHtml,
|
unescapeHtml: utils.unescapeHtml,
|
||||||
isoDateTimeStr: utils.dateStr
|
isoDateTimeStr: utils.dateStr
|
||||||
|
Loading…
x
Reference in New Issue
Block a user