From ddce5c959e2671bbeaf8d5e1c5cb0925019c0167 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 4 Mar 2018 21:05:14 -0500 Subject: [PATCH] fix render --- src/entities/note.js | 7 ++++++- src/public/javascripts/note_editor.js | 4 ++-- src/routes/api/script.js | 2 +- src/services/script.js | 27 +++++++++++++++++---------- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/entities/note.js b/src/entities/note.js index cd20e222a..f0a18a00e 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -28,6 +28,10 @@ class Note extends Entity { && (this.mime === "application/javascript" || this.mime === "application/x-javascript"); } + isHtml() { + return (this.type === "code" || this.type === "file") && this.mime === "text/html"; + } + async getAttributes() { return this.repository.getEntities("SELECT * FROM attributes WHERE noteId = ? AND isDeleted = 0", [this.noteId]); } @@ -73,7 +77,8 @@ class Note extends Entity { JOIN notes USING(noteId) WHERE notes.isDeleted = 0 AND note_tree.isDeleted = 0 - AND note_tree.parentNoteId = ?`, [this.noteId]); + AND note_tree.parentNoteId = ? + ORDER BY note_tree.notePosition`, [this.noteId]); } async getParents() { diff --git a/src/public/javascripts/note_editor.js b/src/public/javascripts/note_editor.js index eca919fb7..57a93eb2e 100644 --- a/src/public/javascripts/note_editor.js +++ b/src/public/javascripts/note_editor.js @@ -217,9 +217,9 @@ const noteEditor = (function() { if (currentNote.detail.type === 'render') { $noteDetailRender.show(); - const subTree = await server.get('script/render/' + getCurrentNoteId()); + const html = await server.get('script/render/' + getCurrentNoteId()); - $noteDetailRender.html(subTree); + $noteDetailRender.html(html); } else if (currentNote.detail.type === 'file') { $noteDetailAttachment.show(); diff --git a/src/routes/api/script.js b/src/routes/api/script.js index 9519af69f..7655c4deb 100644 --- a/src/routes/api/script.js +++ b/src/routes/api/script.js @@ -49,7 +49,7 @@ router.get('/render/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => const repository = new Repository(req); const note = await repository.getNote(req.params.noteId); - res.send(await script.getRenderScript(note, repository)); + res.send(await script.getRenderScript(note)); })); module.exports = router; \ No newline at end of file diff --git a/src/services/script.js b/src/services/script.js index c78571eec..774cb925e 100644 --- a/src/services/script.js +++ b/src/services/script.js @@ -24,7 +24,7 @@ async function executeNote(note) { } async function executeScript(dataKey, script, params) { - const ctx = new ScriptContext(dataKey); + const ctx = new ScriptContext(dataKey, null, []); const paramsStr = getParams(params); return await sql.doInTransaction(async () => execute(ctx, script, paramsStr)); @@ -95,20 +95,21 @@ function getParams(params) { } async function getRenderScript(note) { - const subTreeScripts = await getModules(note, [note.noteId]); + const bundle = await getScriptBundle(note); - // last \r\n is necessary if script contains line comment on its last line - return "async function() {" + subTreeScripts + note.content + "\r\n}"; + return `` + + bundle.html; } async function getScriptBundle(note, includedNoteIds = []) { - if (!note.isJavaScript()) { + if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') { return; } const bundle = { note: note, script: '', + html: '', allNotes: [note] }; @@ -126,18 +127,24 @@ async function getScriptBundle(note, includedNoteIds = []) { if (childBundle) { modules.push(childBundle.note); bundle.script += childBundle.script; + bundle.html += childBundle.html; bundle.allNotes = bundle.allNotes.concat(childBundle.allNotes); } } - bundle.script += ` + if (note.isJavaScript()) { + bundle.script += ` api.__modules['${note.noteId}'] = {}; -await (async function(module, api, startNote, currentNote` + (modules.length > 0 ? ', ' : '') + - modules.map(child => child.title).join(', ') + `) { +await (async function(exports, module, api, startNote, currentNote` + (modules.length > 0 ? ', ' : '') + + modules.map(child => child.title).join(', ') + `) { ${note.content} -})(api.__modules['${note.noteId}'], api, api.__startNote, api.__notes['${note.noteId}']` + (modules.length > 0 ? ', ' : '') + - modules.map(mod => `api.__modules['${mod.noteId}'].exports`).join(', ') + `); +})({}, api.__modules['${note.noteId}'], api, api.__startNote, api.__notes['${note.noteId}']` + (modules.length > 0 ? ', ' : '') + + modules.map(mod => `api.__modules['${mod.noteId}'].exports`).join(', ') + `); `; + } + else if (note.isHtml()) { + bundle.html += note.content; + } return bundle; }