From 67f906ab24acb80a8ffbad29ff8ddda5fc570cf0 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 21 Aug 2016 13:32:36 -0700 Subject: Import stubs --- ext/bg/js/dictionary.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index a6438523..082d1479 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -21,20 +21,32 @@ class Dictionary { constructor() { this.termDicts = {}; this.kanjiDicts = {}; + this.db = new Dexie('dict'); + this.dbVer = 1; } - addTermDict(name, dict) { + loadDb() { + return this.db.open().then((db) => { + if (db.verno !== this.dbVer) { + Promise.reject('db version mismatch'); + } + + return db.verno; + }); + } + + importTermDict(name, dict) { this.termDicts[name] = dict; } - addKanjiDict(name, dict) { + importKanjiDict(name, dict) { this.kanjiDicts[name] = dict; } findTerm(term) { let results = []; - for (let name in this.termDicts) { + for (const name in this.termDicts) { const dict = this.termDicts[name]; if (!(term in dict.i)) { continue; @@ -62,7 +74,7 @@ class Dictionary { findKanji(kanji) { const results = []; - for (let name in this.kanjiDicts) { + for (const name in this.kanjiDicts) { const def = this.kanjiDicts[name].c[kanji]; if (def) { const [k, o, t, ...g] = def; -- cgit v1.2.3 From a062b25178dd8d916a8cdf87cffa36b5365ab021 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 21 Aug 2016 19:51:12 -0700 Subject: Work on DB integration --- ext/bg/js/dictionary.js | 99 +++++++++++++++++++++++++++---------------------- ext/bg/js/translator.js | 7 ++-- 2 files changed, 58 insertions(+), 48 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 082d1479..7b885db2 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -19,10 +19,9 @@ class Dictionary { constructor() { - this.termDicts = {}; - this.kanjiDicts = {}; this.db = new Dexie('dict'); this.dbVer = 1; + this.entities = null; } loadDb() { @@ -35,59 +34,69 @@ class Dictionary { }); } - importTermDict(name, dict) { - this.termDicts[name] = dict; + initDb() { + this.entities = {}; + return this.db.version(this.dbVer).stores({ + terms: 'expression, reading', + entities: 'name', + kanji: 'character', + }); } - importKanjiDict(name, dict) { - this.kanjiDicts[name] = dict; - } + importTermDict(dict) { + this.entities = {}; + return this.db.terms.bulkAdd(dict.d, 'expression, reading, tags, glossary').then(() => { + for (const [key, value] of dict.e) { + this.entities[key] = value; + } - findTerm(term) { - let results = []; + return this.db.entities.bulkAdd(dict.e, 'name, value'); + }); + } - for (const name in this.termDicts) { - const dict = this.termDicts[name]; - if (!(term in dict.i)) { - continue; - } + importKanjiDict(dict) { + return this.db.kanji.bulkAdd(dict.d, 'character, onyomi, kunyomi, tags, glossary'); + } - const indices = dict.i[term].split(' ').map(Number); - results = results.concat( - indices.map(index => { - const [e, r, t, ...g] = dict.d[index]; - return { - expression: e, - reading: r, - tags: t.split(' '), - glossary: g, - entities: dict.e, - id: index - }; - }) - ); + fetchEntities() { + if (this.entities !== null) { + return Promise.resolve(this.entities); } - return results; + this.entities = {}; + return this.db.entities.each((row) => { + this.entities[row.name] = row.value; + }).then(() => { + return Promise.resolve(this.entities); + }); } - findKanji(kanji) { + findterm(term) { const results = []; + return this.db.terms.where('expression').equals(term).or('reading').equals(term).each((row) => { + results.push({ + expression: row.expression, + reading: row.reading, + tags: row.tags.split(' '), + glossary: row.glossary, + entities: this.entities, + id: results.length + }); + }).then(() => { + Promise.resolve(results); + }); + } - for (const name in this.kanjiDicts) { - const def = this.kanjiDicts[name].c[kanji]; - if (def) { - const [k, o, t, ...g] = def; - results.push({ - character: kanji, - kunyomi: k.split(' '), - onyomi: o.split(' '), - tags: t.split(' '), - glossary: g - }); - } - } - - return results; + findKanji(kanji) { + const results = []; + return this.db.kanji.where('character').equals(kanji).each((row) => { + results.push({ + character: row.character, + onyomi: row.onyomi.split(' '), + kunyomi: row.kunyomi.split(' '), + tags: row.tags.split(' '), + glossary: row.glossary + }); + }); } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index f373fe83..9fd1ab59 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -41,15 +41,16 @@ class Translator { this.loaded = true; callback(); }).catch(() => { + this.dictionary.initDb(); return Translator.loadData('bg/data/edict.json'); }).then((response) => { - this.dictionary.importTermDict('edict', JSON.parse(response)); + this.dictionary.importTermDict(JSON.parse(response)); return Translator.loadData('bg/data/enamdict.json'); }).then((response) => { - this.dictionary.importTermDict('enamdict', JSON.parse(response)); + this.dictionary.importTermDict(JSON.parse(response)); return Translator.loadData('bg/data/kanjidic.json'); }).then((response) => { - this.dictionary.importKanjiDict('kanjidic', JSON.parse(response)); + this.dictionary.importKanjiDict(JSON.parse(response)); this.loaded = true; callback(); }); -- cgit v1.2.3 From 9621a0cd4b8ea395b78418d2aca210183394f4a4 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 22 Aug 2016 08:48:19 -0700 Subject: WIP --- ext/bg/js/dictionary.js | 75 +++++++++++++++++++++++++++---------------------- ext/bg/js/translator.js | 24 +++++++++------- 2 files changed, 55 insertions(+), 44 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 7b885db2..a3793cd6 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -19,43 +19,48 @@ class Dictionary { constructor() { - this.db = new Dexie('dict'); - this.dbVer = 1; + this.db = null; this.entities = null; } loadDb() { - return this.db.open().then((db) => { - if (db.verno !== this.dbVer) { - Promise.reject('db version mismatch'); - } + this.db = null; + this.entities = null; - return db.verno; + return new Dexie('dict').open().then((db) => { + this.db = db; }); } - initDb() { - this.entities = {}; - return this.db.version(this.dbVer).stores({ - terms: 'expression, reading', - entities: 'name', - kanji: 'character', + resetDb() { + this.db = null; + this.entities = null; + + return new Dexie('dict').delete().then(() => { + return Promise.resolve(new Dexie('dict')); + }).then((db) => { + this.db = db; + return this.db.version(1).stores({ + terms: '++id, e, r', + entities: 'n', + kanji: 'c' + }); }); } importTermDict(dict) { - this.entities = {}; - return this.db.terms.bulkAdd(dict.d, 'expression, reading, tags, glossary').then(() => { - for (const [key, value] of dict.e) { - this.entities[key] = value; + return this.db.terms.bulkAdd(dict.d).then(() => { + this.entities = {}; + for (const name in dict.e) { + this.entities[name] = dict.e[name]; } - return this.db.entities.bulkAdd(dict.e, 'name, value'); + return this.db.entities.bulkAdd(dict.e); }); } importKanjiDict(dict) { - return this.db.kanji.bulkAdd(dict.d, 'character, onyomi, kunyomi, tags, glossary'); + return this.db.kanji.bulkAdd(dict.d); } fetchEntities() { @@ -63,9 +68,11 @@ class Dictionary { return Promise.resolve(this.entities); } - this.entities = {}; - return this.db.entities.each((row) => { - this.entities[row.name] = row.value; + return this.db.entities.toArray((rows) => { + this.entities = {}; + for (const row of rows) { + this.entities[row.name] = row.value; + } }).then(() => { return Promise.resolve(this.entities); }); @@ -73,14 +80,14 @@ class Dictionary { findterm(term) { const results = []; - return this.db.terms.where('expression').equals(term).or('reading').equals(term).each((row) => { + return this.db.terms.where('e').equals(term).or('r').equals(term).each((row) => { results.push({ - expression: row.expression, - reading: row.reading, - tags: row.tags.split(' '), - glossary: row.glossary, + expression: row.e, + reading: row.r, + tags: row.t.split(' '), + glossary: row.g, entities: this.entities, - id: results.length + id: results.id }); }).then(() => { Promise.resolve(results); @@ -89,13 +96,13 @@ class Dictionary { findKanji(kanji) { const results = []; - return this.db.kanji.where('character').equals(kanji).each((row) => { + return this.db.kanji.where('c').equals(kanji).each((row) => { results.push({ - character: row.character, - onyomi: row.onyomi.split(' '), - kunyomi: row.kunyomi.split(' '), - tags: row.tags.split(' '), - glossary: row.glossary + character: row.c, + onyomi: row.o.split(' '), + kunyomi: row.k.split(' '), + tags: row.t.split(' '), + glossary: row.m }); }); } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 9fd1ab59..75f91055 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -41,16 +41,20 @@ class Translator { this.loaded = true; callback(); }).catch(() => { - this.dictionary.initDb(); - return Translator.loadData('bg/data/edict.json'); - }).then((response) => { - this.dictionary.importTermDict(JSON.parse(response)); - return Translator.loadData('bg/data/enamdict.json'); - }).then((response) => { - this.dictionary.importTermDict(JSON.parse(response)); - return Translator.loadData('bg/data/kanjidic.json'); - }).then((response) => { - this.dictionary.importKanjiDict(JSON.parse(response)); + return this.dictionary.resetDb().then(() => { + return Translator.loadData('bg/data/edict.json'); + }).then((response) => { + return this.dictionary.importTermDict(JSON.parse(response)); + }).then(() => { + return Translator.loadData('bg/data/enamdict.json'); + }).then((response) => { + return this.dictionary.importTermDict(JSON.parse(response)); + }).then(() => { + return Translator.loadData('bg/data/kanjidic.json'); + }).then((response) => { + return this.dictionary.importKanjiDict(JSON.parse(response)); + }); + }).then(() => { this.loaded = true; callback(); }); -- cgit v1.2.3 From f106b64876c975237e8c8bb51518ab53d2a9d2fc Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 23 Aug 2016 20:33:04 -0700 Subject: WIP --- ext/bg/js/dictionary.js | 117 ++++++++++++++++++++++++++++++++++-------------- ext/bg/js/translator.js | 31 ++++++------- 2 files changed, 97 insertions(+), 51 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index a3793cd6..dd46064a 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -23,40 +23,26 @@ class Dictionary { this.entities = null; } - loadDb() { - this.db = null; - this.entities = null; - - return new Dexie('dict').open().then((db) => { - this.db = db; - }); + existsDb() { + return Dexie.exists('dict'); } - resetDb() { + loadDb() { this.db = null; this.entities = null; - return new Dexie('dict').delete().then(() => { - return Promise.resolve(new Dexie('dict')); - }).then((db) => { - this.db = db; - return this.db.version(1).stores({ - terms: '++id, e, r', - entities: 'n', - kanji: 'c' - }); - }); + return this.initDb().open(); } - importTermDict(dict) { - return this.db.terms.bulkAdd(dict.d).then(() => { - this.entities = {}; - for (const name in dict.e) { - this.entities[name] = dict.e[name]; - } - - return this.db.entities.bulkAdd(dict.e); + initDb() { + this.db = new Dexie('dict'); + this.db.version(1).stores({ + terms: '++id,expression,reading', + entities: '++id,name', + kanji: '++id,character' }); + + return this.db; } importKanjiDict(dict) { @@ -78,19 +64,19 @@ class Dictionary { }); } - findterm(term) { + findTerm(term) { const results = []; - return this.db.terms.where('e').equals(term).or('r').equals(term).each((row) => { + return this.db.terms.where('expression').equals(term).or('reading').equals(term).each((row) => { results.push({ - expression: row.e, - reading: row.r, - tags: row.t.split(' '), - glossary: row.g, + expression: row.expression, + reading: row.reading, + tags: row.tags.split(' '), + glossary: row.glossary, entities: this.entities, - id: results.id + id: row.id }); }).then(() => { - Promise.resolve(results); + return Promise.resolve(results); }); } @@ -106,4 +92,67 @@ class Dictionary { }); }); } + + // importTermDict(dict) { + // return this.db.terms.bulkAdd(dict.d).then(() => { + // this.entities = {}; + // for (const name in dict.e) { + // this.entities[name] = dict.e[name]; + // } + + // return this.db.entities.bulkAdd(dict.e); + // }); + // } + + importTermDict(indexUrl) { + return Dictionary.loadJson(indexUrl).then((index) => { + const entities = []; + for (const [name, value] of index.ents) { + entities.push({name, value}); + } + + return this.db.entities.bulkAdd(entities).then(() => { + if (this.entities === null) { + this.entities = {}; + } + + for (const entity of entities) { + this.entities[entity.name] = entity.value; + } + }).then(() => { + const loaders = []; + const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); + + for (let i = 0; i < index.refs; ++i) { + const refUrl = `${indexDir}/ref_${i}.json`; + loaders.push( + Dictionary.loadJson(refUrl).then((refs) => { + const rows = []; + for (const [e, r, t, ...g] of refs) { + rows.push({ + 'expression': e, + 'reading': r, + 'tags': t, + 'glossary': g + }); + } + + return this.db.terms.bulkAdd(rows); + }) + ); + } + + return Promise.all(loaders); + }); + }); + } + + static 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), true); + xhr.send(); + }); + } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 75f91055..f9ac1d56 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -36,27 +36,24 @@ class Translator { return Translator.loadData('bg/data/tags.json'); }).then((response) => { this.tagMeta = JSON.parse(response); - return this.dictionary.loadDb(); + return this.dictionary.existsDb(); + }).then((exists) => { + if (exists) { + return this.dictionary.loadDb(); + } else { + this.dictionary.initDb(); + return Promise.all([ + this.dictionary.importTermDict('bg/data/edict/index.json'), + this.dictionary.importTermDict('bg/data/enamdict/index.json') + ]); + } }).then(() => { this.loaded = true; callback(); - }).catch(() => { - return this.dictionary.resetDb().then(() => { - return Translator.loadData('bg/data/edict.json'); - }).then((response) => { - return this.dictionary.importTermDict(JSON.parse(response)); - }).then(() => { - return Translator.loadData('bg/data/enamdict.json'); - }).then((response) => { - return this.dictionary.importTermDict(JSON.parse(response)); - }).then(() => { - return Translator.loadData('bg/data/kanjidic.json'); - }).then((response) => { - return this.dictionary.importKanjiDict(JSON.parse(response)); + + this.dictionary.findTerm('猫').then((result) => { + console.log(result); }); - }).then(() => { - this.loaded = true; - callback(); }); } -- cgit v1.2.3 From 6366d9bd8e5758d631e5bba14b2c892e0b27c474 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 23 Aug 2016 20:53:11 -0700 Subject: WIP --- ext/bg/js/dictionary.js | 28 ++++++++++++++-------------- ext/bg/js/translator.js | 20 ++++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index dd46064a..44e97752 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -38,8 +38,8 @@ class Dictionary { this.db = new Dexie('dict'); this.db.version(1).stores({ terms: '++id,expression,reading', - entities: '++id,name', - kanji: '++id,character' + entities: '++,name', + kanji: '++,character' }); return this.db; @@ -125,24 +125,24 @@ class Dictionary { for (let i = 0; i < index.refs; ++i) { const refUrl = `${indexDir}/ref_${i}.json`; - loaders.push( - Dictionary.loadJson(refUrl).then((refs) => { + loaders.push(() => { + return Dictionary.loadJson(refUrl).then((refs) => { const rows = []; - for (const [e, r, t, ...g] of refs) { - rows.push({ - 'expression': e, - 'reading': r, - 'tags': t, - 'glossary': g - }); + for (const [expression, reading, tags, ...glossary] of refs) { + rows.push({expression, reading, tags, glossary}); } return this.db.terms.bulkAdd(rows); - }) - ); + }); + }); } - return Promise.all(loaders); + let chain = Promise.resolve(); + for (const loader of loaders) { + chain = chain.then(loader); + } + + return chain; }); }); } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index f9ac1d56..8af0e31b 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -31,11 +31,11 @@ class Translator { return; } - Translator.loadData('bg/data/rules.json').then((response) => { - this.deinflector.setRules(JSON.parse(response)); - return Translator.loadData('bg/data/tags.json'); - }).then((response) => { - this.tagMeta = JSON.parse(response); + Translator.loadJson('bg/data/rules.json').then((rules) => { + this.deinflector.setRules(rules); + return Translator.loadJson('bg/data/tags.json'); + }).then((tagMeta) => { + this.tagMeta = tagMeta; return this.dictionary.existsDb(); }).then((exists) => { if (exists) { @@ -48,12 +48,12 @@ class Translator { ]); } }).then(() => { - this.loaded = true; - callback(); - this.dictionary.findTerm('猫').then((result) => { console.log(result); }); + + this.loaded = true; + callback(); }); } @@ -246,10 +246,10 @@ class Translator { return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0; } - static loadData(url) { + static loadJson(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); - xhr.addEventListener('load', () => resolve(xhr.responseText)); + xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText))); xhr.open('GET', chrome.extension.getURL(url), true); xhr.send(); }); -- cgit v1.2.3 From 8b5f74f99bdbaeb1b7c72614f2a71abfc72be479 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 23 Aug 2016 22:22:09 -0700 Subject: WIP --- ext/bg/js/dictionary.js | 83 ++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 35 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 44e97752..8f04c458 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -27,13 +27,6 @@ class Dictionary { return Dexie.exists('dict'); } - loadDb() { - this.db = null; - this.entities = null; - - return this.initDb().open(); - } - initDb() { this.db = new Dexie('dict'); this.db.version(1).stores({ @@ -45,23 +38,11 @@ class Dictionary { return this.db; } - importKanjiDict(dict) { - return this.db.kanji.bulkAdd(dict.d); - } - - fetchEntities() { - if (this.entities !== null) { - return Promise.resolve(this.entities); - } + loadDb() { + this.db = null; + this.entities = null; - return this.db.entities.toArray((rows) => { - this.entities = {}; - for (const row of rows) { - this.entities[row.name] = row.value; - } - }).then(() => { - return Promise.resolve(this.entities); - }); + return this.initDb().open(); } findTerm(term) { @@ -93,18 +74,24 @@ class Dictionary { }); } - // importTermDict(dict) { - // return this.db.terms.bulkAdd(dict.d).then(() => { - // this.entities = {}; - // for (const name in dict.e) { - // this.entities[name] = dict.e[name]; - // } + getEntities() { + if (this.entities !== null) { + return Promise.resolve(this.entities); + } - // return this.db.entities.bulkAdd(dict.e); - // }); - // } + return this.db.entities.toArray((rows) => { + this.entities = {}; + for (const row of rows) { + this.entities[row.name] = row.value; + } + }).then(() => { + return Promise.resolve(this.entities); + }); + } importTermDict(indexUrl) { + const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); + return Dictionary.loadJson(indexUrl).then((index) => { const entities = []; for (const [name, value] of index.ents) { @@ -121,9 +108,7 @@ class Dictionary { } }).then(() => { const loaders = []; - const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); - - for (let i = 0; i < index.refs; ++i) { + for (let i = 0; i <= index.refs; ++i) { const refUrl = `${indexDir}/ref_${i}.json`; loaders.push(() => { return Dictionary.loadJson(refUrl).then((refs) => { @@ -147,6 +132,34 @@ class Dictionary { }); } + importKanjiDict(indexUrl) { + const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); + + return Dictionary.loadJson(indexUrl).then((index) => { + const loaders = []; + for (let i = 0; i <= index.refs; ++i) { + const refUrl = `${indexDir}/ref_${i}.json`; + loaders.push(() => { + return Dictionary.loadJson(refUrl).then((refs) => { + const rows = []; + for (const [character, onyomi, kunyomi, tags, ...glossary] of refs) { + rows.push({character, onyomi, kunyomi, tags, glossary}); + } + + return this.db.kanji.bulkAdd(rows); + }); + }); + } + + let chain = Promise.resolve(); + for (const loader of loaders) { + chain = chain.then(loader); + } + + return chain; + }); + } + static loadJson(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); -- cgit v1.2.3 From b2d9b613ad3a673abb20033808877962545644d4 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 23 Aug 2016 22:28:37 -0700 Subject: Cleanup --- ext/bg/background.html | 1 + ext/bg/js/dictionary.js | 17 ++++------------- ext/bg/js/translator.js | 18 ++---------------- ext/bg/js/util.js | 33 +++++++++++++++++++++++++++++++++ ext/bg/js/yomichan.js | 2 +- 5 files changed, 41 insertions(+), 30 deletions(-) create mode 100644 ext/bg/js/util.js (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/background.html b/ext/bg/background.html index e68c665b..c490df81 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -4,6 +4,7 @@ + diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 8f04c458..3fceef65 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -92,7 +92,7 @@ class Dictionary { importTermDict(indexUrl) { const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); - return Dictionary.loadJson(indexUrl).then((index) => { + return loadJson(indexUrl).then((index) => { const entities = []; for (const [name, value] of index.ents) { entities.push({name, value}); @@ -111,7 +111,7 @@ class Dictionary { for (let i = 0; i <= index.refs; ++i) { const refUrl = `${indexDir}/ref_${i}.json`; loaders.push(() => { - return Dictionary.loadJson(refUrl).then((refs) => { + return loadJson(refUrl).then((refs) => { const rows = []; for (const [expression, reading, tags, ...glossary] of refs) { rows.push({expression, reading, tags, glossary}); @@ -135,12 +135,12 @@ class Dictionary { importKanjiDict(indexUrl) { const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); - return Dictionary.loadJson(indexUrl).then((index) => { + return loadJson(indexUrl).then((index) => { const loaders = []; for (let i = 0; i <= index.refs; ++i) { const refUrl = `${indexDir}/ref_${i}.json`; loaders.push(() => { - return Dictionary.loadJson(refUrl).then((refs) => { + return loadJson(refUrl).then((refs) => { const rows = []; for (const [character, onyomi, kunyomi, tags, ...glossary] of refs) { rows.push({character, onyomi, kunyomi, tags, glossary}); @@ -159,13 +159,4 @@ class Dictionary { return chain; }); } - - static 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), true); - xhr.send(); - }); - } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 8af0e31b..30045378 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -31,9 +31,9 @@ class Translator { return; } - Translator.loadJson('bg/data/rules.json').then((rules) => { + loadJson('bg/data/rules.json').then((rules) => { this.deinflector.setRules(rules); - return Translator.loadJson('bg/data/tags.json'); + return loadJson('bg/data/tags.json'); }).then((tagMeta) => { this.tagMeta = tagMeta; return this.dictionary.existsDb(); @@ -240,18 +240,4 @@ class Translator { return 0; }); } - - static isKanji(c) { - const code = c.charCodeAt(0); - return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0; - } - - static 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), true); - xhr.send(); - }); - } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js new file mode 100644 index 00000000..97906eda --- /dev/null +++ b/ext/bg/js/util.js @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +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), true); + xhr.send(); + }); +} + +function isKanji(c) { + const code = c.charCodeAt(0); + return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0; +} + diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index fd9b84d3..716c622a 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -23,7 +23,7 @@ class Yomichan { Handlebars.registerHelper('kanjiLinks', function(options) { let result = ''; for (const c of options.fn(this)) { - if (Translator.isKanji(c)) { + if (isKanji(c)) { result += Handlebars.templates['kanji-link.html']({kanji: c}).trim(); } else { result += c; -- cgit v1.2.3 From 4bebe9215999e9983e47e3fc9a1e7d6d94d0a3bb Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 24 Aug 2016 09:14:23 -0700 Subject: WIP --- .gitattributes | 4 +++- ext/bg/js/dictionary.js | 18 +++++++++--------- ext/bg/js/translator.js | 1 + 3 files changed, 13 insertions(+), 10 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/.gitattributes b/.gitattributes index c575774d..c891610e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,5 @@ -util/data/*dic* filter=lfs diff=lfs merge=lfs -text ext/bg/data/*dic* filter=lfs diff=lfs merge=lfs -text +ext/bg/data/edict/*.json filter=lfs diff=lfs merge=lfs -text +ext/bg/data/enamdict/*.json filter=lfs diff=lfs merge=lfs -text +ext/bg/data/kanjidic/*.json filter=lfs diff=lfs merge=lfs -text *.ttf filter=lfs diff=lfs merge=lfs -text diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 3fceef65..4baa41c8 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -108,12 +108,12 @@ class Dictionary { } }).then(() => { const loaders = []; - for (let i = 0; i <= index.refs; ++i) { - const refUrl = `${indexDir}/ref_${i}.json`; + for (let i = 1; i <= index.banks; ++i) { + const bankUrl = `${indexDir}/bank_${i}.json`; loaders.push(() => { - return loadJson(refUrl).then((refs) => { + return loadJson(bankUrl).then((defs) => { const rows = []; - for (const [expression, reading, tags, ...glossary] of refs) { + for (const [expression, reading, tags, ...glossary] of defs) { rows.push({expression, reading, tags, glossary}); } @@ -137,13 +137,13 @@ class Dictionary { return loadJson(indexUrl).then((index) => { const loaders = []; - for (let i = 0; i <= index.refs; ++i) { - const refUrl = `${indexDir}/ref_${i}.json`; + for (let i = 1; i <= index.banks; ++i) { + const bankUrl = `${indexDir}/bank_${i}.json`; loaders.push(() => { - return loadJson(refUrl).then((refs) => { + return loadJson(bankUrl).then((defs) => { const rows = []; - for (const [character, onyomi, kunyomi, tags, ...glossary] of refs) { - rows.push({character, onyomi, kunyomi, tags, glossary}); + for (const [character, onyomi, kunyomi, tags, ...meanings] of defs) { + rows.push({character, onyomi, kunyomi, tags, meanings}); } return this.db.kanji.bulkAdd(rows); diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 30045378..d1a92d08 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -43,6 +43,7 @@ class Translator { } else { this.dictionary.initDb(); return Promise.all([ + this.dictionary.importKanjiDict('bg/data/kanjidic/index.json'), this.dictionary.importTermDict('bg/data/edict/index.json'), this.dictionary.importTermDict('bg/data/enamdict/index.json') ]); -- cgit v1.2.3 From 9462bc397b45aef0034f5ef8d11294125198581b Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 28 Aug 2016 21:02:51 -0700 Subject: Cleanup --- ext/bg/js/dictionary.js | 11 ++--------- ext/bg/js/translator.js | 16 ++++++++-------- 2 files changed, 10 insertions(+), 17 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 4baa41c8..b7b7039c 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -28,21 +28,14 @@ class Dictionary { } initDb() { + this.entities = null; + this.db = new Dexie('dict'); this.db.version(1).stores({ terms: '++id,expression,reading', entities: '++,name', kanji: '++,character' }); - - return this.db; - } - - loadDb() { - this.db = null; - this.entities = null; - - return this.initDb().open(); } findTerm(term) { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index d1a92d08..f7dfae13 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -38,16 +38,16 @@ class Translator { this.tagMeta = tagMeta; return this.dictionary.existsDb(); }).then((exists) => { + this.dictionary.initDb(); if (exists) { - return this.dictionary.loadDb(); - } else { - this.dictionary.initDb(); - return Promise.all([ - this.dictionary.importKanjiDict('bg/data/kanjidic/index.json'), - this.dictionary.importTermDict('bg/data/edict/index.json'), - this.dictionary.importTermDict('bg/data/enamdict/index.json') - ]); + return Promise.resolve(); } + + return Promise.all([ + this.dictionary.importKanjiDict('bg/data/kanjidic/index.json'), + this.dictionary.importTermDict('bg/data/edict/index.json'), + this.dictionary.importTermDict('bg/data/enamdict/index.json') + ]); }).then(() => { this.dictionary.findTerm('猫').then((result) => { console.log(result); -- cgit v1.2.3 From c55ba3b2be267d9dc616bda1a293d136c63cd639 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 29 Aug 2016 19:51:37 -0700 Subject: WIP --- ext/bg/js/dictionary.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index b7b7039c..7cfa58a8 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -19,8 +19,8 @@ class Dictionary { constructor() { - this.db = null; this.entities = null; + this.db = new Dexie('dict'); } existsDb() { @@ -29,8 +29,6 @@ class Dictionary { initDb() { this.entities = null; - - this.db = new Dexie('dict'); this.db.version(1).stores({ terms: '++id,expression,reading', entities: '++,name', @@ -46,11 +44,16 @@ class Dictionary { reading: row.reading, tags: row.tags.split(' '), glossary: row.glossary, - entities: this.entities, id: row.id }); }).then(() => { - return Promise.resolve(results); + return this.getEntities(); + }).then((entities) => { + for (const result of results) { + result.entities = entities; + } + + return results; }); } @@ -67,9 +70,9 @@ class Dictionary { }); } - getEntities() { + getEntities(tags) { if (this.entities !== null) { - return Promise.resolve(this.entities); + return this.entities; } return this.db.entities.toArray((rows) => { @@ -77,8 +80,8 @@ class Dictionary { for (const row of rows) { this.entities[row.name] = row.value; } - }).then(() => { - return Promise.resolve(this.entities); + + return this.entities; }); } -- cgit v1.2.3 From 47ef617eb4d951233d43c8da6e13a8bd2e40a863 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 10 Sep 2016 19:40:56 -0700 Subject: Cleanup --- ext/bg/js/dictionary.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 7cfa58a8..624fe15e 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -38,7 +38,7 @@ class Dictionary { findTerm(term) { const results = []; - return this.db.terms.where('expression').equals(term).or('reading').equals(term).each((row) => { + return this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => { results.push({ expression: row.expression, reading: row.reading, @@ -48,7 +48,7 @@ class Dictionary { }); }).then(() => { return this.getEntities(); - }).then((entities) => { + }).then(entities => { for (const result of results) { result.entities = entities; } @@ -59,7 +59,7 @@ class Dictionary { findKanji(kanji) { const results = []; - return this.db.kanji.where('c').equals(kanji).each((row) => { + return this.db.kanji.where('c').equals(kanji).each(row => { results.push({ character: row.c, onyomi: row.o.split(' '), @@ -75,7 +75,7 @@ class Dictionary { return this.entities; } - return this.db.entities.toArray((rows) => { + return this.db.entities.toArray(rows => { this.entities = {}; for (const row of rows) { this.entities[row.name] = row.value; @@ -88,7 +88,7 @@ class Dictionary { importTermDict(indexUrl) { const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); - return loadJson(indexUrl).then((index) => { + return loadJson(indexUrl).then(index => { const entities = []; for (const [name, value] of index.ents) { entities.push({name, value}); @@ -107,9 +107,9 @@ class Dictionary { for (let i = 1; i <= index.banks; ++i) { const bankUrl = `${indexDir}/bank_${i}.json`; loaders.push(() => { - return loadJson(bankUrl).then((defs) => { + return loadJson(bankUrl).then(definitions => { const rows = []; - for (const [expression, reading, tags, ...glossary] of defs) { + for (const [expression, reading, tags, ...glossary] of definitions) { rows.push({expression, reading, tags, glossary}); } @@ -131,14 +131,14 @@ class Dictionary { importKanjiDict(indexUrl) { const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); - return loadJson(indexUrl).then((index) => { + return loadJson(indexUrl).then(index => { const loaders = []; for (let i = 1; i <= index.banks; ++i) { const bankUrl = `${indexDir}/bank_${i}.json`; loaders.push(() => { - return loadJson(bankUrl).then((defs) => { + return loadJson(bankUrl).then(definitions => { const rows = []; - for (const [character, onyomi, kunyomi, tags, ...meanings] of defs) { + for (const [character, onyomi, kunyomi, tags, ...meanings] of definitions) { rows.push({character, onyomi, kunyomi, tags, meanings}); } -- cgit v1.2.3 From d5ea03171ea997d6734e6d31197c7f233fff7084 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 11 Sep 2016 12:29:18 -0700 Subject: Working with IndexDb --- ext/bg/js/deinflector.js | 12 +++++------- ext/bg/js/dictionary.js | 14 +++++++------- ext/bg/js/translator.js | 35 ++++++++++++++++++++++------------- ext/bg/js/yomichan.js | 18 +++++++++--------- 4 files changed, 43 insertions(+), 36 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js index 4cdc9a3d..e5b1efe5 100644 --- a/ext/bg/js/deinflector.js +++ b/ext/bg/js/deinflector.js @@ -26,14 +26,14 @@ class Deinflection { } validate(validator) { - return validator(this.term).then(tagSets => { - for (const tags of tagSets) { + return validator(this.term).then(sets => { + for (const tags of sets) { if (this.tags.length === 0) { return true; } for (const tag of this.tags) { - if (tags.indexOf(tag) !== -1) { + if (tags.includes(tag)) { return true; } } @@ -55,7 +55,7 @@ class Deinflection { for (const variant of rules[rule]) { let allowed = this.tags.length === 0; for (const tag of this.tags) { - if (variant.ti.indexOf(tag) !== -1) { + if (variant.ti.includes(tag)) { allowed = true; break; } @@ -115,8 +115,6 @@ class Deinflector { deinflect(term, validator) { const node = new Deinflection(term); - return node.deinflect(validator, this.rules).then(success => { - return success ? node.gather() : []; - }); + return node.deinflect(validator, this.rules).then(success => success ? node.gather() : []); } } diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 624fe15e..0c5e4c4a 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -59,15 +59,15 @@ class Dictionary { findKanji(kanji) { const results = []; - return this.db.kanji.where('c').equals(kanji).each(row => { + return this.db.kanji.where('character').equals(kanji).each(row => { results.push({ - character: row.c, - onyomi: row.o.split(' '), - kunyomi: row.k.split(' '), - tags: row.t.split(' '), - glossary: row.m + character: row.character, + onyomi: row.onyomi.split(' '), + kunyomi: row.kunyomi.split(' '), + tags: row.tags.split(' '), + glossary: row.meanings }); - }); + }).then(() => results); } getEntities(tags) { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 9b7b4bf5..6b08f485 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -55,17 +55,23 @@ class Translator { } findTermGroups(text) { - const groups = {}; - + const deinflectGroups = {}; const deinflectPromises = []; + for (let i = text.length; i > 0; --i) { deinflectPromises.push( this.deinflector.deinflect(text.slice(0, i), term => { return this.dictionary.findTerm(term).then(definitions => definitions.map(definition => definition.tags)); - }).then(inflects => { + }).then(deinflects => { const processPromises = []; - for (const inflect of inflects) { - processPromises.push(this.processTerm(groups, inflect.source, inflect.tags, inflect.rules, inflect.root)); + for (const deinflect of deinflects) { + processPromises.push(this.processTerm( + deinflectGroups, + deinflect.source, + deinflect.tags, + deinflect.rules, + deinflect.root + )); } return Promise.all(processPromises); @@ -73,14 +79,14 @@ class Translator { ); } - return Promise.all(deinflectPromises).then(() => groups); + return Promise.all(deinflectPromises).then(() => deinflectGroups); } findTerm(text) { - return this.findTermGroups(text).then(groups => { + return this.findTermGroups(text).then(deinflectGroups => { let definitions = []; - for (const key in groups) { - definitions.push(groups[key]); + for (const key in deinflectGroups) { + definitions.push(deinflectGroups[key]); } definitions = definitions.sort((v1, v2) => { @@ -121,17 +127,20 @@ class Translator { } findKanji(text) { - let definitions = []; const processed = {}; + const promises = []; for (const c of text) { if (!processed[c]) { - definitions = definitions.concat(this.dictionary.findKanji(c)); + promises.push(this.dictionary.findKanji(c).then((definitions) => definitions)); processed[c] = true; } } - return this.processKanji(definitions); + return Promise.all(promises).then((sets) => { + const definitions = sets.reduce((a, b) => a.concat(b)); + return this.processKanji(definitions); + }); } processTerm(groups, source, tags, rules, root) { @@ -143,7 +152,7 @@ class Translator { let matched = tags.length === 0; for (const tag of tags) { - if (definition.tags.indexOf(tag) !== -1) { + if (definition.tags.includes(tag)) { matched = true; break; } diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 557f8780..11f348bf 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -41,10 +41,10 @@ class Yomichan { chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this)); - chrome.tabs.onCreated.addListener((tab) => this.onTabReady(tab.id)); + chrome.tabs.onCreated.addListener(tab => this.onTabReady(tab.id)); chrome.tabs.onUpdated.addListener(this.onTabReady.bind(this)); - loadOptions((opts) => { + loadOptions(opts => { this.setOptions(opts); if (this.options.activateOnStartup) { this.setState('loading'); @@ -118,7 +118,7 @@ class Yomichan { } tabInvokeAll(action, params) { - chrome.tabs.query({}, (tabs) => { + chrome.tabs.query({}, tabs => { for (const tab of tabs) { this.tabInvoke(tab.id, action, params); } @@ -133,7 +133,7 @@ class Yomichan { if (this.ankiConnectVer === this.getApiVersion()) { this.ankiInvoke(action, params, pool, callback); } else { - this.api_getVersion({callback: (version) => { + this.api_getVersion({callback: version => { if (version === this.getApiVersion()) { this.ankiConnectVer = version; this.ankiInvoke(action, params, pool, callback); @@ -209,7 +209,7 @@ class Yomichan { break; case 'tags': if (definition.tags) { - value = definition.tags.map((t) => t.name); + value = definition.tags.map(t => t.name); } break; } @@ -244,7 +244,7 @@ class Yomichan { }; for (const name in fields) { - if (fields[name].indexOf('{audio}') !== -1) { + if (fields[name].includes('{audio}')) { audio.fields.push(name); } } @@ -274,7 +274,7 @@ class Yomichan { } } - this.ankiInvokeSafe('canAddNotes', {notes}, 'notes', (results) => { + this.ankiInvokeSafe('canAddNotes', {notes}, 'notes', results => { const states = []; if (results !== null) { @@ -293,11 +293,11 @@ class Yomichan { } api_findKanji({text, callback}) { - callback(this.translator.findKanji(text)); + this.translator.findKanji(text).then(result => callback(result)); } api_findTerm({text, callback}) { - this.translator.findTerm(text).then((result) => callback(result)); + this.translator.findTerm(text).then(result => callback(result)); } api_getDeckNames({callback}) { -- cgit v1.2.3 From 4241b4f8595813b966371a02f5b2f35d7997b1e2 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 11 Sep 2016 22:47:08 -0700 Subject: WIP --- ext/bg/js/dictionary.js | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 0c5e4c4a..20a94f8f 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -19,24 +19,34 @@ class Dictionary { constructor() { + this.db = null; this.entities = null; - this.db = new Dexie('dict'); - } - - existsDb() { - return Dexie.exists('dict'); } initDb() { - this.entities = null; + this.db = new Dexie('dict'); this.db.version(1).stores({ terms: '++id,expression,reading', entities: '++,name', kanji: '++,character' }); + + this.entities = null; + } + + deleteDb() { + return this.db === null ? Promise.resolve() : this.db.delete(); + } + + existsDb() { + return Dexie.exists('dict'); } findTerm(term) { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + const results = []; return this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => { results.push({ @@ -58,6 +68,10 @@ class Dictionary { } findKanji(kanji) { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + const results = []; return this.db.kanji.where('character').equals(kanji).each(row => { results.push({ @@ -71,8 +85,12 @@ class Dictionary { } getEntities(tags) { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + if (this.entities !== null) { - return this.entities; + return Promise.resolve(this.entities); } return this.db.entities.toArray(rows => { @@ -86,8 +104,11 @@ class Dictionary { } importTermDict(indexUrl) { - const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); + if (this.db === null) { + return Promise.reject('database not initialized'); + } + const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); return loadJson(indexUrl).then(index => { const entities = []; for (const [name, value] of index.ents) { @@ -129,8 +150,11 @@ class Dictionary { } importKanjiDict(indexUrl) { - const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); + if (this.db === null) { + return Promise.reject('database not initialized'); + } + const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); return loadJson(indexUrl).then(index => { const loaders = []; for (let i = 1; i <= index.banks; ++i) { -- cgit v1.2.3 From 0e89d0e7e68d58407480e0e40e80c2f00f3c2f66 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 13 Sep 2016 15:59:18 -0700 Subject: Database stuff --- ext/bg/js/dictionary.js | 41 ++++++++++++++++++++++++++++++++++------- ext/bg/js/translator.js | 5 ++--- 2 files changed, 36 insertions(+), 10 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 20a94f8f..936dc3c1 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -20,26 +20,53 @@ class Dictionary { constructor() { this.db = null; + this.dbVer = 1; this.entities = null; } initDb() { + if (this.db !== null) { + return Promise.reject('database already initialized'); + } + this.db = new Dexie('dict'); this.db.version(1).stores({ terms: '++id,expression,reading', entities: '++,name', - kanji: '++,character' + kanji: '++,character', + meta: 'name,value', }); - - this.entities = null; } - deleteDb() { - return this.db === null ? Promise.resolve() : this.db.delete(); + prepareDb() { + this.initDb(); + + return this.db.meta.get('version').then(row => { + return row ? row.value : 0; + }).catch(() => { + return 0; + }).then(version => { + if (this.dbVer === version) { + return true; + } + + const db = this.db; + this.db.close(); + this.db = null; + + return db.delete().then(() => { + this.initDb(); + return false; + }); + }); } - existsDb() { - return Dexie.exists('dict'); + sealDb() { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + + return this.db.meta.put({name: 'version', value: this.dbVer}); } findTerm(term) { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index b8cad6de..2cc97e1c 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -35,15 +35,14 @@ class Translator { return loadJson('bg/data/tags.json'); }).then(tagMeta => { this.tagMeta = tagMeta; - return this.dictionary.existsDb(); + return this.dictionary.prepareDb(); }).then(exists => { - this.dictionary.initDb(); if (!exists) { return Promise.all([ this.dictionary.importKanjiDict('bg/data/kanjidic/index.json'), this.dictionary.importTermDict('bg/data/edict/index.json'), this.dictionary.importTermDict('bg/data/enamdict/index.json') - ]); + ]).then(() => this.dictionary.sealDb()); } }).then(() => { this.loaded = true; -- cgit v1.2.3 From 17366e521afe2dd3b32f2068db43f972ce89b36f Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 13 Sep 2016 20:36:13 -0700 Subject: Progress callback for dictionary loading --- ext/bg/js/dictionary.js | 16 ++++++++++++---- ext/bg/js/translator.js | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 11 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 936dc3c1..2bd39344 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -130,7 +130,7 @@ class Dictionary { }); } - importTermDict(indexUrl) { + importTermDict(indexUrl, callback) { if (this.db === null) { return Promise.reject('database not initialized'); } @@ -161,7 +161,11 @@ class Dictionary { rows.push({expression, reading, tags, glossary}); } - return this.db.terms.bulkAdd(rows); + return this.db.terms.bulkAdd(rows).then(() => { + if (callback) { + callback(i, index.banks); + } + }); }); }); } @@ -176,7 +180,7 @@ class Dictionary { }); } - importKanjiDict(indexUrl) { + importKanjiDict(indexUrl, callback) { if (this.db === null) { return Promise.reject('database not initialized'); } @@ -193,7 +197,11 @@ class Dictionary { rows.push({character, onyomi, kunyomi, tags, meanings}); } - return this.db.kanji.bulkAdd(rows); + return this.db.kanji.bulkAdd(rows).then(() => { + if (callback) { + callback(i, index.banks); + } + }); }); }); } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 2cc97e1c..1dc0dca1 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -25,7 +25,7 @@ class Translator { this.deinflector = new Deinflector(); } - loadData() { + loadData(callback) { if (this.loaded) { return Promise.resolve(); } @@ -37,13 +37,37 @@ class Translator { this.tagMeta = tagMeta; return this.dictionary.prepareDb(); }).then(exists => { - if (!exists) { - return Promise.all([ - this.dictionary.importKanjiDict('bg/data/kanjidic/index.json'), - this.dictionary.importTermDict('bg/data/edict/index.json'), - this.dictionary.importTermDict('bg/data/enamdict/index.json') - ]).then(() => this.dictionary.sealDb()); + if (exists) { + return; } + + if (callback) { + callback({state: 'begin', progress: 0}); + } + + let banksLoaded = 0; + let banksTotal = 0; + + const bankCallback = (loaded, total) => { + banksLoaded += loaded; + banksTotal += total; + + if (callback) { + callback({state: 'update', progress: Math.ceil(100 * banksLoaded / banksTotal)}); + } + }; + + 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), + ]).then(() => { + return this.dictionary.sealDb(); + }).then(() => { + if (callback) { + callback({state: 'end', progress: 100}); + } + }); }).then(() => { this.loaded = true; }); -- cgit v1.2.3 From ff3896ed01024e612f9a80fb898f84afac98fc6b Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 13 Sep 2016 22:43:16 -0700 Subject: Fix progress counting --- ext/bg/js/dictionary.js | 4 ++-- ext/bg/js/translator.js | 19 +++++++++++++------ ext/bg/js/yomichan.js | 6 +++++- 3 files changed, 20 insertions(+), 9 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 2bd39344..738535bb 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -163,7 +163,7 @@ class Dictionary { return this.db.terms.bulkAdd(rows).then(() => { if (callback) { - callback(i, index.banks); + callback(indexUrl, i, index.banks); } }); }); @@ -199,7 +199,7 @@ class Dictionary { return this.db.kanji.bulkAdd(rows).then(() => { if (callback) { - callback(i, index.banks); + callback(indexUrl, i, index.banks); } }); }); diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 1dc0dca1..6827b5bc 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -45,14 +45,21 @@ class Translator { callback({state: 'begin', progress: 0}); } - let banksLoaded = 0; - let banksTotal = 0; + const banks = {}; + const bankCallback = (indexUrl, loaded, total) => { + banks[indexUrl] = {loaded: loaded, total: total}; + if (Object.keys(banks).length !== 3) { + return; + } - const bankCallback = (loaded, total) => { - banksLoaded += loaded; - banksTotal += total; + let banksLoaded = 0; + let banksTotal = 0; + for (const url in banks) { + banksLoaded += banks[url].loaded; + banksTotal += banks[url].total; + } - if (callback) { + if (callback && banksTotal > 0) { callback({state: 'update', progress: Math.ceil(100 * banksLoaded / banksTotal)}); } }; diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 10a42f47..c9f53253 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -47,6 +47,10 @@ class Yomichan { } } + onImport({state, progress}) { + console.log(`${state}: ${progress}`); + } + onMessage(request, sender, callback) { const {action, params} = request, method = this['api_' + action]; @@ -90,7 +94,7 @@ class Yomichan { break; case 'loading': chrome.browserAction.setBadgeText({text: '...'}); - this.translator.loadData().then(() => this.setState('enabled')); + this.translator.loadData(this.onImport.bind(this)).then(() => this.setState('enabled')); break; } -- cgit v1.2.3 From b9d53f8427dde34900799295287c634a68d00687 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 14 Sep 2016 20:08:49 -0700 Subject: Progress update --- ext/bg/js/dictionary.js | 4 ++-- ext/bg/js/translator.js | 19 ++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 738535bb..4562c821 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -163,7 +163,7 @@ class Dictionary { return this.db.terms.bulkAdd(rows).then(() => { if (callback) { - callback(indexUrl, i, index.banks); + callback(i, index.banks, indexUrl); } }); }); @@ -199,7 +199,7 @@ class Dictionary { return this.db.kanji.bulkAdd(rows).then(() => { if (callback) { - callback(indexUrl, i, index.banks); + callback(i, index.banks, indexUrl); } }); }); diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 6827b5bc..2331bde7 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -46,21 +46,18 @@ class Translator { } const banks = {}; - const bankCallback = (indexUrl, loaded, total) => { - banks[indexUrl] = {loaded: loaded, total: total}; - if (Object.keys(banks).length !== 3) { - return; - } + const bankCallback = (loaded, total, indexUrl) => { + banks[indexUrl] = {loaded, total}; - let banksLoaded = 0; - let banksTotal = 0; + let percent = 0.0; for (const url in banks) { - banksLoaded += banks[url].loaded; - banksTotal += banks[url].total; + percent += banks[url].loaded / banks[url].total; } - if (callback && banksTotal > 0) { - callback({state: 'update', progress: Math.ceil(100 * banksLoaded / banksTotal)}); + percent /= 3; + + if (callback) { + callback({state: 'update', progress: Math.ceil(100 * percent)}); } }; -- cgit v1.2.3