Combined import
This commit is contained in:
parent
84fd2d13f5
commit
3f606a0ab0
@ -20,7 +20,7 @@
|
||||
class Dictionary {
|
||||
constructor() {
|
||||
this.db = null;
|
||||
this.dbVer = 4;
|
||||
this.dbVer = 5;
|
||||
this.entities = null;
|
||||
}
|
||||
|
||||
@ -131,7 +131,7 @@ class Dictionary {
|
||||
});
|
||||
}
|
||||
|
||||
getDictionaries() {
|
||||
getNames() {
|
||||
if (this.db === null) {
|
||||
return Promise.reject('database not initialized');
|
||||
}
|
||||
@ -139,7 +139,7 @@ class Dictionary {
|
||||
return this.db.dictionaries.toArray();
|
||||
}
|
||||
|
||||
importTermDict(indexUrl, callback) {
|
||||
importDb(indexUrl, callback) {
|
||||
if (this.db === null) {
|
||||
return Promise.reject('database not initialized');
|
||||
}
|
||||
@ -161,7 +161,7 @@ class Dictionary {
|
||||
});
|
||||
};
|
||||
|
||||
const entriesLoaded = (dictionary, version, entries, total, current) => {
|
||||
const termsLoaded = (dictionary, version, entries, total, current) => {
|
||||
const rows = [];
|
||||
for (const [expression, reading, tags, ...glossary] of entries) {
|
||||
rows.push({
|
||||
@ -175,24 +175,12 @@ class Dictionary {
|
||||
|
||||
return this.db.terms.bulkAdd(rows).then(() => {
|
||||
if (callback) {
|
||||
callback(current, total, indexUrl);
|
||||
callback(total, current, indexUrl);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return importJsonDb(indexUrl, indexLoaded, entriesLoaded);
|
||||
}
|
||||
|
||||
importKanjiDict(indexUrl, callback) {
|
||||
if (this.db === null) {
|
||||
return Promise.reject('database not initialized');
|
||||
}
|
||||
|
||||
const indexLoaded = (dictionary, version) => {
|
||||
return this.db.dictionaries.add({dictionary, version});
|
||||
};
|
||||
|
||||
const entriesLoaded = (dictionary, version, entries, total, current) => {
|
||||
const kanjiLoaded = (dictionary, version, entries, total, current) => {
|
||||
const rows = [];
|
||||
for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) {
|
||||
rows.push({
|
||||
@ -207,11 +195,11 @@ class Dictionary {
|
||||
|
||||
return this.db.kanji.bulkAdd(rows).then(() => {
|
||||
if (callback) {
|
||||
callback(current, total, indexUrl);
|
||||
callback(total, current, indexUrl);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return importJsonDb(indexUrl, indexLoaded, entriesLoaded);
|
||||
return importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded);
|
||||
}
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ function populateDictionaries() {
|
||||
// const dictGroups = $('.dictionaries');
|
||||
// dictGroups.empty();
|
||||
|
||||
// yomichan().translator.dictionary.getDictionaries().then(rows => {
|
||||
// yomichan().translator.dictionary.getNames().then(rows => {
|
||||
// for (const row of rows) {
|
||||
// const dictPanel = $('<div>', {class: 'dictionary panel panel-default'});
|
||||
// const dictRow = $('<div>', {class: 'panel-body row'}).appendTo(dictPanel);
|
||||
|
@ -46,7 +46,7 @@ class Translator {
|
||||
}
|
||||
|
||||
const banks = {};
|
||||
const bankCallback = (loaded, total, indexUrl) => {
|
||||
const bankCallback = (total, loaded, indexUrl) => {
|
||||
banks[indexUrl] = {loaded, total};
|
||||
|
||||
let percent = 0.0;
|
||||
@ -62,9 +62,9 @@ class Translator {
|
||||
};
|
||||
|
||||
return Promise.all([
|
||||
this.dictionary.importTermDict('bg/data/edict/index.json', bankCallback),
|
||||
this.dictionary.importTermDict('bg/data/enamdict/index.json', bankCallback),
|
||||
this.dictionary.importKanjiDict('bg/data/kanjidic/index.json', bankCallback),
|
||||
this.dictionary.importDb('bg/data/edict/index.json', bankCallback),
|
||||
this.dictionary.importDb('bg/data/enamdict/index.json', bankCallback),
|
||||
this.dictionary.importDb('bg/data/kanjidic/index.json', bankCallback),
|
||||
]).then(() => {
|
||||
return this.dictionary.sealDb();
|
||||
}).then(() => {
|
||||
|
@ -43,15 +43,6 @@ function promiseCallback(promise, callback) {
|
||||
});
|
||||
}
|
||||
|
||||
function loadJson(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText)));
|
||||
xhr.open('GET', chrome.extension.getURL(url));
|
||||
xhr.send();
|
||||
});
|
||||
}
|
||||
|
||||
function sortTags(tags) {
|
||||
return tags.sort((v1, v2) => {
|
||||
const order1 = v1.order;
|
||||
@ -117,19 +108,48 @@ function splitField(field) {
|
||||
return field.length === 0 ? [] : field.split(' ');
|
||||
}
|
||||
|
||||
function importJsonDb(indexUrl, indexLoaded, entriesLoaded) {
|
||||
function loadJson(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText)));
|
||||
xhr.open('GET', chrome.extension.getURL(url));
|
||||
xhr.send();
|
||||
});
|
||||
}
|
||||
|
||||
function importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
|
||||
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
|
||||
return loadJson(indexUrl).then(index => {
|
||||
if (indexLoaded !== null) {
|
||||
return indexLoaded(index.title, index.version, index.entities, index.banks).then(() => index);
|
||||
return indexLoaded(index.title, index.version).then(() => index);
|
||||
}
|
||||
|
||||
return index;
|
||||
}).then(index => {
|
||||
const loaders = [];
|
||||
for (let i = 1; i <= index.banks; ++i) {
|
||||
const bankUrl = `${indexDir}/bank_${i}.json`;
|
||||
loaders.push(() => loadJson(bankUrl).then(entries => entriesLoaded(index.title, index.version, entries, index.banks, i)));
|
||||
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(() => loadJson(bankUrl).then(entries => termsLoaded(
|
||||
index.title,
|
||||
index.version,
|
||||
entries,
|
||||
banksTotal,
|
||||
banksLoaded++
|
||||
)));
|
||||
}
|
||||
|
||||
for (let i = 1; i <= index.kanjiBanks; ++i) {
|
||||
const bankUrl = `${indexDir}/kanji_bank_${i}.json`;
|
||||
loaders.push(() => loadJson(bankUrl).then(entries => kanjiLoaded(
|
||||
index.title,
|
||||
index.version,
|
||||
entries,
|
||||
banksTotal,
|
||||
banksLoaded++
|
||||
)));
|
||||
}
|
||||
|
||||
let chain = Promise.resolve();
|
||||
|
Loading…
Reference in New Issue
Block a user