refactor: display translation inline

close #1779
This commit is contained in:
Stephen Zhou
2024-12-04 21:58:28 +08:00
parent e7b8f660bb
commit e9a204228e
3 changed files with 51 additions and 88 deletions
@@ -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 <ReactVirtuosoItemPlaceholder />
const displayTime = inInCollection ? entry.collections?.createdAt : entry.entries.publishedAt
const envIsSafari = isSafari()
const related = feed || inbox
@@ -78,9 +100,7 @@ export function ListItem({
<div
className={cn(
"-mt-0.5 flex-1 text-sm leading-tight",
// FIXME: Safari bug, not support line-clamp cross elements
!envIsSafari && (settingWideMode ? "line-clamp-2" : "line-clamp-4"),
lineClamp.global,
withAudio && !!hasAudio && "max-w-[calc(100%-88px)]",
)}
>
@@ -110,16 +130,13 @@ export function ListItem({
>
{entry.entries.title ? (
<EntryTranslation
useOverlay
side="top"
className={envIsSafari ? "line-clamp-2 break-all" : undefined}
className={cn("break-all", lineClamp.title)}
source={entry.entries.title}
target={translation?.title}
/>
) : (
<EntryTranslation
useOverlay
side="top"
className={cn("break-all", lineClamp.description)}
source={entry.entries.description}
target={translation?.description}
/>
@@ -136,9 +153,7 @@ export function ListItem({
)}
>
<EntryTranslation
useOverlay
side="top"
className={envIsSafari ? "line-clamp-2 break-all" : undefined}
className={cn("break-all", lineClamp.description)}
source={entry.entries.description}
target={translation?.description}
/>
@@ -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 (
<HoverCard.Root>
<HoverCard.Trigger className={className} ref={ref}>
<i className="i-mgc-translate-2-cute-re mr-1 align-middle" />
{isHTML ? (
<HTML as="div" className="align-middle" noMedia>
{nextTarget}
</HTML>
) : (
<span className="align-middle">{nextTarget}</span>
)}
</HoverCard.Trigger>
<HoverCard.Portal>
<HoverCard.Content
className={cn(
"group relative z-[11] text-sm",
"animate-in fade-in-0 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
)}
style={useOverlay ? { width: bounds.width } : undefined}
side={side}
sideOffset={useOverlay ? 0 : 12}
>
{useOverlay ? (
<div className="absolute inset-x-0">
<ScrollArea.ScrollArea
mask
rootClassName={cn(
"shadow-modal rounded-xl border bg-background p-2",
"group-data-[side=top]:top-0",
"group-data-[side=bottom]:top-full",
)}
viewportClassName="max-h-[12ch]"
>
{isHTML ? (
<HTML as="div" className="align-middle" noMedia>
{source}
</HTML>
) : (
<span className="align-middle">{source}</span>
)}
</ScrollArea.ScrollArea>
</div>
) : isHTML ? (
<HTML as="div" className={cn(tooltipStyle.content)} noMedia>
{source}
</HTML>
) : (
<span className={cn(tooltipStyle.content)}>{source}</span>
)}
</HoverCard.Content>
</HoverCard.Portal>
</HoverCard.Root>
)
}
return isHTML ? (
<HTML as="div" className={cn("prose dark:prose-invert", className)} noMedia>
{source}
</HTML>
) : (
<div className={className}>{source}</div>
return (
<div>
{nextTarget && <i className="i-mgc-translate-2-cute-re mr-1 align-middle" />}
{isHTML ? (
<HTML
as="div"
className={cn("prose inline align-middle dark:prose-invert", className)}
noMedia
>
{content}
</HTML>
) : (
<div className={cn("inline align-middle", className)}>{content}</div>
)}
</div>
)
}
@@ -98,7 +98,6 @@ export const EntryTitle = ({ entryId, compact }: EntryLinkProps) => {
source={entry.entries.title}
target={translation.data?.title}
className="select-text"
useOverlay
/>
</div>
<div className="mt-2 text-[13px] font-medium text-zinc-500">