From e9a204228e0b51daef7bb397a6ccfe28d0aaa995 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Wed, 4 Dec 2024 21:58:28 +0800 Subject: [PATCH] refactor: display translation inline close #1779 --- .../templates/list-item-template.tsx | 39 +++++--- .../src/modules/entry-column/translation.tsx | 99 +++++-------------- .../entry-content/components/EntryTitle.tsx | 1 - 3 files changed, 51 insertions(+), 88 deletions(-) diff --git a/apps/renderer/src/modules/entry-column/templates/list-item-template.tsx b/apps/renderer/src/modules/entry-column/templates/list-item-template.tsx index ffa69ce7b..8d6f8be7f 100644 --- a/apps/renderer/src/modules/entry-column/templates/list-item-template.tsx +++ b/apps/renderer/src/modules/entry-column/templates/list-item-template.tsx @@ -1,6 +1,7 @@ import { useMobile } from "@follow/components/hooks/useMobile.js" import { EllipsisHorizontalTextWithTooltip } from "@follow/components/ui/typography/index.js" import { clsx, cn, isSafari } from "@follow/utils/utils" +import { useMemo } from "react" import { AudioPlayer, useAudioPlayerAtomSelector } from "~/atoms/player" import { useRealInWideMode, useUISettingKey } from "~/atoms/settings/ui" @@ -55,11 +56,32 @@ export function ListItem({ const thumbnailRatio = useUISettingKey("thumbnailRatio") const rid = `list-item-${entryId}` + const envIsSafari = isSafari() + const lineClamp = useMemo(() => { + let lineClampTitle = settingWideMode ? 1 : 2 + let lineClampDescription = settingWideMode ? 1 : 2 + if (translation?.title) { + lineClampTitle += settingWideMode ? 1 : 2 + } + if (translation?.description) { + lineClampDescription += settingWideMode ? 1 : 2 + } + + // for tailwind + // line-clamp-[1] line-clamp-[2] line-clamp-[3] line-clamp-[4] line-clamp-[5] line-clamp-[6] line-clamp-[7] line-clamp-[8] + + // FIXME: Safari bug, not support line-clamp cross elements + return { + global: !envIsSafari ? `line-clamp-[${lineClampTitle + lineClampDescription}]` : "", + title: envIsSafari ? `line-clamp-[${lineClampTitle}]` : "", + description: envIsSafari ? `line-clamp-[${lineClampDescription}]` : "", + } + }, [envIsSafari, translation?.title, translation?.description, settingWideMode]) + // NOTE: prevent 0 height element, react virtuoso will not stop render any more if (!entry || !(feed || inbox)) return const displayTime = inInCollection ? entry.collections?.createdAt : entry.entries.publishedAt - const envIsSafari = isSafari() const related = feed || inbox @@ -78,9 +100,7 @@ export function ListItem({
@@ -110,16 +130,13 @@ export function ListItem({ > {entry.entries.title ? ( ) : ( @@ -136,9 +153,7 @@ export function ListItem({ )} > diff --git a/apps/renderer/src/modules/entry-column/translation.tsx b/apps/renderer/src/modules/entry-column/translation.tsx index 3994a61ec..f13b22bd1 100644 --- a/apps/renderer/src/modules/entry-column/translation.tsx +++ b/apps/renderer/src/modules/entry-column/translation.tsx @@ -1,8 +1,5 @@ -import { ScrollArea } from "@follow/components/ui/scroll-area/index.js" -import { tooltipStyle } from "@follow/components/ui/tooltip/styles.js" -import { useMeasure } from "@follow/hooks" import { cn } from "@follow/utils/utils" -import * as HoverCard from "@radix-ui/react-hover-card" +import { useMemo } from "react" import { HTML } from "~/components/ui/markdown/HTML" @@ -10,83 +7,35 @@ export const EntryTranslation: Component<{ source?: string | null target?: string showTranslation?: boolean - - side?: "top" | "bottom" - - useOverlay?: boolean isHTML?: boolean -}> = ({ source, target, showTranslation = true, className, side, useOverlay, isHTML }) => { - let nextTarget = target - if (source === target) { - nextTarget = undefined - } +}> = ({ source, target, showTranslation = true, className, isHTML }) => { + const nextTarget = useMemo(() => { + if (!target || !showTranslation || source === target) { + return "" + } + return target + }, [source, target, showTranslation]) - const [ref, bounds] = useMeasure({ debounce: 60 }) + const content = useMemo(() => (source + nextTarget ? ` ${nextTarget}` : ""), [source, nextTarget]) if (!source) { return null } - if (nextTarget && showTranslation) { - return ( - - - - {isHTML ? ( - - {nextTarget} - - ) : ( - {nextTarget} - )} - - - - {useOverlay ? ( -
- - {isHTML ? ( - - {source} - - ) : ( - {source} - )} - -
- ) : isHTML ? ( - - {source} - - ) : ( - {source} - )} -
-
-
- ) - } - return isHTML ? ( - - {source} - - ) : ( -
{source}
+ return ( +
+ {nextTarget && } + {isHTML ? ( + + {content} + + ) : ( +
{content}
+ )} +
) } diff --git a/apps/renderer/src/modules/entry-content/components/EntryTitle.tsx b/apps/renderer/src/modules/entry-content/components/EntryTitle.tsx index a2698b372..7b135bb9c 100644 --- a/apps/renderer/src/modules/entry-content/components/EntryTitle.tsx +++ b/apps/renderer/src/modules/entry-content/components/EntryTitle.tsx @@ -98,7 +98,6 @@ export const EntryTitle = ({ entryId, compact }: EntryLinkProps) => { source={entry.entries.title} target={translation.data?.title} className="select-text" - useOverlay />