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); return instYomi().definitionsAddable(definitions, modes);
} }
textRender(template, data) { templateRender(template, data) {
return instYomi().textRender(template, data); return instYomi().templateRender(template, data);
} }
kanjiFind(character) { kanjiFind(character) {

View File

@ -187,10 +187,7 @@ function onDictionaryPurge(e) {
return optionsLoad(); return optionsLoad();
}).then(options => { }).then(options => {
options.dictionaries = {}; options.dictionaries = {};
return optionsSave(options).then(() => { return optionsSave(options).then(populateDictionaries);
populateDictionaries(options);
instYomi().optionsSet(options);
});
}); });
} }
@ -208,7 +205,7 @@ function onDictionaryImport() {
optionsLoad().then(options => { optionsLoad().then(options => {
instDb().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => { instDb().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => {
options.dictionaries[summary.title] = {enabled: true, priority: 0}; options.dictionaries[summary.title] = {enabled: true, priority: 0};
return optionsSave(options).then(() => instYomi().optionsSet(options)); return optionsSave(options);
}).then(() => populateDictionaries(options)).catch(showDictionaryError).then(() => { }).then(() => populateDictionaries(options)).catch(showDictionaryError).then(() => {
showDictionarySpinner(false); showDictionarySpinner(false);
dictProgress.hide(); dictProgress.hide();
@ -339,7 +336,7 @@ function onAnkiModelChanged(e) {
optionsNew.anki[tabId].fields = {}; optionsNew.anki[tabId].fields = {};
populateAnkiFields(element, optionsNew).then(() => { populateAnkiFields(element, optionsNew).then(() => {
optionsSave(optionsNew).then(() => instYomi().optionsSet(optionsNew)); optionsSave(optionsNew);
}).catch(showAnkiError).then(() => showAnkiSpinner(false)); }).catch(showAnkiError).then(() => showAnkiSpinner(false));
}); });
} }
@ -351,7 +348,6 @@ function onOptionsChanged(e) {
getFormData().then(({optionsNew, optionsOld}) => { getFormData().then(({optionsNew, optionsOld}) => {
return optionsSave(optionsNew).then(() => { return optionsSave(optionsNew).then(() => {
instYomi().optionsSet(optionsNew);
updateVisibility(optionsNew); updateVisibility(optionsNew);
const ankiUpdated = 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 * Options
@ -188,6 +204,9 @@ function optionsSave(options) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
chrome.storage.sync.set(options, resolve); chrome.storage.sync.set(options, resolve);
}).then(() => {
instYomi().optionsSet(options);
fgOptionsSet(options);
}); });
} }
@ -468,7 +487,7 @@ function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
* Helpers * Helpers
*/ */
function helperKanjiLinks(options) { function handlebarsKanjiLinks(options) {
const isKanji = c => { const isKanji = c => {
const code = c.charCodeAt(0); const code = c.charCodeAt(0);
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0; return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0;
@ -486,6 +505,16 @@ function helperKanjiLinks(options) {
return result; return result;
} }
function helperMultiLine(options) { function handlebarsMultiLine(options) {
return options.fn(this).split('\n').join('<br>'); 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 { window.yomichan = new class {
constructor() { constructor() {
Handlebars.partials = Handlebars.templates; handlebarsRegister();
Handlebars.registerHelper('kanjiLinks', helperKanjiLinks);
Handlebars.registerHelper('multiLine', helperMultiLine);
this.translator = new Translator(); this.translator = new Translator();
this.anki = new AnkiNull(); this.anki = new AnkiNull();
@ -38,32 +36,22 @@ window.yomichan = new class {
optionsSet(options) { optionsSet(options) {
this.options = options; this.options = options;
let usable = false; let configured = false;
for (const title in options.dictionaries) { for (const title in options.dictionaries) {
if (options.dictionaries[title].enabled) { if (options.dictionaries[title].enabled) {
usable = true; configured = true;
break; break;
} }
} }
chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'}); chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'});
chrome.browserAction.setBadgeText({text: usable ? '' : '!'}); chrome.browserAction.setBadgeText({text: configured ? '' : '!'});
if (options.anki.enable) { if (options.anki.enable) {
this.anki = new AnkiConnect(this.options.anki.server); this.anki = new AnkiConnect(this.options.anki.server);
} else { } else {
this.anki = new AnkiNull(); 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) { noteFormat(definition, mode) {
@ -155,8 +143,8 @@ window.yomichan = new class {
}); });
} }
textRender(template, data) { templateRender(template, data) {
return Promise.resolve(Handlebars.templates[template](data)); return Promise.resolve(handlebarsRender(template, data));
} }
onInstalled(details) { onInstalled(details) {
@ -179,8 +167,8 @@ window.yomichan = new class {
promiseCallback(this.termsFind(text), callback); promiseCallback(this.termsFind(text), callback);
} }
api_textRender({template, data, callback}) { api_templateRender({template, data, callback}) {
promiseCallback(this.textRender(template, data), callback); promiseCallback(this.templateRender(template, data), callback);
} }
api_definitionAdd({definition, mode, callback}) { api_definitionAdd({definition, mode, callback}) {

View File

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

View File

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

View File

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