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

View File

@ -77,7 +77,7 @@ function optionsVersion(options) {
copy(options.general, 'autoStart', options, 'activateOnStartup');
copy(options.general, 'audioPlayback', options, 'enableAudioPlayback');
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.scanning, 'requireShift', options, 'holdShiftToScan');
@ -89,9 +89,9 @@ function optionsVersion(options) {
copy(options.anki, 'tags', options, 'ankiCardTags');
copy(options.anki, 'sentenceExt', options, 'sentenceExtent');
copy(options.anki.term, 'deck', options, 'ankiTermDeck');
copy(options.anki.term, 'model', options, 'ankiTermModel');
copy(options.anki.term, 'fields', options, 'ankiTermFields');
copy(options.anki.terms, 'deck', options, 'ankiTermDeck');
copy(options.anki.terms, 'model', options, 'ankiTermModel');
copy(options.anki.terms, 'fields', options, 'ankiTermFields');
copy(options.anki.kanji, 'deck', options, 'ankiKanjiDeck');
copy(options.anki.kanji, 'model', options, 'ankiKanjiModel');
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 = {};
return this.findTermDeinflections(text, dictionaries, cache).then(deinfLiteral => {
const textHiragana = wanakana._katakanaToHiragana(text);
if (text !== textHiragana && enableSoftKatakanaSearch) {
if (text !== textHiragana && softKatakana) {
return this.findTermDeinflections(textHiragana, dictionaries, cache).then(deinfHiragana => deinfLiteral.concat(deinfHiragana));
} else {
return deinfLiteral;
@ -82,8 +82,8 @@ class Translator {
});
}
findTermGrouped(text, dictionaries, enableSoftKatakanaSearch) {
return this.findTerm(text, dictionaries, enableSoftKatakanaSearch).then(({length, definitions}) => {
findTermGrouped(text, dictionaries, softKatakana) {
return this.findTerm(text, dictionaries, softKatakana).then(({length, definitions}) => {
return {length, definitions: groupTermDefs(definitions)};
});
}

View File

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

View File

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

View File

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