fix .runOnBackend() API call

This commit is contained in:
zadam 2021-05-09 21:29:59 +02:00
parent 1d008cad13
commit d8d15b528e
2 changed files with 13 additions and 11 deletions

View File

@ -51,6 +51,8 @@ export default class NoteTypeWidget extends TabAwareWidget {
() => ["file", "image", "search"].includes(note.type));
this.$noteTypeDesc.text(await this.findTypeTitle(note.type, note.mime));
this.$noteTypeButton.dropdown('hide');
}
/** actual body is rendered lazily on note-type button click */
@ -93,8 +95,6 @@ export default class NoteTypeWidget extends TabAwareWidget {
const $link = $(e.target).closest('.dropdown-item');
this.save('code', $link.attr('data-mime-type'));
this.$noteTypeButton.dropdown('hide');
});
if (this.note.type === 'code' && this.note.mime === mimeType.mime) {
@ -147,7 +147,7 @@ export default class NoteTypeWidget extends TabAwareWidget {
}
async entitiesReloadedEvent({loadResults}) {
if (loadResults.isNoteReloaded(this.noteId, this.componentId)) {
if (loadResults.isNoteReloaded(this.noteId)) {
this.refresh();
}
}

View File

@ -57,11 +57,11 @@ async function executeScript(script, params, startNoteId, currentNoteId, originE
const currentNote = becca.getNote(currentNoteId);
const originEntity = becca.getEntityFromName(originEntityName, originEntityId);
currentNote.content = `return (${script}\r\n)(${getParams(params)})`;
currentNote.type = 'code';
currentNote.mime = 'application/javascript;env=backend';
// we're just executing an excerpt of the original frontend script in the backend context so we must
// override normal note's content and it's mime type / script environment
const backendOverrideContent = `return (${script}\r\n)(${getParams(params)})`;
const bundle = getScriptBundle(currentNote);
const bundle = getScriptBundle(currentNote, true, null, [], backendOverrideContent);
return await executeBundle(bundle, { startNote, originEntity });
}
@ -102,7 +102,7 @@ function getScriptBundleForFrontend(note) {
return bundle;
}
function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = []) {
function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = [], backendOverrideContent = null) {
if (!note.isContentAvailable) {
return;
}
@ -116,10 +116,12 @@ function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds =
}
if (root) {
scriptEnv = note.getScriptEnv();
scriptEnv = !!backendOverrideContent
? 'backend'
: note.getScriptEnv();
}
if (note.type !== 'file' && scriptEnv !== note.getScriptEnv()) {
if (note.type !== 'file' && !root && scriptEnv !== note.getScriptEnv()) {
return;
}
@ -157,7 +159,7 @@ apiContext.modules['${note.noteId}'] = {};
${root ? 'return ' : ''}await ((async function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') +
modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
try {
${note.getContent()};
${backendOverrideContent || note.getContent()};
} catch (e) { throw new Error("Load of script note \\"${note.title}\\" (${note.noteId}) failed with: " + e.message); }
if (!module.exports) module.exports = {};
for (const exportKey in exports) module.exports[exportKey] = exports[exportKey];