Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Polish Space feed captions and photo indicators

Anand Baburajan26232bad5a7f
diff --git a/web/apps/space/src/components/CaptionText.tsx b/web/apps/space/src/components/CaptionText.tsx
index c8e529480a..61a06c545c 100644
--- a/web/apps/space/src/components/CaptionText.tsx
+++ b/web/apps/space/src/components/CaptionText.tsx
@@ -13,17 +13,78 @@ export const SpaceCaptionText: React.FC<{
caption: string;
lineClamp?: number;
}> = ({ caption, lineClamp }) => {
+ const measureRef = React.useRef<HTMLSpanElement>(null);
+ const [clampedCaption, setClampedCaption] = React.useState(caption);
+
+ React.useLayoutEffect(() => {
+ const measure = measureRef.current;
+ if (!measure || !lineClamp) return;
+
+ const text = measure.firstElementChild!;
+ const updateCaption = () => {
+ const maxHeight =
+ parseFloat(getComputedStyle(measure).lineHeight) * lineClamp;
+ text.textContent = caption;
+ if (measure.getBoundingClientRect().height <= maxHeight) {
+ setClampedCaption(caption);
+ return;
+ }
+
+ const characters = Array.from(
+ new Intl.Segmenter(undefined, {
+ granularity: "grapheme",
+ }).segment(caption),
+ ({ segment }) => segment,
+ );
+ const truncated = (length: number) =>
+ `${characters.slice(0, length).join("").trimEnd()}…`;
+ let start = 0;
+ let end = characters.length;
+ while (start < end) {
+ const middle = Math.ceil((start + end) / 2);
+ text.textContent = truncated(middle);
+ if (measure.getBoundingClientRect().height <= maxHeight) {
+ start = middle;
+ } else {
+ end = middle - 1;
+ }
+ }
+ text.textContent = caption;
+ setClampedCaption(truncated(start));
+ };
+
+ updateCaption();
+ const observer = new ResizeObserver(updateCaption);
+ observer.observe(measure);
+ document.fonts.addEventListener("loadingdone", updateCaption);
+ return () => {
+ observer.disconnect();
+ document.fonts.removeEventListener("loadingdone", updateCaption);
+ };
+ }, [caption, lineClamp]);
+
+ const displayCaption = lineClamp ? clampedCaption : caption;
const layerSx = {
- display: "-webkit-box",
+ display: "block",
gridArea: "1 / 1",
minWidth: 0,
- overflow: lineClamp ? "hidden" : undefined,
- WebkitBoxOrient: "vertical",
- WebkitLineClamp: lineClamp,
+ overflowWrap: lineClamp ? "anywhere" : undefined,
} as const;
return (
<Box component="span" sx={{ display: "grid" }}>
+ {lineClamp ? (
+ <Box
+ component="span"
+ sx={{ ...layerSx, height: 0, overflow: "hidden" }}
+ >
+ <Box component="span" ref={measureRef} sx={layerSx}>
+ <Box component="span" sx={captionBubbleSx}>
+ {caption}
+ </Box>
+ </Box>
+ </Box>
+ ) : null}
<Box
component="span"
aria-hidden
@@ -34,13 +95,17 @@ export const SpaceCaptionText: React.FC<{
sx={{ ...captionBubbleSx, bgcolor: "#202020" }}
>
<Box component="span" sx={{ opacity: 0 }}>
- {caption}
+ {displayCaption}
</Box>
</Box>
</Box>
- <Box component="span" sx={{ ...layerSx, zIndex: 1 }}>
+ <Box
+ component="span"
+ aria-hidden={lineClamp ? true : undefined}
+ sx={{ ...layerSx, zIndex: 1 }}
+ >
<Box component="span" sx={captionBubbleSx}>
- {caption}
+ {displayCaption}
</Box>
</Box>
</Box>
diff --git a/web/apps/space/src/components/FileViewer.tsx b/web/apps/space/src/components/FileViewer.tsx
index 40b5c49cdd..fcfa8358a6 100644
--- a/web/apps/space/src/components/FileViewer.tsx
+++ b/web/apps/space/src/components/FileViewer.tsx
@@ -21,6 +21,7 @@ import { SpaceCaptionText } from "components/CaptionText";
import { ConfirmationActionSheet } from "components/ConfirmationActionSheet";
import { spacePostLikePopDurationMs } from "components/post-like-animation";
import { SpacePostPhotosCounter } from "components/PostPhotosCounter";
+import { SpacePostPhotosDots } from "components/PostPhotosDots";
import { SpacePostReplyControls } from "components/PostReplyControls";
import log from "ente-base/log";
import type PhotoSwipe from "photoswipe";
@@ -253,7 +254,11 @@ const viewerCaptionTextSx = {
whiteSpace: "pre-wrap",
} as const;
-const SpaceViewerCaption: React.FC<{ caption: string }> = ({ caption }) => {
+const SpaceViewerPhotoOverlay: React.FC<{
+ caption: string;
+ photoIndex: number;
+ photoCount: number;
+}> = ({ caption, photoIndex, photoCount }) => {
const bubbleRef = React.useRef<HTMLParagraphElement | null>(null);
const [isLongCaption, setIsLongCaption] = React.useState(false);
@@ -276,19 +281,16 @@ const SpaceViewerCaption: React.FC<{ caption: string }> = ({ caption }) => {
return (
<>
<Box
- ref={bubbleRef}
- component="p"
aria-hidden={isLongCaption || undefined}
data-space-viewer-chrome="true"
- title={caption}
sx={{
- ...viewerCaptionTextSx,
+ alignItems: "center",
bottom: "14%",
+ display: "flex",
+ flexDirection: "column",
+ gap: "16px",
left: "50%",
- m: 0,
- maxWidth: "78vw",
- minWidth: 0,
- overflowWrap: "break-word",
+ pointerEvents: "none",
position: "fixed",
transform: "translateX(-50%)",
visibility: isLongCaption ? "hidden" : "visible",
@@ -296,35 +298,62 @@ const SpaceViewerCaption: React.FC<{ caption: string }> = ({ caption }) => {
zIndex: 2,
}}
>
- <SpaceCaptionText caption={caption} />
+ <SpacePostPhotosDots index={photoIndex} count={photoCount} />
+ <Box
+ ref={bubbleRef}
+ component="p"
+ title={caption}
+ sx={{
+ ...viewerCaptionTextSx,
+ display: caption ? "block" : "none",
+ m: 0,
+ minWidth: 0,
+ overflowWrap: "break-word",
+ width: "100%",
+ }}
+ >
+ <SpaceCaptionText caption={caption} />
+ </Box>
</Box>
{isLongCaption && (
<Box
- role="region"
- aria-label="Caption"
- tabIndex={0}
sx={{
- ...viewerCaptionTextSx,
- bgcolor: "rgba(32, 32, 32, 0.85)",
- borderRadius: "16px",
- boxSizing: "border-box",
- fontWeight: 400,
- lineHeight: "22px",
- maxHeight: "33svh",
- overflowWrap: "anywhere",
- overflowY: "auto",
- overscrollBehaviorY: "contain",
- p: "14px 16px",
- scrollbarWidth: "thin",
- textAlign: "left",
- textWrap: "wrap",
- "&:focus-visible": {
- outline: `2px solid ${green}`,
- outlineOffset: 2,
- },
+ display: "flex",
+ flexDirection: "column",
+ gap: "16px",
}}
>
- {caption}
+ <SpacePostPhotosDots
+ index={photoIndex}
+ count={photoCount}
+ />
+ <Box
+ role="region"
+ aria-label="Caption"
+ tabIndex={0}
+ sx={{
+ ...viewerCaptionTextSx,
+ bgcolor: "rgba(32, 32, 32, 0.85)",
+ borderRadius: "16px",
+ boxSizing: "border-box",
+ fontWeight: 400,
+ lineHeight: "22px",
+ maxHeight: "33svh",
+ overflowWrap: "anywhere",
+ overflowY: "auto",
+ overscrollBehaviorY: "contain",
+ p: "14px 16px",
+ scrollbarWidth: "thin",
+ textAlign: "left",
+ textWrap: "wrap",
+ "&:focus-visible": {
+ outline: `2px solid ${green}`,
+ outlineOffset: 2,
+ },
+ }}
+ >
+ {caption}
+ </Box>
</Box>
)}
</>
@@ -2130,7 +2159,10 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
</Box>
</Box>
)}
- {(hasDisplayCaption || showPhotoLikeButton) &&
+ {!isDraftPost &&
+ (hasDisplayCaption ||
+ showPhotoLikeButton ||
+ postPhotoCount > 1) &&
!isCaptionEditing && (
<Box
data-space-viewer-bottom="true"
@@ -2148,10 +2180,12 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
zIndex: 2,
}}
>
- {hasDisplayCaption && (
- <SpaceViewerCaption
+ {(hasDisplayCaption || postPhotoCount > 1) && (
+ <SpaceViewerPhotoOverlay
key={activePostKey}
caption={displayCaption}
+ photoIndex={postPhotoIndex}
+ photoCount={postPhotoCount}
/>
)}
{showPhotoLikeButton && (
diff --git a/web/apps/space/src/components/PostPhotosDots.tsx b/web/apps/space/src/components/PostPhotosDots.tsx
new file mode 100644
index 0000000000..34a8fa8815
--- /dev/null
+++ b/web/apps/space/src/components/PostPhotosDots.tsx
@@ -0,0 +1,40 @@
+import { Box } from "@mui/material";
+
+export const SpacePostPhotosDots = ({
+ index,
+ count,
+}: {
+ index: number;
+ count: number;
+}) =>
+ count > 1 ? (
+ <Box
+ component="span"
+ aria-hidden
+ sx={{
+ alignItems: "center",
+ alignSelf: "center",
+ display: "inline-flex",
+ flexShrink: 0,
+ gap: "6px",
+ pointerEvents: "none",
+ }}
+ >
+ {Array.from({ length: count }, (_, photoIndex) => (
+ <Box
+ key={photoIndex}
+ component="span"
+ sx={{
+ bgcolor:
+ photoIndex == index
+ ? "#FFFFFF"
+ : "rgba(255, 255, 255, 0.4)",
+ borderRadius: "50%",
+ boxShadow: "0 1px 2px rgba(0, 0, 0, 0.4)",
+ height: 5,
+ width: 5,
+ }}
+ />
+ ))}
+ </Box>
+ ) : null;
diff --git a/web/apps/space/src/screens/HomeScreen.tsx b/web/apps/space/src/screens/HomeScreen.tsx
index c43c0a5437..6ff7d608f1 100644
--- a/web/apps/space/src/screens/HomeScreen.tsx
+++ b/web/apps/space/src/screens/HomeScreen.tsx
@@ -23,6 +23,7 @@ import {
} from "components/post-like-animation";
import { SpacePostPhotoInput } from "components/PostPhotoInput";
import { SpacePostPhotosCounter } from "components/PostPhotosCounter";
+import { SpacePostPhotosDots } from "components/PostPhotosDots";
import { SpacePWAInstallPrompt } from "components/PWAInstallPrompt";
import { SpaceLoadingSpinner } from "components/RouteFallback";
import { SpaceShareInviteButton } from "components/ShareInviteButton";
@@ -616,13 +617,19 @@ const FeedLikeButton: React.FC<FeedLikeButtonProps> = ({
);
};
-const FeedPhotoCaption: React.FC<{ caption: string }> = ({ caption }) => {
+const FeedPhotoOverlay: React.FC<{
+ caption?: string;
+ photoIndex: number;
+ photoCount: number;
+}> = ({ caption, photoIndex, photoCount }) => {
return (
<Box
- title={caption}
sx={{
...feedPhotoCaptionTextSx,
bottom: 20,
+ display: "grid",
+ gap: "16px",
+ justifyItems: "center",
left: "50%",
maxWidth: "78%",
pointerEvents: "none",
@@ -632,7 +639,12 @@ const FeedPhotoCaption: React.FC<{ caption: string }> = ({ caption }) => {
zIndex: 2,
}}
>
- <SpaceCaptionText caption={caption} lineClamp={2} />
+ <SpacePostPhotosDots index={photoIndex} count={photoCount} />
+ {caption && (
+ <Box title={caption} sx={{ minWidth: 0, width: "100%" }}>
+ <SpaceCaptionText caption={caption} lineClamp={2} />
+ </Box>
+ )}
</Box>
);
};
@@ -1136,7 +1148,7 @@ const FeedItem: React.FC<FeedItemProps> = ({
sx={{
aspectRatio: frameAspectRatio,
bgcolor: "transparent",
- borderRadius: "16px",
+ borderRadius: isOwnPost ? "16px" : "16px 16px 0 0",
maxWidth: "100%",
minWidth: 0,
overflow: "hidden",
@@ -1529,8 +1541,12 @@ const FeedItem: React.FC<FeedItemProps> = ({
)}
</Box>
</Box>
- {!isPostUnavailable && displayCaption && (
- <FeedPhotoCaption caption={displayCaption} />
+ {(photoCount > 1 || (!isPostUnavailable && displayCaption)) && (
+ <FeedPhotoOverlay
+ caption={isPostUnavailable ? undefined : displayCaption}
+ photoIndex={photoIndex}
+ photoCount={photoCount}
+ />
)}
</Box>
{showFooter && (