fix render

This commit is contained in:
azivner 2018-03-04 21:05:14 -05:00
parent 3b9d1df05c
commit ddce5c959e
4 changed files with 26 additions and 14 deletions

View File

@ -28,6 +28,10 @@ class Note extends Entity {
&& (this.mime === "application/javascript" || this.mime === "application/x-javascript"); && (this.mime === "application/javascript" || this.mime === "application/x-javascript");
} }
isHtml() {
return (this.type === "code" || this.type === "file") && this.mime === "text/html";
}
async getAttributes() { async getAttributes() {
return this.repository.getEntities("SELECT * FROM attributes WHERE noteId = ? AND isDeleted = 0", [this.noteId]); 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) JOIN notes USING(noteId)
WHERE notes.isDeleted = 0 WHERE notes.isDeleted = 0
AND note_tree.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() { async getParents() {

View File

@ -217,9 +217,9 @@ const noteEditor = (function() {
if (currentNote.detail.type === 'render') { if (currentNote.detail.type === 'render') {
$noteDetailRender.show(); $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') { else if (currentNote.detail.type === 'file') {
$noteDetailAttachment.show(); $noteDetailAttachment.show();

View File

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

View File

@ -24,7 +24,7 @@ async function executeNote(note) {
} }
async function executeScript(dataKey, script, params) { async function executeScript(dataKey, script, params) {
const ctx = new ScriptContext(dataKey); const ctx = new ScriptContext(dataKey, null, []);
const paramsStr = getParams(params); const paramsStr = getParams(params);
return await sql.doInTransaction(async () => execute(ctx, script, paramsStr)); return await sql.doInTransaction(async () => execute(ctx, script, paramsStr));
@ -95,20 +95,21 @@ function getParams(params) {
} }
async function getRenderScript(note) { 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 `<script type="text/javascript">(async function() {\r\nconst api = Api();\r\n${bundle.script}\r\n})();</script>`
return "async function() {" + subTreeScripts + note.content + "\r\n}"; + bundle.html;
} }
async function getScriptBundle(note, includedNoteIds = []) { async function getScriptBundle(note, includedNoteIds = []) {
if (!note.isJavaScript()) { if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') {
return; return;
} }
const bundle = { const bundle = {
note: note, note: note,
script: '', script: '',
html: '',
allNotes: [note] allNotes: [note]
}; };
@ -126,18 +127,24 @@ async function getScriptBundle(note, includedNoteIds = []) {
if (childBundle) { if (childBundle) {
modules.push(childBundle.note); modules.push(childBundle.note);
bundle.script += childBundle.script; bundle.script += childBundle.script;
bundle.html += childBundle.html;
bundle.allNotes = bundle.allNotes.concat(childBundle.allNotes); bundle.allNotes = bundle.allNotes.concat(childBundle.allNotes);
} }
} }
if (note.isJavaScript()) {
bundle.script += ` bundle.script += `
api.__modules['${note.noteId}'] = {}; api.__modules['${note.noteId}'] = {};
await (async function(module, api, startNote, currentNote` + (modules.length > 0 ? ', ' : '') + await (async function(exports, module, api, startNote, currentNote` + (modules.length > 0 ? ', ' : '') +
modules.map(child => child.title).join(', ') + `) { modules.map(child => child.title).join(', ') + `) {
${note.content} ${note.content}
})(api.__modules['${note.noteId}'], api, api.__startNote, api.__notes['${note.noteId}']` + (modules.length > 0 ? ', ' : '') + })({}, api.__modules['${note.noteId}'], api, api.__startNote, api.__notes['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
modules.map(mod => `api.__modules['${mod.noteId}'].exports`).join(', ') + `); modules.map(mod => `api.__modules['${mod.noteId}'].exports`).join(', ') + `);
`; `;
}
else if (note.isHtml()) {
bundle.html += note.content;
}
return bundle; return bundle;
} }