Skip to content

Commit e99d5e9

Browse files
authored
refactor(web): hide download button from builtin infobox video block and story video block (#1947)
1 parent 77d7c22 commit e99d5e9

File tree

3 files changed

+77
-24
lines changed

3 files changed

+77
-24
lines changed

web/src/app/features/Visualizer/Crust/Infobox/Block/builtin/Video/index.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import useVideoAspectRatio from "@reearth/app/features/Visualizer/hooks/useVideoAspectRatio";
12
import BlockWrapper from "@reearth/app/features/Visualizer/shared/components/BlockWrapper";
23
import { CommonBlockProps } from "@reearth/app/features/Visualizer/shared/types";
34
import { ValueTypes } from "@reearth/app/utils/value";
45
import { styled } from "@reearth/services/theme";
5-
import { FC, useCallback, useMemo, useState } from "react";
6-
import type ReactPlayer from "react-player";
6+
import { FC, useMemo } from "react";
77
import Player from "react-player";
88

99
import { InfoboxBlock } from "../../../types";
@@ -16,8 +16,6 @@ const VideoBlock: FC<CommonBlockProps<InfoboxBlock>> = ({
1616
selectedFeature,
1717
...props
1818
}) => {
19-
const [aspectRatio, setAspectRatio] = useState(56.25);
20-
2119
const src = useMemo(
2220
() => block?.property?.default?.src?.value as ValueTypes["string"],
2321
[block?.property?.default?.src]
@@ -31,12 +29,7 @@ const VideoBlock: FC<CommonBlockProps<InfoboxBlock>> = ({
3129
}
3230
);
3331

34-
const handleVideoReady = useCallback((player: ReactPlayer) => {
35-
const height = player.getInternalPlayer().videoHeight;
36-
const width = player.getInternalPlayer().videoWidth;
37-
if (!height || !width) return;
38-
setAspectRatio((height / width) * 100);
39-
}, []);
32+
const { playerRef, aspectRatio } = useVideoAspectRatio({ src: evaluatedSrc });
4033

4134
return (
4235
<BlockWrapper
@@ -51,14 +44,23 @@ const VideoBlock: FC<CommonBlockProps<InfoboxBlock>> = ({
5144
{evaluatedSrc !== undefined ? (
5245
<Wrapper aspectRatio={aspectRatio}>
5346
<StyledPlayer
47+
ref={playerRef}
5448
url={evaluatedSrc}
5549
width="100%"
5650
height="100%"
57-
onReady={handleVideoReady}
5851
playsinline
5952
pip
6053
controls
6154
light
55+
config={{
56+
file: {
57+
attributes: {
58+
controlsList: "nodownload",
59+
preload: "metadata",
60+
onContextMenu: (e: React.SyntheticEvent) => e.preventDefault()
61+
}
62+
}
63+
}}
6264
/>
6365
</Wrapper>
6466
) : null}
@@ -68,11 +70,11 @@ const VideoBlock: FC<CommonBlockProps<InfoboxBlock>> = ({
6870

6971
export default VideoBlock;
7072

71-
const Wrapper = styled("div")<{ aspectRatio: number }>(({ aspectRatio }) => ({
72-
position: "relative",
73-
paddingTop: `${aspectRatio}%`,
74-
width: "100%"
75-
}));
73+
const Wrapper = styled("div")<{ aspectRatio: number }>`
74+
position: relative;
75+
padding-top: ${({ aspectRatio }) => `${aspectRatio}%`};
76+
width: 100%;
77+
`;
7678

7779
const StyledPlayer = styled(Player)({
7880
position: "absolute",
Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import useVideoAspectRatio from "@reearth/app/features/Visualizer/hooks/useVideoAspectRatio";
12
import { styled } from "@reearth/services/theme";
23
import { FC } from "react";
34
import Player from "react-player";
@@ -8,31 +9,52 @@ type Props = {
89
inEditor?: boolean;
910
};
1011
const VideoPlayer: FC<Props> = ({ isSelected, src, inEditor }) => {
12+
const { playerRef, aspectRatio } = useVideoAspectRatio({ src });
13+
1114
return (
12-
<StyledWrapper>
15+
<Wrapper aspectRatio={aspectRatio} test-id="video-player">
1316
{inEditor && <Overlay />}
14-
<Player
17+
<StyledPlayer
18+
ref={playerRef}
1519
url={src}
1620
width="100%"
21+
height="100%"
1722
playsinline
1823
pip
1924
controls
2025
light
2126
isselected={isSelected}
27+
config={{
28+
file: {
29+
attributes: {
30+
controlsList: "nodownload",
31+
preload: "metadata",
32+
onContextMenu: (e: React.SyntheticEvent) => e.preventDefault()
33+
}
34+
}
35+
}}
2236
/>
23-
</StyledWrapper>
37+
</Wrapper>
2438
);
2539
};
2640

2741
export default VideoPlayer;
2842

29-
const StyledWrapper = styled("div")(() => ({
30-
width: "100%",
31-
position: "relative"
32-
}));
43+
const Wrapper = styled("div")<{ aspectRatio: number }>`
44+
position: relative;
45+
padding-top: ${({ aspectRatio }) => `${aspectRatio}%`};
46+
width: 100%;
47+
`;
3348

3449
const Overlay = styled("div")(() => ({
3550
width: "100%",
3651
height: "100%",
37-
position: "absolute"
52+
position: "absolute",
53+
top: 0
3854
}));
55+
56+
const StyledPlayer = styled(Player)({
57+
position: "absolute",
58+
top: 0,
59+
left: 0
60+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useEffect, useRef, useState } from "react";
2+
import type ReactPlayer from "react-player";
3+
4+
export default ({ src }: { src: string | undefined }) => {
5+
const playerRef = useRef<ReactPlayer>(null);
6+
const [aspectRatio, setAspectRatio] = useState(56.25);
7+
8+
useEffect(() => {
9+
const player =
10+
playerRef.current?.getInternalPlayer() as HTMLVideoElement | null;
11+
if (!player) return;
12+
13+
const handleMeta = () => {
14+
const w = player.videoWidth;
15+
const h = player.videoHeight;
16+
if (w && h) setAspectRatio((h / w) * 100);
17+
};
18+
19+
handleMeta();
20+
player.addEventListener("loadedmetadata", handleMeta);
21+
22+
return () => player.removeEventListener("loadedmetadata", handleMeta);
23+
}, [src]);
24+
25+
return {
26+
playerRef,
27+
aspectRatio
28+
};
29+
};

0 commit comments

Comments
 (0)