Make reverse reading/expressions optional during database import

This commit is contained in:
toasted-nutbread 2019-11-23 22:54:06 -05:00
parent 1659340898
commit 1a0a345ae7
5 changed files with 60 additions and 11 deletions

View File

@ -332,9 +332,11 @@ class Database {
return result; return result;
} }
async importDictionary(archive, progressCallback, exceptions) { async importDictionary(archive, progressCallback, exceptions, details) {
this.validate(); this.validate();
const prefixWildcardsSupported = details.prefixWildcardsSupported;
const maxTransactionLength = 1000; const maxTransactionLength = 1000;
const bulkAdd = async (objectStoreName, items, total, current) => { const bulkAdd = async (objectStoreName, items, total, current) => {
const db = this.db; const db = this.db;
@ -389,9 +391,7 @@ class Database {
rules, rules,
score, score,
glossary, glossary,
dictionary: summary.title, dictionary: summary.title
expressionReverse: stringReverse(expression),
readingReverse: stringReverse(reading)
}); });
} }
} else { } else {
@ -405,13 +405,18 @@ class Database {
glossary, glossary,
sequence, sequence,
termTags, termTags,
dictionary: summary.title, dictionary: summary.title
expressionReverse: stringReverse(expression),
readingReverse: stringReverse(reading)
}); });
} }
} }
if (prefixWildcardsSupported) {
for (const row of rows) {
row.expressionReverse = stringReverse(row.expression);
row.readingReverse = stringReverse(row.reading);
}
}
await bulkAdd('terms', rows, total, current); await bulkAdd('terms', rows, total, current);
}; };

View File

@ -378,7 +378,15 @@ function profileOptionsUpdateVersion(options) {
* ] * ]
*/ */
const optionsVersionUpdates = []; const optionsVersionUpdates = [
(options) => {
options.global = {
database: {
prefixWildcardsSupported: false
}
};
}
];
function optionsUpdateVersion(options, defaultProfileOptions) { function optionsUpdateVersion(options, defaultProfileOptions) {
// Ensure profiles is an array // Ensure profiles is an array
@ -423,6 +431,11 @@ function optionsUpdateVersion(options, defaultProfileOptions) {
profile.options = profileOptionsUpdateVersion(profile.options); profile.options = profileOptionsUpdateVersion(profile.options);
} }
// Version
if (typeof options.version !== 'number') {
options.version = 0;
}
// Generic updates // Generic updates
return optionsGenericApplyUpdates(options, optionsVersionUpdates); return optionsGenericApplyUpdates(options, optionsVersionUpdates);
} }

View File

@ -356,6 +356,7 @@ async function dictSettingsInitialize() {
document.querySelector('#dict-file-button').addEventListener('click', (e) => onDictionaryImportButtonClick(e), false); document.querySelector('#dict-file-button').addEventListener('click', (e) => onDictionaryImportButtonClick(e), false);
document.querySelector('#dict-file').addEventListener('change', (e) => onDictionaryImport(e), false); document.querySelector('#dict-file').addEventListener('change', (e) => onDictionaryImport(e), false);
document.querySelector('#dict-main').addEventListener('change', (e) => onDictionaryMainChanged(e), false); document.querySelector('#dict-main').addEventListener('change', (e) => onDictionaryMainChanged(e), false);
document.querySelector('#database-enable-prefix-wildcard-searches').addEventListener('change', (e) => onDatabaseEnablePrefixWildcardSearchesChanged(e), false);
const optionsContext = getOptionsContext(); const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext); const options = await apiOptionsGet(optionsContext);
@ -366,6 +367,9 @@ async function dictSettingsInitialize() {
async function onDictionaryOptionsChanged(options) { async function onDictionaryOptionsChanged(options) {
if (dictionaryUI === null) { return; } if (dictionaryUI === null) { return; }
dictionaryUI.setOptionsDictionaries(options.dictionaries); dictionaryUI.setOptionsDictionaries(options.dictionaries);
const optionsFull = await apiOptionsGetFull();
document.querySelector('#database-enable-prefix-wildcard-searches').checked = optionsFull.global.database.prefixWildcardsSupported;
} }
async function onDatabaseUpdated(options) { async function onDatabaseUpdated(options) {
@ -575,6 +579,12 @@ async function onDictionaryImport(e) {
const exceptions = []; const exceptions = [];
const files = [...e.target.files]; const files = [...e.target.files];
const optionsFull = await apiOptionsGetFull();
const importDetails = {
prefixWildcardsSupported: optionsFull.global.database.prefixWildcardsSupported
};
for (let i = 0, ii = files.length; i < ii; ++i) { for (let i = 0, ii = files.length; i < ii; ++i) {
setProgress(0.0); setProgress(0.0);
if (ii > 1) { if (ii > 1) {
@ -582,7 +592,7 @@ async function onDictionaryImport(e) {
dictImportInfo.textContent = `(${i + 1} of ${ii})`; dictImportInfo.textContent = `(${i + 1} of ${ii})`;
} }
const summary = await utilDatabaseImport(files[i], updateProgress, exceptions); const summary = await utilDatabaseImport(files[i], updateProgress, exceptions, importDetails);
for (const options of toIterable(await getOptionsArray())) { for (const options of toIterable(await getOptionsArray())) {
const dictionaryOptions = SettingsDictionaryListUI.createDictionaryOptions(); const dictionaryOptions = SettingsDictionaryListUI.createDictionaryOptions();
dictionaryOptions.enabled = true; dictionaryOptions.enabled = true;
@ -616,3 +626,12 @@ async function onDictionaryImport(e) {
dictProgress.hide(); dictProgress.hide();
} }
} }
async function onDatabaseEnablePrefixWildcardSearchesChanged(e) {
const optionsFull = await apiOptionsGetFull();
const v = !!e.target.checked;
if (optionsFull.global.database.prefixWildcardsSupported === v) { return; }
optionsFull.global.database.prefixWildcardsSupported = !!e.target.checked;
await settingsSaveOptions();
}

View File

@ -94,13 +94,13 @@ function utilDatabaseDeleteDictionary(dictionaryName, onProgress) {
return utilBackend().translator.database.deleteDictionary(dictionaryName, onProgress); return utilBackend().translator.database.deleteDictionary(dictionaryName, onProgress);
} }
async function utilDatabaseImport(data, progress, exceptions) { async function utilDatabaseImport(data, progress, exceptions, details) {
// Edge cannot read data on the background page due to the File object // Edge cannot read data on the background page due to the File object
// being created from a different window. Read on the same page instead. // being created from a different window. Read on the same page instead.
if (EXTENSION_IS_BROWSER_EDGE) { if (EXTENSION_IS_BROWSER_EDGE) {
data = await utilReadFile(data); data = await utilReadFile(data);
} }
return utilBackend().translator.database.importDictionary(data, progress, exceptions); return utilBackend().translator.database.importDictionary(data, progress, exceptions, details);
} }
function utilReadFile(file) { function utilReadFile(file) {

View File

@ -491,6 +491,18 @@
<div hidden><input type="file" id="dict-file" accept=".zip,application/zip" multiple></div> <div hidden><input type="file" id="dict-file" accept=".zip,application/zip" multiple></div>
</div> </div>
<div>
<h3>Dictionary Options</h3>
</div>
<div class="checkbox">
<label><input type="checkbox" id="database-enable-prefix-wildcard-searches"> Enable prefix wildcard searches</label>
<p class="help-block">
This option only applies to newly imported dictionaries.
Enabling this option will also cause dictionary data to take up slightly more storage space.
</p>
</div>
<div class="modal fade" tabindex="-1" role="dialog" id="dict-purge-modal"> <div class="modal fade" tabindex="-1" role="dialog" id="dict-purge-modal">
<div class="modal-dialog modal-dialog-centered"> <div class="modal-dialog modal-dialog-centered">
<div class="modal-content"> <div class="modal-content">