From 80e6c12ff653e739cf0665338c7c9259fe7a6831 Mon Sep 17 00:00:00 2001 From: Dario Baumberger Date: Wed, 11 Oct 2023 18:31:00 +0200 Subject: [PATCH 1/9] added suggestModal --- src/deepl/translateModal.ts | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/deepl/translateModal.ts diff --git a/src/deepl/translateModal.ts b/src/deepl/translateModal.ts new file mode 100644 index 0000000..f78c09f --- /dev/null +++ b/src/deepl/translateModal.ts @@ -0,0 +1,46 @@ +import { App, SuggestModal } from "obsidian"; +import { toLanguages } from "src/deepl/toLanguages"; + +export interface Language { + code: string; + name: string; +} + +export class TranslateModal extends SuggestModal { + languages: Language[] = Object.entries(toLanguages).map(([code, name]) => ({ + code, + name, + })); + + constructor( + app: App, + placeholder: string, + public callback: (result: Language) => void + ) { + super(app); + this.setPlaceholder(placeholder); + } + + getSuggestions(query: string): Language[] { + if (query) { + return this.languages.filter( + (language) => + language.code.toLowerCase().includes(query.toLowerCase()) || + language.name.toLowerCase().includes(query.toLowerCase()) + ); + } + + return this.languages; + } + renderSuggestion(language: Language, el: HTMLElement) { + el.createEl("div", { text: `${language.name} (${language.code})` }); + } + onChooseSuggestion(language: Language) { + this.callback(language); + } + + onClose() { + let { contentEl } = this; + contentEl.empty(); + } +} From 787291809724e63e491ba3dd7801add5ba3f8ea8 Mon Sep 17 00:00:00 2001 From: Dario Baumberger Date: Wed, 11 Oct 2023 18:53:10 +0200 Subject: [PATCH 2/9] Added command to translate selection with language option, which appends the translation to the selection. --- src/main.ts | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 98b3e0b..2e50edf 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,10 +4,11 @@ import { DeepLException } from "./deepl/deeplException"; import { DeepLService } from "./deepl/deeplService"; import { DeepLPluginSettings, - defaultSettings + defaultSettings, } from "./settings/pluginSettings"; import { SettingTab } from "./settings/settingTab"; import { addStatusBar } from "./settings/statusbar"; +import { TranslateModal } from "./deepl/translateModal"; export default class DeepLPlugin extends Plugin { public deeplService: DeepLService; @@ -49,6 +50,40 @@ export default class DeepLPlugin extends Plugin { } }, }); + + this.addCommand({ + 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", async (language) => { + try { + const translation = await this.deeplService.translate( + selection, + language.code, + this.settings.fromLanguage + ); + editor.replaceSelection( + `${selection} ${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(); + }, + }); } async loadSettings() { From dad04ba3667e4f1ee7dff2663b9458e73095ef28 Mon Sep 17 00:00:00 2001 From: Dario Baumberger Date: Wed, 11 Oct 2023 18:54:44 +0200 Subject: [PATCH 3/9] Added command to translate selection with language "to" option, which replaces the translation to the selection. --- src/main.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main.ts b/src/main.ts index 2e50edf..0d0a3df 100644 --- a/src/main.ts +++ b/src/main.ts @@ -84,6 +84,36 @@ export default class DeepLPlugin extends Plugin { }).open(); }, }); + + this.addCommand({ + id: "deepl-translate-selection-to", + name: "Translate selection: to language", + editorCallback: async (editor: Editor) => { + if (editor.getSelection() === "") { + return; + } + + new TranslateModal(app, "To", async (language) => { + try { + const translation = await this.deeplService.translate( + editor.getSelection(), + language.code, + this.settings.fromLanguage + ); + 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(); + }, + }); } async loadSettings() { From 63fb4549a6119d7c5f08e518ff0c23cab4d7f716 Mon Sep 17 00:00:00 2001 From: Dario Baumberger Date: Wed, 11 Oct 2023 18:56:17 +0200 Subject: [PATCH 4/9] Added command to translate selection with language "from" and "to" option, which replaces the translation to the selection. --- src/main.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main.ts b/src/main.ts index 0d0a3df..357a45c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -114,6 +114,39 @@ export default class DeepLPlugin extends Plugin { }).open(); }, }); + + 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", async (from) => { + new TranslateModal(app, "To", 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(); + }, + }); } async loadSettings() { From 8cebb2ae69f05f129ac206d33324dfa6e9610fae Mon Sep 17 00:00:00 2001 From: Dario Baumberger Date: Wed, 11 Oct 2023 19:11:06 +0200 Subject: [PATCH 5/9] updated version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index ea8249b..e25bfcb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-deepl", - "version": "1.0.1", + "version": "1.0.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "obsidian-deepl", - "version": "1.0.1", + "version": "1.0.6", "license": "MIT", "devDependencies": { "@types/node": "^16.11.6", diff --git a/package.json b/package.json index 59c3a6c..b9a7711 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-deepl", - "version": "1.0.5", + "version": "1.0.6", "description": "Allows translation of selected texts into more than 25 languages with DeepL for Obsidian.md.", "main": "main.js", "scripts": { From 0ed89d5d01264a2e5e55f214a41901ea480ef093 Mon Sep 17 00:00:00 2001 From: Dario Baumberger Date: Fri, 13 Oct 2023 13:46:55 +0200 Subject: [PATCH 6/9] pass langugaes as argument for dynamic proposals --- src/deepl/translateModal.ts | 9 +-- src/main.ts | 145 ++++++++++++++++++++++-------------- 2 files changed, 93 insertions(+), 61 deletions(-) diff --git a/src/deepl/translateModal.ts b/src/deepl/translateModal.ts index f78c09f..7a15845 100644 --- a/src/deepl/translateModal.ts +++ b/src/deepl/translateModal.ts @@ -1,20 +1,15 @@ import { App, SuggestModal } from "obsidian"; -import { toLanguages } from "src/deepl/toLanguages"; -export interface Language { +interface Language { code: string; name: string; } export class TranslateModal extends SuggestModal { - languages: Language[] = Object.entries(toLanguages).map(([code, name]) => ({ - code, - name, - })); - constructor( app: App, placeholder: string, + public languages: Language[], public callback: (result: Language) => void ) { super(app); diff --git a/src/main.ts b/src/main.ts index 357a45c..f98c75b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,6 +9,7 @@ import { import { SettingTab } from "./settings/settingTab"; import { addStatusBar } from "./settings/statusbar"; import { TranslateModal } from "./deepl/translateModal"; +import { fromLanguages } from "./deepl/fromLanguages"; export default class DeepLPlugin extends Plugin { public deeplService: DeepLService; @@ -61,27 +62,36 @@ export default class DeepLPlugin extends Plugin { const selection = editor.getSelection(); - new TranslateModal(app, "To", async (language) => { - try { - const translation = await this.deeplService.translate( - selection, - language.code, - this.settings.fromLanguage - ); - editor.replaceSelection( - `${selection} ${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." + 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 + ); + editor.replaceSelection( + `${selection} ${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(); }, }); @@ -93,44 +103,20 @@ export default class DeepLPlugin extends Plugin { return; } - new TranslateModal(app, "To", async (language) => { - try { - const translation = await this.deeplService.translate( - editor.getSelection(), - language.code, - this.settings.fromLanguage - ); - 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(); - }, - }); - - 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", async (from) => { - new TranslateModal(app, "To", async (to) => { + new TranslateModal( + app, + "To", + Object.entries(toLanguages).map(([code, name]) => ({ + code, + name, + })), + async (language) => { try { const translation = await this.deeplService.translate( editor.getSelection(), - to.code, - from.code + language.code, + this.settings.fromLanguage ); editor.replaceSelection(translation[0].text); } catch (error) { @@ -143,8 +129,59 @@ export default class DeepLPlugin extends Plugin { ); } } - }).open(); - }).open(); + } + ).open(); + }, + }); + + 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(); }, }); } From cdec3160fca89dff83302bbfbd50d7b60aa03439 Mon Sep 17 00:00:00 2001 From: Dario Baumberger Date: Sun, 15 Oct 2023 15:17:28 +0200 Subject: [PATCH 7/9] added command descriptions in readme and settings --- README.md | 13 ++++++++++--- src/settings/settingTab.ts | 26 +++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ba69295..144eded 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,17 @@ Translate your texts with [DeepL](https://www.deepl.com/) in [Obsidian](https:// ## Commands -Currently there is one command for translating selected texts `DeepL: Translate selection`. You can set the language from and to which you want to translate in the settings. Later, more commands are added. +| Command | Description | +| -------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Translate selection | You can set the language from and to which you want to translate in the settings. | +| Translate selection: to language | The target language can be selected by suggestion modal. The target language can be selected by suggestion modal. The selection will be replaced by the translation. | +| Translate selection: From a language to another | The source and target languages can be selected by suggestion modal. The target language can be selected by suggestion modal. The selection will be replaced by the translation. | +| Translate selection: To language and append to selection | The target language can be selected by suggestion modal. The translation will be appended to the selection. | ## Requirements -To use the plugin you need a DeepL API key. You can get it for free after creating a DeepL account at [https://deepl.com/pro](https://deepl.com/pro). +To use the plugin you need a DeepL API key. You can get it for free after creating a DeepL account +at [https://deepl.com/pro](https://deepl.com/pro). ## Disclaimer @@ -19,4 +25,5 @@ This is not an official plugin from DeepL, but was developed by me. ## Privacy policy -Your texts will be sent to DeepL, please check the [privacy policy](https://www.deepl.com/en/privacy/) of DeepL. Don't use the plugin if you don't agree with the privacy policy. +Your texts will be sent to DeepL, please check the [privacy policy](https://www.deepl.com/en/privacy/) of DeepL. Don't +use the plugin if you don't agree with the privacy policy. diff --git a/src/settings/settingTab.ts b/src/settings/settingTab.ts index 2114c6f..eb7f811 100644 --- a/src/settings/settingTab.ts +++ b/src/settings/settingTab.ts @@ -25,9 +25,29 @@ export class SettingTab extends PluginSettingTab { text: "Commands", }); - containerEl.createEl("p", { - text: 'Translate selection: Translates the selected text from the "From language" to the "To language".', - }); + new Setting(containerEl) + .setName("Translate selection") + .setDesc( + 'Translates the selected text from the "From language" to the "To language".' + ); + + new Setting(containerEl) + .setName("Translate selection: to language") + .setDesc( + "The target language can be selected by suggestion modal. The target language can be selected by suggestion modal. The selection will be replaced by the translation." + ); + + new Setting(containerEl) + .setName("Translate selection: From a language to another") + .setDesc( + "The source and target languages can be selected by suggestion modal. The target language can be selected by suggestion modal. The selection will be replaced by the translation." + ); + + new Setting(containerEl) + .setName("Translate selection: To language and append to selection") + .setDesc( + "The target language can be selected by suggestion modal. The translation will be appended to the selection." + ); containerEl.createEl("h4", { text: "Language settings", From 4e544e2707f0ad0d61db18824d6b4b7ad3dadbe4 Mon Sep 17 00:00:00 2001 From: Dario Baumberger Date: Sun, 15 Oct 2023 15:24:57 +0200 Subject: [PATCH 8/9] reverted unwanted formatting --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 144eded..e2601bf 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,7 @@ Translate your texts with [DeepL](https://www.deepl.com/) in [Obsidian](https:// ## Requirements -To use the plugin you need a DeepL API key. You can get it for free after creating a DeepL account -at [https://deepl.com/pro](https://deepl.com/pro). +To use the plugin you need a DeepL API key. You can get it for free after creating a DeepL account at [https://deepl.com/pro](https://deepl.com/pro). ## Disclaimer @@ -25,5 +24,4 @@ This is not an official plugin from DeepL, but was developed by me. ## Privacy policy -Your texts will be sent to DeepL, please check the [privacy policy](https://www.deepl.com/en/privacy/) of DeepL. Don't -use the plugin if you don't agree with the privacy policy. +Your texts will be sent to DeepL, please check the [privacy policy](https://www.deepl.com/en/privacy/) of DeepL. Don't use the plugin if you don't agree with the privacy policy. From 8d2a0badf3b0902a80fd2005b5ffeec4791bb85a Mon Sep 17 00:00:00 2001 From: Dario Baumberger Date: Sun, 15 Oct 2023 17:59:54 +0200 Subject: [PATCH 9/9] updated manifest and version json --- manifest.json | 2 +- versions.json | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/manifest.json b/manifest.json index e869254..5a03780 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "deepl", "name": "DeepL", - "version": "1.0.5", + "version": "1.0.6", "minAppVersion": "0.15.0", "description": "Allows translation of selected texts into more than 25 languages with DeepL.", "author": "Till Friebe", diff --git a/versions.json b/versions.json index 1a3cb8a..af1fa47 100644 --- a/versions.json +++ b/versions.json @@ -4,5 +4,6 @@ "1.0.2": "0.15.0", "1.0.3": "0.15.0", "1.0.4": "0.15.0", - "1.0.5": "0.15.0" + "1.0.5": "0.15.0", + "1.0.6": "0.15.0" }