From bf28983f4a39b2475b080b937b1fe6d6ed0e9ac9 Mon Sep 17 00:00:00 2001 From: Brenny <94570993+arcticpinecone@users.noreply.github.com> Date: Tue, 14 May 2024 01:43:23 +0300 Subject: [PATCH 1/2] Add new automatic append translation feature and newline toggle Implement a new command 'Translate selection: Automatically append selection' that automatically uses the predefined 'To language' to translate and append the selected text without requiring user interaction through a modal. Enhance both append functionalities with a configurable newline toggle, allowing users to decide if a newline should be added before appending the translated text. --- src/main.ts | 29 +++++++++++++++++++++++++++++ src/settings/pluginSettings.ts | 2 ++ src/settings/settingTab.ts | 16 ++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/src/main.ts b/src/main.ts index f98c75b..03cfdbf 100644 --- a/src/main.ts +++ b/src/main.ts @@ -95,6 +95,35 @@ export default class DeepLPlugin extends Plugin { }, }); + this.addCommand({ + id: "translate-and-append-selection-automatically", + name: "Translate selection: Automatically append selection", + editorCallback: async (editor: Editor) => { + const selection = editor.getSelection(); + if (selection === "") { + new Notice("No text selected."); + return; + } + + try { + const translation = await this.deeplService.translate( + selection, + this.settings.toLanguage, + this.settings.fromLanguage + ); + const textToAppend = this.settings.appendNewLine ? '\n' + translation[0].text : translation[0].text; + editor.replaceSelection(selection + textToAppend); + } catch (error) { + if (error instanceof DeepLException) { + new Notice(error.message); + } else { + console.error(error, error.stack); + new Notice("An unknown error occurred. See console for details."); + } + } + } + }); + this.addCommand({ id: "deepl-translate-selection-to", name: "Translate selection: to language", diff --git a/src/settings/pluginSettings.ts b/src/settings/pluginSettings.ts index 668dddd..ee05f21 100644 --- a/src/settings/pluginSettings.ts +++ b/src/settings/pluginSettings.ts @@ -5,6 +5,7 @@ export interface DeepLPluginSettings { showStatusBar: boolean; useProAPI: boolean; formality: string; + appendNewLine: boolean; } export const formalities: Record = { @@ -20,4 +21,5 @@ export const defaultSettings: Partial = { showStatusBar: true, useProAPI: false, formality: "default", + appendNewLine: false, }; diff --git a/src/settings/settingTab.ts b/src/settings/settingTab.ts index eb7f811..5828f96 100644 --- a/src/settings/settingTab.ts +++ b/src/settings/settingTab.ts @@ -49,6 +49,10 @@ export class SettingTab extends PluginSettingTab { "The target language can be selected by suggestion modal. The translation will be appended to the selection." ); + new Setting(containerEl) + .setName("Translate selection: Automatically append selection") + .setDesc("The translation will be appended to the selection using the 'To language' setting automatically, without the suggestion modal."); + containerEl.createEl("h4", { text: "Language settings", }); @@ -106,6 +110,18 @@ export class SettingTab extends PluginSettingTab { }) ); + new Setting(containerEl) + .setName("When appending text, add a new line before appending the translation?") + .setDesc("If enabled, adds a new line before appending the translated text.") + .addToggle((toggle) => + toggle + .setValue(this.plugin.settings.appendNewLine) + .onChange(async (value) => { + this.plugin.settings.appendNewLine = value; + await this.plugin.saveSettings(); + }) + ); + containerEl.createEl("h4", { text: "Authentication settings", }); From f8bd5ba4fd77c9b74cd387700dc2d723f40a1dd2 Mon Sep 17 00:00:00 2001 From: Brenny <94570993+arcticpinecone@users.noreply.github.com> Date: Tue, 14 May 2024 01:57:50 +0300 Subject: [PATCH 2/2] Update main.ts The new line toggle should work also with the existing `Translate selection: To language and append to selection` option. --- src/main.ts | 87 ++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 48 deletions(-) diff --git a/src/main.ts b/src/main.ts index 03cfdbf..979a186 100644 --- a/src/main.ts +++ b/src/main.ts @@ -164,54 +164,45 @@ export default class DeepLPlugin extends Plugin { }); this.addCommand({ - id: "deepl-translate-selection-from-to", - name: "Translate selection: From a language to another", - editorCallback: async (editor: Editor) => { - if (editor.getSelection() === "") { - return; - } - - new TranslateModal( - app, - "From", - Object.entries(fromLanguages).map(([code, name]) => ({ - code, - name, - })), - async (from) => { - new TranslateModal( - app, - "To", - Object.entries(toLanguages).map(([code, name]) => ({ - code, - name, - })), - async (to) => { - try { - const translation = - await this.deeplService.translate( - editor.getSelection(), - to.code, - from.code - ); - editor.replaceSelection( - translation[0].text - ); - } catch (error) { - if (error instanceof DeepLException) { - new Notice(error.message); - } else { - console.error(error, error.stack); - new Notice( - "An unknown error occured. See console for details." - ); - } - } - } - ).open(); - } - ).open(); - }, + id: "deepl-translate-selection-append", + name: "Translate selection: To language and append to selection", + editorCallback: async (editor: Editor) => { + if (editor.getSelection() === "") { + return; + } + + const selection = editor.getSelection(); + + new TranslateModal( + app, + "To", + Object.entries(toLanguages).map(([code, name]) => ({ + code, + name, + })), + async (language) => { + try { + const translation = await this.deeplService.translate( + selection, + language.code, + this.settings.fromLanguage + ); + // Append a newline if the setting is enabled + const textToAppend = this.settings.appendNewLine ? `\n${translation[0].text}` : translation[0].text; + editor.replaceSelection(`${selection}${textToAppend}`); + } catch (error) { + if (error instanceof DeepLException) { + new Notice(error.message); + } else { + console.error(error, error.stack); + new Notice( + "An unknown error occurred. See console for details." + ); + } + } + } + ).open(); + }, }); }