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");
}
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() {

View file

@ -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();

View file

@ -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;

View file

@ -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 `<script type="text/javascript">(async function() {\r\nconst api = Api();\r\n${bundle.script}\r\n})();</script>`
+ 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;
}