Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wtetsu committed May 21, 2022
1 parent bbcd00b commit 04d0f34
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 42 deletions.
46 changes: 8 additions & 38 deletions src/main/core/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,13 @@
* Licensed under MIT
*/

import entryGeneratorJa from "./entry/ja";
import entryGeneratorEn from "./entry/en";

const generators = {
en: entryGeneratorEn,
ja: entryGeneratorJa,
default: entryGeneratorEn,
};

let languageDetector = (text) => (isEnglishText(text) ? "en" : "ja");

const build = (text, withCapitalized, mustIncludeOriginalText) => {
const lang = languageDetector(text);
const generator = generators[lang] ?? generators.default;
const entries = generator(text, withCapitalized, mustIncludeOriginalText);
return { entries, lang };
};

const isEnglishText = (str) => {
let result = true;
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i);
const isEnglishLike = (0x20 <= code && code <= 0x7e) || code === 0x2011 || code === 0x200c;
if (!isEnglishLike) {
result = false;
break;
}
}
return result;
};

const registerGenerator = (lang, generator) => {
generators[lang] = generator;
};

const registerLanguageDetector = (detector) => {
languageDetector = detector;
const build = (languageDetector, generators) => {
return (text, withCapitalized, mustIncludeOriginalText) => {
const lang = languageDetector(text);
const generator = generators[lang] ?? generators.default;
const entries = generator(text, withCapitalized, mustIncludeOriginalText);
return { entries, lang };
};
};

export default { build, registerGenerator, registerLanguageDetector };
export default { build };
37 changes: 37 additions & 0 deletions src/main/core/entry/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Mouse Dictionary (https://github.com/wtetsu/mouse-dictionary/)
* Copyright 2018-present wtetsu
* Licensed under MIT
*/

import entry from "../entry";
import entryGeneratorJa from "./ja";
import entryGeneratorEn from "./en";

// Can add other languages here
const generators = {
en: entryGeneratorEn,
ja: entryGeneratorJa,
default: entryGeneratorEn,
};

let languageDetector = (text) => (isEnglishText(text) ? "en" : "ja");

const isEnglishText = (str) => {
let result = true;
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i);
const isEnglishLike = (0x20 <= code && code <= 0x7e) || code === 0x2011 || code === 0x200c;
if (!isEnglishLike) {
result = false;
break;
}
}
return result;
};

const build = () => {
return entry.build(languageDetector, generators);
};

export default build;
3 changes: 2 additions & 1 deletion src/main/core/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import config from "./config";
import rule from "./rule";
import entryDefault from "./entry/default";
import Lookuper from "./lookuper";
import dom from "../lib/dom";
import traverser from "../lib/traverser";
Expand All @@ -19,7 +20,7 @@ const attach = async (settings, dialog, doUpdateContent) => {
let enableDefault = true;

const traverse = traverser.build(rule.doLetters, settings.parseWordsLimit);
const lookuper = new Lookuper(settings, doUpdateContent);
const lookuper = new Lookuper(settings, entryDefault(), doUpdateContent);

const draggable = new Draggable(settings.normalDialogStyles, settings.movingDialogStyles);
draggable.events.change = (e) => config.savePosition(e);
Expand Down
6 changes: 3 additions & 3 deletions src/main/core/lookuper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Licensed under MIT
*/

import entry from "./entry";
import Generator from "./generator";
import dom from "../lib/dom";
import storage from "../lib/storage";
Expand All @@ -14,9 +13,10 @@ import utils from "../lib/utils";
const TEXT_LENGTH_LIMIT = 128;

export default class Lookuper {
constructor(settings, doUpdateContent) {
constructor(settings, doBuildEntry, doUpdateContent) {
this.lookupWithCapitalized = settings.lookupWithCapitalized;
this.doUpdateContent = doUpdateContent;
this.doBuildEntry = doBuildEntry;

this.lastText = null;
this.aimed = false;
Expand Down Expand Up @@ -127,7 +127,7 @@ export default class Lookuper {
const langs = [];
for (let i = 0; i < textList.length; i++) {
const text = textList[i];
const { entries, lang } = entry.build(text, withCapitalized, includeOrgText);
const { entries, lang } = this.doBuildEntry(text, withCapitalized, includeOrgText);
console.info(`${entries.join(",")}`);
console.info(`${entries.length}`);

Expand Down

0 comments on commit 04d0f34

Please sign in to comment.