@livo-tv/blocks 1.19.0 → 1.20.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.cjs CHANGED
@@ -4245,6 +4245,7 @@ function LiveCaptionStack({
4245
4245
 
4246
4246
  // src/components/video/nativeCaptionTrack.ts
4247
4247
  var NATIVE_CAPTION_HOLD_S = 4;
4248
+ var NATIVE_SUBTITLE_KIND = "subtitles";
4248
4249
  function joinCaptionTexts(cues) {
4249
4250
  return cues.map((cue) => cue.text.trim()).filter(Boolean).join(" ");
4250
4251
  }
@@ -4256,12 +4257,46 @@ function clearNativeCaptionCues(track) {
4256
4257
  if (cue) track.removeCue(cue);
4257
4258
  }
4258
4259
  }
4259
- function ensureNativeCaptionTrack(video, locale, existing) {
4260
- if (existing && existing.language === locale) return existing;
4261
- if (existing) existing.mode = "disabled";
4262
- const track = video.addTextTrack("captions", "Livo", locale);
4263
- track.mode = "hidden";
4264
- return track;
4260
+ function subtitleTracksForLocales(video) {
4261
+ return Array.from(video.textTracks).filter(
4262
+ (track) => track.kind === NATIVE_SUBTITLE_KIND && isCaptionLocale(track.language)
4263
+ );
4264
+ }
4265
+ function findSubtitleTrack(video, locale) {
4266
+ return subtitleTracksForLocales(video).find(
4267
+ (track) => track.language === locale
4268
+ ) ?? null;
4269
+ }
4270
+ function showingCaptionLocale(video) {
4271
+ for (const track of subtitleTracksForLocales(video)) {
4272
+ if (track.mode === "showing" && isCaptionLocale(track.language)) {
4273
+ return track.language;
4274
+ }
4275
+ }
4276
+ return "off";
4277
+ }
4278
+ function applyCaptionTrackModes(video, locale, nativeVisible) {
4279
+ for (const track of subtitleTracksForLocales(video)) {
4280
+ if (locale !== "off" && track.language === locale) {
4281
+ track.mode = nativeVisible ? "showing" : "hidden";
4282
+ } else {
4283
+ track.mode = "disabled";
4284
+ }
4285
+ }
4286
+ }
4287
+ function ensureLiveSubtitleTracks(video, labels) {
4288
+ const existing = subtitleTracksForLocales(video);
4289
+ return CAPTION_LOCALES.map((locale) => {
4290
+ const found = existing.find((track2) => track2.language === locale);
4291
+ if (found) return found;
4292
+ const track = video.addTextTrack(
4293
+ NATIVE_SUBTITLE_KIND,
4294
+ labels[locale],
4295
+ locale
4296
+ );
4297
+ track.mode = "disabled";
4298
+ return track;
4299
+ });
4265
4300
  }
4266
4301
  function syncNativeCaptionCue(video, track, text) {
4267
4302
  clearNativeCaptionCues(track);
@@ -4270,17 +4305,45 @@ function syncNativeCaptionCue(video, track, text) {
4270
4305
  const start = Number.isFinite(video.currentTime) ? video.currentTime : 0;
4271
4306
  track.addCue(new VTTCue(start, start + NATIVE_CAPTION_HOLD_S, trimmed));
4272
4307
  }
4273
- function syncNativeVodCues(track, cues) {
4274
- clearNativeCaptionCues(track);
4275
- for (const cue of cues) {
4276
- const text = cue.text.trim();
4277
- if (!text) continue;
4278
- track.addCue(new VTTCue(cue.start, cue.end, text));
4308
+ function zeroAnchorNativeTrackCues(track) {
4309
+ const list = track.cues;
4310
+ if (!list || list.length === 0) return;
4311
+ let origin = Number.POSITIVE_INFINITY;
4312
+ for (let index = 0; index < list.length; index += 1) {
4313
+ const cue = list[index];
4314
+ if (cue && Number.isFinite(cue.startTime) && cue.startTime < origin) {
4315
+ origin = cue.startTime;
4316
+ }
4279
4317
  }
4318
+ if (!Number.isFinite(origin) || origin <= 0) return;
4319
+ const next = [];
4320
+ for (let index = 0; index < list.length; index += 1) {
4321
+ const cue = list[index];
4322
+ if (!(cue instanceof VTTCue)) continue;
4323
+ next.push(
4324
+ new VTTCue(
4325
+ Math.max(0, cue.startTime - origin),
4326
+ Math.max(0, cue.endTime - origin),
4327
+ cue.text
4328
+ )
4329
+ );
4330
+ }
4331
+ clearNativeCaptionCues(track);
4332
+ for (const cue of next) track.addCue(cue);
4280
4333
  }
4281
- function setNativeCaptionTrackVisible(track, showing) {
4282
- if (!track) return;
4283
- track.mode = showing ? "showing" : "hidden";
4334
+ function listenTextTracksChange(tracks, onChange) {
4335
+ if (typeof tracks.addEventListener === "function") {
4336
+ tracks.addEventListener("change", onChange);
4337
+ return () => {
4338
+ if (typeof tracks.removeEventListener === "function") {
4339
+ tracks.removeEventListener("change", onChange);
4340
+ }
4341
+ };
4342
+ }
4343
+ tracks.onchange = onChange;
4344
+ return () => {
4345
+ if (tracks.onchange === onChange) tracks.onchange = null;
4346
+ };
4284
4347
  }
4285
4348
  var DEFAULT_LABELS3 = {
4286
4349
  play: "Play",
@@ -4374,7 +4437,8 @@ function VideoPlayer({
4374
4437
  const t = { ...DEFAULT_LABELS3, ...labels };
4375
4438
  const containerRef = React9__namespace.useRef(null);
4376
4439
  const firstPlayFiredRef = React9__namespace.useRef(false);
4377
- const nativeTrackRef = React9__namespace.useRef(null);
4440
+ const applyingTrackModesRef = React9__namespace.useRef(false);
4441
+ const captionLocaleRef = React9__namespace.useRef("off");
4378
4442
  const cssFullscreenRef = React9__namespace.useRef(false);
4379
4443
  const webkitFsRef = React9__namespace.useRef(false);
4380
4444
  const [userStarted, setUserStarted] = React9__namespace.useState(false);
@@ -4435,8 +4499,10 @@ function VideoPlayer({
4435
4499
  const [embedded, setEmbedded] = React9__namespace.useState(false);
4436
4500
  const [cssFullscreen, setCssFullscreen] = React9__namespace.useState(false);
4437
4501
  const [appleMobile, setAppleMobile] = React9__namespace.useState(false);
4502
+ const [webkitNativeFs, setWebkitNativeFs] = React9__namespace.useState(false);
4438
4503
  const hideTimerRef = React9__namespace.useRef(null);
4439
4504
  cssFullscreenRef.current = cssFullscreen;
4505
+ captionLocaleRef.current = captionLocale;
4440
4506
  React9__namespace.useEffect(() => {
4441
4507
  const el = containerRef.current;
4442
4508
  if (!el || typeof ResizeObserver === "undefined") return;
@@ -4527,17 +4593,42 @@ function VideoPlayer({
4527
4593
  document.addEventListener("fullscreenchange", onFsChange);
4528
4594
  return () => document.removeEventListener("fullscreenchange", onFsChange);
4529
4595
  }, []);
4596
+ function applyNativeTrackModes(video, locale, nativeVisible) {
4597
+ applyingTrackModesRef.current = true;
4598
+ try {
4599
+ applyCaptionTrackModes(video, locale, nativeVisible);
4600
+ } finally {
4601
+ applyingTrackModesRef.current = false;
4602
+ }
4603
+ }
4604
+ function onSidecarTrackLoad(event) {
4605
+ const track = event.currentTarget.track;
4606
+ if (!track) return;
4607
+ if (isLivePlaylistSrc(src)) {
4608
+ zeroAnchorNativeTrackCues(track);
4609
+ }
4610
+ const video = videoRef.current;
4611
+ if (video) {
4612
+ applyNativeTrackModes(
4613
+ video,
4614
+ captionLocaleRef.current,
4615
+ webkitFsRef.current
4616
+ );
4617
+ }
4618
+ }
4530
4619
  React9__namespace.useEffect(() => {
4531
4620
  const video = asWebkitVideo(videoRef.current);
4532
4621
  if (!video) return;
4533
4622
  const onBegin = () => {
4534
4623
  webkitFsRef.current = true;
4624
+ setWebkitNativeFs(true);
4535
4625
  setIsFullscreen(true);
4536
- setNativeCaptionTrackVisible(nativeTrackRef.current, true);
4626
+ applyNativeTrackModes(video, captionLocaleRef.current, true);
4537
4627
  };
4538
4628
  const onEnd = () => {
4539
4629
  webkitFsRef.current = false;
4540
- setNativeCaptionTrackVisible(nativeTrackRef.current, false);
4630
+ setWebkitNativeFs(false);
4631
+ applyNativeTrackModes(video, captionLocaleRef.current, false);
4541
4632
  if (!cssFullscreenRef.current) setIsFullscreen(false);
4542
4633
  };
4543
4634
  video.addEventListener("webkitbeginfullscreen", onBegin);
@@ -4634,41 +4725,56 @@ function VideoPlayer({
4634
4725
  const activeCaption = liveCaptionsBaseUrl || endedLiveReplay ? null : captionAtTime(captionCues, currentTime);
4635
4726
  React9__namespace.useEffect(() => {
4636
4727
  const video = videoRef.current;
4637
- if (!video || captionLocale === "off") {
4638
- if (nativeTrackRef.current) {
4639
- nativeTrackRef.current.mode = "disabled";
4640
- nativeTrackRef.current = null;
4641
- }
4642
- return;
4643
- }
4728
+ if (!video || !liveCaptionsBaseUrl) return;
4644
4729
  try {
4645
- nativeTrackRef.current = ensureNativeCaptionTrack(
4646
- video,
4647
- captionLocale,
4648
- nativeTrackRef.current
4649
- );
4650
- setNativeCaptionTrackVisible(nativeTrackRef.current, webkitFsRef.current);
4730
+ ensureLiveSubtitleTracks(video, {
4731
+ en: t.captionsEn,
4732
+ es: t.captionsEs,
4733
+ pt: t.captionsPt
4734
+ });
4651
4735
  } catch {
4652
- nativeTrackRef.current = null;
4736
+ return;
4653
4737
  }
4654
- }, [videoRef, active, captionLocale]);
4738
+ applyNativeTrackModes(video, captionLocale, webkitFsRef.current);
4739
+ }, [
4740
+ videoRef,
4741
+ active,
4742
+ liveCaptionsBaseUrl,
4743
+ captionLocale,
4744
+ t.captionsEn,
4745
+ t.captionsEs,
4746
+ t.captionsPt
4747
+ ]);
4748
+ React9__namespace.useEffect(() => {
4749
+ const video = videoRef.current;
4750
+ if (!video) return;
4751
+ applyNativeTrackModes(video, captionLocale, webkitFsRef.current);
4752
+ }, [videoRef, active, captionLocale, src]);
4655
4753
  React9__namespace.useEffect(() => {
4656
- const track = nativeTrackRef.current;
4657
- if (!track || captionLocale === "off") return;
4658
- if (liveCaptionsBaseUrl || endedLiveReplay) return;
4659
- syncNativeVodCues(track, captionCues);
4660
- }, [captionCues, captionLocale, liveCaptionsBaseUrl, endedLiveReplay]);
4754
+ const video = videoRef.current;
4755
+ if (!video) return;
4756
+ const onChange = () => {
4757
+ if (applyingTrackModesRef.current) return;
4758
+ const showing = showingCaptionLocale(video);
4759
+ if (!webkitFsRef.current) {
4760
+ if (showing !== "off") setCaptionLocale(showing);
4761
+ return;
4762
+ }
4763
+ setCaptionLocale(showing);
4764
+ };
4765
+ return listenTextTracksChange(video.textTracks, onChange);
4766
+ }, [videoRef, active, src, liveCaptionsBaseUrl]);
4661
4767
  React9__namespace.useEffect(() => {
4662
4768
  const video = videoRef.current;
4663
- const track = nativeTrackRef.current;
4664
- if (!video || !track) return;
4665
- if (!(liveCaptionsBaseUrl || endedLiveReplay)) return;
4769
+ if (!video || captionLocale === "off" || !liveCaptionsBaseUrl) return;
4770
+ const track = findSubtitleTrack(video, captionLocale);
4771
+ if (!track) return;
4666
4772
  syncNativeCaptionCue(
4667
4773
  video,
4668
4774
  track,
4669
4775
  lastCaptionLines(joinCaptionTexts(captionStackCues))
4670
4776
  );
4671
- }, [captionStackCues, liveCaptionsBaseUrl, endedLiveReplay]);
4777
+ }, [captionStackCues, captionLocale, liveCaptionsBaseUrl]);
4672
4778
  function togglePlay() {
4673
4779
  const video = videoRef.current;
4674
4780
  if (!video) return;
@@ -4853,7 +4959,22 @@ function VideoPlayer({
4853
4959
  muted: isMuted,
4854
4960
  className: "livo-video-player-media h-full w-full bg-black object-contain",
4855
4961
  onClick: active ? togglePlay : void 0,
4856
- poster
4962
+ poster,
4963
+ children: !liveCaptionsBaseUrl ? CAPTION_LOCALES.map((locale) => {
4964
+ const url = captionsVttUrlFromPlayback(src, locale);
4965
+ if (!url) return null;
4966
+ return /* @__PURE__ */ jsxRuntime.jsx(
4967
+ "track",
4968
+ {
4969
+ kind: "subtitles",
4970
+ srcLang: locale,
4971
+ label: captionLocaleLabel(locale, t),
4972
+ src: url,
4973
+ onLoad: onSidecarTrackLoad
4974
+ },
4975
+ `${locale}:${url}`
4976
+ );
4977
+ }) : null
4857
4978
  }
4858
4979
  ),
4859
4980
  showPoster && /* @__PURE__ */ jsxRuntime.jsx(
@@ -4919,7 +5040,7 @@ function VideoPlayer({
4919
5040
  ] }),
4920
5041
  typeof stats?.viewers === "number" && /* @__PURE__ */ jsxRuntime.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}` })
4921
5042
  ] }),
4922
- (liveCaptionsBaseUrl || endedLiveReplay) && captionLocale !== "off" && /* @__PURE__ */ jsxRuntime.jsx(
5043
+ (liveCaptionsBaseUrl || endedLiveReplay) && captionLocale !== "off" && !webkitNativeFs && /* @__PURE__ */ jsxRuntime.jsx(
4923
5044
  LiveCaptionStack,
4924
5045
  {
4925
5046
  cues: captionStackCues,
@@ -4927,7 +5048,7 @@ function VideoPlayer({
4927
5048
  variant: appleMobile ? "window" : "scroll"
4928
5049
  }
4929
5050
  ),
4930
- !liveCaptionsBaseUrl && !endedLiveReplay && activeCaption && captionLocale !== "off" && /* @__PURE__ */ jsxRuntime.jsx(
5051
+ !liveCaptionsBaseUrl && !endedLiveReplay && activeCaption && captionLocale !== "off" && !webkitNativeFs && /* @__PURE__ */ jsxRuntime.jsx(
4931
5052
  "div",
4932
5053
  {
4933
5054
  className: "pointer-events-none absolute inset-x-0 bottom-16 z-[1] flex justify-center px-4",