scrap sandbox

This commit is contained in:
Alex Yatskov 2017-09-09 12:59:49 -07:00
parent 9a5a9aa47f
commit 5f46006e8d
9 changed files with 51 additions and 118 deletions

View File

@ -24,7 +24,5 @@
<script src="/mixed/js/japanese.js"></script>
<script src="/bg/js/backend.js"></script>
<iframe src="/sb/sandbox.html" id="sandbox"></iframe>
</body>
</html>

View File

@ -100,27 +100,9 @@ async function apiNoteView(noteId) {
async function apiTemplateRender(template, data, dynamic) {
if (dynamic) {
return new Promise((resolve, reject) => {
const sequence = utilBackend().sequenceNew();
const handler = event => {
if (event.data.sequence === sequence) {
if (event.data.command === 'error') {
reject(event.data.result);
return handlebarsRenderDynamic(template, data);
} else {
resolve(event.data.result);
}
window.removeEventListener('message', handler);
}
};
window.addEventListener('message', handler);
const sandbox = utilBackend().sandbox();
sandbox.postMessage({template, data, sequence, command: 'render'}, '*');
});
} else {
return handlebarsRender(template, data);
return handlebarsRenderStatic(template, data);
}
}

View File

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

View File

@ -75,7 +75,7 @@ function handlebarsMultiLine(options) {
return options.fn(this).split('\n').join('<br>');
}
function handlebarsRender(template, data) {
function handlebarsRegisterHelpers() {
if (Handlebars.partials !== Handlebars.templates) {
Handlebars.partials = Handlebars.templates;
Handlebars.registerHelper('dumpObject', handlebarsDumpObject);
@ -84,6 +84,21 @@ function handlebarsRender(template, data) {
Handlebars.registerHelper('kanjiLinks', handlebarsKanjiLinks);
Handlebars.registerHelper('multiLine', handlebarsMultiLine);
}
return Handlebars.templates[template](data).trim();
}
function handlebarsRenderStatic(name, data) {
handlebarsRegisterHelpers();
return Handlebars.templates[name](data).trim();
}
function handlebarsRenderDynamic(template, data) {
handlebarsRegisterHelpers();
Handlebars.yomichan_cache = Handlebars.yomichan_cache || {};
let instance = Handlebars.yomichan_cache[template];
if (!instance) {
instance = Handlebars.yomichan_cache[template] = Handlebars.compile(template);
}
return instance(data).trim();
}

View File

@ -218,7 +218,7 @@ async function dictionaryGroupsPopulate(options) {
for (const dictRow of dictRowsSort(dictRows, options)) {
const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0};
const dictHtml = handlebarsRender('dictionary.html', {
const dictHtml = await apiTemplateRender('dictionary.html', {
title: dictRow.title,
version: dictRow.version,
revision: dictRow.revision,

View File

@ -22,7 +22,7 @@
#field-templates {
font-family: monospace;
overflow-x: hidden;
white-space: nowrap;
white-space: pre;
}
</style>
</head>
@ -195,11 +195,6 @@
<input type="text" id="interface-server" class="form-control">
</div>
<div class="form-group options-advanced">
<label for="field-templates">Field templates (<a href="#" id="field-templates-reset">reset to defaults</a>)</label>
<textarea class="form-control" rows="10" id="field-templates"></textarea>
</div>
<div id="anki-format">
<p class="help-block">
Specify the information you would like included in your flashcards in the field editor below.
@ -252,6 +247,17 @@
</table>
</div>
</div>
<div class="options-advanced">
<p class="help-block">
Fields are formatted using the <a href="http://handlebarsjs.com/">Handlebars.js</a> template rendering
engine. Advanced users can modify these templates for ultimate control of what information gets included in
their Anki cards. If you encounter problems with your changes you can always <a href="#" id="field-templates-reset">reset to default</a>
template settings.
</p>
<textarea class="form-control" rows="10" id="field-templates"></textarea>
</div>
</div>
</div>
</div>
@ -272,7 +278,6 @@
</div>
<pre id="debug"></pre>
</div>
<div class="pull-right">
<small><a href="https://foosoft.net/projects/yomichan/" target="_blank">Homepage</a> &bull; <a href="legal.html">Legal</a></small>

View File

@ -49,14 +49,12 @@
}
},
"web_accessible_resources": ["fg/float.html"],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"applications": {
"gecko": {
"id": "yomichan-live@foosoft.net",
"strict_min_version": "52.0",
"update_url": "https://foosoft.net/projects/yomichan/dl/updates.json"
}
},
"sandbox": {
"pages": ["sb/sandbox.html"]
}
}

View File

@ -1,46 +0,0 @@
/*
* Copyright (c) 2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
window.addEventListener('message', event => {
if (event.data.command === 'render') {
window.yomichan_cache = window.yomichan_cache || {};
let template = window.yomichan_cache[event.data.template];
if (!template) {
template = Handlebars.compile(event.data.template || '');
window.yomichan_cache[event.data.template] = template;
}
let result = null;
let command = null;
try {
command = 'render';
result = template(event.data.data || {});
} catch (e) {
command = 'error';
result = e;
}
event.source.postMessage({result, command, sequence: event.data.sequence}, '*');
}
});

View File

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<script src="/mixed/lib/handlebars.min.js"></script>
<script src="/sb/js/sandbox.js"></script>
</body>
</html>