fixed render notes

This commit is contained in:
azivner 2018-03-07 00:17:18 -05:00
parent 766a567a32
commit 8e95b080da
7 changed files with 20 additions and 30 deletions

View File

@ -37,6 +37,10 @@ class Note extends Entity {
return "frontend"; return "frontend";
} }
if (this.type === 'render') {
return "frontend";
}
if (this.isJavaScript() && this.mime.endsWith('env=backend')) { if (this.isJavaScript() && this.mime.endsWith('env=backend')) {
return "backend"; return "backend";
} }

View File

@ -1,12 +1,12 @@
function ApiContext(startNote, allNotes) { function ScriptContext(startNote, allNotes) {
return { return {
modules: {}, modules: {},
notes: toObject(allNotes, note => [note.noteId, note]), notes: toObject(allNotes, note => [note.noteId, note]),
apis: toObject(allNotes, note => [note.noteId, Api(startNote, note)]), apis: toObject(allNotes, note => [note.noteId, ScriptApi(startNote, note)]),
}; };
} }
function Api(startNote, currentNote) { function ScriptApi(startNote, currentNote) {
const $pluginButtons = $("#plugin-buttons"); const $pluginButtons = $("#plugin-buttons");
async function activateNote(notePath) { async function activateNote(notePath) {

View File

@ -217,9 +217,11 @@ const noteEditor = (function() {
if (currentNote.detail.type === 'render') { if (currentNote.detail.type === 'render') {
$noteDetailRender.show(); $noteDetailRender.show();
const html = await server.get('script/render/' + getCurrentNoteId()); const bundle = await server.get('script/bundle/' + getCurrentNoteId());
$noteDetailRender.html(html); $noteDetailRender.html(bundle.html);
executeBundle(bundle);
} }
else if (currentNote.detail.type === 'file') { else if (currentNote.detail.type === 'file') {
$noteDetailAttachment.show(); $noteDetailAttachment.show();

View File

@ -116,9 +116,7 @@ async function stopWatch(what, func) {
} }
async function executeBundle(bundle) { async function executeBundle(bundle) {
const apiContext = ApiContext(bundle.note, bundle.allNotes); const apiContext = ScriptContext(bundle.note, bundle.allNotes);
console.log((`const apiContext = this; (async function() { ${bundle.script}\r\n})()`));
return await (function() { return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`); }.call(apiContext)); return await (function() { return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`); }.call(apiContext));
} }

View File

@ -50,12 +50,4 @@ router.get('/bundle/:noteId', auth.checkApiAuth, wrap(async (req, res, next) =>
res.send(bundle); res.send(bundle);
})); }));
router.get('/render/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
const repository = new Repository(req);
const note = await repository.getNote(req.params.noteId);
const bundle = await script.getRenderScript(note);
res.send(bundle);
}));
module.exports = router; module.exports = router;

View File

@ -8,6 +8,7 @@ async function runNotesWithAttribute(runAttrValue) {
SELECT notes.* SELECT notes.*
FROM notes FROM notes
JOIN attributes ON attributes.noteId = notes.noteId JOIN attributes ON attributes.noteId = notes.noteId
AND attributes.isDeleted = 0
AND attributes.name = 'run' AND attributes.name = 'run'
AND attributes.value = ? AND attributes.value = ?
WHERE WHERE

View File

@ -40,7 +40,8 @@ async function executeScript(dataKey, script, params, startNoteId, currentNoteId
const startNote = await repository.getNote(startNoteId); const startNote = await repository.getNote(startNoteId);
const currentNote = await repository.getNote(currentNoteId); const currentNote = await repository.getNote(currentNoteId);
currentNote.content = `(${script}\r\n)(${getParams(params)})`; currentNote.content = `return await (${script}\r\n)(${getParams(params)})`;
currentNote.type = 'code';
currentNote.mime = 'application/javascript;env=backend'; currentNote.mime = 'application/javascript;env=backend';
const bundle = await getScriptBundle(currentNote); const bundle = await getScriptBundle(currentNote);
@ -67,14 +68,7 @@ function getParams(params) {
}).join(","); }).join(",");
} }
async function getRenderScript(note) { async function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = []) {
const bundle = await getScriptBundle(note);
return `<script type="text/javascript">(async function() {\r\nconst api = Api();\r\n${bundle.script}\r\n})();</script>`
+ bundle.html;
}
async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') { if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') {
return; return;
} }
@ -83,7 +77,7 @@ async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
return; return;
} }
if (!scriptEnv) { if (root) {
scriptEnv = note.getScriptEnv(); scriptEnv = note.getScriptEnv();
} }
@ -107,7 +101,7 @@ async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
const modules = []; const modules = [];
for (const child of await note.getChildren()) { for (const child of await note.getChildren()) {
const childBundle = await getScriptBundle(child, scriptEnv, includedNoteIds); const childBundle = await getScriptBundle(child, false, scriptEnv, includedNoteIds);
if (childBundle) { if (childBundle) {
modules.push(childBundle.note); modules.push(childBundle.note);
@ -120,7 +114,7 @@ async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
if (note.isJavaScript()) { if (note.isJavaScript()) {
bundle.script += ` bundle.script += `
apiContext.modules['${note.noteId}'] = {}; apiContext.modules['${note.noteId}'] = {};
await (async function(exports, module, api` + (modules.length > 0 ? ', ' : '') + ${root ? 'return ' : ''}await (async function(exports, module, api` + (modules.length > 0 ? ', ' : '') +
modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) { modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
${note.content} ${note.content}
})({}, apiContext.modules['${note.noteId}'], apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') + })({}, apiContext.modules['${note.noteId}'], apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
@ -141,6 +135,5 @@ function sanitizeVariableName(str) {
module.exports = { module.exports = {
executeNote, executeNote,
executeScript, executeScript,
getScriptBundle, getScriptBundle
getRenderScript
}; };