This commit is contained in:
Alex Yatskov 2017-01-13 21:06:33 -08:00
parent 11fcc465ac
commit 8fe82573cc
6 changed files with 78 additions and 78 deletions

View File

@ -25,29 +25,29 @@ function yomichan() {
} }
function getFormValues() { function getFormValues() {
return loadOptions().then(optsOld => { return optionsLoad().then(optsOld => {
const optsNew = $.extend({}, optsOld); const optsNew = $.extend({}, optsOld);
optsNew.activateOnStartup = $('#activate-on-startup').prop('checked'); optsNew.general.autoStart = $('#activate-on-startup').prop('checked');
optsNew.enableAudioPlayback = $('#enable-audio-playback').prop('checked'); optsNew.general.audioPlayback = $('#enable-audio-playback').prop('checked');
optsNew.enableSoftKatakanaSearch = $('#enable-soft-katakana-search').prop('checked'); optsNew.general.softKatakana = $('#enable-soft-katakana-search').prop('checked');
optsNew.groupTermResults = $('#group-term-results').prop('checked'); optsNew.general.groupResults = $('#group-term-results').prop('checked');
optsNew.showAdvancedOptions = $('#show-advanced-options').prop('checked'); optsNew.general.showAdvanced = $('#show-advanced-options').prop('checked');
optsNew.holdShiftToScan = $('#hold-shift-to-scan').prop('checked'); optsNew.scanning.requireShift = $('#hold-shift-to-scan').prop('checked');
optsNew.selectMatchedText = $('#select-matched-text').prop('checked'); optsNew.scanning.selectText = $('#select-matched-text').prop('checked');
optsNew.scanDelay = parseInt($('#scan-delay').val(), 10); optsNew.scanning.delay = parseInt($('#scan-delay').val(), 10);
optsNew.scanLength = parseInt($('#scan-length').val(), 10); optsNew.scanning.length = parseInt($('#scan-length').val(), 10);
optsNew.ankiEnable = $('#anki-enable').prop('checked'); optsNew.anki.enable = $('#anki-enable').prop('checked');
optsNew.ankiCardTags = $('#anki-card-tags').val().split(/[,; ]+/); optsNew.anki.tags = $('#anki-card-tags').val().split(/[,; ]+/);
optsNew.sentenceExtent = parseInt($('#sentence-extent').val(), 10); optsNew.anki.sentenceExt = parseInt($('#sentence-extent').val(), 10);
optsNew.ankiTermDeck = $('#anki-term-deck').val(); optsNew.anki.terms.deck = $('#anki-term-deck').val();
optsNew.ankiTermModel = $('#anki-term-model').val(); optsNew.anki.terms.model = $('#anki-term-model').val();
optsNew.ankiTermFields = fieldsToDict($('#term .anki-field-value')); optsNew.anki.terms.fields = fieldsToDict($('#term .anki-field-value'));
optsNew.ankiKanjiDeck = $('#anki-kanji-deck').val(); optsNew.anki.kanji.deck = $('#anki-kanji-deck').val();
optsNew.ankiKanjiModel = $('#anki-kanji-model').val(); optsNew.anki.kanji.model = $('#anki-kanji-model').val();
optsNew.ankiKanjiFields = fieldsToDict($('#kanji .anki-field-value')); optsNew.anki.kanji.fields = fieldsToDict($('#kanji .anki-field-value'));
$('.dict-group').each((index, element) => { $('.dict-group').each((index, element) => {
const dictionary = $(element); const dictionary = $(element);
@ -65,13 +65,13 @@ function getFormValues() {
} }
function updateVisibility(opts) { function updateVisibility(opts) {
if (opts.ankiEnable) { if (opts.anki.enable) {
$('#anki-general').show(); $('#anki-general').show();
} else { } else {
$('#anki-general').hide(); $('#anki-general').hide();
} }
if (opts.showAdvancedOptions) { if (opts.general.showAdvanced) {
$('.options-advanced').show(); $('.options-advanced').show();
} else { } else {
$('.options-advanced').hide(); $('.options-advanced').hide();
@ -81,21 +81,21 @@ function updateVisibility(opts) {
$(document).ready(() => { $(document).ready(() => {
Handlebars.partials = Handlebars.templates; Handlebars.partials = Handlebars.templates;
loadOptions().then(opts => { optionsLoad().then(opts => {
$('#activate-on-startup').prop('checked', opts.activateOnStartup); $('#activate-on-startup').prop('checked', opts.general.autoStart);
$('#enable-audio-playback').prop('checked', opts.enableAudioPlayback); $('#enable-audio-playback').prop('checked', opts.general.audioPlayback);
$('#enable-soft-katakana-search').prop('checked', opts.enableSoftKatakanaSearch); $('#enable-soft-katakana-search').prop('checked', opts.general.softKatakana);
$('#group-term-results').prop('checked', opts.groupTermResults); $('#group-term-results').prop('checked', opts.general.groupResults);
$('#show-advanced-options').prop('checked', opts.showAdvancedOptions); $('#show-advanced-options').prop('checked', opts.general.showAdvanced);
$('#hold-shift-to-scan').prop('checked', opts.holdShiftToScan); $('#hold-shift-to-scan').prop('checked', opts.scanning.requireShift);
$('#select-matched-text').prop('checked', opts.selectMatchedText); $('#select-matched-text').prop('checked', opts.scanning.selectText);
$('#scan-delay').val(opts.scanDelay); $('#scan-delay').val(opts.scanning.delay);
$('#scan-length').val(opts.scanLength); $('#scan-length').val(opts.scanning.length);
$('#anki-enable').prop('checked', opts.ankiEnable); $('#anki-enable').prop('checked', opts.anki.enable);
$('#anki-card-tags').val(opts.ankiCardTags.join(' ')); $('#anki-card-tags').val(opts.anki.tags.join(' '));
$('#sentence-extent').val(opts.sentenceExtent); $('#sentence-extent').val(opts.anki.sentenceExt);
$('input, select').not('.anki-model').change(onOptionsChanged); $('input, select').not('.anki-model').change(onOptionsChanged);
$('.anki-model').change(onAnkiModelChanged); $('.anki-model').change(onAnkiModelChanged);
@ -189,7 +189,7 @@ function onDictionaryPurge(e) {
showDictionarySpinner(false); showDictionarySpinner(false);
dictControls.show(); dictControls.show();
dictProgress.hide(); dictProgress.hide();
return loadOptions().then(opts => populateDictionaries(opts)); return optionsLoad().then(opts => populateDictionaries(opts));
}); });
} }
@ -212,7 +212,7 @@ function onDictionaryDelete() {
showDictionarySpinner(false); showDictionarySpinner(false);
dictProgress.hide(); dictProgress.hide();
dictControls.show(); dictControls.show();
return loadOptions().then(opts => populateDictionaries(opts)); return optionsLoad().then(opts => populateDictionaries(opts));
}); });
} }
@ -229,10 +229,10 @@ function onDictionaryImport() {
setProgress(0.0); setProgress(0.0);
loadOptions().then(opts => { optionsLoad().then(opts => {
database().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => { database().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => {
opts.dictionaries[summary.title] = {enableTerms: summary.hasTerms, enableKanji: summary.hasKanji}; opts.dictionaries[summary.title] = {enableTerms: summary.hasTerms, enableKanji: summary.hasKanji};
return saveOptions(opts).then(() => yomichan().setOptions(opts)); return optionsSave(opts).then(() => yomichan().setOptions(opts));
}).then(() => { }).then(() => {
return populateDictionaries(opts); return populateDictionaries(opts);
}).catch(error => { }).catch(error => {
@ -303,8 +303,8 @@ function fieldsToDict(selection) {
function modelIdToFieldOptKey(id) { function modelIdToFieldOptKey(id) {
return { return {
'anki-term-model': 'ankiTermFields', 'anki-term-model': 'anki.terms.fields',
'anki-kanji-model': 'ankiKanjiFields' 'anki-kanji-model': 'anki.kanji.fields'
}[id]; }[id];
} }
@ -344,16 +344,16 @@ function populateAnkiDeckAndModel(opts) {
ankiDeck.find('option').remove(); ankiDeck.find('option').remove();
deckNames.forEach(name => ankiDeck.append($('<option/>', {value: name, text: name}))); deckNames.forEach(name => ankiDeck.append($('<option/>', {value: name, text: name})));
$('#anki-term-deck').val(opts.ankiTermDeck); $('#anki-term-deck').val(opts.anki.terms.deck);
$('#anki-kanji-deck').val(opts.ankiKanjiDeck); $('#anki-kanji-deck').val(opts.anki.kanji.deck);
const ankiModel = $('.anki-model'); const ankiModel = $('.anki-model');
ankiModel.find('option').remove(); ankiModel.find('option').remove();
modelNames.forEach(name => ankiModel.append($('<option/>', {value: name, text: name}))); modelNames.forEach(name => ankiModel.append($('<option/>', {value: name, text: name})));
return Promise.all([ return Promise.all([
populateAnkiFields($('#anki-term-model').val(opts.ankiTermModel), opts), populateAnkiFields($('#anki-term-model').val(opts.anki.terms.model), opts),
populateAnkiFields($('#anki-kanji-model').val(opts.ankiKanjiModel), opts) populateAnkiFields($('#anki-kanji-model').val(opts.anki.kanji.model), opts)
]); ]);
}).then(() => { }).then(() => {
ankiFormat.show(); ankiFormat.show();
@ -403,7 +403,7 @@ function onAnkiModelChanged(e) {
getFormValues().then(({optsNew, optsOld}) => { getFormValues().then(({optsNew, optsOld}) => {
optsNew[modelIdToFieldOptKey($(this).id)] = {}; optsNew[modelIdToFieldOptKey($(this).id)] = {};
populateAnkiFields($(this), optsNew).then(() => { populateAnkiFields($(this), optsNew).then(() => {
saveOptions(optsNew).then(() => yomichan().setOptions(optsNew)); optionsSave(optsNew).then(() => yomichan().setOptions(optsNew));
}).catch(error => { }).catch(error => {
showAnkiError(error); showAnkiError(error);
}).then(() => { }).then(() => {
@ -418,10 +418,10 @@ function onOptionsChanged(e) {
} }
getFormValues().then(({optsNew, optsOld}) => { getFormValues().then(({optsNew, optsOld}) => {
return saveOptions(optsNew).then(() => { return optionsSave(optsNew).then(() => {
yomichan().setOptions(optsNew); yomichan().setOptions(optsNew);
updateVisibility(optsNew); updateVisibility(optsNew);
if (optsNew.ankiEnable !== optsOld.ankiEnable) { if (optsNew.anki.enable !== optsOld.anki.enable) {
showAnkiError(null); showAnkiError(null);
showAnkiSpinner(true); showAnkiSpinner(true);
return populateAnkiDeckAndModel(optsNew); return populateAnkiDeckAndModel(optsNew);

View File

@ -77,7 +77,7 @@ function optionsVersion(options) {
copy(options.general, 'autoStart', options, 'activateOnStartup'); copy(options.general, 'autoStart', options, 'activateOnStartup');
copy(options.general, 'audioPlayback', options, 'enableAudioPlayback'); copy(options.general, 'audioPlayback', options, 'enableAudioPlayback');
copy(options.general, 'softKatakana', options, 'enableSoftKatakanaSearch'); copy(options.general, 'softKatakana', options, 'enableSoftKatakanaSearch');
copy(options.general, 'groupResults', options, 'goupTermResults'); copy(options.general, 'groupResults', options, 'groupTermResults');
copy(options.general, 'showAdvanced', options, 'showAdvancedOptions'); copy(options.general, 'showAdvanced', options, 'showAdvancedOptions');
copy(options.scanning, 'requireShift', options, 'holdShiftToScan'); copy(options.scanning, 'requireShift', options, 'holdShiftToScan');
@ -89,9 +89,9 @@ function optionsVersion(options) {
copy(options.anki, 'tags', options, 'ankiCardTags'); copy(options.anki, 'tags', options, 'ankiCardTags');
copy(options.anki, 'sentenceExt', options, 'sentenceExtent'); copy(options.anki, 'sentenceExt', options, 'sentenceExtent');
copy(options.anki.term, 'deck', options, 'ankiTermDeck'); copy(options.anki.terms, 'deck', options, 'ankiTermDeck');
copy(options.anki.term, 'model', options, 'ankiTermModel'); copy(options.anki.terms, 'model', options, 'ankiTermModel');
copy(options.anki.term, 'fields', options, 'ankiTermFields'); copy(options.anki.terms, 'fields', options, 'ankiTermFields');
copy(options.anki.kanji, 'deck', options, 'ankiKanjiDeck'); copy(options.anki.kanji, 'deck', options, 'ankiKanjiDeck');
copy(options.anki.kanji, 'model', options, 'ankiKanjiModel'); copy(options.anki.kanji, 'model', options, 'ankiKanjiModel');
copy(options.anki.kanji, 'fields', options, 'ankiKanjiFields'); copy(options.anki.kanji, 'fields', options, 'ankiKanjiFields');

View File

@ -41,11 +41,11 @@ class Translator {
}); });
} }
findTerm(text, dictionaries, enableSoftKatakanaSearch) { findTerm(text, dictionaries, softKatakana) {
const cache = {}; const cache = {};
return this.findTermDeinflections(text, dictionaries, cache).then(deinfLiteral => { return this.findTermDeinflections(text, dictionaries, cache).then(deinfLiteral => {
const textHiragana = wanakana._katakanaToHiragana(text); const textHiragana = wanakana._katakanaToHiragana(text);
if (text !== textHiragana && enableSoftKatakanaSearch) { if (text !== textHiragana && softKatakana) {
return this.findTermDeinflections(textHiragana, dictionaries, cache).then(deinfHiragana => deinfLiteral.concat(deinfHiragana)); return this.findTermDeinflections(textHiragana, dictionaries, cache).then(deinfHiragana => deinfLiteral.concat(deinfHiragana));
} else { } else {
return deinfLiteral; return deinfLiteral;
@ -82,8 +82,8 @@ class Translator {
}); });
} }
findTermGrouped(text, dictionaries, enableSoftKatakanaSearch) { findTermGrouped(text, dictionaries, softKatakana) {
return this.findTerm(text, dictionaries, enableSoftKatakanaSearch).then(({length, definitions}) => { return this.findTerm(text, dictionaries, softKatakana).then(({length, definitions}) => {
return {length, definitions: groupTermDefs(definitions)}; return {length, definitions: groupTermDefs(definitions)};
}); });
} }

View File

@ -32,9 +32,9 @@ class Yomichan {
chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this)); chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this));
chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this)); chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
loadOptions().then(opts => { optionsLoad().then(opts => {
this.setOptions(opts); this.setOptions(opts);
if (this.options.activateOnStartup) { if (this.options.general.autoStart) {
this.setState('loading'); this.setState('loading');
} }
}); });
@ -94,7 +94,7 @@ class Yomichan {
setOptions(options) { setOptions(options) {
this.options = options; this.options = options;
if (options.ankiEnable) { if (options.anki.enable) {
this.anki = new AnkiConnect(); this.anki = new AnkiConnect();
} else { } else {
this.anki = new AnkiNull(); this.anki = new AnkiNull();
@ -116,17 +116,17 @@ class Yomichan {
} }
formatNote(definition, mode) { formatNote(definition, mode) {
const note = {fields: {}, tags: this.options.ankiCardTags}; const note = {fields: {}, tags: this.options.anki.tags};
let fields = []; let fields = [];
if (mode === 'kanji') { if (mode === 'kanji') {
fields = this.options.ankiKanjiFields; fields = this.options.anki.kanji.fields;
note.deckName = this.options.ankiKanjiDeck; note.deckName = this.options.anki.kanji.deck;
note.modelName = this.options.ankiKanjiModel; note.modelName = this.options.anki.kanji.model;
} else { } else {
fields = this.options.ankiTermFields; fields = this.options.anki.terms.fields;
note.deckName = this.options.ankiTermDeck; note.deckName = this.options.anki.terms.deck;
note.modelName = this.options.ankiTermModel; note.modelName = this.options.anki.terms.model;
const audio = { const audio = {
kanji: definition.expression, kanji: definition.expression,
@ -157,7 +157,7 @@ class Yomichan {
} }
api_getOptions({callback}) { api_getOptions({callback}) {
promiseCallback(loadOptions(), callback); promiseCallback(optionsLoad(), callback);
} }
api_findKanji({text, callback}) { api_findKanji({text, callback}) {
@ -186,7 +186,7 @@ class Yomichan {
this.translator.findTerm( this.translator.findTerm(
text, text,
dictionaries, dictionaries,
this.options.enableSoftKatakanaSearch this.options.general.softKatakana
), ),
callback callback
); );
@ -204,7 +204,7 @@ class Yomichan {
this.translator.findTermGrouped( this.translator.findTermGrouped(
text, text,
dictionaries, dictionaries,
this.options.enableSoftKatakanaSearch this.options.general.softKatakana
), ),
callback callback
); );

View File

@ -44,7 +44,7 @@ class Driver {
popupTimerSet(callback) { popupTimerSet(callback) {
this.popupTimerClear(); this.popupTimerClear();
this.popupTimer = window.setTimeout(callback, this.options.scanDelay); this.popupTimer = window.setTimeout(callback, this.options.scanning.delay);
} }
popupTimerClear() { popupTimerClear() {
@ -82,7 +82,7 @@ class Driver {
return; return;
} }
if (this.options.holdShiftToScan && !e.shiftKey) { if (this.options.scanning.requireShift && !e.shiftKey) {
return; return;
} }
@ -144,16 +144,16 @@ class Driver {
} }
searchTerms(textSource) { searchTerms(textSource) {
textSource.setEndOffset(this.options.scanLength); textSource.setEndOffset(this.options.scanning.length);
const findFunc = this.options.groupTermResults ? findTermGrouped : findTerm; const findFunc = this.options.general.groupResults ? findTermGrouped : findTerm;
return findFunc(textSource.text()).then(({definitions, length}) => { return findFunc(textSource.text()).then(({definitions, length}) => {
if (definitions.length === 0) { if (definitions.length === 0) {
return false; return false;
} else { } else {
textSource.setEndOffset(length); textSource.setEndOffset(length);
const sentence = extractSentence(textSource, this.options.sentenceExtent); const sentence = extractSentence(textSource, this.options.anki.sentenceExt);
definitions.forEach(definition => { definitions.forEach(definition => {
definition.url = window.location.href; definition.url = window.location.href;
definition.sentence = sentence; definition.sentence = sentence;
@ -162,7 +162,7 @@ class Driver {
this.popup.showNextTo(textSource.getRect()); this.popup.showNextTo(textSource.getRect());
this.popup.showTermDefs(definitions, this.options); this.popup.showTermDefs(definitions, this.options);
this.lastTextSource = textSource; this.lastTextSource = textSource;
if (this.options.selectMatchedText) { if (this.options.scanning.selectText) {
textSource.select(); textSource.select();
} }
@ -186,7 +186,7 @@ class Driver {
this.popup.showNextTo(textSource.getRect()); this.popup.showNextTo(textSource.getRect());
this.popup.showKanjiDefs(definitions, this.options); this.popup.showKanjiDefs(definitions, this.options);
this.lastTextSource = textSource; this.lastTextSource = textSource;
if (this.options.selectMatchedText) { if (this.options.scanning.selectText) {
textSource.select(); textSource.select();
} }
@ -201,7 +201,7 @@ class Driver {
searchClear() { searchClear() {
this.popup.hide(); this.popup.hide();
if (this.options.selectMatchedText && this.lastTextSource !== null) { if (this.options.scanning.selectText && this.lastTextSource !== null) {
this.lastTextSource.deselect(); this.lastTextSource.deselect();
} }

View File

@ -34,9 +34,9 @@ class Frame {
const sequence = ++this.sequence; const sequence = ++this.sequence;
const context = { const context = {
definitions, definitions,
grouped: options.groupTermResults, grouped: options.general.groupResults,
addable: options.ankiMethod !== 'disabled', addable: options.ankiMethod !== 'disabled',
playback: options.enableAudioPlayback playback: options.general.audioPlayback
}; };
this.definitions = definitions; this.definitions = definitions;