|
| 1 | +import { DefaultFormatter } from "./components/default-formatter.component"; |
1 | 2 | import { DOMTooltip } from "./components/tooltip.component"; |
2 | 3 | import { GoogleTranslationFormatter } from "./components/translation-formatter.component"; |
| 4 | +import { Config } from "./config"; |
| 5 | +import { TranslationModeValue } from "./popups/interfaces.popup"; |
| 6 | +import { TranslationSettings } from "./popups/settings/settings.popup"; |
| 7 | +import { menuCommandSingleton, MenuKey } from "./services/menu"; |
| 8 | +import { StorageKey } from "./services/storage"; |
| 9 | +import { gmStorageSingleton } from "./services/storage/storage.service"; |
| 10 | +import { GeminiTranslator } from "./services/translators/apibots/google/gemini.translator"; |
| 11 | +import { GoogleTranslator } from "./services/translators/apibots/google/google.translator"; |
| 12 | +import { BrowserSelectionService } from "./services/translators/selection.service"; |
| 13 | +import { TranslationHandler } from "./services/translators/translation-handler.service"; |
| 14 | + |
| 15 | +// Constants |
| 16 | +const DEFAULT_TRANSLATION_MODE = TranslationModeValue.google; |
| 17 | +const SETTINGS_PATH = Config.settings; |
| 18 | + |
| 19 | +// Translator Factory - encapsulates translator creation logic |
| 20 | +class TranslatorFactory { |
| 21 | + static create(translationMode: TranslationModeValue) { |
| 22 | + switch (translationMode) { |
| 23 | + case TranslationModeValue.geminiApi: |
| 24 | + return { |
| 25 | + translator: new GeminiTranslator(), |
| 26 | + formatter: new DefaultFormatter(), |
| 27 | + }; |
| 28 | + default: |
| 29 | + return { |
| 30 | + translator: new GoogleTranslator(), |
| 31 | + formatter: new GoogleTranslationFormatter(), |
| 32 | + }; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + static getCurrentTranslator() { |
| 37 | + const translationMode = gmStorageSingleton.get<TranslationModeValue>( |
| 38 | + StorageKey.translationMode, |
| 39 | + DEFAULT_TRANSLATION_MODE, |
| 40 | + ); |
| 41 | + return this.create(translationMode); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Application Service - main coordination logic |
| 46 | +class TranslationApplication { |
| 47 | + private selectionService: BrowserSelectionService; |
| 48 | + private tooltip: DOMTooltip; |
| 49 | + private translationHandler: TranslationHandler; |
| 50 | + |
| 51 | + constructor() { |
| 52 | + this.selectionService = new BrowserSelectionService(); |
| 53 | + this.tooltip = new DOMTooltip(); |
| 54 | + const { translator, formatter } = TranslatorFactory.getCurrentTranslator(); |
| 55 | + |
| 56 | + this.translationHandler = new TranslationHandler( |
| 57 | + this.tooltip, |
| 58 | + translator, |
| 59 | + formatter, |
| 60 | + this.selectionService, |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + initialize() { |
| 65 | + this.setupEventListeners(); |
| 66 | + this.registerMenuCommands(); |
| 67 | + this.handleSettingsPage(); |
| 68 | + } |
| 69 | + |
| 70 | + private setupEventListeners() { |
| 71 | + this.translationHandler.setupListeners(); |
| 72 | + } |
| 73 | + |
| 74 | + private registerMenuCommands() { |
| 75 | + menuCommandSingleton.register( |
| 76 | + MenuKey.translateText, |
| 77 | + this.translationHandler.handleTextSelection.bind(this.translationHandler), |
| 78 | + ); |
| 79 | + |
| 80 | + menuCommandSingleton.register(MenuKey.settings, () => { |
| 81 | + window.location.href = SETTINGS_PATH; |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + private handleSettingsPage() { |
| 86 | + if (window.location.href === SETTINGS_PATH) { |
| 87 | + new TranslationSettings().togglePopup(); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Main entry point with error handling |
| 93 | +async function bootstrapApplication() { |
| 94 | + try { |
| 95 | + const app = new TranslationApplication(); |
| 96 | + await app.initialize(); |
| 97 | + } catch (error) { |
| 98 | + console.error("Application initialization failed:", error); |
| 99 | + // Consider adding error reporting here |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Start the application |
| 104 | +bootstrapApplication(); |
| 105 | + |
| 106 | +/* |
| 107 | +
|
| 108 | +import { DefaultFormatter } from "./components/default-formatter.component"; |
| 109 | +import { DOMTooltip } from "./components/tooltip.component"; |
| 110 | +import { GoogleTranslationFormatter } from "./components/translation-formatter.component"; |
| 111 | +import { Config } from "./config"; |
| 112 | +import { TranslationModeValue } from "./popups/interfaces.popup"; |
| 113 | +import { TranslationSettings } from "./popups/settings/settings.popup"; |
3 | 114 | import { MenuKey, menuCommandSingleton } from "./services/menu"; |
| 115 | +import { StorageKey } from "./services/storage"; |
| 116 | +import { gmStorageSingleton } from "./services/storage/storage.service"; |
| 117 | +import { GeminiTranslator } from "./services/translators/apibots/google/gemini.translator"; |
4 | 118 | import { GoogleTranslator } from "./services/translators/apibots/google/google.translator"; |
5 | | -import { LocalStorageLanguageService } from "./services/translators/language-storage.service"; |
6 | 119 | import { BrowserSelectionService } from "./services/translators/selection.service"; |
7 | 120 | import { TranslationHandler } from "./services/translators/translation-handler.service"; |
8 | 121 |
|
| 122 | +function getTranslator() { |
| 123 | + const translationMode = gmStorageSingleton.get<TranslationModeValue>( |
| 124 | + StorageKey.translationMode, |
| 125 | + TranslationModeValue.google, |
| 126 | + ); |
| 127 | + if (translationMode === TranslationModeValue.geminiApi) { |
| 128 | + return { |
| 129 | + translator: new GeminiTranslator(), |
| 130 | + formatter: new DefaultFormatter(), |
| 131 | + }; |
| 132 | + } |
| 133 | +
|
| 134 | + return { |
| 135 | + translator: new GoogleTranslator(), |
| 136 | + formatter: new GoogleTranslationFormatter(), |
| 137 | + }; |
| 138 | +} |
| 139 | +
|
9 | 140 | async function main() { |
10 | | - const languageStorage = new LocalStorageLanguageService(); |
11 | | - const targetLang = languageStorage.getTargetLanguage(); |
12 | | - const sourceLang = languageStorage.getSourceLanguage(); |
| 141 | + const url = location.href; |
13 | 142 | const selectionService = new BrowserSelectionService(); |
14 | 143 | const tooltip = new DOMTooltip(); |
15 | | - const translator = new GoogleTranslator(sourceLang, targetLang); |
16 | | - const formatter = new GoogleTranslationFormatter(); |
| 144 | + const { translator, formatter } = getTranslator(); |
17 | 145 |
|
18 | 146 | const translationHandler = new TranslationHandler( |
19 | 147 | tooltip, |
20 | 148 | translator, |
21 | 149 | formatter, |
22 | 150 | selectionService, |
23 | | - languageStorage, |
24 | 151 | ); |
25 | 152 | translationHandler.setupListeners(); |
26 | | - translationHandler.registerLanguageMenu(); |
| 153 | + // translationHandler.registerLanguageMenu(); |
27 | 154 |
|
28 | 155 | menuCommandSingleton.register( |
29 | 156 | MenuKey.translateText, |
30 | 157 | translationHandler.handleTextSelection, |
31 | 158 | ); |
| 159 | + menuCommandSingleton.register(MenuKey.settings, () => { |
| 160 | + location.href = Config.settings; |
| 161 | + }); |
| 162 | +
|
| 163 | + if (url === Config.settings) { |
| 164 | + const translationSettings = new TranslationSettings(); |
| 165 | + translationSettings.togglePopup(); |
| 166 | + } |
32 | 167 | } |
33 | 168 |
|
34 | 169 | main().catch((e) => { |
35 | 170 | console.log(e); |
36 | 171 | }); |
| 172 | +
|
| 173 | +
|
| 174 | +*/ |
0 commit comments