startNote/currentNote is now accessible on frontend as well

This commit is contained in:
azivner 2018-03-04 23:28:26 -05:00
parent 1c6fc9029f
commit 61c2456cf6
6 changed files with 38 additions and 21 deletions

View File

@ -1,4 +1,4 @@
function Api() { function Api(startNote, allNotes) {
const $pluginButtons = $("#plugin-buttons"); const $pluginButtons = $("#plugin-buttons");
async function activateNote(notePath) { async function activateNote(notePath) {
@ -33,14 +33,19 @@ function Api() {
script = script.toString(); script = script.toString();
} }
const ret = await server.post('script/exec', { script: script, params: prepareParams(params) }); const ret = await server.post('script/exec', {
script: script,
params: prepareParams(params),
startNoteId: startNote.noteId
});
return ret.executionResult; return ret.executionResult;
} }
return { return {
__startNote: startNote,
__notes: toObject(allNotes, note => [note.noteId, note]),
__modules: {}, __modules: {},
__notes: {},
addButtonToToolbar, addButtonToToolbar,
activateNote, activateNote,
getInstanceName: noteTree.getInstanceName, getInstanceName: noteTree.getInstanceName,

View File

@ -203,7 +203,7 @@ $("#logout-button").toggle(!isElectron());
$(document).ready(() => { $(document).ready(() => {
server.get("script/startup").then(scripts => { server.get("script/startup").then(scripts => {
for (const script of scripts) { for (const script of scripts) {
executeScript(script); executeBundle(script);
} }
}); });
}); });

View File

@ -298,9 +298,9 @@ const noteEditor = (function() {
// make sure note is saved so we load latest changes // make sure note is saved so we load latest changes
await saveNoteIfChanged(); await saveNoteIfChanged();
const script = await server.get('script/subtree/' + getCurrentNoteId()); const bundle = await server.get('script/subtree/' + getCurrentNoteId());
executeScript(script); executeBundle(bundle);
} }
} }

View File

@ -115,15 +115,10 @@ async function stopWatch(what, func) {
return ret; return ret;
} }
function executeScript(script) { async function executeBundle(bundle) {
const completeScript = ` const api = Api(bundle.note, bundle.allNotes);
(async function() {
const api = Api();
${script}
})();`;
eval(completeScript); return await (function() { return eval(`const api = this; (async function() { ${bundle.script}\r\n})()`); }.call(api));
} }
function formatValueWithWhitespace(val) { function formatValueWithWhitespace(val) {
@ -212,4 +207,16 @@ function download(url) {
else { else {
window.location.href = url; window.location.href = url;
} }
}
function toObject(array, fn) {
const obj = {};
for (const item of array) {
const ret = fn(item);
obj[ret[0]] = ret[1];
}
return obj;
} }

View File

@ -9,7 +9,7 @@ const script = require('../../services/script');
const Repository = require('../../services/repository'); const Repository = require('../../services/repository');
router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => { router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => {
const ret = await script.executeScript(req, req.body.script, req.body.params); const ret = await script.executeScript(req, req.body.script, req.body.params, req.body.startNoteId);
res.send({ res.send({
executionResult: ret executionResult: ret
@ -25,7 +25,7 @@ router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => {
for (const note of notes) { for (const note of notes) {
const bundle = await script.getScriptBundle(note); const bundle = await script.getScriptBundle(note);
scripts.push(bundle.script); scripts.push(bundle);
} }
res.send(scripts); res.send(scripts);
@ -36,14 +36,15 @@ router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) =>
const note = await repository.getNote(req.params.noteId); const note = await repository.getNote(req.params.noteId);
const bundle = await script.getScriptBundle(note); const bundle = await script.getScriptBundle(note);
res.send(bundle.script); res.send(bundle);
})); }));
router.get('/render/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { 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);
const bundle = await script.getRenderScript(note);
res.send(await script.getRenderScript(note)); res.send(bundle);
})); }));
module.exports = router; module.exports = router;

View File

@ -1,5 +1,6 @@
const sql = require('./sql'); const sql = require('./sql');
const ScriptContext = require('./script_context'); const ScriptContext = require('./script_context');
const Repository = require('./repository');
async function executeNote(note) { async function executeNote(note) {
if (note.isProtected || !note.isJavaScript()) { if (note.isProtected || !note.isJavaScript()) {
@ -23,15 +24,18 @@ async function executeNote(note) {
} }
} }
async function executeScript(dataKey, script, params) { async function executeScript(dataKey, script, params, startNoteId) {
const ctx = new ScriptContext(dataKey, null, []); const repository = new Repository(dataKey);
const startNote = await repository.getNote(startNoteId);
const ctx = new ScriptContext(dataKey, startNote, []);
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));
} }
async function execute(ctx, script, paramsStr) { async function execute(ctx, script, paramsStr) {
return await (function() { return eval(`const api = this;\r\n(${script})(${paramsStr})`); }.call(ctx)); return await (function() { return eval(`const api = this;const startNote = api.__startNote;\r\n(${script}\r\n)(${paramsStr})`); }.call(ctx));
} }
function getParams(params) { function getParams(params) {