diff --git a/README.md b/README.md index ba69295..e2601bf 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,12 @@ 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 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/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": { diff --git a/src/deepl/translateModal.ts b/src/deepl/translateModal.ts new file mode 100644 index 0000000..7a15845 --- /dev/null +++ b/src/deepl/translateModal.ts @@ -0,0 +1,41 @@ +import { App, SuggestModal } from "obsidian"; + +interface Language { + code: string; + name: string; +} + +export class TranslateModal extends SuggestModal { + constructor( + app: App, + placeholder: string, + public languages: Language[], + 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(); + } +} diff --git a/src/main.ts b/src/main.ts index 98b3e0b..f98c75b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,10 +4,12 @@ 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"; +import { fromLanguages } from "./deepl/fromLanguages"; export default class DeepLPlugin extends Plugin { public deeplService: DeepLService; @@ -49,6 +51,139 @@ 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", + 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(); + }, + }); + + this.addCommand({ + id: "deepl-translate-selection-to", + name: "Translate selection: to language", + editorCallback: async (editor: Editor) => { + if (editor.getSelection() === "") { + return; + } + + new TranslateModal( + app, + "To", + Object.entries(toLanguages).map(([code, name]) => ({ + code, + name, + })), + 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", + 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(); + }, + }); } async loadSettings() { 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", 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" }