Skip to content

Build your personal knowledge base with Trilium Notes

render notes should now return elements to append instead of modifying DOM directly

zadamaead6a44de21
diff --git a/src/public/javascripts/services/bundle.js b/src/public/javascripts/services/bundle.js
index a8de63181b..bcfa0b807f 100644
--- a/src/public/javascripts/services/bundle.js
+++ b/src/public/javascripts/services/bundle.js
@@ -8,8 +8,8 @@ async function getAndExecuteBundle(noteId, originEntity = null) {
await executeBundle(bundle, originEntity);
}
-async function executeBundle(bundle, originEntity) {
- const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity);
+async function executeBundle(bundle, originEntity, tabContext) {
+ const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, tabContext);
try {
return await (function () {
diff --git a/src/public/javascripts/services/frontend_script_api.js b/src/public/javascripts/services/frontend_script_api.js
index caca3c1e67..0e9a8bc927 100644
--- a/src/public/javascripts/services/frontend_script_api.js
+++ b/src/public/javascripts/services/frontend_script_api.js
@@ -16,7 +16,7 @@ import dateNotesService from'./date_notes.js';
* @constructor
* @hideconstructor
*/
-function FrontendScriptApi(startNote, currentNote, originEntity = null) {
+function FrontendScriptApi(startNote, currentNote, originEntity = null, tabContext = null) {
const $pluginButtons = $("#plugin-buttons");
/** @property {object} note where script started executing */
@@ -29,6 +29,9 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null) {
// to keep consistency with backend API
this.dayjs = dayjs;
+ /** @property {TabContext|null} - experimental! */
+ this.tabContext = tabContext;
+
/**
* Activates note in the tree and in the note detail.
*
diff --git a/src/public/javascripts/services/note_detail_render.js b/src/public/javascripts/services/note_detail_render.js
index 840c4ae17a..5490a44195 100644
--- a/src/public/javascripts/services/note_detail_render.js
+++ b/src/public/javascripts/services/note_detail_render.js
@@ -1,7 +1,5 @@
import bundleService from "./bundle.js";
import server from "./server.js";
-import noteDetailService from "./note_detail.js";
-import attributeService from "./attributes.js";
class NoteDetailRender {
/**
@@ -14,7 +12,7 @@ class NoteDetailRender {
this.$noteDetailRenderContent = ctx.$tabContent.find('.note-detail-render-content');
this.$renderButton = ctx.$tabContent.find('.render-button');
- this.$renderButton.click(this.render);
+ this.$renderButton.click(() => this.render()); // long form!
}
async render() {
@@ -35,7 +33,11 @@ class NoteDetailRender {
this.$noteDetailRenderContent.append(bundle.html);
- await bundleService.executeBundle(bundle, noteDetailService.getActiveNote());
+ const $result = await bundleService.executeBundle(bundle, this.ctx.note, this.ctx);
+
+ if ($result) {
+ this.$noteDetailRenderContent.append($result);
+ }
}
}
diff --git a/src/public/javascripts/services/script_context.js b/src/public/javascripts/services/script_context.js
index e6ebbd8113..799c10523b 100644
--- a/src/public/javascripts/services/script_context.js
+++ b/src/public/javascripts/services/script_context.js
@@ -2,7 +2,7 @@ import FrontendScriptApi from './frontend_script_api.js';
import utils from './utils.js';
import treeCache from './tree_cache.js';
-async function ScriptContext(startNoteId, allNoteIds, originEntity = null) {
+async function ScriptContext(startNoteId, allNoteIds, originEntity = null, tabContext = null) {
const modules = {};
const startNote = await treeCache.getNote(startNoteId);
@@ -11,7 +11,7 @@ async function ScriptContext(startNoteId, allNoteIds, originEntity = null) {
return {
modules: modules,
notes: utils.toObject(allNotes, note => [note.noteId, note]),
- apis: utils.toObject(allNotes, note => [note.noteId, new FrontendScriptApi(startNote, note, originEntity)]),
+ apis: utils.toObject(allNotes, note => [note.noteId, new FrontendScriptApi(startNote, note, originEntity, tabContext)]),
require: moduleNoteIds => {
return moduleName => {
const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
diff --git a/src/public/javascripts/services/tab_context.js b/src/public/javascripts/services/tab_context.js
index ee2f1c33a4..27a259dbed 100644
--- a/src/public/javascripts/services/tab_context.js
+++ b/src/public/javascripts/services/tab_context.js
@@ -18,7 +18,6 @@ import noteDetailRelationMap from "./note_detail_relation_map.js";
import noteDetailProtectedSession from "./note_detail_protected_session.js";
import protectedSessionService from "./protected_session.js";
import linkService from "./link.js";
-import treeCache from "./tree_cache.js";
const $tabContentsContainer = $("#note-tab-container");