forked from oliverschwendener/ueli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc95cef
commit 8c50467
Showing
16 changed files
with
410 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { TranslationOptions } from "./translation-options"; | ||
import { TranslationLanguage } from "../../main/plugins/translation-execution-plugin/translation-language"; | ||
|
||
export const defaultTranslationOptions: TranslationOptions = { | ||
debounceDelay: 250, | ||
enabled: true, | ||
minSearchTermLength: 3, | ||
prefix: "t?", | ||
sourceLanguage: TranslationLanguage.German, | ||
targetLanguage: TranslationLanguage.English, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { TranslationLanguage } from "../../main/plugins/translation-execution-plugin/translation-language"; | ||
|
||
export interface TranslationOptions { | ||
enabled: boolean; | ||
debounceDelay: number; | ||
minSearchTermLength: number; | ||
prefix: string; | ||
sourceLanguage: TranslationLanguage; | ||
targetLanguage: TranslationLanguage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { SearchResultItem } from "./search-result-item"; | ||
import { IconType } from "./icon/icon-type"; | ||
import { PluginType } from "../main/plugin-type"; | ||
|
||
export const errorSearchResultItem: SearchResultItem = { | ||
description: "Check ueli's log to see more details", | ||
executionArgument: "", | ||
hideMainWindowAfterExecution: false, | ||
icon: { | ||
parameter: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 32 32" version="1.1"> | ||
<g id="surface1"> | ||
<path style=" " d="M 16 3.21875 L 15.125 4.71875 L 3.125 25.5 L 2.28125 27 L 29.71875 27 L 28.875 25.5 L 16.875 4.71875 Z M 16 7.21875 L 26.25 25 L 5.75 25 Z M 15 14 L 15 20 L 17 20 L 17 14 Z M 15 21 L 15 23 L 17 23 L 17 21 Z "></path> | ||
</g> | ||
</svg>`, | ||
type: IconType.SVG, | ||
}, | ||
name: "An error occured", | ||
originPluginType: PluginType.None, | ||
searchable: [], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
export enum PluginType { | ||
None = "none", | ||
ApplicationSearchPlugin = "application-search-plugin", | ||
UeliCommandSearchPlugin = "ueli-command-search-plugin", | ||
ShortcutsSearchPlugin = "shortcuts-search-plugin", | ||
EverythingSearchPlugin = "everything-search-plugin", | ||
MdFindExecutionPlugin = "md-find-execution-plugin", | ||
TranslationPlugin = "translation-plugin", | ||
} |
126 changes: 126 additions & 0 deletions
126
src/main/plugins/translation-execution-plugin/translation-execution-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { ExecutionPlugin } from "../../execution-plugin"; | ||
import { SearchResultItem } from "../../../common/search-result-item"; | ||
import { UserConfigOptions } from "../../../common/config/user-config-options"; | ||
import { PluginType } from "../../plugin-type"; | ||
import axios from "axios"; | ||
import { IconType } from "../../../common/icon/icon-type"; | ||
import { Icon } from "../../../common/icon/icon"; | ||
import { clipboard } from "electron"; | ||
|
||
interface Translation { | ||
text: string; | ||
} | ||
|
||
interface ExactMatch { | ||
translations: Translation[]; | ||
} | ||
|
||
interface TranslationResponse { | ||
exact_matches?: ExactMatch[]; | ||
} | ||
|
||
export class TranslationExecutionPlugin implements ExecutionPlugin { | ||
public readonly pluginType = PluginType.TranslationPlugin; | ||
private config: UserConfigOptions; | ||
private readonly defaultIcon: Icon = { | ||
parameter: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 32 32" version="1.1"> | ||
<g id="surface1"> | ||
<path style=" " d="M 4 4 L 4 22 L 10 22 L 10 28 L 28 28 L 28 10 L 22 10 L 22 4 Z M 6 6 L 20 6 L 20 10.5625 L 10.5625 20 L 6 20 Z M 11 8 L 11 9 L 8 9 L 8 11 L 12.9375 11 C 12.808594 12.148438 12.457031 13.054688 11.875 13.6875 C 11.53125 13.574219 11.222656 13.433594 10.96875 13.28125 C 10.265625 12.863281 10 12.417969 10 12 L 8 12 C 8 13.191406 8.734375 14.183594 9.71875 14.84375 C 9.226563 14.949219 8.65625 15 8 15 L 8 17 C 9.773438 17 11.25 16.59375 12.375 15.84375 C 12.898438 15.933594 13.429688 16 14 16 L 14 14.125 C 14.542969 13.214844 14.832031 12.152344 14.9375 11 L 16 11 L 16 9 L 13 9 L 13 8 Z M 21.4375 12 L 26 12 L 26 26 L 12 26 L 12 21.4375 Z M 20 13.84375 L 19.0625 16.6875 L 17.0625 22.6875 L 17 22.84375 L 17 24 L 19 24 L 19 23.125 L 19.03125 23 L 20.96875 23 L 21 23.125 L 21 24 L 23 24 L 23 22.84375 L 22.9375 22.6875 L 20.9375 16.6875 Z M 20 20.125 L 20.28125 21 L 19.71875 21 Z "></path> | ||
</g> | ||
</svg>`, | ||
type: IconType.SVG, | ||
}; | ||
private delay: NodeJS.Timeout | number; | ||
|
||
constructor(config: UserConfigOptions) { | ||
this.config = config; | ||
} | ||
|
||
public execute(searchResultItem: SearchResultItem): Promise<void> { | ||
return new Promise((resolve) => { | ||
clipboard.writeText(searchResultItem.executionArgument); | ||
resolve(); | ||
}); | ||
} | ||
|
||
public getSearchResults(userInput: string): Promise<SearchResultItem[]> { | ||
return new Promise((resolve, reject) => { | ||
const textToTranslate = userInput.replace(this.config.translationOptions.prefix, ""); | ||
const source = this.config.translationOptions.sourceLanguage; | ||
const target = this.config.translationOptions.targetLanguage; | ||
const url = `https://linguee-api.herokuapp.com/api?q=${textToTranslate}&src=${source}&dst=${target}`; | ||
|
||
if (this.delay !== undefined) { | ||
clearTimeout(this.delay as number); | ||
} | ||
|
||
this.delay = setTimeout(() => { | ||
this.getTranslationResults(url) | ||
.then((result) => resolve(result)) | ||
.catch((err) => reject(err)); | ||
}, this.config.translationOptions.debounceDelay); | ||
}); | ||
} | ||
|
||
public isEnabled() { | ||
return this.config.translationOptions.enabled; | ||
} | ||
|
||
public isValidUserInput(userInput: string) { | ||
return userInput.startsWith(this.config.translationOptions.prefix) | ||
&& userInput.replace(this.config.translationOptions.prefix, "").length > this.config.translationOptions.minSearchTermLength; | ||
} | ||
|
||
public updateConfig(updatedConfig: UserConfigOptions): Promise<void> { | ||
return new Promise((resolve) => { | ||
this.config = updatedConfig; | ||
resolve(); | ||
}); | ||
} | ||
|
||
private getTranslationResults(url: string): Promise<SearchResultItem[]> { | ||
return new Promise((resolve, reject) => { | ||
axios.get(url) | ||
.then((response) => { | ||
const data: TranslationResponse = response.data; | ||
let translationList: string[] = []; | ||
if (data.exact_matches) { | ||
data.exact_matches | ||
.map((exactMatch) => exactMatch.translations.map((t) => t.text)) | ||
.forEach((t) => translationList = translationList.concat(t)); | ||
} | ||
const result = translationList.map((t): SearchResultItem => { | ||
return { | ||
description: "Press ENTER to copy to clipboard", | ||
executionArgument: t, | ||
hideMainWindowAfterExecution: true, | ||
icon: this.defaultIcon, | ||
name: t, | ||
originPluginType: this.pluginType, | ||
searchable: [], | ||
}; | ||
}); | ||
if (result.length > 0) { | ||
resolve(result); | ||
} else { | ||
resolve([this.getErrorResult("No translations found")]); | ||
} | ||
}) | ||
.catch((err) => { | ||
resolve([this.getErrorResult(err.response.data.message, err.message)]); | ||
}); | ||
}); | ||
} | ||
|
||
private getErrorResult(errorMessage: string, details?: string): SearchResultItem { | ||
return { | ||
description: details ? details : "", | ||
executionArgument: "", | ||
hideMainWindowAfterExecution: false, | ||
icon: this.defaultIcon, | ||
name: errorMessage, | ||
originPluginType: this.pluginType, | ||
searchable: [], | ||
}; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/plugins/translation-execution-plugin/translation-language.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export enum TranslationLanguage { | ||
German = "de", | ||
English = "en", | ||
Spanish = "es", | ||
French = "fr", | ||
Portuguese = "pt", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.