work on sandbox

This commit is contained in:
Alex Yatskov 2017-09-05 21:10:19 -07:00
parent 722ddf1638
commit 083999212e
3 changed files with 21 additions and 21 deletions

View File

@ -96,25 +96,25 @@ async function apiNoteView(noteId) {
return utilBackend().anki.guiBrowse(`nid:${noteId}`); return utilBackend().anki.guiBrowse(`nid:${noteId}`);
} }
async function apiTemplateRender(template, data) { async function apiTemplateRender(template, data, dynamic) {
return handlebarsRender(template, data); if (dynamic) {
} return new Promise((resolve, reject) => {
const sequence = utilBackend().sequenceNew();
const handler = event => {
if (event.data.sequence === sequence) {
resolve(event.data.result);
window.removeEventListener('message', handler);
}
};
async function apiTemplateRenderDynamic(template, data) { window.addEventListener('message', handler);
return new Promise((resolve, reject) => {
const sequence = utilBackend().sequenceNew();
const handler = event => {
if (event.data.sequence === sequence) {
resolve(event.data.result);
window.removeEventListener('message', handler);
}
};
window.addEventListener('message', handler); const sandbox = utilBackend().sandbox();
sandbox.postMessage({template, data, sequence, command: 'render'}, '*');
const sandbox = utilBackend().sandbox(); });
sandbox.postMessage({template, data, sequence, command: 'render'}, '*'); } else {
}); return handlebarsRender(template, data);
}
} }
async function apiCommandExec(command) { async function apiCommandExec(command) {

View File

@ -113,8 +113,8 @@ class Backend {
forward(apiNoteView(noteId), callback); forward(apiNoteView(noteId), callback);
}, },
templateRender: ({template, data, callback}) => { templateRender: ({template, data, dynamic, callback}) => {
forward(apiTemplateRender(template, data), callback); forward(apiTemplateRender(template, data, dynamic), callback);
}, },
commandExec: ({command, callback}) => { commandExec: ({command, callback}) => {

View File

@ -45,8 +45,8 @@ function apiNoteView(noteId) {
return utilInvoke('noteView', {noteId}); return utilInvoke('noteView', {noteId});
} }
function apiTemplateRender(template, data) { function apiTemplateRender(template, data, dynamic) {
return utilInvoke('templateRender', {data, template}); return utilInvoke('templateRender', {data, template, dynamic});
} }
function apiCommandExec(command) { function apiCommandExec(command) {