mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	refactoring of script subtree execution
This commit is contained in:
		| @@ -216,11 +216,12 @@ const noteEditor = (function() { | |||||||
|  |  | ||||||
|     async function executeScript() { |     async function executeScript() { | ||||||
|         if (getCurrentNoteType() === 'code') { |         if (getCurrentNoteType() === 'code') { | ||||||
|             // we take script value directly from editor so we don't need to wait for it to be saved |             // make sure note is saved so we load latest changes | ||||||
|             const script = codeEditor.getValue(); |             await saveNoteIfChanged(); | ||||||
|  |  | ||||||
|             const subTreeScripts = await server.get('script/subtree/' + getCurrentNoteId()); |             const subTreeScripts = await server.get('script/subtree/' + getCurrentNoteId()); | ||||||
|  |  | ||||||
|             eval("(async function() {" + subTreeScripts + script + "})()"); |             eval("(async function() {" + subTreeScripts + "})()"); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -61,6 +61,10 @@ function formatDate(date) { | |||||||
|     return padNum(date.getDate()) + ". " + padNum(date.getMonth() + 1) + ". " + date.getFullYear(); |     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) { | function formatDateTime(date) { | ||||||
|     return formatDate(date) + " " + formatTime(date); |     return formatDate(date) + " " + formatTime(date); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,6 +6,7 @@ const auth = require('../../services/auth'); | |||||||
| const wrap = require('express-promise-wrap').wrap; | const wrap = require('express-promise-wrap').wrap; | ||||||
| const log = require('../../services/log'); | const log = require('../../services/log'); | ||||||
| const sql = require('../../services/sql'); | const sql = require('../../services/sql'); | ||||||
|  | const notes = require('../../services/notes'); | ||||||
| const protected_session = require('../../services/protected_session'); | const protected_session = require('../../services/protected_session'); | ||||||
|  |  | ||||||
| router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => { | 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) => { | router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const noteId = req.params.noteId; |     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) { | async function getSubTreeScripts(parentId, includedNoteIds, dataKey) { | ||||||
|   | |||||||
| @@ -2,11 +2,18 @@ const sql = require('./sql'); | |||||||
| const options = require('./options'); | const options = require('./options'); | ||||||
| const utils = require('./utils'); | const utils = require('./utils'); | ||||||
| const notes = require('./notes'); | const notes = require('./notes'); | ||||||
| const data_encryption = require('./data_encryption'); |  | ||||||
| const sync_table = require('./sync_table'); | const sync_table = require('./sync_table'); | ||||||
| const attributes = require('./attributes'); | const attributes = require('./attributes'); | ||||||
| const protected_session = require('./protected_session'); | 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) { | async function createNewNote(parentNoteId, note, sourceId) { | ||||||
|     const noteId = utils.newNoteId(); |     const noteId = utils.newNoteId(); | ||||||
|     const noteTreeId = utils.newNoteTreeId(); |     const noteTreeId = utils.newNoteTreeId(); | ||||||
| @@ -41,8 +48,8 @@ async function createNewNote(parentNoteId, note, sourceId) { | |||||||
|             note_title: note.note_title, |             note_title: note.note_title, | ||||||
|             note_text: note.note_text ? note.note_text : '', |             note_text: note.note_text ? note.note_text : '', | ||||||
|             is_protected: note.is_protected, |             is_protected: note.is_protected, | ||||||
|             type: 'text', |             type: note.type ? note.type : 'text', | ||||||
|             mime: 'text/html', |             mime: note.mime ? note.mime : 'text/html', | ||||||
|             date_created: now, |             date_created: now, | ||||||
|             date_modified: now |             date_modified: now | ||||||
|         }); |         }); | ||||||
| @@ -277,6 +284,7 @@ async function deleteNote(noteTreeId, sourceId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|  |     getNoteById, | ||||||
|     createNewNote, |     createNewNote, | ||||||
|     updateNote, |     updateNote, | ||||||
|     deleteNote, |     deleteNote, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user