Support switching between edict and enamdict
This commit is contained in:
parent
bcd34149ab
commit
f079db0471
@ -31,35 +31,24 @@ class Dictionary {
|
|||||||
this.kanjiDicts[name] = dict;
|
this.kanjiDicts[name] = dict;
|
||||||
}
|
}
|
||||||
|
|
||||||
findTerm(term) {
|
findTerm(term, dict) {
|
||||||
let results = [];
|
const db = this.termDicts[dict];
|
||||||
|
const indices = db.indices[term] || [];
|
||||||
|
|
||||||
for (const name in this.termDicts) {
|
return indices.map(index => {
|
||||||
const dict = this.termDicts[name];
|
const [e, r, t, ...g] = db.defs[index];
|
||||||
const indices = dict.indices[term] || [];
|
return {id: index, expression: e, reading: r, glossary: g, tags: t.split(' ')};
|
||||||
|
});
|
||||||
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 results;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
findKanji(kanji) {
|
findKanji(kanji, dict) {
|
||||||
const results = [];
|
const def = this.termDicts[dict][kanji];
|
||||||
|
|
||||||
for (const name in this.termDicts) {
|
if (def) {
|
||||||
const def = this.termDicts[name][kanji];
|
const [c, k, o, g] = def;
|
||||||
if (def) {
|
return {id: index, character: c, kunyomi: k, onyomi: o, glossary: g};
|
||||||
const [c, k, o, g] = def;
|
|
||||||
results.push({id: index, character: c, kunyomi: k, onyomi: o, glossary: g});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ templates['defs.html'] = template({"1":function(container,depth0,helpers,partial
|
|||||||
|
|
||||||
return "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\">\n <title></title>\n <link rel=\"stylesheet\" href=\""
|
return "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\">\n <title></title>\n <link rel=\"stylesheet\" href=\""
|
||||||
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
|
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
|
||||||
+ "/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\n"
|
+ "/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\n"
|
||||||
+ ((stack1 = helpers.each.call(alias1,(depth0 != null ? depth0.defs : depth0),{"name":"each","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
|
+ ((stack1 = helpers.each.call(alias1,(depth0 != null ? depth0.defs : depth0),{"name":"each","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
|
||||||
+ "\n <script src=\""
|
+ "\n <script src=\""
|
||||||
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
|
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
|
||||||
|
@ -63,14 +63,14 @@ class Translator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
findTerm(text) {
|
findTerm(text, dict) {
|
||||||
const groups = {};
|
const groups = {};
|
||||||
for (let i = text.length; i > 0; --i) {
|
for (let i = text.length; i > 0; --i) {
|
||||||
const term = text.slice(0, i);
|
const term = text.slice(0, i);
|
||||||
|
|
||||||
const dfs = this.deinflector.deinflect(term, t => {
|
const dfs = this.deinflector.deinflect(term, t => {
|
||||||
const tags = [];
|
const tags = [];
|
||||||
for (const d of this.dictionary.findTerm(t)) {
|
for (const d of this.dictionary.findTerm(t, dict)) {
|
||||||
tags.push(d.tags);
|
tags.push(d.tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ class Translator {
|
|||||||
|
|
||||||
if (dfs !== null) {
|
if (dfs !== null) {
|
||||||
for (const df of dfs) {
|
for (const df of dfs) {
|
||||||
this.processTerm(groups, df.source, df.tags, df.rules, df.root);
|
this.processTerm(dict, groups, df.source, df.tags, df.rules, df.root);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,13 +125,13 @@ class Translator {
|
|||||||
return {results: results, length: length};
|
return {results: results, length: length};
|
||||||
}
|
}
|
||||||
|
|
||||||
findKanji(text) {
|
findKanji(text, dict) {
|
||||||
let results = [];
|
let results = [];
|
||||||
|
|
||||||
const processed = {};
|
const processed = {};
|
||||||
for (const c of text) {
|
for (const c of text) {
|
||||||
if (!processed.has(c)) {
|
if (!processed.has(c)) {
|
||||||
results = results.concat(this.dictionary.findKanji(c));
|
results = results.concat(this.dictionary.findKanji(c, dict));
|
||||||
processed[c] = true;
|
processed[c] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,8 +139,8 @@ class Translator {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
processTerm(groups, source, tags, rules=[], root='') {
|
processTerm(dict, groups, source, tags, rules=[], root='') {
|
||||||
for (const entry of this.dictionary.findTerm(root)) {
|
for (const entry of this.dictionary.findTerm(root, dict)) {
|
||||||
if (entry.id in groups) {
|
if (entry.id in groups) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -39,8 +39,8 @@ class Yomichan {
|
|||||||
onMessage(request, sender, callback) {
|
onMessage(request, sender, callback) {
|
||||||
const {action, data} = request;
|
const {action, data} = request;
|
||||||
const handlers = {
|
const handlers = {
|
||||||
findKanji: ({text}) => this.translator.onFindKanji(text),
|
findKanji: ({text, dict}) => this.translator.onFindKanji(text, dict),
|
||||||
findTerm: ({text}) => this.translator.findTerm(text),
|
findTerm: ({text, dict}) => this.translator.findTerm(text, dict),
|
||||||
getState: () => this.state,
|
getState: () => this.state,
|
||||||
getOptions: () => this.options,
|
getOptions: () => this.options,
|
||||||
renderText: ({data, template}) => Handlebars.templates[template](data)
|
renderText: ({data, template}) => Handlebars.templates[template](data)
|
||||||
|
@ -21,8 +21,8 @@ function sendMessage(action, data, callback) {
|
|||||||
chrome.runtime.sendMessage({action: action, data: data}, callback);
|
chrome.runtime.sendMessage({action: action, data: data}, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
function findTerm(text, callback) {
|
function findTerm(text, dict, callback) {
|
||||||
sendMessage('findTerm', {text: text}, callback);
|
sendMessage('findTerm', {text: text, dict: dict}, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
function findKanji(text, callback) {
|
function findKanji(text, callback) {
|
||||||
|
@ -19,11 +19,12 @@
|
|||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.lastMosePos = null;
|
this.popupMousePos = null;
|
||||||
this.popupQuery = '';
|
this.popupQuery = '';
|
||||||
this.popupOffset = 10;
|
this.popupOffset = 10;
|
||||||
this.enabled = false;
|
this.enabled = false;
|
||||||
this.options = null;
|
this.options = {};
|
||||||
|
this.activeDict = '';
|
||||||
|
|
||||||
this.popup = document.createElement('iframe');
|
this.popup = document.createElement('iframe');
|
||||||
this.popup.classList.add('yomichan-popup');
|
this.popup.classList.add('yomichan-popup');
|
||||||
@ -40,29 +41,29 @@ class Client {
|
|||||||
window.addEventListener('resize', (e) => this.hidePopup());
|
window.addEventListener('resize', (e) => this.hidePopup());
|
||||||
|
|
||||||
getOptions((opts) => {
|
getOptions((opts) => {
|
||||||
this.setDict('edict');
|
this.setActiveDict('edict');
|
||||||
this.setOptions(opts);
|
this.setOptions(opts);
|
||||||
getState((state) => this.setEnabled(state === 'enabled'));
|
getState((state) => this.setEnabled(state === 'enabled'));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onKeyDown(e) {
|
onKeyDown(e) {
|
||||||
if (this.enabled && this.lastMousePos !== null && (e.keyCode === 16 || e.charCode === 16)) {
|
if (this.enabled && this.popupMousePos !== null && (e.keyCode === 16 || e.charCode === 16)) {
|
||||||
this.searchAtPoint(this.lastMousePos);
|
this.searchAtPoint(this.popupMousePos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMouseMove(e) {
|
onMouseMove(e) {
|
||||||
this.lastMousePos = {x: e.clientX, y: e.clientY};
|
this.popupMousePos = {x: e.clientX, y: e.clientY};
|
||||||
if (this.enabled && (e.shiftKey || e.which === 2)) {
|
if (this.enabled && (e.shiftKey || e.which === 2)) {
|
||||||
this.searchAtPoint(this.lastMousePos);
|
this.searchAtPoint(this.popupMousePos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMouseDown(e) {
|
onMouseDown(e) {
|
||||||
this.lastMousePos = {x: e.clientX, y: e.clientY};
|
this.popupMousePos = {x: e.clientX, y: e.clientY};
|
||||||
if (this.enabled && (e.shiftKey || e.which === 2)) {
|
if (this.enabled && (e.shiftKey || e.which === 2)) {
|
||||||
this.searchAtPoint(this.lastMousePos);
|
this.searchAtPoint(this.popupMousePos);
|
||||||
} else {
|
} else {
|
||||||
this.hidePopup();
|
this.hidePopup();
|
||||||
}
|
}
|
||||||
@ -84,8 +85,8 @@ class Client {
|
|||||||
onFrameMessage(e) {
|
onFrameMessage(e) {
|
||||||
const {action, data} = e.data;
|
const {action, data} = e.data;
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case 'selectDict':
|
case 'setActiveDict':
|
||||||
this.setDict(data);
|
this.setActiveDict(data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -113,11 +114,11 @@ class Client {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
findTerm(popupQuery, ({results, length}) => {
|
findTerm(popupQuery, this.activeDict, ({results, length}) => {
|
||||||
if (length === 0) {
|
if (length === 0) {
|
||||||
this.hidePopup();
|
this.hidePopup();
|
||||||
} else {
|
} else {
|
||||||
const params = {defs: results, root: chrome.extension.getURL('fg')};
|
const params = {defs: results, root: chrome.extension.getURL('fg'), activeDict: this.activeDict};
|
||||||
renderText(params, 'defs.html', (html) => this.showPopup(range, html, popupQuery, length));
|
renderText(params, 'defs.html', (html) => this.showPopup(range, html, popupQuery, length));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -168,9 +169,8 @@ class Client {
|
|||||||
this.options = opts;
|
this.options = opts;
|
||||||
}
|
}
|
||||||
|
|
||||||
setDict(dict) {
|
setActiveDict(activeDict) {
|
||||||
this.dict = dict;
|
this.activeDict = activeDict;
|
||||||
alert(dict);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
ext/fg/js/popup.js
Normal file
22
ext/fg/js/popup.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* 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}, '*');
|
||||||
|
}
|
@ -7,7 +7,7 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="dictionary">
|
<div class="dictionary">
|
||||||
<a href="javascript:selectDict('edict');">単</a><a href="javascript:selectDict('enamdict')">名</a><a href="javascript:selectDict('kanjidic');">漢</a>
|
<a href="javascript:setActiveDict('edict');">単</a><a href="javascript:setActiveDict('enamdict')">名</a><a href="javascript:setActiveDict('kanjidic');">漢</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{#each defs}}
|
{{#each defs}}
|
||||||
|
Loading…
Reference in New Issue
Block a user