Skip to content

Build your personal knowledge base with Trilium Notes

refactoring of script subtree execution

azivnera42fd9b0905f
diff --git a/public/javascripts/note_editor.js b/public/javascripts/note_editor.js
index c0b83b76a4..a278ade4cb 100644
--- a/public/javascripts/note_editor.js
+++ b/public/javascripts/note_editor.js
@@ -216,11 +216,12 @@ const noteEditor = (function() {
async function executeScript() {
if (getCurrentNoteType() === 'code') {
- // we take script value directly from editor so we don't need to wait for it to be saved
- const script = codeEditor.getValue();
+ // make sure note is saved so we load latest changes
+ await saveNoteIfChanged();
+
const subTreeScripts = await server.get('script/subtree/' + getCurrentNoteId());
- eval("(async function() {" + subTreeScripts + script + "})()");
+ eval("(async function() {" + subTreeScripts + "})()");
}
}
diff --git a/public/javascripts/utils.js b/public/javascripts/utils.js
index 130d8394fb..5347ec53bc 100644
--- a/public/javascripts/utils.js
+++ b/public/javascripts/utils.js
@@ -61,6 +61,10 @@ function formatDate(date) {
return padNum(date.getDate()) + ". " + padNum(date.getMonth() + 1) + ". " + date.getFullYear();
}
+function formatDateISO(date) {
+ return date.getFullYear() + "-" + padNum(date.getMonth() + 1) + "-" + padNum(date.getDate());
+}
+
function formatDateTime(date) {
return formatDate(date) + " " + formatTime(date);
}
diff --git a/routes/api/script.js b/routes/api/script.js
index 45d64c268d..c0c5f1bd13 100644
--- a/routes/api/script.js
+++ b/routes/api/script.js
@@ -6,6 +6,7 @@ const auth = require('../../services/auth');
const wrap = require('express-promise-wrap').wrap;
const log = require('../../services/log');
const sql = require('../../services/sql');
+const notes = require('../../services/notes');
const protected_session = require('../../services/protected_session');
router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => {
@@ -21,7 +22,11 @@ router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => {
router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteId = req.params.noteId;
- res.send(await getSubTreeScripts(noteId, [noteId], req));
+ const noteScript = (await notes.getNoteById(noteId, req)).note_text;
+
+ const subTreeScripts = await getSubTreeScripts(noteId, [noteId], req);
+
+ res.send(subTreeScripts + noteScript);
}));
async function getSubTreeScripts(parentId, includedNoteIds, dataKey) {
diff --git a/services/notes.js b/services/notes.js
index 4abd7d3625..741af8e55e 100644
--- a/services/notes.js
+++ b/services/notes.js
@@ -2,11 +2,18 @@ const sql = require('./sql');
const options = require('./options');
const utils = require('./utils');
const notes = require('./notes');
-const data_encryption = require('./data_encryption');
const sync_table = require('./sync_table');
const attributes = require('./attributes');
const protected_session = require('./protected_session');
+async function getNoteById(noteId, dataKey) {
+ const note = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId]);
+
+ protected_session.decryptNote(dataKey, note);
+
+ return note;
+}
+
async function createNewNote(parentNoteId, note, sourceId) {
const noteId = utils.newNoteId();
const noteTreeId = utils.newNoteTreeId();
@@ -41,8 +48,8 @@ async function createNewNote(parentNoteId, note, sourceId) {
note_title: note.note_title,
note_text: note.note_text ? note.note_text : '',
is_protected: note.is_protected,
- type: 'text',
- mime: 'text/html',
+ type: note.type ? note.type : 'text',
+ mime: note.mime ? note.mime : 'text/html',
date_created: now,
date_modified: now
});
@@ -277,6 +284,7 @@ async function deleteNote(noteTreeId, sourceId) {
}
module.exports = {
+ getNoteById,
createNewNote,
updateNote,
deleteNote,