work on sandbox

This commit is contained in:
Alex Yatskov 2017-09-05 20:35:39 -07:00
parent 9eac50ea05
commit 722ddf1638
4 changed files with 29 additions and 3 deletions

View File

@ -25,6 +25,6 @@
<script src="/bg/js/backend.js"></script> <script src="/bg/js/backend.js"></script>
<iframe src="/sb/sandbox.html"></iframe> <iframe src="/sb/sandbox.html" id="sandbox"></iframe>
</body> </body>
</html> </html>

View File

@ -100,6 +100,23 @@ async function apiTemplateRender(template, data) {
return handlebarsRender(template, data); return handlebarsRender(template, data);
} }
async function apiTemplateRenderDynamic(template, data) {
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'}, '*');
});
}
async function apiCommandExec(command) { async function apiCommandExec(command) {
const handlers = { const handlers = {
search: () => { search: () => {

View File

@ -22,6 +22,7 @@ class Backend {
this.translator = new Translator(); this.translator = new Translator();
this.anki = new AnkiNull(); this.anki = new AnkiNull();
this.options = null; this.options = null;
this.sequence = 0;
} }
async prepare() { async prepare() {
@ -36,6 +37,14 @@ class Backend {
} }
} }
sequenceNew() {
return this.sequence++;
}
sandbox() {
return document.getElementById('sandbox').contentWindow;
}
onOptionsUpdated(options) { onOptionsUpdated(options) {
this.options = utilIsolate(options); this.options = utilIsolate(options);

View File

@ -24,7 +24,7 @@
window.addEventListener('message', event => { window.addEventListener('message', event => {
if (event.data.command === 'render') { if (event.data.command === 'render') {
const template = Handlebars.compile(event.data.template || ''); const template = Handlebars.compile(event.data.template || '');
const result = template(event.data.context || {}); const result = template(event.data.data || {});
event.source.postMessage({result, sequence: event.data.sequence}); event.source.postMessage({result, sequence: event.data.sequence}, '*');
} }
}); });