From 69ad4a7c9b1f859733909a75534e2005a9f56178 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 3 Oct 2017 07:20:02 +0300 Subject: merged mode: implement missing stuff, refactoring - use correct tags - indicate popular and rare terms - indicate definitions restricted to specific terms - frequencies (Innocent Corpus) --- ext/bg/js/util.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'ext/bg/js/util.js') diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index f44582eb..9a3a2a3e 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -26,6 +26,32 @@ function utilIsolate(data) { return JSON.parse(JSON.stringify(data)); } +function utilSetEqual(setA, setB) { + if (setA.size !== setB.size) { + return false; + } + + for (const value of setA) { + if (!setB.has(value)) { + return false; + } + } + + return true; +} + +function utilSetIntersection(setA, setB) { + return new Set( + [...setA].filter(value => setB.has(value)) + ); +} + +function utilSetDifference(setA, setB) { + return new Set( + [...setA].filter(value => !setB.has(value)) + ); +} + function utilBackend() { return chrome.extension.getBackgroundPage().yomichan_backend; } -- cgit v1.2.3 From 7e556e8d32251844723d665cec54a9a69ad94e76 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Wed, 18 Oct 2017 23:40:56 +0300 Subject: field template fixup for modified templates only --- ext/bg/js/options.js | 14 +++++++++----- ext/bg/js/util.js | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) (limited to 'ext/bg/js/util.js') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index c56c1945..00981096 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -269,11 +269,15 @@ function optionsVersion(options) { } options.general.compactTags = false; options.general.compactGlossaries = false; - options.anki.fieldTemplates = '{{#if merge}}\n' + - optionsFieldTemplates() + - '\n{{else}}\n' + - options.anki.fieldTemplates + - '\n{{/if}}'; + if (utilStringHashCode(options.anki.fieldTemplates) !== -1895236672) { // a3c8508031a1073629803d0616a2ee416cd3cccc + options.anki.fieldTemplates = '{{#if merge}}\n' + + optionsFieldTemplates() + + '\n{{else}}\n' + + options.anki.fieldTemplates + + '\n{{/if}}'; + } else { + options.anki.fieldTemplates = optionsFieldTemplates(); + } } ]; diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 9a3a2a3e..88d96688 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -52,6 +52,21 @@ function utilSetDifference(setA, setB) { ); } +function utilStringHashCode(string) { + let hashCode = 0; + + if (string.length === 0) { + return hashCode; + } + + for (let i = 0, charCode = string.charCodeAt(i); i < string.length; i++) { + hashCode = ((hashCode << 5) - hashCode) + charCode; + hashCode |= 0; + } + + return hashCode; +} + function utilBackend() { return chrome.extension.getBackgroundPage().yomichan_backend; } -- cgit v1.2.3 From 9d4b75de20d4a734d3f260498cb50b4e20ae8310 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 19 Oct 2017 00:47:08 +0300 Subject: fix error in utilStringHashCode --- ext/bg/js/options.js | 2 +- ext/bg/js/util.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'ext/bg/js/util.js') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 00981096..5902bc30 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -269,7 +269,7 @@ function optionsVersion(options) { } options.general.compactTags = false; options.general.compactGlossaries = false; - if (utilStringHashCode(options.anki.fieldTemplates) !== -1895236672) { // a3c8508031a1073629803d0616a2ee416cd3cccc + if (utilStringHashCode(options.anki.fieldTemplates) !== -805327496) { // a3c8508031a1073629803d0616a2ee416cd3cccc options.anki.fieldTemplates = '{{#if merge}}\n' + optionsFieldTemplates() + '\n{{else}}\n' + diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 88d96688..91b16e79 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -59,7 +59,7 @@ function utilStringHashCode(string) { return hashCode; } - for (let i = 0, charCode = string.charCodeAt(i); i < string.length; i++) { + for (let i = 0, charCode = string.charCodeAt(i); i < string.length; charCode = string.charCodeAt(++i)) { hashCode = ((hashCode << 5) - hashCode) + charCode; hashCode |= 0; } -- cgit v1.2.3 From 78442fff1a995632f2b315994f2540459ee20eca Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 24 Oct 2017 16:23:13 +0300 Subject: settings: main dictionary selection as dropdown --- ext/bg/js/database.js | 15 +++++++++++ ext/bg/js/options.js | 3 ++- ext/bg/js/settings.js | 66 ++++++++++++++++++++++++++++++++----------------- ext/bg/js/templates.js | 4 +-- ext/bg/js/translator.js | 5 ++-- ext/bg/js/util.js | 4 +++ ext/bg/settings.html | 5 ++++ tmpl/dictionary.html | 3 --- 8 files changed, 73 insertions(+), 32 deletions(-) (limited to 'ext/bg/js/util.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 9e90e6de..fcf8ef3f 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -228,6 +228,21 @@ class Database { } } + async getTitlesWithSequences() { + if (!this.db) { + throw 'Database not initialized'; + } + + const titles = []; + await this.db.dictionaries.each(row => { + if (row.hasSequences) { + titles.push(row.title); + } + }); + + return titles; + } + async importDictionary(archive, callback) { if (!this.db) { throw 'Database not initialized'; diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 2cb7dbec..4d18c166 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -203,7 +203,8 @@ function optionsSetDefaults(options) { popupOffset: 10, showGuide: true, compactTags: false, - compactGlossaries: false + compactGlossaries: false, + mainDictionary: '' }, scanning: { diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 37e5126e..36f2bdf9 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -25,6 +25,7 @@ async function formRead() { optionsNew.general.compactTags = $('#compact-tags').prop('checked'); optionsNew.general.compactGlossaries = $('#compact-glossaries').prop('checked'); optionsNew.general.resultOutputMode = $('#result-output-mode').val(); + optionsNew.general.mainDictionary = $('#main-dictionary').val(); optionsNew.general.audioSource = $('#audio-playback-source').val(); optionsNew.general.audioVolume = parseFloat($('#audio-playback-volume').val()); optionsNew.general.debugInfo = $('#show-debug-info').prop('checked'); @@ -62,9 +63,8 @@ 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'); - const main = dictionary.find('.dict-main').prop('checked'); const allowSecondarySearches = dictionary.find('.dict-allow-secondary-searches').prop('checked'); - optionsNew.dictionaries[title] = {priority, enabled, main, allowSecondarySearches}; + optionsNew.dictionaries[title] = {priority, enabled, allowSecondarySearches}; }); return {optionsNew, optionsOld}; @@ -85,6 +85,13 @@ function formUpdateVisibility(options) { advanced.hide(); } + const merge = $('.options-merge'); + if (options.general.resultOutputMode === 'merge') { + merge.show(); + } else { + merge.hide(); + } + const debug = $('#debug'); if (options.general.debugInfo) { const temp = utilIsolate(options); @@ -97,6 +104,29 @@ function formUpdateVisibility(options) { } } +async function formMainDictionaryOptionsPopulate(options) { + const select = $('#main-dictionary').empty(); + + let titles = await utilDatabaseGetTitlesWithSequences(); + titles = titles.filter(title => options.dictionaries[title].enabled); + const formOptionsHtml = []; + let mainDictionarySelected = false; + for (title of titles) { + if (options.general.mainDictionary === title) { + mainDictionarySelected = true; + } + formOptionsHtml.push(``); + } + + if (!mainDictionarySelected) { + options.general.mainDictionary = ''; + } + + const notSelectedOptionHtml = ``; + + select.append($([notSelectedOptionHtml].concat(formOptionsHtml).join(''))); +} + async function onFormOptionsChanged(e) { try { if (!e.originalEvent && !e.isTrigger) { @@ -104,6 +134,7 @@ async function onFormOptionsChanged(e) { } const {optionsNew, optionsOld} = await formRead(); + await formMainDictionaryOptionsPopulate(optionsNew); await optionsSave(optionsNew); formUpdateVisibility(optionsNew); @@ -131,6 +162,7 @@ async function onReady() { $('#compact-tags').prop('checked', options.general.compactTags); $('#compact-glossaries').prop('checked', options.general.compactGlossaries); $('#result-output-mode').val(options.general.resultOutputMode); + $('#main-dictionary').val(options.general.mainDictionary); $('#audio-playback-source').val(options.general.audioSource); $('#audio-playback-volume').val(options.general.audioVolume); $('#show-debug-info').prop('checked', options.general.debugInfo); @@ -172,6 +204,8 @@ async function onReady() { ankiErrorShow(e); } + await formMainDictionaryOptionsPopulate(options); + formUpdateVisibility(options); } @@ -243,18 +277,6 @@ 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(); @@ -265,14 +287,13 @@ async function dictionaryGroupsPopulate(options) { } for (const dictRow of dictRowsSort(dictRows, options)) { - const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0, main: false, allowSecondarySearches: false}; + const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0, allowSecondarySearches: false}; const dictHtml = await apiTemplateRender('dictionary.html', { title: dictRow.title, version: dictRow.version, revision: dictRow.revision, priority: dictOptions.priority, enabled: dictOptions.enabled, - main: dictOptions.main, allowSecondarySearches: dictOptions.allowSecondarySearches }); @@ -285,11 +306,6 @@ async function dictionaryGroupsPopulate(options) { dictionaryGroupsSort(); onFormOptionsChanged(e); }); - - $('.dict-main').change(e => { - dictionarySetMain(e); - onFormOptionsChanged(e); - }); } async function onDictionaryPurge(e) { @@ -305,9 +321,11 @@ async function onDictionaryPurge(e) { await utilDatabasePurge(); const options = await optionsLoad(); options.dictionaries = {}; + options.general.mainDictionary = ''; await optionsSave(options); await dictionaryGroupsPopulate(options); + await formMainDictionaryOptionsPopulate(options); } catch (e) { dictionaryErrorShow(e); } finally { @@ -333,10 +351,14 @@ 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, main: false, allowSecondarySearches: false}; + options.dictionaries[summary.title] = {enabled: true, priority: 0, allowSecondarySearches: false}; + if (summary.hasSequences && !options.general.mainDictionary) { + options.general.mainDictionary = summary.title; + } await optionsSave(options); await dictionaryGroupsPopulate(options); + await formMainDictionaryOptionsPopulate(options); } catch (e) { dictionaryErrorShow(e); } finally { diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index e5640394..9f72e661 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -15,9 +15,7 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p + ((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\n \n
\n \n
\n
\n \n
\n
\n \n
\n
\n \n options.dictionaries[dict].main).concat([''])[0]; const secondarySearchTitles = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].allowSecondarySearches); const titles = Object.keys(dictionaries); const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric); - const definitionsBySequence = dictTermsMergeBySequence(definitions, mainDictionary); + const definitionsBySequence = dictTermsMergeBySequence(definitions, options.general.mainDictionary); const definitionsMerged = []; const mergedByTermIndices = new Set(); @@ -73,7 +72,7 @@ class Translator { const result = definitionsBySequence[sequence]; - const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence), mainDictionary); + const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence), options.general.mainDictionary); const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence); const secondarySearchResults = []; diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 91b16e79..60f6452b 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -87,6 +87,10 @@ function utilDatabaseGetTitles() { return utilBackend().translator.database.getTitles(); } +function utilDatabaseGetTitlesWithSequences() { + return utilBackend().translator.database.getTitlesWithSequences(); +} + function utilDatabasePurge() { return utilBackend().translator.database.purge(); } diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 408d5aab..8c457652 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -60,6 +60,11 @@
+
+ + +
+
Allow secondary searches
-
- -
-- cgit v1.2.3 From 6f43fffb4eca76246a3f458b973a7aada84c1488 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 24 Oct 2017 23:12:55 +0300 Subject: util.js: remove unnecessary conditional --- ext/bg/js/util.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'ext/bg/js/util.js') diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 60f6452b..091137ed 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -55,10 +55,6 @@ function utilSetDifference(setA, setB) { function utilStringHashCode(string) { let hashCode = 0; - if (string.length === 0) { - return hashCode; - } - for (let i = 0, charCode = string.charCodeAt(i); i < string.length; charCode = string.charCodeAt(++i)) { hashCode = ((hashCode << 5) - hashCode) + charCode; hashCode |= 0; -- cgit v1.2.3