This commit is contained in:
Alex Yatskov 2017-01-08 15:33:45 -08:00
parent 0498ea5d36
commit 148291d3a7
7 changed files with 78 additions and 4 deletions

View File

@ -82,6 +82,12 @@ class Translator {
}); });
} }
findTermGrouped(text, dictionaries, enableSoftKatakanaSearch) {
return this.findTerm(text, dictionaries, enableSoftKatakanaSearch).then(({length, definitions}) => {
return {length, definitions: groupTermDefs(definitions)};
});
}
findKanji(text, dictionaries) { findKanji(text, dictionaries) {
const processed = {}, promises = []; const processed = {}, promises = [];
for (const c of text) { for (const c of text) {

View File

@ -116,7 +116,7 @@ function groupTermDefs(definitions) {
const tagCounts = {}; const tagCounts = {};
for (const tag of groupDefs.map(def => def.tags)) { for (const tag of groupDefs.map(def => def.tags)) {
const count = tagsGlobal[tag.name] || 0; const count = tagCounts[tag.name] || 0;
tagCounts[tag.name] = count + 1; tagCounts[tag.name] = count + 1;
} }

View File

@ -188,7 +188,29 @@ class Yomichan {
} }
promiseCallback( promiseCallback(
this.translator.findTerm(text, dictionaries, this.options.enableSoftKatakanaSearch), this.translator.findTerm(
text,
dictionaries,
this.options.enableSoftKatakanaSearch
),
callback
);
}
api_findTermGrouped({text, callback}) {
const dictionaries = [];
for (const title in this.options.dictionaries) {
if (this.options.dictionaries[title].enableTerms) {
dictionaries.push(title);
}
}
promiseCallback(
this.translator.findTermGrouped(
text,
dictionaries,
this.options.enableSoftKatakanaSearch
),
callback callback
); );
} }

View File

@ -146,7 +146,14 @@ class Driver {
searchTerms(textSource) { searchTerms(textSource) {
textSource.setEndOffset(this.options.scanLength); textSource.setEndOffset(this.options.scanLength);
return findTerm(textSource.text()).then(({definitions, length}) => { let findFunc = findTerm;
let showFunc = this.popup.showTermDefs.bind(this.popup);
if (this.options.groupTermResults) {
findFunc = findTermGrouped;
showFunc = this.popup.showTermGroupedDefs.bind(this.popup);
}
return findFunc(textSource.text()).then(({definitions, length}) => {
if (definitions.length === 0) { if (definitions.length === 0) {
return false; return false;
} else { } else {
@ -159,7 +166,7 @@ class Driver {
}); });
this.popup.showNextTo(textSource.getRect()); this.popup.showNextTo(textSource.getRect());
this.popup.showTermDefs(definitions, this.options); showFunc(definitions, this.options);
this.lastTextSource = textSource; this.lastTextSource = textSource;
if (this.options.selectMatchedText) { if (this.options.selectMatchedText) {
textSource.select(); textSource.select();

View File

@ -61,6 +61,37 @@ class Frame {
}); });
} }
api_showTermGroupedDefs({definitions, options}) {
const sequence = ++this.sequence;
const context = {
definitions,
addable: options.ankiMethod !== 'disabled',
playback: options.enableAudioPlayback
};
this.definitions = definitions;
this.showSpinner(false);
window.scrollTo(0, 0);
renderText(context, 'term-list.html').then(content => {
$('.content').html(content);
$('.action-add-note').click(this.onAddNote.bind(this));
$('.kanji-link').click(e => {
e.preventDefault();
findKanji($(e.target).text()).then(kdefs => this.api_showKanjiDefs({options, definitions: kdefs}));
});
$('.action-play-audio').click(e => {
e.preventDefault();
const index = $(e.currentTarget).data('index');
this.playAudio(this.definitions[index]);
});
this.updateAddNoteButtons(['term_kanji', 'term_kana'], sequence);
});
}
api_showKanjiDefs({definitions, options}) { api_showKanjiDefs({definitions, options}) {
const sequence = ++this.sequence; const sequence = ++this.sequence;
const context = { const context = {

View File

@ -74,6 +74,10 @@ class Popup {
this.invokeApi('showTermDefs', {definitions, options}); this.invokeApi('showTermDefs', {definitions, options});
} }
showTermGroupedDefs(definitions, options) {
this.invokeApi('showTermGroupedDefs', {definitions, options});
}
showKanjiDefs(definitions, options) { showKanjiDefs(definitions, options) {
this.invokeApi('showKanjiDefs', {definitions, options}); this.invokeApi('showKanjiDefs', {definitions, options});
} }

View File

@ -41,6 +41,10 @@ function findTerm(text) {
return invokeBgApi('findTerm', {text}); return invokeBgApi('findTerm', {text});
} }
function findTermGrouped(text) {
return invokeBgApi('findTermGrouped', {text});
}
function findKanji(text) { function findKanji(text) {
return invokeBgApi('findKanji', {text}); return invokeBgApi('findKanji', {text});
} }