From aead6a44de215c3ab8fb863070ef3654f4f9206b Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 19 May 2019 21:52:28 +0200 Subject: [PATCH] render notes should now return elements to append instead of modifying DOM directly --- src/public/javascripts/services/bundle.js | 4 ++-- src/public/javascripts/services/frontend_script_api.js | 5 ++++- src/public/javascripts/services/note_detail_render.js | 10 ++++++---- src/public/javascripts/services/script_context.js | 4 ++-- src/public/javascripts/services/tab_context.js | 1 - 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/public/javascripts/services/bundle.js b/src/public/javascripts/services/bundle.js index a8de63181..bcfa0b807 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 caca3c1e6..0e9a8bc92 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 840c4ae17..5490a4419 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 e6ebbd811..799c10523 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 ee2f1c33a..27a259dbe 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");