@rxdrag/website-lib-core 0.0.63 → 0.0.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,255 @@
1
+ import React, { useCallback, useEffect, useRef, useState } from "react";
2
+ import Hls from "hls.js";
3
+ import type { Media } from "@rxdrag/rxcms-models";
4
+ import { ReactModalTrigger } from "./ReactModalTrigger";
5
+ import clsx from "clsx";
6
+
7
+ export type ID = string | number;
8
+
9
+ export type VideoPlayerClassNames = {
10
+ container?: string;
11
+ video?: string;
12
+ playButton?: string;
13
+ playButtonOuter?: string;
14
+ playButtonInner?: string;
15
+ playIcon?: string;
16
+ overlay?: string;
17
+ overlayTitle?: string;
18
+ ctaButton?: string;
19
+ overlayReplayButton?: string;
20
+ };
21
+
22
+ export function ReactVideoPlayer(props: {
23
+ endTitle?: string;
24
+ media: Media;
25
+ posterUrl?: string;
26
+ classNames?: VideoPlayerClassNames;
27
+ callToAction?: string;
28
+ onToggleSelect?: (id: ID) => void;
29
+ }) {
30
+ const {
31
+ endTitle,
32
+ media,
33
+ onToggleSelect,
34
+ posterUrl,
35
+ classNames,
36
+ callToAction,
37
+ } = props;
38
+ const videoRef = useRef<HTMLVideoElement>(null);
39
+ const hlsRef = useRef<Hls | null>(null);
40
+ const [isPlaying, setIsPlaying] = useState(false);
41
+ const [isEnded, setIsEnded] = useState(false);
42
+
43
+ const handleContainerClick = useCallback(
44
+ (e: React.MouseEvent) => {
45
+ if (isPlaying && videoRef.current) {
46
+ e.stopPropagation();
47
+ videoRef.current.pause();
48
+ return;
49
+ }
50
+ if (media.id != null) {
51
+ onToggleSelect?.(media.id);
52
+ }
53
+ },
54
+ [isPlaying, media.id, onToggleSelect]
55
+ );
56
+
57
+ const handlePlayClick = useCallback(
58
+ (e: React.MouseEvent) => {
59
+ e.stopPropagation();
60
+ const v = videoRef.current;
61
+ if (!v) return;
62
+ if (isPlaying) v.pause();
63
+ else v.play();
64
+ },
65
+ [isPlaying]
66
+ );
67
+
68
+ useEffect(() => {
69
+ const video = videoRef.current;
70
+ const url = media.file?.original;
71
+ if (!video || !url) return;
72
+
73
+ const onPlay = () => {
74
+ setIsPlaying(true);
75
+ setIsEnded(false);
76
+ };
77
+ const onPause = () => setIsPlaying(false);
78
+ const onEnded = () => {
79
+ setIsPlaying(false);
80
+ setIsEnded(true);
81
+ };
82
+
83
+ video.addEventListener("play", onPlay);
84
+ video.addEventListener("pause", onPause);
85
+ video.addEventListener("ended", onEnded);
86
+
87
+ if (media.storageType === "cloudflare_stream") {
88
+ if (!Hls.isSupported()) {
89
+ if (video.canPlayType("application/vnd.apple.mpegurl")) {
90
+ video.src = url;
91
+ }
92
+ return () => {
93
+ video.removeEventListener("play", onPlay);
94
+ video.removeEventListener("pause", onPause);
95
+ video.removeEventListener("ended", onEnded);
96
+ };
97
+ }
98
+
99
+ const hls = new Hls({ enableWorker: true });
100
+ hls.on(Hls.Events.ERROR, (_event, data) => {
101
+ if (data.fatal) {
102
+ switch (data.type) {
103
+ case Hls.ErrorTypes.NETWORK_ERROR:
104
+ hls.startLoad();
105
+ break;
106
+ case Hls.ErrorTypes.MEDIA_ERROR:
107
+ hls.recoverMediaError();
108
+ break;
109
+ default:
110
+ hls.destroy();
111
+ break;
112
+ }
113
+ }
114
+ });
115
+
116
+ hls.loadSource(url);
117
+ hls.attachMedia(video);
118
+ hlsRef.current = hls;
119
+
120
+ return () => {
121
+ video.removeEventListener("play", onPlay);
122
+ video.removeEventListener("pause", onPause);
123
+ video.removeEventListener("ended", onEnded);
124
+ hls.destroy();
125
+ hlsRef.current = null;
126
+ };
127
+ } else {
128
+ video.src = url;
129
+ return () => {
130
+ video.removeEventListener("play", onPlay);
131
+ video.removeEventListener("pause", onPause);
132
+ video.removeEventListener("ended", onEnded);
133
+ };
134
+ }
135
+ }, [media.file?.original, media.storageType]);
136
+
137
+ return (
138
+ <div
139
+ className={[
140
+ "relative w-full aspect-video rounded-2xl",
141
+ classNames?.container,
142
+ ]
143
+ .filter(Boolean)
144
+ .join(" ")}
145
+ onClick={handleContainerClick}
146
+ >
147
+ <video
148
+ ref={videoRef}
149
+ preload="metadata"
150
+ poster={posterUrl ?? media.file?.thumbnail}
151
+ className={["w-full h-full rounded-lg object-cover", classNames?.video]
152
+ .filter(Boolean)
153
+ .join(" ")}
154
+ controls
155
+ >
156
+ {!media.storageType && media.file?.original ? (
157
+ <source src={media.file.original} />
158
+ ) : null}
159
+ Your browser does not support video playback.
160
+ </video>
161
+
162
+ {!isPlaying && !isEnded && (
163
+ <button
164
+ type="button"
165
+ onClick={handlePlayClick}
166
+ className={[
167
+ "absolute inset-0 flex items-center justify-center w-full h-full transition rounded-lg z-10",
168
+ classNames?.playButton,
169
+ ]
170
+ .filter(Boolean)
171
+ .join(" ")}
172
+ >
173
+ <div
174
+ className={[
175
+ "flex items-center justify-center size-[130px] bg-white/15 rounded-full backdrop-blur-sm hover:shadow-md transition-all duration-300 hover:scale-110 group",
176
+ classNames?.playButtonOuter,
177
+ ]
178
+ .filter(Boolean)
179
+ .join(" ")}
180
+ >
181
+ <div
182
+ className={[
183
+ "size-[90px] bg-white relative overflow-hidden rounded-full",
184
+ classNames?.playButtonInner,
185
+ ]
186
+ .filter(Boolean)
187
+ .join(" ")}
188
+ >
189
+ <div className="absolute inset-0 cross-gradient box-shadow-1">
190
+ <div className="w-full h-full flex items-center justify-center">
191
+ <svg
192
+ className={["size-8 text-gray-700", classNames?.playIcon]
193
+ .filter(Boolean)
194
+ .join(" ")}
195
+ viewBox="0 0 30 32"
196
+ fill="currentColor"
197
+ xmlns="http://www.w3.org/2000/svg"
198
+ >
199
+ <path d="M27.2003 19.4972C29.945 17.9735 29.945 14.0264 27.2003 12.5027L6.44148 0.978573C3.77538 -0.501503 0.5 1.42643 0.5 4.47581V27.5241C0.5 30.5735 3.77537 32.5014 6.44147 31.0214L27.2003 19.4972Z" />
200
+ </svg>
201
+ </div>
202
+ </div>
203
+ </div>
204
+ </div>
205
+ </button>
206
+ )}
207
+
208
+ <div
209
+ className={[
210
+ "absolute inset-0 bg-black/50 flex-col gap-6 items-center justify-center text-center text-white hidden z-10 rounded-2xl",
211
+ classNames?.overlay,
212
+ ]
213
+ .filter(Boolean)
214
+ .join(" ")}
215
+ style={{ display: isEnded ? "flex" : "none" }}
216
+ >
217
+ <p
218
+ className={clsx("text-4xl drop-shadow-lg", classNames?.overlayTitle)}
219
+ >
220
+ {endTitle ?? "Contact Us Now"}
221
+ </p>
222
+ <div className="flex items-center gap-4">
223
+ <ReactModalTrigger
224
+ className={clsx(
225
+ "btn btn-primary px-10 py-2.5 rounded-full ripple",
226
+ classNames?.ctaButton
227
+ )}
228
+ callToAction={callToAction}
229
+ >
230
+ Consult Now
231
+ </ReactModalTrigger>
232
+ <button
233
+ type="button"
234
+ className={[
235
+ "px-10 py-2.5 bg-white/20 hover:bg-white/30 rounded-full transition text-sm",
236
+ classNames?.overlayReplayButton,
237
+ ]
238
+ .filter(Boolean)
239
+ .join(" ")}
240
+ onClick={(e) => {
241
+ e.stopPropagation();
242
+ const v = videoRef.current;
243
+ if (!v) return;
244
+ v.currentTime = 0;
245
+ setIsEnded(false);
246
+ v.play();
247
+ }}
248
+ >
249
+ Replay
250
+ </button>
251
+ </div>
252
+ </div>
253
+ </div>
254
+ );
255
+ }
@@ -11,5 +11,7 @@ export * from "./SearchInput";
11
11
  export * from "./ToTop";
12
12
  export * from "./BackgroundVideoPlayer";
13
13
  export * from "./BackgroundHlsVideoPlayer";
14
- export * from "./VideoPlayer";
14
+ export * from "./ReactModalTrigger";
15
+ export * from "./ReactVideoPlayer";
16
+
15
17
 
@@ -1,31 +0,0 @@
1
- import { useEffect, useState } from "react";
2
-
3
- export interface VisitorInfo {
4
- visitorId: string | null;
5
- sessionId: string | null;
6
- }
7
-
8
- export function useVisitorInfo() {
9
- const [visitorInfo, setVisitorInfo] = useState<VisitorInfo>({
10
- visitorId: null,
11
- sessionId: null,
12
- });
13
-
14
- useEffect(() => {
15
- const getVisitorId = () => {
16
- const match = document.cookie.match(/visitor_id=([^;]+)/);
17
- return match ? match[1] : null;
18
- };
19
-
20
- const getSessionId = () => {
21
- return sessionStorage.getItem("session_id");
22
- };
23
-
24
- setVisitorInfo({
25
- visitorId: getVisitorId(),
26
- sessionId: getSessionId(),
27
- });
28
- }, []);
29
-
30
- return visitorInfo;
31
- }
@@ -1,146 +0,0 @@
1
- import { ID } from "@rxdrag/entify-lib";
2
- import { Media } from "@rxdrag/rxcms-models";
3
- import React, { useEffect, useRef, useState } from "react";
4
- import Hls from "hls.js";
5
- import { Icon } from "@iconify/react";
6
-
7
- export function VideoPlayer(props: {
8
- media: Media;
9
- onToggleSelect?: (id: ID) => void;
10
- }) {
11
- const { media, onToggleSelect } = props;
12
- const videoRef = useRef<HTMLVideoElement>(null);
13
- const hlsRef = useRef<Hls | null>(null);
14
- const [isPlaying, setIsPlaying] = useState(false);
15
-
16
- const handleContainerClick = React.useCallback(
17
- (e: React.MouseEvent) => {
18
- // 如果正在播放,点击暂停并阻止事件冒泡(避免同时触发选择)
19
- if (isPlaying && videoRef.current) {
20
- e.stopPropagation();
21
- videoRef.current.pause();
22
- return;
23
- }
24
-
25
- // 未播放时,触发选择事件
26
- onToggleSelect?.(media.id!);
27
- },
28
- [media.id, onToggleSelect, isPlaying]
29
- );
30
-
31
- const handlePlayClick = React.useCallback(
32
- (e: React.MouseEvent) => {
33
- e.stopPropagation();
34
- if (videoRef.current) {
35
- if (isPlaying) {
36
- videoRef.current.pause();
37
- } else {
38
- videoRef.current.play();
39
- }
40
- }
41
- },
42
- [isPlaying]
43
- );
44
-
45
- useEffect(() => {
46
- if (!videoRef.current || !media.file?.original) return;
47
-
48
- const video = videoRef.current;
49
- const videoUrl = media.file.original;
50
-
51
- // 监听播放状态
52
- const handlePlay = () => {
53
- setIsPlaying(true);
54
- };
55
-
56
- const handlePause = () => {
57
- setIsPlaying(false);
58
- };
59
-
60
- video.addEventListener("play", handlePlay);
61
- video.addEventListener("pause", handlePause);
62
-
63
- // Cloudflare Stream 使用 HLS
64
- if (media.storageType === "cloudflare_stream") {
65
- if (!Hls.isSupported()) {
66
- // Safari 原生支持 HLS
67
- if (video.canPlayType("application/vnd.apple.mpegurl")) {
68
- video.src = videoUrl;
69
- }
70
- return () => {
71
- video.removeEventListener("play", handlePlay);
72
- video.removeEventListener("pause", handlePause);
73
- };
74
- }
75
-
76
- const hls = new Hls({
77
- enableWorker: true,
78
- });
79
-
80
- hls.on(Hls.Events.ERROR, (event, data) => {
81
- if (data.fatal) {
82
- switch (data.type) {
83
- case Hls.ErrorTypes.NETWORK_ERROR:
84
- hls.startLoad();
85
- break;
86
- case Hls.ErrorTypes.MEDIA_ERROR:
87
- hls.recoverMediaError();
88
- break;
89
- default:
90
- hls.destroy();
91
- break;
92
- }
93
- }
94
- });
95
-
96
- hls.loadSource(videoUrl);
97
- hls.attachMedia(video);
98
- hlsRef.current = hls;
99
-
100
- return () => {
101
- video.removeEventListener("play", handlePlay);
102
- video.removeEventListener("pause", handlePause);
103
- hls.destroy();
104
- hlsRef.current = null;
105
- };
106
- } else {
107
- // 普通视频使用原生 video
108
- video.src = videoUrl;
109
-
110
- return () => {
111
- video.removeEventListener("play", handlePlay);
112
- video.removeEventListener("pause", handlePause);
113
- };
114
- }
115
- }, [media.file?.original, media.storageType]);
116
-
117
- return (
118
- <div
119
- className="z-0 w-full bg-default-100 cursor-pointer aspect-square relative group"
120
- onClick={handleContainerClick}
121
- >
122
- <video
123
- ref={videoRef}
124
- preload="metadata"
125
- poster={media.file?.thumbnail}
126
- className="absolute inset-0 w-full h-full object-cover"
127
- />
128
-
129
- {/* 播放按钮 - 播放时隐藏 */}
130
- {!isPlaying && (
131
- <button
132
- onClick={handlePlayClick}
133
- className="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 z-10 cursor-pointer"
134
- >
135
- <div className="w-12 h-12 rounded-full bg-white/90 hover:bg-white flex items-center justify-center shadow-lg transition-all hover:scale-110">
136
- <Icon
137
- icon="mdi:play"
138
- className="text-2xl text-gray-800"
139
- style={{ marginLeft: "3px" }}
140
- />
141
- </div>
142
- </button>
143
- )}
144
- </div>
145
- );
146
- }