This commit is contained in:
Alex Yatskov 2017-03-04 19:16:19 -08:00
parent 30999c13d3
commit db7e80dabf
7 changed files with 52 additions and 39 deletions

View File

@ -31,8 +31,8 @@ window.displayWindow = new class extends Display {
return instYomi().definitionsAddable(definitions, modes);
}
textRender(template, data) {
return instYomi().textRender(template, data);
templateRender(template, data) {
return instYomi().templateRender(template, data);
}
kanjiFind(character) {

View File

@ -187,10 +187,7 @@ function onDictionaryPurge(e) {
return optionsLoad();
}).then(options => {
options.dictionaries = {};
return optionsSave(options).then(() => {
populateDictionaries(options);
instYomi().optionsSet(options);
});
return optionsSave(options).then(populateDictionaries);
});
}
@ -208,7 +205,7 @@ function onDictionaryImport() {
optionsLoad().then(options => {
instDb().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => {
options.dictionaries[summary.title] = {enabled: true, priority: 0};
return optionsSave(options).then(() => instYomi().optionsSet(options));
return optionsSave(options);
}).then(() => populateDictionaries(options)).catch(showDictionaryError).then(() => {
showDictionarySpinner(false);
dictProgress.hide();
@ -339,7 +336,7 @@ function onAnkiModelChanged(e) {
optionsNew.anki[tabId].fields = {};
populateAnkiFields(element, optionsNew).then(() => {
optionsSave(optionsNew).then(() => instYomi().optionsSet(optionsNew));
optionsSave(optionsNew);
}).catch(showAnkiError).then(() => showAnkiSpinner(false));
});
}
@ -351,7 +348,6 @@ function onOptionsChanged(e) {
getFormData().then(({optionsNew, optionsOld}) => {
return optionsSave(optionsNew).then(() => {
instYomi().optionsSet(optionsNew);
updateVisibility(optionsNew);
const ankiUpdated =

View File

@ -50,6 +50,22 @@ function instAnki() {
}
/*
* Foreground
*/
function fgBroadcast(action, params) {
chrome.tabs.query({}, tabs => {
for (const tab of tabs) {
chrome.tabs.sendMessage(tab.id, {action, params}, () => null);
}
});
}
function fgOptionsSet(options) {
fgBroadcast('optionsSet', options);
}
/*
* Options
@ -188,6 +204,9 @@ function optionsSave(options) {
return new Promise((resolve, reject) => {
chrome.storage.sync.set(options, resolve);
}).then(() => {
instYomi().optionsSet(options);
fgOptionsSet(options);
});
}
@ -468,7 +487,7 @@ function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
* Helpers
*/
function helperKanjiLinks(options) {
function handlebarsKanjiLinks(options) {
const isKanji = c => {
const code = c.charCodeAt(0);
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0;
@ -486,6 +505,16 @@ function helperKanjiLinks(options) {
return result;
}
function helperMultiLine(options) {
function handlebarsMultiLine(options) {
return options.fn(this).split('\n').join('<br>');
}
function handlebarsRegister() {
Handlebars.partials = Handlebars.templates;
Handlebars.registerHelper('kanjiLinks', handlebarsKanjiLinks);
Handlebars.registerHelper('multiLine', handlebarsMultiLine);
}
function handlebarsRender(template, data) {
return Handlebars.templates[template](data);
}

View File

@ -19,9 +19,7 @@
window.yomichan = new class {
constructor() {
Handlebars.partials = Handlebars.templates;
Handlebars.registerHelper('kanjiLinks', helperKanjiLinks);
Handlebars.registerHelper('multiLine', helperMultiLine);
handlebarsRegister();
this.translator = new Translator();
this.anki = new AnkiNull();
@ -38,32 +36,22 @@ window.yomichan = new class {
optionsSet(options) {
this.options = options;
let usable = false;
let configured = false;
for (const title in options.dictionaries) {
if (options.dictionaries[title].enabled) {
usable = true;
configured = true;
break;
}
}
chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'});
chrome.browserAction.setBadgeText({text: usable ? '' : '!'});
chrome.browserAction.setBadgeText({text: configured ? '' : '!'});
if (options.anki.enable) {
this.anki = new AnkiConnect(this.options.anki.server);
} else {
this.anki = new AnkiNull();
}
this.tabInvokeAll('optionsSet', this.options);
}
tabInvokeAll(action, params) {
chrome.tabs.query({}, tabs => {
for (const tab of tabs) {
chrome.tabs.sendMessage(tab.id, {action, params}, () => null);
}
});
}
noteFormat(definition, mode) {
@ -155,8 +143,8 @@ window.yomichan = new class {
});
}
textRender(template, data) {
return Promise.resolve(Handlebars.templates[template](data));
templateRender(template, data) {
return Promise.resolve(handlebarsRender(template, data));
}
onInstalled(details) {
@ -179,8 +167,8 @@ window.yomichan = new class {
promiseCallback(this.termsFind(text), callback);
}
api_textRender({template, data, callback}) {
promiseCallback(this.textRender(template, data), callback);
api_templateRender({template, data, callback}) {
promiseCallback(this.templateRender(template, data), callback);
}
api_definitionAdd({definition, mode, callback}) {

View File

@ -31,8 +31,8 @@ window.displayFrame = new class extends Display {
return bgDefinitionsAddable(definitions, modes);
}
textRender(template, data) {
return bgTextRender(template, data);
templateRender(template, data) {
return bgTemplateRender(template, data);
}
kanjiFind(character) {

View File

@ -50,8 +50,8 @@ function bgKanjiFind(text) {
return bgInvoke('kanjiFind', {text});
}
function bgTextRender(template, data) {
return bgInvoke('textRender', {data, template});
function bgTemplateRender(template, data) {
return bgInvoke('templateRender', {data, template});
}
function bgDefinitionsAddable(definitions, modes) {

View File

@ -34,7 +34,7 @@ class Display {
throw 'override me';
}
textRender(template, data) {
templateRender(template, data) {
throw 'override me';
}
@ -64,7 +64,7 @@ class Display {
this.definitions = definitions;
this.spinner.hide();
this.textRender('terms.html', params).then(content => {
this.templateRender('terms.html', params).then(content => {
this.container.html(content);
$('.action-add-note').click(this.onActionAddNote.bind(this));
$('.action-play-audio').click(this.onActionPlayAudio.bind(this));
@ -90,7 +90,7 @@ class Display {
this.definitions = definitions;
this.spinner.hide();
this.textRender('kanji.html', params).then(content => {
this.templateRender('kanji.html', params).then(content => {
this.container.html(content);
$('.action-add-note').click(this.onActionAddNote.bind(this));
return this.adderButtonsUpdate(['kanji'], sequence);