mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
inclusion of scripts based on script environment
This commit is contained in:
parent
b3209a9bbf
commit
d26170762b
@ -32,6 +32,18 @@ class Note extends Entity {
|
||||
return (this.type === "code" || this.type === "file") && this.mime === "text/html";
|
||||
}
|
||||
|
||||
getScriptEnv() {
|
||||
if (this.isHtml() || (this.isJavaScript() && this.mime.endsWith('env=frontend'))) {
|
||||
return "frontend";
|
||||
}
|
||||
|
||||
if (this.isJavaScript() && this.mime.endsWith('env=backend')) {
|
||||
return "backend";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async getAttributes() {
|
||||
return this.repository.getEntities("SELECT * FROM attributes WHERE noteId = ? AND isDeleted = 0", [this.noteId]);
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ async function getRenderScript(note) {
|
||||
+ bundle.html;
|
||||
}
|
||||
|
||||
async function getScriptBundle(note, includedNoteIds = []) {
|
||||
async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
|
||||
if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') {
|
||||
return;
|
||||
}
|
||||
@ -69,6 +69,10 @@ async function getScriptBundle(note, includedNoteIds = []) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!scriptEnv) {
|
||||
scriptEnv = note.getScriptEnv();
|
||||
}
|
||||
|
||||
const bundle = {
|
||||
note: note,
|
||||
script: '',
|
||||
@ -82,10 +86,16 @@ async function getScriptBundle(note, includedNoteIds = []) {
|
||||
|
||||
includedNoteIds.push(note.noteId);
|
||||
|
||||
bundle.script = `api.__modules['${note.noteId}'] = { __noteId: '${note.noteId}', __env: '${note.getScriptEnv()}'};`;
|
||||
|
||||
if (note.type !== 'file' && scriptEnv !== note.getScriptEnv()) {
|
||||
return bundle;
|
||||
}
|
||||
|
||||
const modules = [];
|
||||
|
||||
for (const child of await note.getChildren()) {
|
||||
const childBundle = await getScriptBundle(child, includedNoteIds);
|
||||
const childBundle = await getScriptBundle(child, scriptEnv, includedNoteIds);
|
||||
|
||||
if (childBundle) {
|
||||
modules.push(childBundle.note);
|
||||
@ -97,7 +107,6 @@ async function getScriptBundle(note, includedNoteIds = []) {
|
||||
|
||||
if (note.isJavaScript()) {
|
||||
bundle.script += `
|
||||
api.__modules['${note.noteId}'] = {};
|
||||
await (async function(exports, module, api, startNote, currentNote` + (modules.length > 0 ? ', ' : '') +
|
||||
modules.map(child => child.title).join(', ') + `) {
|
||||
${note.content}
|
||||
|
Loading…
x
Reference in New Issue
Block a user