Revert "Support switching between edict and enamdict"

This reverts commit f079db0471.
This commit is contained in:
Alex Yatskov 2016-04-17 18:38:29 -07:00
parent 52a8e2207c
commit 5bebf3ed2c
8 changed files with 58 additions and 69 deletions

View File

@ -31,24 +31,35 @@ class Dictionary {
this.kanjiDicts[name] = dict;
}
findTerm(term, dict) {
const db = this.termDicts[dict];
const indices = db.indices[term] || [];
findTerm(term) {
let results = [];
return indices.map(index => {
const [e, r, t, ...g] = db.defs[index];
return {id: index, expression: e, reading: r, glossary: g, tags: t.split(' ')};
});
}
for (const name in this.termDicts) {
const dict = this.termDicts[name];
const indices = dict.indices[term] || [];
findKanji(kanji, dict) {
const def = this.termDicts[dict][kanji];
if (def) {
const [c, k, o, g] = def;
return {id: index, character: c, kunyomi: k, onyomi: o, glossary: g};
results = results.concat(
indices.map(index => {
const [e, r, t, ...g] = dict.defs[index];
return {id: index, expression: e, reading: r, glossary: g, tags: t.split(' ')};
})
);
}
return null;
return results;
}
findKanji(kanji) {
const results = [];
for (const name in this.termDicts) {
const def = this.termDicts[name][kanji];
if (def) {
const [c, k, o, g] = def;
results.push({id: index, character: c, kunyomi: k, onyomi: o, glossary: g});
}
}
return results;
}
}

View File

@ -12,7 +12,7 @@ templates['header.html'] = template({"compiler":[7,">= 4.0.0"],"main":function(c
return "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\">\n <title></title>\n <link rel=\"stylesheet\" href=\""
+ container.escapeExpression(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},{"name":"root","hash":{},"data":data}) : helper)))
+ "/css/popup.css\">\n </head>\n <body>\n <div class=\"dictionary\">\n <a href=\"javascript:setActiveDict('edict');\">単</a><a href=\"javascript:setActiveDict('enamdict')\">名</a><a href=\"javascript:setActiveDict('kanjidic');\">漢</a>\n </div>\n";
+ "/css/popup.css\">\n </head>\n <body>\n <div class=\"dictionary\">\n <a href=\"javascript:selectDict('edict');\">単</a><a href=\"javascript:selectDict('enamdict')\">名</a><a href=\"javascript:selectDict('kanjidic');\">漢</a>\n </div>\n";
},"useData":true});
templates['kanji.html'] = template({"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
var helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression;

View File

@ -63,14 +63,14 @@ class Translator {
}
}
findTerm(text, dict) {
findTerm(text) {
const groups = {};
for (let i = text.length; i > 0; --i) {
const term = text.slice(0, i);
const dfs = this.deinflector.deinflect(term, t => {
const tags = [];
for (const d of this.dictionary.findTerm(t, dict)) {
for (const d of this.dictionary.findTerm(t)) {
tags.push(d.tags);
}
@ -79,7 +79,7 @@ class Translator {
if (dfs !== null) {
for (const df of dfs) {
this.processTerm(dict, groups, df.source, df.tags, df.rules, df.root);
this.processTerm(groups, df.source, df.tags, df.rules, df.root);
}
}
}
@ -125,13 +125,13 @@ class Translator {
return {results: results, length: length};
}
findKanji(text, dict) {
findKanji(text) {
let results = [];
const processed = {};
for (const c of text) {
if (!processed.has(c)) {
results = results.concat(this.dictionary.findKanji(c, dict));
results = results.concat(this.dictionary.findKanji(c));
processed[c] = true;
}
}
@ -139,8 +139,8 @@ class Translator {
return results;
}
processTerm(dict, groups, source, tags, rules=[], root='') {
for (const entry of this.dictionary.findTerm(root, dict)) {
processTerm(groups, source, tags, rules=[], root='') {
for (const entry of this.dictionary.findTerm(root)) {
if (entry.id in groups) {
continue;
}

View File

@ -39,8 +39,8 @@ class Yomichan {
onMessage(request, sender, callback) {
const {action, data} = request;
const handlers = {
findKanji: ({text, dict}) => this.translator.onFindKanji(text, dict),
findTerm: ({text, dict}) => this.translator.findTerm(text, dict),
findKanji: ({text}) => this.translator.onFindKanji(text),
findTerm: ({text}) => this.translator.findTerm(text),
getState: () => this.state,
getOptions: () => this.options,
renderText: ({data, template}) => Handlebars.templates[template](data)

View File

@ -21,8 +21,8 @@ function sendMessage(action, data, callback) {
chrome.runtime.sendMessage({action: action, data: data}, callback);
}
function findTerm(text, dict, callback) {
sendMessage('findTerm', {text: text, dict: dict}, callback);
function findTerm(text, callback) {
sendMessage('findTerm', {text: text}, callback);
}
function findKanji(text, callback) {

View File

@ -19,12 +19,11 @@
class Client {
constructor() {
this.popupMousePos = null;
this.popupQuery = '';
this.popupOffset = 10;
this.enabled = false;
this.options = {};
this.activeDict = '';
this.lastMosePos = null;
this.popupQuery = '';
this.popupOffset = 10;
this.enabled = false;
this.options = null;
this.popup = document.createElement('iframe');
this.popup.classList.add('yomichan-popup');
@ -41,29 +40,29 @@ class Client {
window.addEventListener('resize', (e) => this.hidePopup());
getOptions((opts) => {
this.setActiveDict('edict');
this.setDict('edict');
this.setOptions(opts);
getState((state) => this.setEnabled(state === 'enabled'));
});
}
onKeyDown(e) {
if (this.enabled && this.popupMousePos !== null && (e.keyCode === 16 || e.charCode === 16)) {
this.searchAtPoint(this.popupMousePos);
if (this.enabled && this.lastMousePos !== null && (e.keyCode === 16 || e.charCode === 16)) {
this.searchAtPoint(this.lastMousePos);
}
}
onMouseMove(e) {
this.popupMousePos = {x: e.clientX, y: e.clientY};
this.lastMousePos = {x: e.clientX, y: e.clientY};
if (this.enabled && (e.shiftKey || e.which === 2)) {
this.searchAtPoint(this.popupMousePos);
this.searchAtPoint(this.lastMousePos);
}
}
onMouseDown(e) {
this.popupMousePos = {x: e.clientX, y: e.clientY};
this.lastMousePos = {x: e.clientX, y: e.clientY};
if (this.enabled && (e.shiftKey || e.which === 2)) {
this.searchAtPoint(this.popupMousePos);
this.searchAtPoint(this.lastMousePos);
} else {
this.hidePopup();
}
@ -85,8 +84,8 @@ class Client {
onFrameMessage(e) {
const {action, data} = e.data;
switch (action) {
case 'setActiveDict':
this.setActiveDict(data);
case 'selectDict':
this.setDict(data);
break;
}
}
@ -114,11 +113,11 @@ class Client {
return;
}
findTerm(popupQuery, this.activeDict, ({results, length}) => {
findTerm(popupQuery, ({results, length}) => {
if (length === 0) {
this.hidePopup();
} else {
const params = {defs: results, root: chrome.extension.getURL('fg'), activeDict: this.activeDict};
const params = {defs: results, root: chrome.extension.getURL('fg')};
renderText(params, 'term-list.html', (html) => this.showPopup(range, html, popupQuery, length));
}
});
@ -169,8 +168,9 @@ class Client {
this.options = opts;
}
setActiveDict(activeDict) {
this.activeDict = activeDict;
setDict(dict) {
this.dict = dict;
alert(dict);
}
}

View File

@ -1,22 +0,0 @@
/*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
function setActiveDict(dict) {
parent.postMessage({action: 'setActiveDict', data: dict}, '*');
}

View File

@ -7,5 +7,5 @@
</head>
<body>
<div class="dictionary">
<a href="javascript:setActiveDict('edict');"></a><a href="javascript:setActiveDict('enamdict')"></a><a href="javascript:setActiveDict('kanjidic');"></a>
<a href="javascript:selectDict('edict');"></a><a href="javascript:selectDict('enamdict')"></a><a href="javascript:selectDict('kanjidic');"></a>
</div>