Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Google translate] Add a preference for the default action #12078

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions extensions/google-translate/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 20 additions & 1 deletion extensions/google-translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"AlanHuang",
"nirtamir2",
"pernielsentikaer",
"rasitayaz"
"rasitayaz",
"popalay"
],
"license": "MIT",
"commands": [
Expand Down Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -63,31 +64,31 @@ export function QuickTranslateListItem(props: {
},
]}
detail={<List.Item.Detail markdown={result.translatedText} />}
actions={
<ActionPanel>
<ActionPanel.Section>
<Action.CopyToClipboard title="Copy" content={result.translatedText} />
<Action
title="Toggle Full Text"
icon={Icon.Text}
onAction={() => props.setIsShowingDetail(!props.isShowingDetail)}
/>
<Action.OpenInBrowser
title="Open in Google Translate"
shortcut={{ modifiers: ["opt"], key: "enter" }}
url={
"https://translate.google.com/?sl=" +
result.langFrom +
"&tl=" +
result.langTo +
"&text=" +
encodeURIComponent(props.debouncedText) +
"&op=translate"
}
/>
</ActionPanel.Section>
</ActionPanel>
}
actions={getActions({
value: result.translatedText,
otherActions: [
<Action
key="toggle-full-text"
title="Toggle Full Text"
icon={Icon.Text}
onAction={() => props.setIsShowingDetail(!props.isShowingDetail)}
/>,
<Action.OpenInBrowser
key="open-in-google-translate"
title="Open in Google Translate"
shortcut={{ modifiers: ["opt"], key: "enter" }}
url={
"https://translate.google.com/?sl=" +
result.langFrom +
"&tl=" +
result.langTo +
"&text=" +
encodeURIComponent(props.debouncedText) +
"&op=translate"
}
/>,
],
})}
/>
);
}
56 changes: 56 additions & 0 deletions extensions/google-translate/src/actions.tsx
Original file line number Diff line number Diff line change
@@ -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;
}
Popalay marked this conversation as resolved.
Show resolved Hide resolved

interface ActionsOpts {
value: string;
defaultActionsPrefix?: string;
firstSectionTitle?: string;
otherActions?: React.ReactElement[];
otherSections?: React.ReactElement[];
Popalay marked this conversation as resolved.
Show resolved Hide resolved
}

export function getActions({
value,
defaultActionsPrefix,
firstSectionTitle,
otherActions,
otherSections,
}: ActionsOpts) {
const defaultPreference = getPreferenceValues<Preferences>().defaultAction;
const action = [
Popalay marked this conversation as resolved.
Show resolved Hide resolved
<Action.CopyToClipboard
title={defaultActionsPrefix ? `Copy ${defaultActionsPrefix}` : `Copy`}
key={DefaultActionPreference.CopyToClipboard}
content={value}
/>,
<Action.Paste
title={defaultActionsPrefix ? `Paste ${defaultActionsPrefix}` : `Paste`}
key={DefaultActionPreference.PasteToApp}
content={value}
/>,
];
const defaultAction = action.find((action) => action.key === defaultPreference);
const secondaryAction = action.filter((action) => action.key !== defaultPreference);

return (
<ActionPanel>
<ActionPanel.Section title={firstSectionTitle}>
<>
{defaultAction}
{secondaryAction}
{otherActions}
</>
</ActionPanel.Section>
<>{otherSections}</>
</ActionPanel>
);
}
70 changes: 36 additions & 34 deletions extensions/google-translate/src/translate-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -55,37 +56,38 @@ export default function TranslateForm() {
return (
<Form
isLoading={isLoading}
actions={
<ActionPanel>
<ActionPanel.Section title="Generals">
<Action.CopyToClipboard title="Copy Translated" content={translated?.translatedText ?? ""} />
<Action.CopyToClipboard title="Copy Text" content={text ?? ""} />
Popalay marked this conversation as resolved.
Show resolved Hide resolved
<Action.CopyToClipboard
title="Copy Pronunciation"
shortcut={{ modifiers: ["cmd", "shift"], key: "p" }}
content={translated?.pronunciationText ?? ""}
/>
<Action.OpenInBrowser
title="Open in Google Translate"
shortcut={{ modifiers: ["opt"], key: "enter" }}
url={
"https://translate.google.com/?sl=" +
langFrom +
"&tl=" +
langTo +
"&text=" +
encodeURIComponent(text) +
"&op=translate"
}
/>
<Action.Push
icon={Icon.Pencil}
title="Manage language sets..."
shortcut={{ modifiers: ["cmd"], key: "l" }}
target={<LanguagesManagerList />}
/>
</ActionPanel.Section>

actions={getActions({
value: translated?.translatedText ?? "",
defaultActionsPrefix: "Translated",
firstSectionTitle: "Generals",
otherActions: [
<Action.CopyToClipboard title="Copy Text" content={text ?? ""} />,
<Action.CopyToClipboard
title="Copy Pronunciation"
shortcut={{ modifiers: ["cmd", "shift"], key: "p" }}
content={translated?.pronunciationText ?? ""}
/>,
<Action.OpenInBrowser
title="Open in Google Translate"
shortcut={{ modifiers: ["opt"], key: "enter" }}
url={
"https://translate.google.com/?sl=" +
langFrom +
"&tl=" +
langTo +
"&text=" +
encodeURIComponent(text) +
"&op=translate"
}
/>,
<Action.Push
icon={Icon.Pencil}
title="Manage language sets..."
shortcut={{ modifiers: ["cmd"], key: "l" }}
target={<LanguagesManagerList />}
/>,
],
otherSections: [
<ActionPanel.Section title="Settings">
<Action
shortcut={{ modifiers: ["cmd", "shift"], key: "s" }}
Expand Down Expand Up @@ -131,9 +133,9 @@ export default function TranslateForm() {
))}
</ActionPanel.Submenu>
</ActionPanel.Submenu>
</ActionPanel.Section>
</ActionPanel>
}
</ActionPanel.Section>,
],
})}
>
<Form.TextArea id="text" title="Text" value={text} onChange={handleChange} />
<Form.Dropdown
Expand Down
93 changes: 45 additions & 48 deletions extensions/google-translate/src/translate.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { ReactElement, useState } from "react";
import { List, ActionPanel, showToast, Toast, Action, Icon } from "@raycast/api";
import { List, showToast, Toast, Action, Icon } from "@raycast/api";
import { usePromise } from "@raycast/utils";
import { useDebouncedValue, useSelectedLanguagesSet, useTextState } from "./hooks";
import { getLanguageFlag, supportedLanguagesByCode } from "./languages";
import { LanguageManagerListDropdown } from "./LanguagesManager";
import { doubleWayTranslate, playTTS } from "./simple-translate";
import { getActions } from "./actions";

export default function Translate(): ReactElement {
const [selectedLanguageSet] = useSelectedLanguagesSet();
Expand Down Expand Up @@ -48,21 +49,50 @@ export default function Translate(): ReactElement {
title={r.translatedText}
accessories={[{ text: languages, tooltip: tooltip }]}
detail={<List.Item.Detail markdown={r.translatedText} />}
actions={
<ActionPanel>
<ActionPanel.Section>
Popalay marked this conversation as resolved.
Show resolved Hide resolved
<Action.CopyToClipboard title="Copy" content={r.translatedText} />
actions={getActions({
value: r.translatedText,
otherActions: [
<Action
title="Toggle Full Text"
icon={Icon.Text}
onAction={() => setIsShowingDetail(!isShowingDetail)}
/>,
<Action
title="Play Text-To-Speech"
icon={Icon.Play}
shortcut={{ modifiers: ["cmd"], key: "t" }}
onAction={() => playTTS(r.translatedText, r.langTo)}
/>,
<Action.OpenInBrowser
title="Open in Google Translate"
shortcut={{ modifiers: ["opt"], key: "enter" }}
url={
"https://translate.google.com/?sl=" +
r.langFrom +
"&tl=" +
r.langTo +
"&text=" +
encodeURIComponent(debouncedValue) +
"&op=translate"
}
/>,
],
})}
/>
{r.pronunciationText && (
<List.Item
key={index}
title={r.pronunciationText}
accessories={[{ text: languages, tooltip: tooltip }]}
detail={<List.Item.Detail markdown={r.pronunciationText} />}
actions={getActions({
value: r.translatedText,
otherActions: [
<Action
title="Toggle Full Text"
icon={Icon.Text}
onAction={() => setIsShowingDetail(!isShowingDetail)}
/>
<Action
title="Play Text-To-Speech"
icon={Icon.Play}
shortcut={{ modifiers: ["cmd"], key: "t" }}
onAction={() => playTTS(r.translatedText, r.langTo)}
/>
/>,
<Action.OpenInBrowser
title="Open in Google Translate"
shortcut={{ modifiers: ["opt"], key: "enter" }}
Expand All @@ -75,42 +105,9 @@ export default function Translate(): ReactElement {
encodeURIComponent(debouncedValue) +
"&op=translate"
}
/>
</ActionPanel.Section>
</ActionPanel>
}
/>
{r.pronunciationText && (
<List.Item
key={index}
title={r.pronunciationText}
accessories={[{ text: languages, tooltip: tooltip }]}
detail={<List.Item.Detail markdown={r.pronunciationText} />}
actions={
<ActionPanel>
<ActionPanel.Section>
<Action.CopyToClipboard title="Copy" content={r.pronunciationText} />
<Action
title="Toggle Full Text"
icon={Icon.Text}
onAction={() => setIsShowingDetail(!isShowingDetail)}
/>
<Action.OpenInBrowser
title="Open in Google Translate"
shortcut={{ modifiers: ["opt"], key: "enter" }}
url={
"https://translate.google.com/?sl=" +
r.langFrom +
"&tl=" +
r.langTo +
"&text=" +
encodeURIComponent(debouncedValue) +
"&op=translate"
}
/>
</ActionPanel.Section>
</ActionPanel>
}
/>,
],
})}
/>
)}
</>
Expand Down