mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
fixed render notes
This commit is contained in:
parent
766a567a32
commit
8e95b080da
@ -37,6 +37,10 @@ class Note extends Entity {
|
||||
return "frontend";
|
||||
}
|
||||
|
||||
if (this.type === 'render') {
|
||||
return "frontend";
|
||||
}
|
||||
|
||||
if (this.isJavaScript() && this.mime.endsWith('env=backend')) {
|
||||
return "backend";
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
function ApiContext(startNote, allNotes) {
|
||||
function ScriptContext(startNote, allNotes) {
|
||||
return {
|
||||
modules: {},
|
||||
notes: toObject(allNotes, note => [note.noteId, note]),
|
||||
apis: toObject(allNotes, note => [note.noteId, Api(startNote, note)]),
|
||||
apis: toObject(allNotes, note => [note.noteId, ScriptApi(startNote, note)]),
|
||||
};
|
||||
}
|
||||
|
||||
function Api(startNote, currentNote) {
|
||||
function ScriptApi(startNote, currentNote) {
|
||||
const $pluginButtons = $("#plugin-buttons");
|
||||
|
||||
async function activateNote(notePath) {
|
||||
|
@ -217,9 +217,11 @@ const noteEditor = (function() {
|
||||
if (currentNote.detail.type === 'render') {
|
||||
$noteDetailRender.show();
|
||||
|
||||
const html = await server.get('script/render/' + getCurrentNoteId());
|
||||
const bundle = await server.get('script/bundle/' + getCurrentNoteId());
|
||||
|
||||
$noteDetailRender.html(html);
|
||||
$noteDetailRender.html(bundle.html);
|
||||
|
||||
executeBundle(bundle);
|
||||
}
|
||||
else if (currentNote.detail.type === 'file') {
|
||||
$noteDetailAttachment.show();
|
||||
|
@ -116,9 +116,7 @@ async function stopWatch(what, func) {
|
||||
}
|
||||
|
||||
async function executeBundle(bundle) {
|
||||
const apiContext = ApiContext(bundle.note, bundle.allNotes);
|
||||
|
||||
console.log((`const apiContext = this; (async function() { ${bundle.script}\r\n})()`));
|
||||
const apiContext = ScriptContext(bundle.note, bundle.allNotes);
|
||||
|
||||
return await (function() { return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`); }.call(apiContext));
|
||||
}
|
||||
|
@ -50,12 +50,4 @@ router.get('/bundle/:noteId', auth.checkApiAuth, wrap(async (req, res, next) =>
|
||||
res.send(bundle);
|
||||
}));
|
||||
|
||||
router.get('/render/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
||||
const repository = new Repository(req);
|
||||
const note = await repository.getNote(req.params.noteId);
|
||||
const bundle = await script.getRenderScript(note);
|
||||
|
||||
res.send(bundle);
|
||||
}));
|
||||
|
||||
module.exports = router;
|
@ -8,6 +8,7 @@ async function runNotesWithAttribute(runAttrValue) {
|
||||
SELECT notes.*
|
||||
FROM notes
|
||||
JOIN attributes ON attributes.noteId = notes.noteId
|
||||
AND attributes.isDeleted = 0
|
||||
AND attributes.name = 'run'
|
||||
AND attributes.value = ?
|
||||
WHERE
|
||||
|
@ -40,7 +40,8 @@ async function executeScript(dataKey, script, params, startNoteId, currentNoteId
|
||||
const startNote = await repository.getNote(startNoteId);
|
||||
const currentNote = await repository.getNote(currentNoteId);
|
||||
|
||||
currentNote.content = `(${script}\r\n)(${getParams(params)})`;
|
||||
currentNote.content = `return await (${script}\r\n)(${getParams(params)})`;
|
||||
currentNote.type = 'code';
|
||||
currentNote.mime = 'application/javascript;env=backend';
|
||||
|
||||
const bundle = await getScriptBundle(currentNote);
|
||||
@ -67,14 +68,7 @@ function getParams(params) {
|
||||
}).join(",");
|
||||
}
|
||||
|
||||
async function getRenderScript(note) {
|
||||
const bundle = await getScriptBundle(note);
|
||||
|
||||
return `<script type="text/javascript">(async function() {\r\nconst api = Api();\r\n${bundle.script}\r\n})();</script>`
|
||||
+ bundle.html;
|
||||
}
|
||||
|
||||
async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
|
||||
async function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = []) {
|
||||
if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') {
|
||||
return;
|
||||
}
|
||||
@ -83,7 +77,7 @@ async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!scriptEnv) {
|
||||
if (root) {
|
||||
scriptEnv = note.getScriptEnv();
|
||||
}
|
||||
|
||||
@ -107,7 +101,7 @@ async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
|
||||
const modules = [];
|
||||
|
||||
for (const child of await note.getChildren()) {
|
||||
const childBundle = await getScriptBundle(child, scriptEnv, includedNoteIds);
|
||||
const childBundle = await getScriptBundle(child, false, scriptEnv, includedNoteIds);
|
||||
|
||||
if (childBundle) {
|
||||
modules.push(childBundle.note);
|
||||
@ -120,7 +114,7 @@ async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
|
||||
if (note.isJavaScript()) {
|
||||
bundle.script += `
|
||||
apiContext.modules['${note.noteId}'] = {};
|
||||
await (async function(exports, module, api` + (modules.length > 0 ? ', ' : '') +
|
||||
${root ? 'return ' : ''}await (async function(exports, module, api` + (modules.length > 0 ? ', ' : '') +
|
||||
modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
|
||||
${note.content}
|
||||
})({}, apiContext.modules['${note.noteId}'], apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
|
||||
@ -141,6 +135,5 @@ function sanitizeVariableName(str) {
|
||||
module.exports = {
|
||||
executeNote,
|
||||
executeScript,
|
||||
getScriptBundle,
|
||||
getRenderScript
|
||||
getScriptBundle
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user