@livo-tv/blocks 1.15.0 → 1.17.0

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.
package/dist/index.js CHANGED
@@ -3616,7 +3616,13 @@ function useHlsPlayback({
3616
3616
  el.muted = true;
3617
3617
  void el.play().then(
3618
3618
  () => setAutoplayState(muted ? "playing" : "muted"),
3619
- () => setAutoplayState("blocked")
3619
+ () => {
3620
+ if (el.paused) {
3621
+ setAutoplayState("blocked");
3622
+ return;
3623
+ }
3624
+ setAutoplayState(el.muted && !muted ? "muted" : "playing");
3625
+ }
3620
3626
  );
3621
3627
  }
3622
3628
  );
@@ -3682,6 +3688,9 @@ function useHlsPlayback({
3682
3688
  mediaErrorRetries = 0;
3683
3689
  clearRetryTimer();
3684
3690
  setState("ready");
3691
+ setAutoplayState(
3692
+ (prev) => prev === "blocked" || prev === "none" ? "playing" : prev
3693
+ );
3685
3694
  onPlayingChangeRef.current?.(true);
3686
3695
  }
3687
3696
  function onPaused() {
@@ -3928,6 +3937,15 @@ function captionAtTime(cues, time, options) {
3928
3937
  if (matched) return matched;
3929
3938
  return null;
3930
3939
  }
3940
+ function vttCuesDueAt(cues, currentTime, cap = LIVE_STACK_MAX_CUES) {
3941
+ if (!Number.isFinite(currentTime)) return [];
3942
+ return cues.filter(
3943
+ (cue) => Boolean(cue.text.trim()) && Number.isFinite(cue.start) && cue.start <= currentTime
3944
+ ).sort((left, right) => left.start - right.start).slice(-Math.max(1, cap)).map((cue) => ({
3945
+ text: cue.text.trim(),
3946
+ receivedAt: cue.start * 1e3
3947
+ }));
3948
+ }
3931
3949
  function liveCuesDue(cues, now, lagMs, extraLagMs = LIVE_CUE_EXTRA_LAG_MS) {
3932
3950
  if (lagMs == null || !Number.isFinite(lagMs) || lagMs < 0) return [];
3933
3951
  const threshold = lagMs + extraLagMs;
@@ -4047,6 +4065,27 @@ async function loadLiveCaptions(url) {
4047
4065
  return { ok: false, cues: [] };
4048
4066
  }
4049
4067
  }
4068
+
4069
+ // src/components/video/fullscreen.ts
4070
+ function asWebkitVideo(video) {
4071
+ return video;
4072
+ }
4073
+ function hasWebkitVideoFullscreen(video) {
4074
+ return typeof asWebkitVideo(video)?.webkitEnterFullscreen === "function";
4075
+ }
4076
+ function hasStandardFullscreen() {
4077
+ if (typeof document === "undefined") return false;
4078
+ if (document.fullscreenEnabled) return true;
4079
+ const doc = document;
4080
+ return !!doc.webkitFullscreenEnabled;
4081
+ }
4082
+ function isEmbeddedWindow(win = window) {
4083
+ try {
4084
+ return win.self !== win.top;
4085
+ } catch {
4086
+ return true;
4087
+ }
4088
+ }
4050
4089
  var NEAR_BOTTOM_PX = 24;
4051
4090
  function LiveCaptionStack({ cues, latestLabel }) {
4052
4091
  const scrollerRef = React9.useRef(null);
@@ -4108,7 +4147,7 @@ function LiveCaptionStack({ cues, latestLabel }) {
4108
4147
  ref: scrollerRef,
4109
4148
  role: "log",
4110
4149
  "aria-relevant": "additions",
4111
- className: "pointer-events-auto max-h-[4.5rem] overflow-x-hidden overflow-y-auto rounded-sm bg-black/70 px-3 py-1.5 text-sm font-medium text-white shadow-sm",
4150
+ className: "pointer-events-auto max-h-[min(4.5rem,18cqh)] overflow-x-hidden overflow-y-auto rounded-sm bg-black/70 px-3 py-1.5 text-[length:clamp(10px,2.6cqh,0.875rem)] font-medium text-white shadow-sm [-webkit-text-size-adjust:100%] [text-size-adjust:100%]",
4112
4151
  onScroll: (event) => {
4113
4152
  if (ignoreScrollRef.current) return;
4114
4153
  const scroller = event.currentTarget;
@@ -4120,7 +4159,7 @@ function LiveCaptionStack({ cues, latestLabel }) {
4120
4159
  "p",
4121
4160
  {
4122
4161
  className: cn(
4123
- "text-justify [text-align-last:left] leading-5 break-words",
4162
+ "text-justify [text-align-last:left] leading-snug break-words",
4124
4163
  !prefersReduced && "transition-opacity"
4125
4164
  ),
4126
4165
  children: cues.map((cue, index) => {
@@ -4148,6 +4187,46 @@ function LiveCaptionStack({ cues, latestLabel }) {
4148
4187
  }
4149
4188
  );
4150
4189
  }
4190
+
4191
+ // src/components/video/nativeCaptionTrack.ts
4192
+ var NATIVE_CAPTION_HOLD_S = 4;
4193
+ function joinCaptionTexts(cues) {
4194
+ return cues.map((cue) => cue.text.trim()).filter(Boolean).join(" ");
4195
+ }
4196
+ function clearNativeCaptionCues(track) {
4197
+ const cues = track.cues;
4198
+ if (!cues) return;
4199
+ for (let index = cues.length - 1; index >= 0; index -= 1) {
4200
+ const cue = cues[index];
4201
+ if (cue) track.removeCue(cue);
4202
+ }
4203
+ }
4204
+ function ensureNativeCaptionTrack(video, locale, existing) {
4205
+ if (existing && existing.language === locale) return existing;
4206
+ if (existing) existing.mode = "disabled";
4207
+ const track = video.addTextTrack("captions", "Livo", locale);
4208
+ track.mode = "hidden";
4209
+ return track;
4210
+ }
4211
+ function syncNativeCaptionCue(video, track, text) {
4212
+ clearNativeCaptionCues(track);
4213
+ const trimmed = text.trim();
4214
+ if (!trimmed) return;
4215
+ const start = Number.isFinite(video.currentTime) ? video.currentTime : 0;
4216
+ track.addCue(new VTTCue(start, start + NATIVE_CAPTION_HOLD_S, trimmed));
4217
+ }
4218
+ function syncNativeVodCues(track, cues) {
4219
+ clearNativeCaptionCues(track);
4220
+ for (const cue of cues) {
4221
+ const text = cue.text.trim();
4222
+ if (!text) continue;
4223
+ track.addCue(new VTTCue(cue.start, cue.end, text));
4224
+ }
4225
+ }
4226
+ function setNativeCaptionTrackVisible(track, showing) {
4227
+ if (!track) return;
4228
+ track.mode = showing ? "showing" : "hidden";
4229
+ }
4151
4230
  var DEFAULT_LABELS3 = {
4152
4231
  play: "Play",
4153
4232
  pause: "Pause",
@@ -4183,19 +4262,15 @@ var PLAYBACK_RATES = [0.75, 1, 1.25, 1.5, 2];
4183
4262
  var CONTROLS_HIDE_MS = 3e3;
4184
4263
  var LIVE_EDGE_THRESHOLD_S = 8;
4185
4264
  var SEEK_STEP_S = 5;
4186
- function asWebkitVideo(video) {
4187
- return video;
4188
- }
4189
- function hasWebkitVideoFullscreen(video) {
4190
- const webkit = asWebkitVideo(video);
4191
- return typeof webkit?.webkitEnterFullscreen === "function";
4265
+ var VIDEO_MEDIA_STYLE = `
4266
+ .livo-video-player-media::-webkit-media-controls-start-playback-button {
4267
+ display: none !important;
4268
+ -webkit-appearance: none;
4192
4269
  }
4193
- function hasStandardFullscreen() {
4194
- if (typeof document === "undefined") return false;
4195
- if (document.fullscreenEnabled) return true;
4196
- const doc = document;
4197
- return !!doc.webkitFullscreenEnabled;
4270
+ .livo-video-player-media::-webkit-media-controls {
4271
+ display: none !important;
4198
4272
  }
4273
+ `;
4199
4274
  function captionLocaleLabel(locale, labels) {
4200
4275
  switch (locale) {
4201
4276
  case "en":
@@ -4241,6 +4316,9 @@ function VideoPlayer({
4241
4316
  const t = { ...DEFAULT_LABELS3, ...labels };
4242
4317
  const containerRef = React9.useRef(null);
4243
4318
  const firstPlayFiredRef = React9.useRef(false);
4319
+ const nativeTrackRef = React9.useRef(null);
4320
+ const cssFullscreenRef = React9.useRef(false);
4321
+ const webkitFsRef = React9.useRef(false);
4244
4322
  const [userStarted, setUserStarted] = React9.useState(false);
4245
4323
  const active = autoPlay || !poster || userStarted;
4246
4324
  const autoplayActive = autoPlay || userStarted;
@@ -4292,9 +4370,12 @@ function VideoPlayer({
4292
4370
  const [liveLagMs, setLiveLagMs] = React9.useState(null);
4293
4371
  const [behindLive, setBehindLive] = React9.useState(false);
4294
4372
  const [webkitFullscreenSupported, setWebkitFullscreenSupported] = React9.useState(false);
4373
+ const [embedded, setEmbedded] = React9.useState(false);
4374
+ const [cssFullscreen, setCssFullscreen] = React9.useState(false);
4295
4375
  const hideTimerRef = React9.useRef(null);
4376
+ cssFullscreenRef.current = cssFullscreen;
4296
4377
  const pipSupported = typeof document !== "undefined" && "pictureInPictureEnabled" in document && document.pictureInPictureEnabled;
4297
- const fullscreenSupported = hasStandardFullscreen() || webkitFullscreenSupported;
4378
+ const fullscreenSupported = hasStandardFullscreen() || webkitFullscreenSupported || !embedded;
4298
4379
  React9.useEffect(() => {
4299
4380
  const video = videoRef.current;
4300
4381
  if (!video || !active) return;
@@ -4347,19 +4428,36 @@ function VideoPlayer({
4347
4428
  video.pause();
4348
4429
  }, [paused, videoRef, isPlaying]);
4349
4430
  React9.useEffect(() => {
4431
+ setEmbedded(isEmbeddedWindow());
4350
4432
  setWebkitFullscreenSupported(hasWebkitVideoFullscreen(videoRef.current));
4351
4433
  videoRef.current?.setAttribute("x-webkit-airplay", "allow");
4434
+ videoRef.current?.setAttribute("webkit-playsinline", "");
4352
4435
  }, [videoRef, active]);
4353
4436
  React9.useEffect(() => {
4354
- const onFsChange = () => setIsFullscreen(document.fullscreenElement === containerRef.current);
4437
+ const onFsChange = () => {
4438
+ if (document.fullscreenElement === containerRef.current) {
4439
+ setIsFullscreen(true);
4440
+ return;
4441
+ }
4442
+ if (cssFullscreenRef.current || webkitFsRef.current) return;
4443
+ setIsFullscreen(false);
4444
+ };
4355
4445
  document.addEventListener("fullscreenchange", onFsChange);
4356
4446
  return () => document.removeEventListener("fullscreenchange", onFsChange);
4357
4447
  }, []);
4358
4448
  React9.useEffect(() => {
4359
4449
  const video = asWebkitVideo(videoRef.current);
4360
4450
  if (!video) return;
4361
- const onBegin = () => setIsFullscreen(true);
4362
- const onEnd = () => setIsFullscreen(false);
4451
+ const onBegin = () => {
4452
+ webkitFsRef.current = true;
4453
+ setIsFullscreen(true);
4454
+ setNativeCaptionTrackVisible(nativeTrackRef.current, true);
4455
+ };
4456
+ const onEnd = () => {
4457
+ webkitFsRef.current = false;
4458
+ setNativeCaptionTrackVisible(nativeTrackRef.current, false);
4459
+ if (!cssFullscreenRef.current) setIsFullscreen(false);
4460
+ };
4363
4461
  video.addEventListener("webkitbeginfullscreen", onBegin);
4364
4462
  video.addEventListener("webkitendfullscreen", onEnd);
4365
4463
  return () => {
@@ -4367,6 +4465,21 @@ function VideoPlayer({
4367
4465
  video.removeEventListener("webkitendfullscreen", onEnd);
4368
4466
  };
4369
4467
  }, [videoRef, active]);
4468
+ React9.useEffect(() => {
4469
+ if (!cssFullscreen) return;
4470
+ const previousOverflow = document.body.style.overflow;
4471
+ document.body.style.overflow = "hidden";
4472
+ const onKey = (event) => {
4473
+ if (event.key !== "Escape") return;
4474
+ setCssFullscreen(false);
4475
+ setIsFullscreen(false);
4476
+ };
4477
+ document.addEventListener("keydown", onKey);
4478
+ return () => {
4479
+ document.body.style.overflow = previousOverflow;
4480
+ document.removeEventListener("keydown", onKey);
4481
+ };
4482
+ }, [cssFullscreen]);
4370
4483
  React9.useEffect(() => {
4371
4484
  const video = videoRef.current;
4372
4485
  if (!video) return;
@@ -4431,10 +4544,44 @@ function VideoPlayer({
4431
4544
  window.clearInterval(timer);
4432
4545
  };
4433
4546
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4547
+ const endedLiveReplay = !liveCaptionsBaseUrl && isLivePlaylistSrc(src);
4434
4548
  const liveDueCues = liveCaptionsBaseUrl ? coalesceLiveCaptionCues(
4435
4549
  liveCuesDue(liveCaptionCues, Date.now(), liveLagMs)
4436
- ) : [];
4437
- const activeCaption = liveCaptionsBaseUrl ? null : captionAtTime(captionCues, currentTime);
4550
+ ) : endedLiveReplay ? coalesceLiveCaptionCues(vttCuesDueAt(captionCues, currentTime)) : [];
4551
+ const activeCaption = liveCaptionsBaseUrl || endedLiveReplay ? null : captionAtTime(captionCues, currentTime);
4552
+ React9.useEffect(() => {
4553
+ const video = videoRef.current;
4554
+ if (!video || captionLocale === "off") {
4555
+ if (nativeTrackRef.current) {
4556
+ nativeTrackRef.current.mode = "disabled";
4557
+ nativeTrackRef.current = null;
4558
+ }
4559
+ return;
4560
+ }
4561
+ try {
4562
+ nativeTrackRef.current = ensureNativeCaptionTrack(
4563
+ video,
4564
+ captionLocale,
4565
+ nativeTrackRef.current
4566
+ );
4567
+ setNativeCaptionTrackVisible(nativeTrackRef.current, webkitFsRef.current);
4568
+ } catch {
4569
+ nativeTrackRef.current = null;
4570
+ }
4571
+ }, [videoRef, active, captionLocale]);
4572
+ React9.useEffect(() => {
4573
+ const track = nativeTrackRef.current;
4574
+ if (!track || captionLocale === "off") return;
4575
+ if (liveCaptionsBaseUrl || endedLiveReplay) return;
4576
+ syncNativeVodCues(track, captionCues);
4577
+ }, [captionCues, captionLocale, liveCaptionsBaseUrl, endedLiveReplay]);
4578
+ React9.useEffect(() => {
4579
+ const video = videoRef.current;
4580
+ const track = nativeTrackRef.current;
4581
+ if (!video || !track) return;
4582
+ if (!(liveCaptionsBaseUrl || endedLiveReplay)) return;
4583
+ syncNativeCaptionCue(video, track, joinCaptionTexts(liveDueCues));
4584
+ }, [liveDueCues, liveCaptionsBaseUrl, endedLiveReplay]);
4438
4585
  function togglePlay() {
4439
4586
  const video = videoRef.current;
4440
4587
  if (!video) return;
@@ -4469,6 +4616,10 @@ function VideoPlayer({
4469
4616
  video.currentTime = video.seekable.end(video.seekable.length - 1);
4470
4617
  if (video.paused) void video.play();
4471
4618
  }
4619
+ function exitCssFullscreen() {
4620
+ setCssFullscreen(false);
4621
+ setIsFullscreen(false);
4622
+ }
4472
4623
  function toggleFullscreen() {
4473
4624
  const el = containerRef.current;
4474
4625
  const video = asWebkitVideo(videoRef.current);
@@ -4480,13 +4631,20 @@ function VideoPlayer({
4480
4631
  video.webkitExitFullscreen();
4481
4632
  return;
4482
4633
  }
4634
+ if (cssFullscreen) {
4635
+ exitCssFullscreen();
4636
+ return;
4637
+ }
4483
4638
  if (el && hasStandardFullscreen() && typeof el.requestFullscreen === "function") {
4484
4639
  void el.requestFullscreen();
4485
4640
  return;
4486
4641
  }
4487
- if (video && typeof video.webkitEnterFullscreen === "function") {
4642
+ if (embedded && video && typeof video.webkitEnterFullscreen === "function") {
4488
4643
  video.webkitEnterFullscreen();
4644
+ return;
4489
4645
  }
4646
+ setCssFullscreen(true);
4647
+ setIsFullscreen(true);
4490
4648
  }
4491
4649
  function togglePip() {
4492
4650
  const video = videoRef.current;
@@ -4521,6 +4679,12 @@ function VideoPlayer({
4521
4679
  case "f":
4522
4680
  toggleFullscreen();
4523
4681
  break;
4682
+ case "Escape":
4683
+ if (cssFullscreen) {
4684
+ e.preventDefault();
4685
+ exitCssFullscreen();
4686
+ }
4687
+ break;
4524
4688
  case "ArrowLeft":
4525
4689
  if (mode === "vod") {
4526
4690
  e.preventDefault();
@@ -4576,9 +4740,11 @@ function VideoPlayer({
4576
4740
  "data-slot": "video-player",
4577
4741
  "data-mode": mode,
4578
4742
  "data-fit": fit,
4743
+ "data-css-fullscreen": cssFullscreen ? "" : void 0,
4579
4744
  className: cn(
4580
- "group relative w-full overflow-hidden rounded-lg bg-black text-white select-none",
4745
+ "@container group relative w-full overflow-hidden rounded-lg bg-black text-white select-none",
4581
4746
  fit === "contain" ? "h-full min-h-0 aspect-auto" : "aspect-video",
4747
+ cssFullscreen && "fixed inset-0 z-50 h-dvh w-dvw rounded-none",
4582
4748
  className
4583
4749
  ),
4584
4750
  onMouseMove: revealControls,
@@ -4590,13 +4756,14 @@ function VideoPlayer({
4590
4756
  role: "region",
4591
4757
  "aria-label": "Video player",
4592
4758
  children: [
4759
+ /* @__PURE__ */ jsx("style", { children: VIDEO_MEDIA_STYLE }),
4593
4760
  /* @__PURE__ */ jsx(
4594
4761
  "video",
4595
4762
  {
4596
4763
  ref: videoRef,
4597
4764
  playsInline: true,
4598
4765
  muted: isMuted,
4599
- className: "h-full w-full bg-black object-contain",
4766
+ className: "livo-video-player-media h-full w-full bg-black object-contain",
4600
4767
  onClick: active ? togglePlay : void 0,
4601
4768
  poster
4602
4769
  }
@@ -4635,7 +4802,7 @@ function VideoPlayer({
4635
4802
  }
4636
4803
  )
4637
4804
  ] }),
4638
- state === "ready" && autoplayState === "blocked" && /* @__PURE__ */ jsx("div", { className: "absolute inset-0 flex items-center justify-center bg-black/40", children: /* @__PURE__ */ jsx(
4805
+ state === "ready" && autoplayState === "blocked" && !isPlaying && /* @__PURE__ */ jsx("div", { className: "absolute inset-0 flex items-center justify-center bg-black/40", children: /* @__PURE__ */ jsx(
4639
4806
  "button",
4640
4807
  {
4641
4808
  type: "button",
@@ -4664,13 +4831,13 @@ function VideoPlayer({
4664
4831
  ] }),
4665
4832
  typeof stats?.viewers === "number" && /* @__PURE__ */ jsx("span", { className: "rounded bg-black/60 px-2 py-0.5 text-xs", children: t.formatViewers ? t.formatViewers(stats.viewers) : `\u{1F441} ${stats.viewers}` })
4666
4833
  ] }),
4667
- liveCaptionsBaseUrl && captionLocale !== "off" && /* @__PURE__ */ jsx(LiveCaptionStack, { cues: liveDueCues, latestLabel: t.captionsLatest }),
4668
- !liveCaptionsBaseUrl && activeCaption && captionLocale !== "off" && /* @__PURE__ */ jsx(
4834
+ (liveCaptionsBaseUrl || endedLiveReplay) && captionLocale !== "off" && /* @__PURE__ */ jsx(LiveCaptionStack, { cues: liveDueCues, latestLabel: t.captionsLatest }),
4835
+ !liveCaptionsBaseUrl && !endedLiveReplay && activeCaption && captionLocale !== "off" && /* @__PURE__ */ jsx(
4669
4836
  "div",
4670
4837
  {
4671
4838
  className: "pointer-events-none absolute inset-x-0 bottom-16 z-[1] flex justify-center px-4",
4672
4839
  "data-slot": "video-captions",
4673
- children: /* @__PURE__ */ jsx("p", { className: "line-clamp-2 max-w-[90%] break-words rounded-sm bg-black/70 px-3 py-1.5 text-center text-sm font-medium text-white shadow-sm", children: activeCaption.text })
4840
+ children: /* @__PURE__ */ jsx("p", { className: "line-clamp-2 max-w-[90%] break-words rounded-sm bg-black/70 px-3 py-1.5 text-center text-[length:clamp(10px,2.6cqh,0.875rem)] font-medium text-white shadow-sm [-webkit-text-size-adjust:100%] [text-size-adjust:100%]", children: activeCaption.text })
4674
4841
  }
4675
4842
  ),
4676
4843
  active && /* @__PURE__ */ jsxs(