From b7d7bcdff94fec4e8b75a86530705ff462111540 Mon Sep 17 00:00:00 2001 From: memodack <81513409+memodack@users.noreply.github.com> Date: Tue, 1 Apr 2025 19:34:14 +0300 Subject: [PATCH] feat: add TranslateCommandService --- src/translate-command.service.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/translate-command.service.ts diff --git a/src/translate-command.service.ts b/src/translate-command.service.ts new file mode 100644 index 0000000..ab9a487 --- /dev/null +++ b/src/translate-command.service.ts @@ -0,0 +1,32 @@ +import { Command, Editor } from 'obsidian'; + +import { IActionsService } from './actions.service'; +import { ITranslationService } from './translation.service'; + +export class TranslateCommandService implements Command { + id = 'translate'; + name = 'Translate'; + + private translationService: ITranslationService; + private actionService: IActionsService; + + constructor( + translationService: ITranslationService, + actionService: IActionsService, + ) { + this.translationService = translationService; + this.actionService = actionService; + } + + async editorCallback(editor: Editor): Promise { + const selection = editor.getSelection(); + const translation = await this.translationService.translate(selection); + + if (!translation) { + return; + } + + editor.replaceSelection(`{${selection}|${translation}}`); + await this.actionService.playValueAndTranslation(selection, translation); + } +}