From 6bfd2e5a83b9a2a147b7f5e37f7cc92ff9936712 Mon Sep 17 00:00:00 2001 From: Denis Nykyforov Date: Mon, 29 Apr 2024 18:58:29 +0300 Subject: [PATCH 1/9] Add a preference for the default action in Google Translate extension --- extensions/google-translate/CHANGELOG.md | 3 + extensions/google-translate/package.json | 21 ++++- .../QuickTranslate/QuickTranslateListItem.tsx | 53 +++++------ extensions/google-translate/src/actions.tsx | 56 +++++++++++ .../google-translate/src/translate-form.tsx | 70 +++++++------- extensions/google-translate/src/translate.tsx | 93 +++++++++---------- 6 files changed, 187 insertions(+), 109 deletions(-) create mode 100644 extensions/google-translate/src/actions.tsx diff --git a/extensions/google-translate/CHANGELOG.md b/extensions/google-translate/CHANGELOG.md index f08af3a01a1..312a2b8a487 100644 --- a/extensions/google-translate/CHANGELOG.md +++ b/extensions/google-translate/CHANGELOG.md @@ -1,5 +1,8 @@ # Google Translate Changelog +## [Default Action preference for translations] - 2024-04-29 +- Add a preference to set the default action for the translations + ## [Re-added previously removed contributor] - 2024-04-26 ## [Fix] - 2024-03-08 diff --git a/extensions/google-translate/package.json b/extensions/google-translate/package.json index 10cafc5b05c..06d4307d02b 100644 --- a/extensions/google-translate/package.json +++ b/extensions/google-translate/package.json @@ -16,7 +16,8 @@ "AlanHuang", "nirtamir2", "pernielsentikaer", - "rasitayaz" + "rasitayaz", + "popalay" ], "license": "MIT", "commands": [ @@ -943,6 +944,24 @@ "required": false, "type": "checkbox", "default": true + }, + { + "name": "defaultAction", + "title": "Default Action", + "description": "The default action to take when translating.", + "type": "dropdown", + "required": false, + "default": "copy", + "data": [ + { + "title": "Copy to Clipboard", + "value": "copy" + }, + { + "title": "Paste to App", + "value": "paste" + } + ] } ], "dependencies": { diff --git a/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx b/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx index 0631f065853..1eca0fbce79 100644 --- a/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx +++ b/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx @@ -1,8 +1,9 @@ -import { Action, ActionPanel, Icon, List, Toast, showToast } from "@raycast/api"; +import { Action, Icon, List, Toast, showToast } from "@raycast/api"; import { usePromise } from "@raycast/utils"; import { getLanguageFlag, supportedLanguagesByCode } from "../languages"; import { simpleTranslate } from "../simple-translate"; import { LanguageCodeSet } from "../types"; +import { getActions } from "../actions"; export function QuickTranslateListItem(props: { debouncedText: string; @@ -63,31 +64,31 @@ export function QuickTranslateListItem(props: { }, ]} detail={} - actions={ - - - - props.setIsShowingDetail(!props.isShowingDetail)} - /> - - - - } + actions={getActions({ + value: result.translatedText, + otherActions: [ + props.setIsShowingDetail(!props.isShowingDetail)} + />, + , + ], + })} /> ); } diff --git a/extensions/google-translate/src/actions.tsx b/extensions/google-translate/src/actions.tsx new file mode 100644 index 00000000000..7ed7fcf0cc3 --- /dev/null +++ b/extensions/google-translate/src/actions.tsx @@ -0,0 +1,56 @@ +import React from "react"; +import { Action, ActionPanel, getPreferenceValues } from "@raycast/api"; + +enum DefaultActionPreference { + CopyToClipboard = "copy", + PasteToApp = "paste", +} + +interface Preferences { + defaultAction?: DefaultActionPreference; +} + +interface ActionsOpts { + value: string; + defaultActionsPrefix?: string; + firstSectionTitle?: string; + otherActions?: React.ReactElement[]; + otherSections?: React.ReactElement[]; +} + +export function getActions({ + value, + defaultActionsPrefix, + firstSectionTitle, + otherActions, + otherSections, +}: ActionsOpts) { + const defaultPreference = getPreferenceValues().defaultAction; + const action = [ + , + , + ]; + const defaultAction = action.find((action) => action.key === defaultPreference); + const secondaryAction = action.filter((action) => action.key !== defaultPreference); + + return ( + + + <> + {defaultAction} + {secondaryAction} + {otherActions} + + + <>{otherSections} + + ); +} diff --git a/extensions/google-translate/src/translate-form.tsx b/extensions/google-translate/src/translate-form.tsx index 1461324ffdc..813ab17851b 100644 --- a/extensions/google-translate/src/translate-form.tsx +++ b/extensions/google-translate/src/translate-form.tsx @@ -5,6 +5,7 @@ import { useDebouncedValue, useSelectedLanguagesSet, useTextState } from "./hook import { LanguageCode, supportedLanguagesByCode, languages, getLanguageFlag } from "./languages"; import { AUTO_DETECT, simpleTranslate } from "./simple-translate"; import { LanguagesManagerList } from "./LanguagesManager"; +import { getActions } from "./actions"; export default function TranslateForm() { const [selectedLanguageSet, setSelectedLanguageSet] = useSelectedLanguagesSet(); @@ -55,37 +56,38 @@ export default function TranslateForm() { return (
- - - - - - } - /> - - + actions={getActions({ + value: translated?.translatedText ?? "", + defaultActionsPrefix: "Translated", + firstSectionTitle: "Generals", + otherActions: [ + , + , + , + } + />, + ], + otherSections: [ - - - } + , + ], + })} > } - actions={ - - - + actions={getActions({ + value: r.translatedText, + otherActions: [ + setIsShowingDetail(!isShowingDetail)} + />, + playTTS(r.translatedText, r.langTo)} + />, + , + ], + })} + /> + {r.pronunciationText && ( + } + actions={getActions({ + value: r.translatedText, + otherActions: [ setIsShowingDetail(!isShowingDetail)} - /> - playTTS(r.translatedText, r.langTo)} - /> + />, - - - } - /> - {r.pronunciationText && ( - } - actions={ - - - - setIsShowingDetail(!isShowingDetail)} - /> - - - - } + />, + ], + })} /> )} From d4470f07467ffff90bbd4a790e0556a62e49e44b Mon Sep 17 00:00:00 2001 From: gebeto Date: Tue, 30 Apr 2024 13:07:58 +0300 Subject: [PATCH 2/9] refactoring --- .../QuickTranslate/QuickTranslateListItem.tsx | 56 ++++++++------- extensions/google-translate/src/actions.tsx | 48 +++++-------- .../google-translate/src/translate-form.tsx | 72 +++++++++---------- extensions/google-translate/src/translate.tsx | 40 +++++------ 4 files changed, 101 insertions(+), 115 deletions(-) diff --git a/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx b/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx index 1eca0fbce79..d5a34add0a0 100644 --- a/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx +++ b/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx @@ -1,9 +1,9 @@ -import { Action, Icon, List, Toast, showToast } from "@raycast/api"; +import { Action, ActionPanel, Icon, List, Toast, showToast } from "@raycast/api"; import { usePromise } from "@raycast/utils"; import { getLanguageFlag, supportedLanguagesByCode } from "../languages"; import { simpleTranslate } from "../simple-translate"; import { LanguageCodeSet } from "../types"; -import { getActions } from "../actions"; +import { ConfigurableAction } from "../actions"; export function QuickTranslateListItem(props: { debouncedText: string; @@ -64,31 +64,33 @@ export function QuickTranslateListItem(props: { }, ]} detail={} - actions={getActions({ - value: result.translatedText, - otherActions: [ - props.setIsShowingDetail(!props.isShowingDetail)} - />, - , - ], - })} + actions={ + + + + props.setIsShowingDetail(!props.isShowingDetail)} + /> + + + + } /> ); } diff --git a/extensions/google-translate/src/actions.tsx b/extensions/google-translate/src/actions.tsx index 7ed7fcf0cc3..867eb75a029 100644 --- a/extensions/google-translate/src/actions.tsx +++ b/extensions/google-translate/src/actions.tsx @@ -1,5 +1,5 @@ import React from "react"; -import { Action, ActionPanel, getPreferenceValues } from "@raycast/api"; +import { Action, getPreferenceValues } from "@raycast/api"; enum DefaultActionPreference { CopyToClipboard = "copy", @@ -18,39 +18,25 @@ interface ActionsOpts { otherSections?: React.ReactElement[]; } -export function getActions({ - value, - defaultActionsPrefix, - firstSectionTitle, - otherActions, - otherSections, -}: ActionsOpts) { +export const ConfigurableAction = ({ defaultActionsPrefix, value }: ActionsOpts) => { const defaultPreference = getPreferenceValues().defaultAction; - const action = [ + + if (defaultPreference === DefaultActionPreference.PasteToApp) { + return ( + + ); + } + + // DefaultActionPreference.CopyToClipboard is default action + return ( , - , - ]; - const defaultAction = action.find((action) => action.key === defaultPreference); - const secondaryAction = action.filter((action) => action.key !== defaultPreference); - - return ( - - - <> - {defaultAction} - {secondaryAction} - {otherActions} - - - <>{otherSections} - + /> ); -} +}; diff --git a/extensions/google-translate/src/translate-form.tsx b/extensions/google-translate/src/translate-form.tsx index 813ab17851b..a2cebffa56b 100644 --- a/extensions/google-translate/src/translate-form.tsx +++ b/extensions/google-translate/src/translate-form.tsx @@ -5,7 +5,7 @@ import { useDebouncedValue, useSelectedLanguagesSet, useTextState } from "./hook import { LanguageCode, supportedLanguagesByCode, languages, getLanguageFlag } from "./languages"; import { AUTO_DETECT, simpleTranslate } from "./simple-translate"; import { LanguagesManagerList } from "./LanguagesManager"; -import { getActions } from "./actions"; +import { ConfigurableAction } from "./actions"; export default function TranslateForm() { const [selectedLanguageSet, setSelectedLanguageSet] = useSelectedLanguagesSet(); @@ -56,38 +56,38 @@ export default function TranslateForm() { return ( , - , - , - } - />, - ], - otherSections: [ + actions={ + + + + , + + , + + , + } + /> + - , - ], - })} + + + } > ${langTo?.name ?? langTo?.code}`; return ( - <> + } - actions={getActions({ - value: r.translatedText, - otherActions: [ + actions={ + + setIsShowingDetail(!isShowingDetail)} - />, + /> playTTS(r.translatedText, r.langTo)} - />, + /> , - ], - })} + /> + + } /> {r.pronunciationText && ( } - actions={getActions({ - value: r.translatedText, - otherActions: [ + actions={ + + setIsShowingDetail(!isShowingDetail)} - />, + /> , - ], - })} + /> + + } /> )} - + ); })} From be6daa0b1356777c27eeae0af0dfda9b5b320662 Mon Sep 17 00:00:00 2001 From: gebeto Date: Tue, 30 Apr 2024 13:18:18 +0300 Subject: [PATCH 3/9] add missing sections --- .../QuickTranslate/QuickTranslateListItem.tsx | 2 +- extensions/google-translate/src/translate.tsx | 40 ++++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx b/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx index d5a34add0a0..e38f965a400 100644 --- a/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx +++ b/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx @@ -66,7 +66,7 @@ export function QuickTranslateListItem(props: { detail={} actions={ - + } actions={ - - setIsShowingDetail(!isShowingDetail)} - /> - + + + setIsShowingDetail(!isShowingDetail)} + /> + + } /> From f62772fab056ec6bf8addbb7712b5357b08fb594 Mon Sep 17 00:00:00 2001 From: gebeto Date: Tue, 30 Apr 2024 13:19:36 +0300 Subject: [PATCH 4/9] remove unused keys --- .../src/QuickTranslate/QuickTranslateListItem.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx b/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx index e38f965a400..61bc7f70350 100644 --- a/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx +++ b/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx @@ -69,13 +69,11 @@ export function QuickTranslateListItem(props: { props.setIsShowingDetail(!props.isShowingDetail)} /> Date: Tue, 30 Apr 2024 13:31:34 +0300 Subject: [PATCH 5/9] fix logic --- extensions/google-translate/src/actions.tsx | 33 ++++++++++++++------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/extensions/google-translate/src/actions.tsx b/extensions/google-translate/src/actions.tsx index 867eb75a029..cffabe01f24 100644 --- a/extensions/google-translate/src/actions.tsx +++ b/extensions/google-translate/src/actions.tsx @@ -21,22 +21,35 @@ interface ActionsOpts { export const ConfigurableAction = ({ defaultActionsPrefix, value }: ActionsOpts) => { const defaultPreference = getPreferenceValues().defaultAction; + const pasteAction = ( + + ); + const copyAction = ( + + ); + if (defaultPreference === DefaultActionPreference.PasteToApp) { return ( - + <> + {pasteAction} + {copyAction} + ); } // DefaultActionPreference.CopyToClipboard is default action return ( - + <> + {copyAction} + {pasteAction} + ); }; From c8f5b65d50e6e14ebe4d98e7e5af9cc714cb85a0 Mon Sep 17 00:00:00 2001 From: gebeto Date: Tue, 30 Apr 2024 13:32:34 +0300 Subject: [PATCH 6/9] remove extra comment --- extensions/google-translate/src/actions.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/extensions/google-translate/src/actions.tsx b/extensions/google-translate/src/actions.tsx index cffabe01f24..18e0198c39e 100644 --- a/extensions/google-translate/src/actions.tsx +++ b/extensions/google-translate/src/actions.tsx @@ -45,7 +45,6 @@ export const ConfigurableAction = ({ defaultActionsPrefix, value }: ActionsOpts) ); } - // DefaultActionPreference.CopyToClipboard is default action return ( <> {copyAction} From d76ada285b16aef375cac9171d4da8e5fa276aaa Mon Sep 17 00:00:00 2001 From: gebeto Date: Tue, 30 Apr 2024 13:33:29 +0300 Subject: [PATCH 7/9] remove keys --- extensions/google-translate/src/actions.tsx | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/extensions/google-translate/src/actions.tsx b/extensions/google-translate/src/actions.tsx index 18e0198c39e..a06a01c6740 100644 --- a/extensions/google-translate/src/actions.tsx +++ b/extensions/google-translate/src/actions.tsx @@ -22,18 +22,10 @@ export const ConfigurableAction = ({ defaultActionsPrefix, value }: ActionsOpts) const defaultPreference = getPreferenceValues().defaultAction; const pasteAction = ( - + ); const copyAction = ( - + ); if (defaultPreference === DefaultActionPreference.PasteToApp) { From 4c4d0d33658da07fdc02849aa4adba745e941a93 Mon Sep 17 00:00:00 2001 From: gebeto Date: Tue, 30 Apr 2024 13:34:52 +0300 Subject: [PATCH 8/9] update configurable actions name --- .../src/QuickTranslate/QuickTranslateListItem.tsx | 4 ++-- extensions/google-translate/src/actions.tsx | 2 +- extensions/google-translate/src/translate-form.tsx | 4 ++-- extensions/google-translate/src/translate.tsx | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx b/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx index 61bc7f70350..f08f5805560 100644 --- a/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx +++ b/extensions/google-translate/src/QuickTranslate/QuickTranslateListItem.tsx @@ -3,7 +3,7 @@ import { usePromise } from "@raycast/utils"; import { getLanguageFlag, supportedLanguagesByCode } from "../languages"; import { simpleTranslate } from "../simple-translate"; import { LanguageCodeSet } from "../types"; -import { ConfigurableAction } from "../actions"; +import { ConfigurableCopyPasteActions } from "../actions"; export function QuickTranslateListItem(props: { debouncedText: string; @@ -67,7 +67,7 @@ export function QuickTranslateListItem(props: { actions={ - + { +export const ConfigurableCopyPasteActions = ({ defaultActionsPrefix, value }: ActionsOpts) => { const defaultPreference = getPreferenceValues().defaultAction; const pasteAction = ( diff --git a/extensions/google-translate/src/translate-form.tsx b/extensions/google-translate/src/translate-form.tsx index a2cebffa56b..17377652fca 100644 --- a/extensions/google-translate/src/translate-form.tsx +++ b/extensions/google-translate/src/translate-form.tsx @@ -5,7 +5,7 @@ import { useDebouncedValue, useSelectedLanguagesSet, useTextState } from "./hook import { LanguageCode, supportedLanguagesByCode, languages, getLanguageFlag } from "./languages"; import { AUTO_DETECT, simpleTranslate } from "./simple-translate"; import { LanguagesManagerList } from "./LanguagesManager"; -import { ConfigurableAction } from "./actions"; +import { ConfigurableCopyPasteActions } from "./actions"; export default function TranslateForm() { const [selectedLanguageSet, setSelectedLanguageSet] = useSelectedLanguagesSet(); @@ -59,7 +59,7 @@ export default function TranslateForm() { actions={ - + , } actions={ - + - + Date: Tue, 30 Apr 2024 13:45:37 +0300 Subject: [PATCH 9/9] Use generated ExtensionPreferences. Minor code style fixes. --- extensions/google-translate/src/actions.tsx | 16 +----- .../google-translate/src/translate-form.tsx | 4 +- extensions/google-translate/src/translate.tsx | 54 ++++++++++--------- extensions/google-translate/tsconfig.json | 2 +- 4 files changed, 32 insertions(+), 44 deletions(-) diff --git a/extensions/google-translate/src/actions.tsx b/extensions/google-translate/src/actions.tsx index 9cd7eccdb40..085c53fad1b 100644 --- a/extensions/google-translate/src/actions.tsx +++ b/extensions/google-translate/src/actions.tsx @@ -1,25 +1,13 @@ import React from "react"; import { Action, getPreferenceValues } from "@raycast/api"; -enum DefaultActionPreference { - CopyToClipboard = "copy", - PasteToApp = "paste", -} - -interface Preferences { - defaultAction?: DefaultActionPreference; -} - interface ActionsOpts { value: string; defaultActionsPrefix?: string; - firstSectionTitle?: string; - otherActions?: React.ReactElement[]; - otherSections?: React.ReactElement[]; } export const ConfigurableCopyPasteActions = ({ defaultActionsPrefix, value }: ActionsOpts) => { - const defaultPreference = getPreferenceValues().defaultAction; + const defaultPreference = getPreferenceValues().defaultAction; const pasteAction = ( @@ -28,7 +16,7 @@ export const ConfigurableCopyPasteActions = ({ defaultActionsPrefix, value }: Ac ); - if (defaultPreference === DefaultActionPreference.PasteToApp) { + if (defaultPreference === "paste") { return ( <> {pasteAction} diff --git a/extensions/google-translate/src/translate-form.tsx b/extensions/google-translate/src/translate-form.tsx index 17377652fca..b49b793a061 100644 --- a/extensions/google-translate/src/translate-form.tsx +++ b/extensions/google-translate/src/translate-form.tsx @@ -60,13 +60,12 @@ export default function TranslateForm() { - , + - , - , } actions={ - - setIsShowingDetail(!isShowingDetail)} - /> - playTTS(r.translatedText, r.langTo)} - /> - + + + setIsShowingDetail(!isShowingDetail)} + /> + playTTS(r.translatedText, r.langTo)} + /> + + } /> @@ -86,7 +88,7 @@ export default function Translate(): ReactElement { actions={ - +