Skip to content

Build your personal knowledge base with Trilium Notes

script bugfixes

azivner72bd2507fe76
diff --git a/src/entities/note.js b/src/entities/note.js
index fe04defaad..f9c504a56c 100644
--- a/src/entities/note.js
+++ b/src/entities/note.js
@@ -40,7 +40,7 @@ class Note extends Entity {
}
beforeSaving() {
- this.content = JSON.stringify(this.jsonContent, null, 4);
+ this.content = JSON.stringify(this.jsonContent, null, '\t');
if (this.isProtected) {
protected_session.encryptNote(this.dataKey, this);
diff --git a/src/services/repository.js b/src/services/repository.js
index 9392a4a1bd..194b8a66f1 100644
--- a/src/services/repository.js
+++ b/src/services/repository.js
@@ -70,7 +70,7 @@ class Repository {
delete clone.jsonContent;
delete clone.repository;
- await sql.replace(entity.constructor.tableName, entity);
+ await sql.replace(entity.constructor.tableName, clone);
const primaryKey = entity[entity.constructor.primaryKeyName];
diff --git a/src/services/script.js b/src/services/script.js
index 82cc057787..826aa0ec06 100644
--- a/src/services/script.js
+++ b/src/services/script.js
@@ -15,8 +15,6 @@ async function executeScript(noteId, dataKey, script, params) {
ret = await (function() { return eval(`(${script})(${paramsStr})`); }.call(ctx));
});
- log.info('Execution result: ' + ret);
-
return ret;
}
diff --git a/src/services/script_context.js b/src/services/script_context.js
index 98edb6a779..a450c99d4d 100644
--- a/src/services/script_context.js
+++ b/src/services/script_context.js
@@ -3,8 +3,6 @@ const protected_session = require('./protected_session');
const notes = require('./notes');
const attributes = require('./attributes');
const date_notes = require('./date_notes');
-const sql = require('./sql');
-const sync_table = require('./sync_table');
const Repository = require('./repository');
function ScriptContext(noteId, dataKey) {
@@ -12,10 +10,6 @@ function ScriptContext(noteId, dataKey) {
this.dataKey = protected_session.getDataKey(dataKey);
this.repository = new Repository(dataKey);
- function serializePayload(payload) {
- return JSON.stringify(payload, null, '\t');
- }
-
this.getNoteById = async function(noteId) {
return this.repository.getNote(noteId);
};
@@ -25,15 +19,15 @@ function ScriptContext(noteId, dataKey) {
};
this.getNoteWithAttribute = async function (attrName, attrValue) {
- const notes = this.getNotesWithAttribute(attrName, attrValue);
+ const notes = await this.getNotesWithAttribute(attrName, attrValue);
return notes.length > 0 ? notes[0] : null;
};
- this.createNote = async function (parentNoteId, name, payload, extraOptions = {}) {
+ this.createNote = async function (parentNoteId, name, jsonContent, extraOptions = {}) {
const note = {
title: name,
- content: extraOptions.json ? serializePayload(payload) : payload,
+ content: extraOptions.json ? JSON.stringify(jsonContent, null, '\t') : jsonContent,
target: 'into',
isProtected: extraOptions.isProtected !== undefined ? extraOptions.isProtected : false,
type: extraOptions.type,
@@ -50,7 +44,7 @@ function ScriptContext(noteId, dataKey) {
note.mime = "text/html";
}
- const noteId = (await notes.createNewNote(parentNoteId, note)).noteId;
+ const noteId = (await notes.createNewNote(parentNoteId, note, this.dataKey)).noteId;
if (extraOptions.attributes) {
for (const attrName in extraOptions.attributes) {
@@ -61,21 +55,7 @@ function ScriptContext(noteId, dataKey) {
return noteId;
};
- this.updateNote = async function (note) {
- if (note.isJson()) {
- note.content = serializePayload(note.jsonContent);
- }
-
- delete note.jsonContent;
-
- if (note.isProtected) {
- protected_session.encryptNote(this.dataKey, note);
- }
-
- await sql.replace("notes", note);
-
- await sync_table.addNoteSync(note.noteId);
- };
+ this.updateEntity = this.repository.updateEntity;
this.log = function(message) {
log.info(`Script ${this.scriptNoteId}: ${message}`);