@livo-tv/blocks 1.13.0 → 1.14.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
@@ -3898,6 +3898,22 @@ function stabilizeLiveLagMs(previous, next) {
3898
3898
  function isCaptionLocale(value) {
3899
3899
  return CAPTION_LOCALES.includes(value);
3900
3900
  }
3901
+ function isLivePlaylistSrc(src) {
3902
+ return /\/live\//i.test(src);
3903
+ }
3904
+ function zeroAnchorCaptionCues(cues) {
3905
+ if (cues.length === 0) return cues;
3906
+ let origin = Number.POSITIVE_INFINITY;
3907
+ for (const cue of cues) {
3908
+ if (Number.isFinite(cue.start) && cue.start < origin) origin = cue.start;
3909
+ }
3910
+ if (!Number.isFinite(origin) || origin <= 0) return cues;
3911
+ return cues.map((cue) => ({
3912
+ ...cue,
3913
+ start: Math.max(0, cue.start - origin),
3914
+ end: Math.max(0, cue.end - origin)
3915
+ }));
3916
+ }
3901
3917
  function captionsVttUrlFromPlayback(src, locale) {
3902
3918
  const trimmed = src.trim();
3903
3919
  if (!trimmed) return null;
@@ -3945,6 +3961,33 @@ function liveCuesDue(cues, now, lagMs, extraLagMs = LIVE_CUE_EXTRA_LAG_MS) {
3945
3961
  (cue) => Number.isFinite(cue.receivedAt) && now - cue.receivedAt >= threshold
3946
3962
  ).sort((left, right) => left.receivedAt - right.receivedAt);
3947
3963
  }
3964
+ function normalizeLiveCaptionText(text) {
3965
+ return text.trim().replace(/\s+/g, " ").toLowerCase();
3966
+ }
3967
+ function coalesceLiveCaptionCues(cues) {
3968
+ const out = [];
3969
+ for (const cue of cues) {
3970
+ const text = cue.text.trim();
3971
+ if (!text) continue;
3972
+ const last = out[out.length - 1];
3973
+ if (!last) {
3974
+ out.push({ text, receivedAt: cue.receivedAt });
3975
+ continue;
3976
+ }
3977
+ const lastNorm = normalizeLiveCaptionText(last.text);
3978
+ const nextNorm = normalizeLiveCaptionText(text);
3979
+ if (lastNorm === nextNorm) continue;
3980
+ if (nextNorm.startsWith(lastNorm) || lastNorm.startsWith(nextNorm)) {
3981
+ out[out.length - 1] = {
3982
+ text: nextNorm.length >= lastNorm.length ? text : last.text,
3983
+ receivedAt: cue.receivedAt
3984
+ };
3985
+ continue;
3986
+ }
3987
+ out.push({ text, receivedAt: cue.receivedAt });
3988
+ }
3989
+ return out;
3990
+ }
3948
3991
  function mergeLiveCaptionCues(existing, incoming, cap = LIVE_STACK_MAX_CUES) {
3949
3992
  const map = /* @__PURE__ */ new Map();
3950
3993
  for (const cue of existing) {
@@ -3996,27 +4039,40 @@ var NEAR_BOTTOM_PX = 24;
3996
4039
  function LiveCaptionStack({ cues, latestLabel }) {
3997
4040
  const scrollerRef = React9__namespace.useRef(null);
3998
4041
  const followRef = React9__namespace.useRef(true);
4042
+ const ignoreScrollRef = React9__namespace.useRef(false);
3999
4043
  const [following, setFollowing] = React9__namespace.useState(true);
4000
4044
  const prefersReduced = useMediaQuery2("(prefers-reduced-motion: reduce)");
4001
4045
  const lastReceivedAt = cues[cues.length - 1]?.receivedAt;
4002
4046
  const scrollToLatest = React9__namespace.useCallback(() => {
4003
4047
  const scroller = scrollerRef.current;
4004
4048
  if (!scroller) return;
4049
+ ignoreScrollRef.current = true;
4005
4050
  scroller.scrollTo({
4006
4051
  top: scroller.scrollHeight,
4007
4052
  behavior: prefersReduced ? "auto" : "smooth"
4008
4053
  });
4009
4054
  followRef.current = true;
4010
4055
  setFollowing(true);
4056
+ window.requestAnimationFrame(() => {
4057
+ window.requestAnimationFrame(() => {
4058
+ ignoreScrollRef.current = false;
4059
+ });
4060
+ });
4011
4061
  }, [prefersReduced]);
4012
4062
  React9__namespace.useLayoutEffect(() => {
4013
4063
  if (!followRef.current) return;
4014
4064
  const scroller = scrollerRef.current;
4015
4065
  if (!scroller) return;
4066
+ ignoreScrollRef.current = true;
4016
4067
  scroller.scrollTo({
4017
4068
  top: scroller.scrollHeight,
4018
4069
  behavior: prefersReduced ? "auto" : "smooth"
4019
4070
  });
4071
+ window.requestAnimationFrame(() => {
4072
+ window.requestAnimationFrame(() => {
4073
+ ignoreScrollRef.current = false;
4074
+ });
4075
+ });
4020
4076
  }, [lastReceivedAt, prefersReduced, cues.length]);
4021
4077
  if (cues.length === 0) return null;
4022
4078
  return /* @__PURE__ */ jsxRuntime.jsx(
@@ -4040,31 +4096,40 @@ function LiveCaptionStack({ cues, latestLabel }) {
4040
4096
  ref: scrollerRef,
4041
4097
  role: "log",
4042
4098
  "aria-relevant": "additions",
4043
- 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-center text-sm font-medium text-white shadow-sm",
4099
+ 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",
4044
4100
  onScroll: (event) => {
4101
+ if (ignoreScrollRef.current) return;
4045
4102
  const scroller = event.currentTarget;
4046
4103
  const nearBottom = scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight < NEAR_BOTTOM_PX;
4047
4104
  followRef.current = nearBottom;
4048
4105
  setFollowing(nearBottom);
4049
4106
  },
4050
- children: cues.map((cue, index) => {
4051
- const fromEnd = cues.length - 1 - index;
4052
- const opacity = fromEnd === 0 ? 1 : fromEnd === 1 ? 0.75 : 0.45;
4053
- const newest = fromEnd === 0;
4054
- return /* @__PURE__ */ jsxRuntime.jsx(
4055
- "p",
4056
- {
4057
- className: cn(
4058
- "leading-5 break-words",
4059
- !prefersReduced && "transition-opacity"
4060
- ),
4061
- style: { opacity },
4062
- "aria-live": newest ? "polite" : void 0,
4063
- children: cue.text
4064
- },
4065
- cue.receivedAt
4066
- );
4067
- })
4107
+ children: /* @__PURE__ */ jsxRuntime.jsx(
4108
+ "p",
4109
+ {
4110
+ className: cn(
4111
+ "text-justify [text-align-last:left] leading-5 break-words",
4112
+ !prefersReduced && "transition-opacity"
4113
+ ),
4114
+ children: cues.map((cue, index) => {
4115
+ const fromEnd = cues.length - 1 - index;
4116
+ const opacity = fromEnd === 0 ? 1 : fromEnd === 1 ? 0.75 : 0.45;
4117
+ const newest = fromEnd === 0;
4118
+ return /* @__PURE__ */ jsxRuntime.jsxs(
4119
+ "span",
4120
+ {
4121
+ style: { opacity },
4122
+ "aria-live": newest ? "polite" : void 0,
4123
+ children: [
4124
+ index > 0 ? " " : "",
4125
+ cue.text
4126
+ ]
4127
+ },
4128
+ cue.receivedAt
4129
+ );
4130
+ })
4131
+ }
4132
+ )
4068
4133
  }
4069
4134
  )
4070
4135
  ] })
@@ -4343,7 +4408,8 @@ function VideoPlayer({
4343
4408
  return;
4344
4409
  }
4345
4410
  const cues = await loadCaptionVtt(url);
4346
- if (!cancelled) setCaptionCues(cues);
4411
+ const next = isLivePlaylistSrc(src) ? zeroAnchorCaptionCues(cues) : cues;
4412
+ if (!cancelled) setCaptionCues(next);
4347
4413
  };
4348
4414
  void load();
4349
4415
  if (!liveCaptionsBaseUrl) return;
@@ -4353,7 +4419,9 @@ function VideoPlayer({
4353
4419
  window.clearInterval(timer);
4354
4420
  };
4355
4421
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4356
- const liveDueCues = liveCaptionsBaseUrl ? liveCuesDue(liveCaptionCues, Date.now(), liveLagMs) : [];
4422
+ const liveDueCues = liveCaptionsBaseUrl ? coalesceLiveCaptionCues(
4423
+ liveCuesDue(liveCaptionCues, Date.now(), liveLagMs)
4424
+ ) : [];
4357
4425
  const activeCaption = liveCaptionsBaseUrl ? null : captionAtTime(captionCues, currentTime);
4358
4426
  function togglePlay() {
4359
4427
  const video = videoRef.current;