support importing from zip files
This commit is contained in:
parent
98d22a92f3
commit
d3c342071f
@ -7,6 +7,7 @@
|
||||
<script src="/mixed/lib/handlebars.min.js"></script>
|
||||
<script src="/mixed/lib/dexie.min.js"></script>
|
||||
<script src="/mixed/lib/wanakana.min.js"></script>
|
||||
<script src="/mixed/lib/jszip.min.js"></script>
|
||||
<script src="/mixed/js/util.js"></script>
|
||||
<script src="/bg/js/templates.js"></script>
|
||||
<script src="/bg/js/util.js"></script>
|
||||
|
@ -155,7 +155,7 @@ class Database {
|
||||
return this.db.dictionaries.toArray();
|
||||
}
|
||||
|
||||
importDictionary(indexUrl, callback) {
|
||||
importDictionary(archive, callback) {
|
||||
if (this.db === null) {
|
||||
return Promise.reject('database not initialized');
|
||||
}
|
||||
@ -204,7 +204,7 @@ class Database {
|
||||
|
||||
return this.db.terms.bulkAdd(rows).then(() => {
|
||||
if (callback) {
|
||||
callback(total, current, indexUrl);
|
||||
callback(total, current);
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -224,11 +224,11 @@ class Database {
|
||||
|
||||
return this.db.kanji.bulkAdd(rows).then(() => {
|
||||
if (callback) {
|
||||
callback(total, current, indexUrl);
|
||||
callback(total, current);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded).then(() => summary);
|
||||
return zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded).then(() => summary);
|
||||
}
|
||||
}
|
||||
|
@ -250,31 +250,27 @@ function onDictionaryPurge(e) {
|
||||
});
|
||||
}
|
||||
|
||||
function onDictionaryImport() {
|
||||
alert('import');
|
||||
function onDictionaryImport(e) {
|
||||
dictionaryErrorShow(null);
|
||||
dictionarySpinnerShow(true);
|
||||
|
||||
// dictionaryErrorShow(null);
|
||||
// dictionarySpinnerShow(true);
|
||||
const dictUrl = $('#dict-url');
|
||||
const dictImporter = $('#dict-importer').hide();
|
||||
const dictProgress = $('#dict-import-progress').show();
|
||||
const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`);
|
||||
|
||||
// const dictUrl = $('#dict-url');
|
||||
// const dictImporter = $('#dict-importer').hide();
|
||||
// const dictProgress = $('#dict-import-progress').show();
|
||||
// const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`);
|
||||
setProgress(0.0);
|
||||
|
||||
// setProgress(0.0);
|
||||
|
||||
// optionsLoad().then(options => {
|
||||
// instDb().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => {
|
||||
// options.dictionaries[summary.title] = {enabled: true, priority: 0};
|
||||
// return optionsSave(options);
|
||||
// }).then(() => dictionaryGroupsPopulate(options)).catch(dictionaryErrorShow).then(() => {
|
||||
// dictionarySpinnerShow(false);
|
||||
// dictProgress.hide();
|
||||
// dictImporter.show();
|
||||
// dictUrl.val('');
|
||||
// dictUrl.trigger('input');
|
||||
// });
|
||||
// });
|
||||
optionsLoad().then(options => {
|
||||
return instDb().importDictionary(e.target.files[0], (total, current) => setProgress(current / total * 100.0)).then(summary => {
|
||||
options.dictionaries[summary.title] = {enabled: true, priority: 0};
|
||||
return optionsSave(options);
|
||||
}).then(() => dictionaryGroupsPopulate(options));
|
||||
}).catch(dictionaryErrorShow).then(() => {
|
||||
dictionarySpinnerShow(false);
|
||||
dictProgress.hide();
|
||||
dictImporter.show();
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -451,14 +451,30 @@ function jsonLoadInt(url) {
|
||||
return jsonLoad(chrome.extension.getURL(url));
|
||||
}
|
||||
|
||||
function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
|
||||
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
|
||||
return jsonLoad(indexUrl).then(index => {
|
||||
/*
|
||||
* Zip
|
||||
*/
|
||||
|
||||
function zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded) {
|
||||
return JSZip.loadAsync(archive).then(files => {
|
||||
const fileMap = {};
|
||||
files.forEach((path, file) => {
|
||||
fileMap[path] = file;
|
||||
});
|
||||
|
||||
return fileMap;
|
||||
}).then(files => {
|
||||
const indexFile = files['index.json'];
|
||||
if (!indexFile) {
|
||||
return Promise.reject('no dictionary index found in archive');
|
||||
}
|
||||
|
||||
return indexFile.async('string').then(indexJson => {
|
||||
const index = JSON.parse(indexJson);
|
||||
if (!index.title || !index.version || !index.revision) {
|
||||
return Promise.reject('unrecognized dictionary format');
|
||||
}
|
||||
|
||||
if (indexLoaded !== null) {
|
||||
return indexLoaded(
|
||||
index.title,
|
||||
index.version,
|
||||
@ -467,32 +483,33 @@ function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
|
||||
index.termBanks > 0,
|
||||
index.kanjiBanks > 0
|
||||
).then(() => index);
|
||||
}
|
||||
|
||||
return index;
|
||||
}).then(index => {
|
||||
const loaders = [];
|
||||
const banksTotal = index.termBanks + index.kanjiBanks;
|
||||
let banksLoaded = 0;
|
||||
|
||||
for (let i = 1; i <= index.termBanks; ++i) {
|
||||
const bankUrl = `${indexDir}/term_bank_${i}.json`;
|
||||
loaders.push(() => jsonLoad(bankUrl).then(entries => termsLoaded(
|
||||
index.title,
|
||||
entries,
|
||||
banksTotal,
|
||||
banksLoaded++
|
||||
)));
|
||||
const bankFile = files[`term_bank_${i}.json`];
|
||||
if (!bankFile) {
|
||||
return Promise.reject('missing term bank file');
|
||||
}
|
||||
|
||||
loaders.push(() => bankFile.async('string').then(bankJson => {
|
||||
const bank = JSON.parse(bankJson);
|
||||
return termsLoaded(index.title, bank, banksTotal, banksLoaded++);
|
||||
}));
|
||||
}
|
||||
|
||||
for (let i = 1; i <= index.kanjiBanks; ++i) {
|
||||
const bankUrl = `${indexDir}/kanji_bank_${i}.json`;
|
||||
loaders.push(() => jsonLoad(bankUrl).then(entries => kanjiLoaded(
|
||||
index.title,
|
||||
entries,
|
||||
banksTotal,
|
||||
banksLoaded++
|
||||
)));
|
||||
const bankFile = files[`kanji_bank_${i}.json`];
|
||||
if (!bankFile) {
|
||||
return Promise.reject('missing kanji bank file');
|
||||
}
|
||||
|
||||
loaders.push(() => bankFile.async('string').then(bankJson => {
|
||||
const bank = JSON.parse(bankJson);
|
||||
return kanjiLoaded(index.title, bank, banksTotal, banksLoaded++);
|
||||
}));
|
||||
}
|
||||
|
||||
let chain = Promise.resolve();
|
||||
@ -502,9 +519,9 @@ function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
|
||||
|
||||
return chain;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Helpers
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user