Fix primary audio source (#886)

* Add abstraction: _getDefinitionPrimaryExpressionAndReading

* Reuse existing definitions in a sequence

* Revert change to related definition source/rawSource/sourceTerm

* Update _getDefinitionPrimaryExpressionAndReading to return best match
This commit is contained in:
toasted-nutbread 2020-10-04 22:04:44 -04:00 committed by GitHub
parent 2d3cf89b49
commit 54810510fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 6 deletions

View File

@ -373,13 +373,16 @@ class Translator {
if (typeof sequencedDefinition === 'undefined') { if (typeof sequencedDefinition === 'undefined') {
sequencedDefinition = { sequencedDefinition = {
sourceDefinitions: [], sourceDefinitions: [],
relatedDefinitions: [] relatedDefinitions: [],
relatedDefinitionIds: new Set()
}; };
sequencedDefinitionMap.set(sequence, sequencedDefinition); sequencedDefinitionMap.set(sequence, sequencedDefinition);
sequencedDefinitions.push(sequencedDefinition); sequencedDefinitions.push(sequencedDefinition);
sequenceList.push(sequence); sequenceList.push(sequence);
} }
sequencedDefinition.sourceDefinitions.push(definition); sequencedDefinition.sourceDefinitions.push(definition);
sequencedDefinition.relatedDefinitions.push(definition);
sequencedDefinition.relatedDefinitionIds.add(definition.id);
} else { } else {
unsequencedDefinitions.push(definition); unsequencedDefinitions.push(definition);
} }
@ -388,13 +391,20 @@ class Translator {
if (sequenceList.length > 0) { if (sequenceList.length > 0) {
const databaseDefinitions = await this._database.findTermsBySequenceBulk(sequenceList, mainDictionary); const databaseDefinitions = await this._database.findTermsBySequenceBulk(sequenceList, mainDictionary);
for (const databaseDefinition of databaseDefinitions) { for (const databaseDefinition of databaseDefinitions) {
const {relatedDefinitions} = sequencedDefinitions[databaseDefinition.index]; const {relatedDefinitions, relatedDefinitionIds} = sequencedDefinitions[databaseDefinition.index];
const {expression} = databaseDefinition; const {id} = databaseDefinition;
const definition = await this._createTermDefinitionFromDatabaseDefinition(databaseDefinition, expression, expression, expression, [], enabledDictionaryMap); if (relatedDefinitionIds.has(id)) { continue; }
const {source, rawSource, sourceTerm} = relatedDefinitions[0];
const definition = await this._createTermDefinitionFromDatabaseDefinition(databaseDefinition, source, rawSource, sourceTerm, [], enabledDictionaryMap);
relatedDefinitions.push(definition); relatedDefinitions.push(definition);
} }
} }
for (const {relatedDefinitions} of sequencedDefinitions) {
this._sortDefinitionsById(relatedDefinitions);
}
return {sequencedDefinitions, unsequencedDefinitions}; return {sequencedDefinitions, unsequencedDefinitions};
} }
@ -1211,6 +1221,11 @@ class Translator {
definitions.sort((a, b) => a.index - b.index); definitions.sort((a, b) => a.index - b.index);
} }
_sortDefinitionsById(definitions) {
if (definitions.length <= 1) { return; }
definitions.sort((a, b) => a.id - b.id);
}
_sortKanjiStats(stats) { _sortKanjiStats(stats) {
if (stats.length <= 1) { return; } if (stats.length <= 1) { return; }
const stringComparer = this._stringComparer; const stringComparer = this._stringComparer;

View File

@ -1386,8 +1386,7 @@ class Display extends EventDispatcher {
const timestamp = Date.now(); const timestamp = Date.now();
const ownerFrameId = this._ownerFrameId; const ownerFrameId = this._ownerFrameId;
const {fields} = modeOptions; const {fields} = modeOptions;
const definitionExpressions = definition.expressions; const {expression, reading} = this._getDefinitionPrimaryExpressionAndReading(definition);
const {expression, reading} = Array.isArray(definitionExpressions) ? definitionExpressions[0] : definition;
const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl} : null); const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl} : null);
const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {ownerFrameId, format, quality} : null); const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {ownerFrameId, format, quality} : null);
const clipboardDetails = { const clipboardDetails = {
@ -1424,4 +1423,20 @@ class Display extends EventDispatcher {
async _getAudioInfo(source, expression, reading, details) { async _getAudioInfo(source, expression, reading, details) {
return await api.getDefinitionAudioInfo(source, expression, reading, details); return await api.getDefinitionAudioInfo(source, expression, reading, details);
} }
_getDefinitionPrimaryExpressionAndReading(definition) {
const termDetailsList = definition.expressions;
let bestIndex = -1;
for (let i = 0, ii = termDetailsList.length; i < ii; ++i) {
const {sourceTerm, expression, reading} = termDetailsList[i];
if (expression === sourceTerm) {
bestIndex = i;
break;
} else if (reading === sourceTerm && bestIndex < 0) {
bestIndex = i;
}
}
const {expression, reading} = termDetailsList[Math.max(0, bestIndex)];
return {expression, reading};
}
} }