@livo-tv/blocks 1.8.0 → 1.9.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
@@ -3867,6 +3867,59 @@ function useHlsPlayback({
3867
3867
  retry
3868
3868
  };
3869
3869
  }
3870
+
3871
+ // src/components/video/captions.ts
3872
+ var CAPTION_LOCALES = ["en", "es", "pt"];
3873
+ function isCaptionLocale(value) {
3874
+ return CAPTION_LOCALES.includes(value);
3875
+ }
3876
+ function captionsVttUrlFromPlayback(src, locale) {
3877
+ const trimmed = src.trim();
3878
+ if (!trimmed) return null;
3879
+ const replaced = trimmed.replace(
3880
+ /index\.m3u8(?:\?.*)?$/i,
3881
+ `subs/${locale}.vtt`
3882
+ );
3883
+ if (replaced === trimmed) return null;
3884
+ return replaced;
3885
+ }
3886
+ function liveCaptionsUrl(base, locale) {
3887
+ return `${base.replace(/\/$/, "")}/${locale}`;
3888
+ }
3889
+ function parseCaptionVtt(text) {
3890
+ const cues = [];
3891
+ const blocks = text.replace(/^\uFEFF?WEBVTT[^\n]*\n/i, "").split(/\n\s*\n/);
3892
+ for (const block of blocks) {
3893
+ const lines = block.split("\n").map((line) => line.trim()).filter(Boolean);
3894
+ const timing = lines.find((line) => line.includes("-->"));
3895
+ if (!timing) continue;
3896
+ const [startRaw, endRaw] = timing.split("-->").map((part) => part.trim());
3897
+ if (!startRaw || !endRaw) continue;
3898
+ const textLines = lines.filter(
3899
+ (line) => line !== timing && !/^\d+$/.test(line)
3900
+ );
3901
+ const payload = textLines.join("\n").trim();
3902
+ if (!payload) continue;
3903
+ cues.push({
3904
+ start: parseVttTimestamp(startRaw.split(" ")[0] ?? startRaw),
3905
+ end: parseVttTimestamp(endRaw.split(" ")[0] ?? endRaw),
3906
+ text: payload
3907
+ });
3908
+ }
3909
+ return cues;
3910
+ }
3911
+ function captionAtTime(cues, time) {
3912
+ return cues.find((cue) => time >= cue.start && time < cue.end) ?? null;
3913
+ }
3914
+ async function loadCaptionVtt(url) {
3915
+ try {
3916
+ const response = await fetch(url, { cache: "no-store" });
3917
+ if (!response.ok) return [];
3918
+ return parseCaptionVtt(await response.text());
3919
+ } catch {
3920
+ return [];
3921
+ }
3922
+ }
3870
3923
  var DEFAULT_LABELS3 = {
3871
3924
  play: "Play",
3872
3925
  pause: "Pause",
@@ -3890,7 +3943,12 @@ var DEFAULT_LABELS3 = {
3890
3943
  errorNetwork: "We couldn't load the video. Check your connection.",
3891
3944
  errorMedia: "There was a playback problem.",
3892
3945
  errorUnsupported: "Your browser can't play this video.",
3893
- errorGeneric: "We couldn't play this video."
3946
+ errorGeneric: "We couldn't play this video.",
3947
+ captions: "Captions",
3948
+ captionsOff: "Off",
3949
+ captionsEn: "English",
3950
+ captionsEs: "Espa\xF1ol",
3951
+ captionsPt: "Portugu\xEAs"
3894
3952
  };
3895
3953
  var PLAYBACK_RATES = [0.75, 1, 1.25, 1.5, 2];
3896
3954
  var CONTROLS_HIDE_MS = 3e3;
@@ -3909,6 +3967,20 @@ function hasStandardFullscreen() {
3909
3967
  const doc = document;
3910
3968
  return !!doc.webkitFullscreenEnabled;
3911
3969
  }
3970
+ function captionLocaleLabel(locale, labels) {
3971
+ switch (locale) {
3972
+ case "en":
3973
+ return labels.captionsEn;
3974
+ case "es":
3975
+ return labels.captionsEs;
3976
+ case "pt":
3977
+ return labels.captionsPt;
3978
+ default: {
3979
+ const _exhaustive = locale;
3980
+ return _exhaustive;
3981
+ }
3982
+ }
3983
+ }
3912
3984
  function formatTime(seconds) {
3913
3985
  if (!Number.isFinite(seconds) || seconds < 0) return "0:00";
3914
3986
  const total = Math.floor(seconds);
@@ -3933,7 +4005,9 @@ function VideoPlayer({
3933
4005
  onFirstPlay,
3934
4006
  onError,
3935
4007
  fit = "aspect-video",
3936
- className
4008
+ className,
4009
+ defaultCaptionLocale,
4010
+ liveCaptionsBaseUrl
3937
4011
  }) {
3938
4012
  const t = { ...DEFAULT_LABELS3, ...labels };
3939
4013
  const containerRef = React8__namespace.useRef(null);
@@ -3980,9 +4054,9 @@ function VideoPlayer({
3980
4054
  const [pipActive, setPipActive] = React8__namespace.useState(false);
3981
4055
  const [controlsVisible, setControlsVisible] = React8__namespace.useState(true);
3982
4056
  const [playbackRate, setPlaybackRate] = React8__namespace.useState(1);
3983
- const [openMenu, setOpenMenu] = React8__namespace.useState(
3984
- null
3985
- );
4057
+ const [openMenu, setOpenMenu] = React8__namespace.useState(null);
4058
+ const [captionLocale, setCaptionLocale] = React8__namespace.useState(defaultCaptionLocale ?? "off");
4059
+ const [captionCues, setCaptionCues] = React8__namespace.useState([]);
3986
4060
  const [behindLive, setBehindLive] = React8__namespace.useState(false);
3987
4061
  const [webkitFullscreenSupported, setWebkitFullscreenSupported] = React8__namespace.useState(false);
3988
4062
  const hideTimerRef = React8__namespace.useRef(null);
@@ -4075,6 +4149,31 @@ function VideoPlayer({
4075
4149
  if (hideTimerRef.current) clearTimeout(hideTimerRef.current);
4076
4150
  };
4077
4151
  }, []);
4152
+ React8__namespace.useEffect(() => {
4153
+ if (captionLocale === "off") {
4154
+ setCaptionCues([]);
4155
+ return;
4156
+ }
4157
+ let cancelled = false;
4158
+ const load = async () => {
4159
+ const liveBase = liveCaptionsBaseUrl?.trim();
4160
+ const url = liveBase ? liveCaptionsUrl(liveBase, captionLocale) : captionsVttUrlFromPlayback(src, captionLocale);
4161
+ if (!url) {
4162
+ if (!cancelled) setCaptionCues([]);
4163
+ return;
4164
+ }
4165
+ const cues = await loadCaptionVtt(url);
4166
+ if (!cancelled) setCaptionCues(cues);
4167
+ };
4168
+ void load();
4169
+ if (!liveCaptionsBaseUrl) return;
4170
+ const timer = window.setInterval(() => void load(), 2e3);
4171
+ return () => {
4172
+ cancelled = true;
4173
+ window.clearInterval(timer);
4174
+ };
4175
+ }, [captionLocale, liveCaptionsBaseUrl, src]);
4176
+ const activeCaption = captionAtTime(captionCues, currentTime);
4078
4177
  function togglePlay() {
4079
4178
  const video = videoRef.current;
4080
4179
  if (!video) return;
@@ -4303,6 +4402,14 @@ function VideoPlayer({
4303
4402
  ] }),
4304
4403
  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}` })
4305
4404
  ] }),
4405
+ activeCaption && captionLocale !== "off" && /* @__PURE__ */ jsxRuntime.jsx(
4406
+ "div",
4407
+ {
4408
+ className: "pointer-events-none absolute inset-x-0 bottom-16 z-[1] flex justify-center px-4",
4409
+ "data-slot": "video-captions",
4410
+ children: /* @__PURE__ */ jsxRuntime.jsx("p", { className: "max-w-[90%] rounded-sm bg-black/70 px-3 py-1.5 text-center text-sm font-medium text-white shadow-sm", children: activeCaption.text })
4411
+ }
4412
+ ),
4306
4413
  active && /* @__PURE__ */ jsxRuntime.jsxs(
4307
4414
  "div",
4308
4415
  {
@@ -4372,6 +4479,34 @@ function VideoPlayer({
4372
4479
  ),
4373
4480
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "ml-auto flex items-center gap-1", children: [
4374
4481
  mode === "vod" && typeof stats?.views === "number" && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "mr-1 text-xs", children: t.formatViews ? t.formatViews(stats.views) : `${stats.views} views` }),
4482
+ /* @__PURE__ */ jsxRuntime.jsxs(
4483
+ MenuControl,
4484
+ {
4485
+ label: t.captions,
4486
+ open: openMenu === "captions",
4487
+ onOpenChange: (o) => setOpenMenu(o ? "captions" : null),
4488
+ trigger: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Captions, { className: "size-5" }),
4489
+ children: [
4490
+ /* @__PURE__ */ jsxRuntime.jsx(
4491
+ MenuItem,
4492
+ {
4493
+ selected: captionLocale === "off",
4494
+ onSelect: () => setCaptionLocale("off"),
4495
+ children: t.captionsOff
4496
+ }
4497
+ ),
4498
+ CAPTION_LOCALES.map((locale) => /* @__PURE__ */ jsxRuntime.jsx(
4499
+ MenuItem,
4500
+ {
4501
+ selected: captionLocale === locale,
4502
+ onSelect: () => setCaptionLocale(locale),
4503
+ children: captionLocaleLabel(locale, t)
4504
+ },
4505
+ locale
4506
+ ))
4507
+ ]
4508
+ }
4509
+ ),
4375
4510
  mode === "vod" && /* @__PURE__ */ jsxRuntime.jsx(
4376
4511
  MenuControl,
4377
4512
  {
@@ -5364,6 +5499,7 @@ function avatarFallbackStyle(name) {
5364
5499
 
5365
5500
  exports.AvatarEditor = AvatarEditor;
5366
5501
  exports.AvatarEditorDialog = AvatarEditorDialog;
5502
+ exports.CAPTION_LOCALES = CAPTION_LOCALES;
5367
5503
  exports.ConfirmDestructiveDialog = ConfirmDestructiveDialog;
5368
5504
  exports.CurtainPicker = CurtainPicker;
5369
5505
  exports.CurtainPickerSkeleton = CurtainPickerSkeleton;
@@ -5400,6 +5536,7 @@ exports.ThemeSwitcher = ThemeSwitcher;
5400
5536
  exports.VideoPlayer = VideoPlayer;
5401
5537
  exports.WaitingRoom = WaitingRoom;
5402
5538
  exports.avatarFallbackStyle = avatarFallbackStyle;
5539
+ exports.captionsVttUrlFromPlayback = captionsVttUrlFromPlayback;
5403
5540
  exports.castHlsUrl = castHlsUrl;
5404
5541
  exports.cn = cn;
5405
5542
  exports.createLiveHlsConfig = createLiveHlsConfig;
@@ -5411,6 +5548,7 @@ exports.describePlaybackError = describePlaybackError;
5411
5548
  exports.fileMatchesAccept = fileMatchesAccept;
5412
5549
  exports.getAvatarColors = getAvatarColors;
5413
5550
  exports.getInitials = getInitials;
5551
+ exports.isCaptionLocale = isCaptionLocale;
5414
5552
  exports.loadCastSender = loadCastSender;
5415
5553
  exports.partRanges = partRanges;
5416
5554
  exports.progressFromBytes = progressFromBytes;