Work with new dictionary format

This commit is contained in:
Alex Yatskov 2016-04-02 12:56:47 -07:00
parent 036639aeaf
commit a6fda6c943
3 changed files with 13 additions and 12 deletions

View File

@ -60,7 +60,7 @@ class Dictionary {
storeIndex(indices, term, index) { storeIndex(indices, term, index) {
if (term.length > 0) { if (term.length > 0) {
const indices = this.termIndices[term] || []; const indices = this.termIndices[term] || [];
indices.push(term); indices.push(index);
this.termIndices[term] = indices; this.termIndices[term] = indices;
} }
} }

View File

@ -21,7 +21,7 @@ class Translator {
constructor() { constructor() {
this.dictionary = new Dictionary(); this.dictionary = new Dictionary();
this.deinflector = new Deinflector(); this.deinflector = new Deinflector();
this.pendingLoads = []; this.initialized = false;
} }
loadData(paths, callback) { loadData(paths, callback) {
@ -30,8 +30,9 @@ class Translator {
return; return;
} }
const pendingLoads = [];
for (const key of ['rules', 'edict', 'enamdict', 'kanjidic']) { for (const key of ['rules', 'edict', 'enamdict', 'kanjidic']) {
this.pendingLoads.push(key); pendingLoads.push(key);
Translator.loadData(paths[key], (response) => { Translator.loadData(paths[key], (response) => {
switch (key) { switch (key) {
case 'rules': case 'rules':
@ -46,9 +47,9 @@ class Translator {
break; break;
} }
const index = this.pendingLoads.indexOf(key); pendingLoads.splice(pendingLoads.indexOf(key), 1);
this.pendingLoads = this.pendingLoads.splice(index, 1); if (pendingLoads.length === 0) {
if (this.pendingLoads.length === 0) { this.initialized = true;
callback(); callback();
} }
}); });
@ -160,8 +161,10 @@ class Translator {
static parseCsv(data) { static parseCsv(data) {
const result = []; const result = [];
for (const row of data.split('\n')) { for (const row of data.split('\n')) {
if (row.length > 0) {
result.push(row.split('\t')); result.push(row.split('\t'));
} }
}
return result; return result;
} }

View File

@ -27,9 +27,7 @@ class Client {
this.popup.classList.add('yomichan-popup'); this.popup.classList.add('yomichan-popup');
this.popup.addEventListener('mousedown', (e) => e.stopPropagation()); this.popup.addEventListener('mousedown', (e) => e.stopPropagation());
this.popup.addEventListener('scroll', (e) => e.stopPropagation()); this.popup.addEventListener('scroll', (e) => e.stopPropagation());
document.body.appendChild(this.popup);
const base = document.body.appendChild('div');
base.createShadowRoot().appendChild(this.popup);
chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
window.addEventListener('mousedown', this.onMouseDown.bind(this)); window.addEventListener('mousedown', this.onMouseDown.bind(this));