merged mode: add main dictionary selection
This commit is contained in:
parent
03fd9dcdb4
commit
cfad3b3099
@ -85,25 +85,26 @@ class Database {
|
||||
return results;
|
||||
}
|
||||
|
||||
async findTermsBySequence(sequence, dictionary) {
|
||||
async findTermsBySequence(sequence, mainDictionary) {
|
||||
if (!this.db) {
|
||||
throw 'Database not initialized';
|
||||
}
|
||||
|
||||
const results = [];
|
||||
await this.db.terms.where('sequence').equals(sequence).each(row => {
|
||||
// if (dictionary === row.dictionary) {
|
||||
results.push({
|
||||
expression: row.expression,
|
||||
reading: row.reading,
|
||||
tags: dictFieldSplit(row.tags),
|
||||
rules: dictFieldSplit(row.rules),
|
||||
glossary: row.glossary,
|
||||
score: row.score,
|
||||
dictionary: row.dictionary,
|
||||
id: row.id,
|
||||
sequence: typeof row.sequence === 'undefined' ? -1 : row.sequence
|
||||
});
|
||||
if (row.dictionary === mainDictionary) {
|
||||
results.push({
|
||||
expression: row.expression,
|
||||
reading: row.reading,
|
||||
tags: dictFieldSplit(row.tags),
|
||||
rules: dictFieldSplit(row.rules),
|
||||
glossary: row.glossary,
|
||||
score: row.score,
|
||||
dictionary: row.dictionary,
|
||||
id: row.id,
|
||||
sequence: typeof row.sequence === 'undefined' ? -1 : row.sequence
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return results;
|
||||
|
@ -144,10 +144,10 @@ function dictTermsGroup(definitions, dictionaries) {
|
||||
return dictTermsSort(results);
|
||||
}
|
||||
|
||||
function dictTermsMergeBySequence(definitions) {
|
||||
function dictTermsMergeBySequence(definitions, mainDictionary) {
|
||||
const definitionsBySequence = {'-1': []};
|
||||
for (const definition of definitions) {
|
||||
if (definition.sequence > 0) {
|
||||
if (mainDictionary === definition.dictionary && definition.sequence > 0) {
|
||||
if (!definitionsBySequence[definition.sequence]) {
|
||||
definitionsBySequence[definition.sequence] = {
|
||||
reasons: definition.reasons,
|
||||
|
@ -154,6 +154,10 @@ function optionsSetDefaults(options) {
|
||||
|
||||
dictionaries: {},
|
||||
|
||||
dictionary: {
|
||||
main: ''
|
||||
},
|
||||
|
||||
anki: {
|
||||
enable: false,
|
||||
server: 'http://127.0.0.1:8765',
|
||||
|
@ -60,7 +60,11 @@ async function formRead() {
|
||||
const title = dictionary.data('title');
|
||||
const priority = parseInt(dictionary.find('.dict-priority').val(), 10);
|
||||
const enabled = dictionary.find('.dict-enabled').prop('checked');
|
||||
optionsNew.dictionaries[title] = {priority, enabled};
|
||||
const main = dictionary.find('.dict-main').prop('checked');
|
||||
if (main) {
|
||||
optionsNew.dictionary.main = title;
|
||||
}
|
||||
optionsNew.dictionaries[title] = {priority, enabled, main};
|
||||
});
|
||||
|
||||
return {optionsNew, optionsOld};
|
||||
@ -237,6 +241,18 @@ function dictionaryGroupsSort() {
|
||||
dictGroups.append(dictGroupChildren);
|
||||
}
|
||||
|
||||
function dictionarySetMain(e) {
|
||||
const mainDictionary = $(e.target).closest('.dict-group');
|
||||
const mainDictionaryTitle = mainDictionary.data('title');
|
||||
|
||||
$('.dict-group').each((index, element) => {
|
||||
const dictionary = $(element);
|
||||
if (dictionary.data('title') !== mainDictionaryTitle) {
|
||||
dictionary.find('.dict-main').prop('checked', false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function dictionaryGroupsPopulate(options) {
|
||||
const dictGroups = $('#dict-groups').empty();
|
||||
const dictWarning = $('#dict-warning').hide();
|
||||
@ -247,13 +263,14 @@ async function dictionaryGroupsPopulate(options) {
|
||||
}
|
||||
|
||||
for (const dictRow of dictRowsSort(dictRows, options)) {
|
||||
const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0};
|
||||
const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0, main: false};
|
||||
const dictHtml = await apiTemplateRender('dictionary.html', {
|
||||
title: dictRow.title,
|
||||
version: dictRow.version,
|
||||
revision: dictRow.revision,
|
||||
priority: dictOptions.priority,
|
||||
enabled: dictOptions.enabled
|
||||
enabled: dictOptions.enabled,
|
||||
main: dictOptions.main
|
||||
});
|
||||
|
||||
dictGroups.append($(dictHtml));
|
||||
@ -265,6 +282,11 @@ async function dictionaryGroupsPopulate(options) {
|
||||
dictionaryGroupsSort();
|
||||
onFormOptionsChanged(e);
|
||||
});
|
||||
|
||||
$('.dict-main').change(e => {
|
||||
dictionarySetMain(e);
|
||||
onFormOptionsChanged(e);
|
||||
});
|
||||
}
|
||||
|
||||
async function onDictionaryPurge(e) {
|
||||
@ -308,7 +330,7 @@ async function onDictionaryImport(e) {
|
||||
|
||||
const options = await optionsLoad();
|
||||
const summary = await utilDatabaseImport(e.target.files[0], updateProgress);
|
||||
options.dictionaries[summary.title] = {enabled: true, priority: 0};
|
||||
options.dictionaries[summary.title] = {enabled: true, priority: 0, main: false};
|
||||
await optionsSave(options);
|
||||
|
||||
await dictionaryGroupsPopulate(options);
|
||||
|
@ -13,7 +13,9 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p
|
||||
+ alias4(((helper = (helper = helpers.revision || (depth0 != null ? depth0.revision : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"revision","hash":{},"data":data}) : helper)))
|
||||
+ "</small></h4>\n\n <div class=\"checkbox\">\n <label><input type=\"checkbox\" class=\"dict-enabled\" "
|
||||
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.enabled : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
|
||||
+ "> Enable search</label>\n </div>\n <div class=\"form-group options-advanced\">\n <label for=\"dict-"
|
||||
+ "> Enable search</label>\n <label><input type=\"radio\" class=\"dict-main\" "
|
||||
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.main : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
|
||||
+ "> Set as main dictionary</label>\n </div>\n <div class=\"form-group options-advanced\">\n <label for=\"dict-"
|
||||
+ alias4(((helper = (helper = helpers.title || (depth0 != null ? depth0.title : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"title","hash":{},"data":data}) : helper)))
|
||||
+ "\">Result priority</label>\n <input type=\"number\" value=\""
|
||||
+ alias4(((helper = (helper = helpers.priority || (depth0 != null ? depth0.priority : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"priority","hash":{},"data":data}) : helper)))
|
||||
|
@ -49,10 +49,11 @@ class Translator {
|
||||
}
|
||||
|
||||
async findTermsMerged(text, dictionaries, alphanumeric) {
|
||||
const options = await apiOptionsGet();
|
||||
const titles = Object.keys(dictionaries);
|
||||
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
|
||||
|
||||
const definitionsBySequence = dictTermsMergeBySequence(definitions);
|
||||
const definitionsBySequence = dictTermsMergeBySequence(definitions, options.dictionary.main);
|
||||
|
||||
const definitionsMerged = dictTermsGroup(definitionsBySequence['-1'], dictionaries);
|
||||
for (const sequence in definitionsBySequence) {
|
||||
@ -62,7 +63,7 @@ class Translator {
|
||||
|
||||
const result = definitionsBySequence[sequence];
|
||||
|
||||
const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence));
|
||||
const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence), options.dictionary.main);
|
||||
const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence);
|
||||
|
||||
// postprocess glossaries
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
<div class="checkbox">
|
||||
<label><input type="checkbox" class="dict-enabled" {{#if enabled}}checked{{/if}}> Enable search</label>
|
||||
<label><input type="radio" class="dict-main" {{#if main}}checked{{/if}}> Set as main dictionary</label>
|
||||
</div>
|
||||
<div class="form-group options-advanced">
|
||||
<label for="dict-{{title}}">Result priority</label>
|
||||
|
Loading…
Reference in New Issue
Block a user