From 21a8ea5d1fc01a0d3368ca62e192a7cb608b999e Mon Sep 17 00:00:00 2001 From: an-lee Date: Fri, 6 Sep 2024 19:10:31 +0800 Subject: [PATCH] Improve: update media tab content translation (#1044) * refactor: rename files * update lookup result * fix style --- .../components/medias/media-captions/index.ts | 6 +- .../media-captions/media-caption-tabs.tsx | 12 +- ...sis.tsx => media-tab-content-analysis.tsx} | 14 +- ...nt-note.tsx => media-tab-content-note.tsx} | 2 +- .../media-tab-content-translation.tsx | 128 ++++++++++++++++++ .../tab-content-translation.tsx | 127 ----------------- 6 files changed, 146 insertions(+), 143 deletions(-) rename enjoy/src/renderer/components/medias/media-captions/{tab-content-analysis.tsx => media-tab-content-analysis.tsx} (93%) rename enjoy/src/renderer/components/medias/media-captions/{tab-content-note.tsx => media-tab-content-note.tsx} (98%) create mode 100644 enjoy/src/renderer/components/medias/media-captions/media-tab-content-translation.tsx delete mode 100644 enjoy/src/renderer/components/medias/media-captions/tab-content-translation.tsx diff --git a/enjoy/src/renderer/components/medias/media-captions/index.ts b/enjoy/src/renderer/components/medias/media-captions/index.ts index 2938ec5f..0e6328ac 100644 --- a/enjoy/src/renderer/components/medias/media-captions/index.ts +++ b/enjoy/src/renderer/components/medias/media-captions/index.ts @@ -1,4 +1,4 @@ export * from "./media-caption-tabs"; -export * from "./tab-content-analysis"; -export * from "./tab-content-note"; -export * from "./tab-content-translation"; \ No newline at end of file +export * from "./media-tab-content-analysis"; +export * from "./media-tab-content-note"; +export * from "./media-tab-content-translation"; diff --git a/enjoy/src/renderer/components/medias/media-captions/media-caption-tabs.tsx b/enjoy/src/renderer/components/medias/media-captions/media-caption-tabs.tsx index 6137521f..6201e379 100644 --- a/enjoy/src/renderer/components/medias/media-captions/media-caption-tabs.tsx +++ b/enjoy/src/renderer/components/medias/media-captions/media-caption-tabs.tsx @@ -7,9 +7,9 @@ import { } from "@renderer/components/ui"; import { t } from "i18next"; import { TimelineEntry } from "echogarden/dist/utilities/Timeline.d.js"; -import { TabContentTranslation } from "./tab-content-translation"; -import { TabContentAnalysis } from "./tab-content-analysis"; -import { TabContentNote } from "./tab-content-note"; +import { MediaTabContentTranslation } from "./media-tab-content-translation"; +import { MediaTabContentAnalysis } from "./media-tab-content-analysis"; +import { MediaTabContentNote } from "./media-tab-content-note"; export const MediaCaptionTabs = (props: { caption: TimelineEntry; @@ -38,18 +38,18 @@ export const MediaCaptionTabs = (props: { {children}
- - - +
diff --git a/enjoy/src/renderer/components/medias/media-captions/tab-content-analysis.tsx b/enjoy/src/renderer/components/medias/media-captions/media-tab-content-analysis.tsx similarity index 93% rename from enjoy/src/renderer/components/medias/media-captions/tab-content-analysis.tsx rename to enjoy/src/renderer/components/medias/media-captions/media-tab-content-analysis.tsx index 1b588179..b7b57701 100644 --- a/enjoy/src/renderer/components/medias/media-captions/tab-content-analysis.tsx +++ b/enjoy/src/renderer/components/medias/media-captions/media-tab-content-analysis.tsx @@ -9,7 +9,7 @@ import { LoaderIcon } from "lucide-react"; import { md5 } from "js-md5"; import Markdown from "react-markdown"; -export function TabContentAnalysis(props: { text: string; }) { +export function MediaTabContentAnalysis(props: { text: string }) { const { text } = props; const { EnjoyApp } = useContext(AppSettingsProviderContext); const [analyzing, setAnalyzing] = useState(false); @@ -55,7 +55,7 @@ export function TabContentAnalysis(props: { text: string; }) { new URL(props.href ?? ""); props.target = "_blank"; props.rel = "noopener noreferrer"; - } catch (e) { } + } catch (e) {} return {children}; }, @@ -82,8 +82,9 @@ export function TabContentAnalysis(props: { text: string; }) { const result = replies.map((m) => m.content).join("\n"); setAnalysisResult(result); EnjoyApp.cacheObjects.set(`analyze-${md5(text)}`, result); - } } - tooltip={t("useAIAssistantToAnalyze")} /> + }} + tooltip={t("useAIAssistantToAnalyze")} + /> ) : ( @@ -98,8 +99,9 @@ export function TabContentAnalysis(props: { text: string; }) { const result = replies.map((m) => m.content).join("\n"); setAnalysisResult(result); EnjoyApp.cacheObjects.set(`analyze-${md5(text)}`, result); - } } - tooltip={t("useAIAssistantToAnalyze")} /> + }} + tooltip={t("useAIAssistantToAnalyze")} + /> )} diff --git a/enjoy/src/renderer/components/medias/media-captions/tab-content-note.tsx b/enjoy/src/renderer/components/medias/media-captions/media-tab-content-note.tsx similarity index 98% rename from enjoy/src/renderer/components/medias/media-captions/tab-content-note.tsx rename to enjoy/src/renderer/components/medias/media-captions/media-tab-content-note.tsx index 47438785..b736b9e6 100644 --- a/enjoy/src/renderer/components/medias/media-captions/tab-content-note.tsx +++ b/enjoy/src/renderer/components/medias/media-captions/media-tab-content-note.tsx @@ -7,7 +7,7 @@ import { NoteCard, NoteForm } from "@renderer/components"; /* * Note tab content. */ -export const TabContentNote = (props: { +export const MediaTabContentNote = (props: { currentSegmentIndex: number; selectedIndices: number[]; setSelectedIndices: (indices: number[]) => void; diff --git a/enjoy/src/renderer/components/medias/media-captions/media-tab-content-translation.tsx b/enjoy/src/renderer/components/medias/media-captions/media-tab-content-translation.tsx new file mode 100644 index 00000000..94eba3ec --- /dev/null +++ b/enjoy/src/renderer/components/medias/media-captions/media-tab-content-translation.tsx @@ -0,0 +1,128 @@ +import { useContext } from "react"; +import { + AppSettingsProviderContext, + MediaPlayerProviderContext, + DictProviderContext, +} from "@renderer/context"; +import { TabsContent, Separator } from "@renderer/components/ui"; +import { t } from "i18next"; +import { TimelineEntry } from "echogarden/dist/utilities/Timeline.d.js"; +import { convertWordIpaToNormal } from "@/utils"; +import { + CamdictLookupResult, + DictLookupResult, + AiLookupResult, + TranslateResult, + DictSelect, +} from "@renderer/components"; + +/* + * Translation tab content. + */ +export function MediaTabContentTranslation(props: { + caption: TimelineEntry; + selectedIndices: number[]; +}) { + const { caption } = props; + + return ( + + + +
+ {t("translateSentence")} +
+ +
+ ); +} + +const SelectedWords = (props: { + caption: TimelineEntry; + selectedIndices: number[]; +}) => { + const { selectedIndices, caption } = props; + + const { currentDictValue } = useContext(DictProviderContext); + const { transcription } = useContext(MediaPlayerProviderContext); + const { learningLanguage, ipaMappings } = useContext( + AppSettingsProviderContext + ); + + const word = selectedIndices + .map((index) => caption.timeline[index]?.text || "") + .join(" ") + .trim(); + + if (selectedIndices.length === 0) + return ( +
+ {t("clickAnyWordToSelect")} +
+ ); + + return ( + <> +
+
+ {selectedIndices.map((index, i) => { + const word = caption.timeline[index]; + if (!word) return; + return ( +
+
+ {word.text} +
+ {word.timeline.length > 0 && ( +
+ + {word.timeline + .map((t) => + learningLanguage.startsWith("en") + ? convertWordIpaToNormal( + t.timeline.map((s) => s.text), + { + mappings: ipaMappings, + } + ).join("") + : t.text + ) + .join(" ")} + +
+ )} +
+ ); + })} +
+
+ +
+
+ + + + {currentDictValue === "cambridge" ? ( + + ) : currentDictValue === "ai" ? ( + + ) : ( + + )} + + ); +}; diff --git a/enjoy/src/renderer/components/medias/media-captions/tab-content-translation.tsx b/enjoy/src/renderer/components/medias/media-captions/tab-content-translation.tsx deleted file mode 100644 index 5ea06ba1..00000000 --- a/enjoy/src/renderer/components/medias/media-captions/tab-content-translation.tsx +++ /dev/null @@ -1,127 +0,0 @@ -import { useContext } from "react"; -import { - AppSettingsProviderContext, - MediaPlayerProviderContext, - DictProviderContext, -} from "@renderer/context"; -import { TabsContent, Separator } from "@renderer/components/ui"; -import { t } from "i18next"; -import { TimelineEntry } from "echogarden/dist/utilities/Timeline.d.js"; -import { convertWordIpaToNormal } from "@/utils"; -import { - CamdictLookupResult, - DictLookupResult, - AiLookupResult, - TranslateResult, -} from "@renderer/components"; - -/* - * Translation tab content. - */ -export function TabContentTranslation(props: { - caption: TimelineEntry; - selectedIndices: number[]; -}) { - const { caption } = props; - - return ( - - - -
- {t("translateSentence")} -
- -
- ); -} - -const SelectedWords = (props: { - caption: TimelineEntry; - selectedIndices: number[]; -}) => { - const { selectedIndices, caption } = props; - - const { currentDictValue } = useContext(DictProviderContext); - const { transcription } = useContext(MediaPlayerProviderContext); - const { learningLanguage, ipaMappings } = useContext( - AppSettingsProviderContext - ); - - const word = selectedIndices - .map((index) => caption.timeline[index]?.text || "") - .join(" ") - .trim(); - - if (selectedIndices.length === 0) - return ( -
- {t("clickAnyWordToSelect")} -
- ); - - return ( - <> -
- {selectedIndices.map((index, i) => { - const word = caption.timeline[index]; - if (!word) return; - return ( -
-
- {word.text} -
- {word.timeline.length > 0 && ( -
- - {word.timeline - .map((t) => - learningLanguage.startsWith("en") - ? convertWordIpaToNormal( - t.timeline.map((s) => s.text), - { - mappings: ipaMappings, - } - ).join("") - : t.text - ) - .join(" ")} - -
- )} -
- ); - })} -
- - {currentDictValue === "cambridge" ? ( - <> - - - - ) : ( - <> - - - - )} - - - - - ); -};