@livo-tv/blocks 1.7.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
  {
@@ -4695,6 +4830,24 @@ function partRanges(fileSize, partSize) {
4695
4830
  };
4696
4831
  });
4697
4832
  }
4833
+ var BYTE_UNITS = ["B", "KB", "MB", "GB", "TB"];
4834
+ function formatUploadBytes(bytes) {
4835
+ if (!Number.isFinite(bytes) || bytes <= 0) {
4836
+ return "0 B";
4837
+ }
4838
+ let value = bytes;
4839
+ let unit = 0;
4840
+ while (value >= 1024 && unit < BYTE_UNITS.length - 1) {
4841
+ value /= 1024;
4842
+ unit += 1;
4843
+ }
4844
+ if (unit === 0 || value >= 10) {
4845
+ return `${Math.round(value)} ${BYTE_UNITS[unit]}`;
4846
+ }
4847
+ const tenths = Math.round(value * 10) / 10;
4848
+ const label = Number.isInteger(tenths) ? String(tenths) : tenths.toFixed(1);
4849
+ return `${label} ${BYTE_UNITS[unit]}`;
4850
+ }
4698
4851
  function progressFromBytes(loaded, total) {
4699
4852
  const safeTotal = Math.max(0, total);
4700
4853
  const safeLoaded = Math.min(Math.max(0, loaded), safeTotal);
@@ -4883,6 +5036,9 @@ function statusLabel(status, labels) {
4883
5036
  }
4884
5037
  }
4885
5038
  }
5039
+ function progressLabel(job) {
5040
+ return `${job.percent}% \xB7 ${formatUploadBytes(job.loaded)} / ${formatUploadBytes(job.total)}`;
5041
+ }
4886
5042
  function FileUploadTray({
4887
5043
  jobs,
4888
5044
  labels: labelOverrides,
@@ -4905,16 +5061,18 @@ function FileUploadTray({
4905
5061
  const canResume = job.status === "paused";
4906
5062
  const canCancel = job.status === "uploading" || job.status === "paused" || job.status === "error";
4907
5063
  return /* @__PURE__ */ jsxRuntime.jsxs("li", { className: "grid min-w-0 gap-1.5", children: [
5064
+ /* @__PURE__ */ jsxRuntime.jsx(
5065
+ "p",
5066
+ {
5067
+ className: "h-[3lh] min-w-0 line-clamp-3 text-sm leading-5 font-medium break-all",
5068
+ title: job.name,
5069
+ children: job.name
5070
+ }
5071
+ ),
4908
5072
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex min-w-0 items-center gap-2", children: [
4909
- /* @__PURE__ */ jsxRuntime.jsx(
4910
- "p",
4911
- {
4912
- className: "min-w-0 flex-1 truncate text-sm font-medium",
4913
- title: job.name,
4914
- children: job.name
4915
- }
4916
- ),
4917
5073
  /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-muted-foreground shrink-0 text-xs", children: statusLabel(job.status, labels) }),
5074
+ /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-muted-foreground shrink-0 text-xs tabular-nums", children: progressLabel(job) }),
5075
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "min-w-0 flex-1" }),
4918
5076
  canPause ? /* @__PURE__ */ jsxRuntime.jsx(
4919
5077
  Button,
4920
5078
  {
@@ -5125,10 +5283,19 @@ function createMultipartUploadController(options) {
5125
5283
  const ranges = partRanges(options.file.size, options.partSize);
5126
5284
  const sleep = options.sleep ?? defaultSleep;
5127
5285
  const state = { status: "idle" };
5128
- const completed = [];
5286
+ const seeded = /* @__PURE__ */ new Map();
5287
+ for (const part of options.completedParts ?? []) {
5288
+ if (part.partNumber < 1 || part.partNumber > ranges.length) continue;
5289
+ seeded.set(part.partNumber, part);
5290
+ }
5291
+ const completed = [...seeded.values()].sort(
5292
+ (left, right) => left.partNumber - right.partNumber
5293
+ );
5294
+ const done = new Set(completed.map((part) => part.partNumber));
5129
5295
  let currentXhr = null;
5130
5296
  let run = null;
5131
- let resumeIndex = 0;
5297
+ let resumeIndex = ranges.findIndex((range) => !done.has(range.partNumber));
5298
+ if (resumeIndex < 0) resumeIndex = ranges.length;
5132
5299
  function currentStatus() {
5133
5300
  return state.status;
5134
5301
  }
@@ -5186,11 +5353,13 @@ function createMultipartUploadController(options) {
5186
5353
  resumeIndex = index;
5187
5354
  const range = ranges[index];
5188
5355
  if (!range) continue;
5356
+ if (done.has(range.partNumber)) continue;
5189
5357
  const body = options.file.slice(range.start, range.end);
5190
5358
  try {
5191
5359
  const etag = await putPartWithRetry(range.partNumber, body);
5192
5360
  if (state.status !== "uploading") return;
5193
5361
  completed.push({ partNumber: range.partNumber, etag });
5362
+ done.add(range.partNumber);
5194
5363
  emitProgress(0);
5195
5364
  } catch (error) {
5196
5365
  currentXhr = null;
@@ -5207,7 +5376,8 @@ function createMultipartUploadController(options) {
5207
5376
  }
5208
5377
  async function start() {
5209
5378
  if (run) return run;
5210
- run = uploadFrom(0).catch((error) => {
5379
+ emitProgress(0);
5380
+ run = uploadFrom(resumeIndex).catch((error) => {
5211
5381
  if (isStopped()) return;
5212
5382
  state.status = "error";
5213
5383
  throw error;
@@ -5329,6 +5499,7 @@ function avatarFallbackStyle(name) {
5329
5499
 
5330
5500
  exports.AvatarEditor = AvatarEditor;
5331
5501
  exports.AvatarEditorDialog = AvatarEditorDialog;
5502
+ exports.CAPTION_LOCALES = CAPTION_LOCALES;
5332
5503
  exports.ConfirmDestructiveDialog = ConfirmDestructiveDialog;
5333
5504
  exports.CurtainPicker = CurtainPicker;
5334
5505
  exports.CurtainPickerSkeleton = CurtainPickerSkeleton;
@@ -5365,6 +5536,7 @@ exports.ThemeSwitcher = ThemeSwitcher;
5365
5536
  exports.VideoPlayer = VideoPlayer;
5366
5537
  exports.WaitingRoom = WaitingRoom;
5367
5538
  exports.avatarFallbackStyle = avatarFallbackStyle;
5539
+ exports.captionsVttUrlFromPlayback = captionsVttUrlFromPlayback;
5368
5540
  exports.castHlsUrl = castHlsUrl;
5369
5541
  exports.cn = cn;
5370
5542
  exports.createLiveHlsConfig = createLiveHlsConfig;
@@ -5376,6 +5548,7 @@ exports.describePlaybackError = describePlaybackError;
5376
5548
  exports.fileMatchesAccept = fileMatchesAccept;
5377
5549
  exports.getAvatarColors = getAvatarColors;
5378
5550
  exports.getInitials = getInitials;
5551
+ exports.isCaptionLocale = isCaptionLocale;
5379
5552
  exports.loadCastSender = loadCastSender;
5380
5553
  exports.partRanges = partRanges;
5381
5554
  exports.progressFromBytes = progressFromBytes;