diff --git a/src/main/core/entry.js b/src/main/core/entry.js index fb4dce13..da2837c2 100644 --- a/src/main/core/entry.js +++ b/src/main/core/entry.js @@ -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 }; diff --git a/src/main/core/entry/default.js b/src/main/core/entry/default.js new file mode 100644 index 00000000..772be3bc --- /dev/null +++ b/src/main/core/entry/default.js @@ -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; diff --git a/src/main/core/events.js b/src/main/core/events.js index 6dea85ad..3701d66d 100644 --- a/src/main/core/events.js +++ b/src/main/core/events.js @@ -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"; @@ -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); diff --git a/src/main/core/lookuper.js b/src/main/core/lookuper.js index cfeb4a3e..b29ab785 100644 --- a/src/main/core/lookuper.js +++ b/src/main/core/lookuper.js @@ -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"; @@ -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; @@ -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}`);