@livo-tv/blocks 1.13.0 → 1.15.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
@@ -3872,6 +3872,22 @@ function stabilizeLiveLagMs(previous, next) {
3872
3872
  function isCaptionLocale(value) {
3873
3873
  return CAPTION_LOCALES.includes(value);
3874
3874
  }
3875
+ function isLivePlaylistSrc(src) {
3876
+ return /\/live\//i.test(src);
3877
+ }
3878
+ function zeroAnchorCaptionCues(cues) {
3879
+ if (cues.length === 0) return cues;
3880
+ let origin = Number.POSITIVE_INFINITY;
3881
+ for (const cue of cues) {
3882
+ if (Number.isFinite(cue.start) && cue.start < origin) origin = cue.start;
3883
+ }
3884
+ if (!Number.isFinite(origin) || origin <= 0) return cues;
3885
+ return cues.map((cue) => ({
3886
+ ...cue,
3887
+ start: Math.max(0, cue.start - origin),
3888
+ end: Math.max(0, cue.end - origin)
3889
+ }));
3890
+ }
3875
3891
  function captionsVttUrlFromPlayback(src, locale) {
3876
3892
  const trimmed = src.trim();
3877
3893
  if (!trimmed) return null;
@@ -3919,6 +3935,71 @@ function liveCuesDue(cues, now, lagMs, extraLagMs = LIVE_CUE_EXTRA_LAG_MS) {
3919
3935
  (cue) => Number.isFinite(cue.receivedAt) && now - cue.receivedAt >= threshold
3920
3936
  ).sort((left, right) => left.receivedAt - right.receivedAt);
3921
3937
  }
3938
+ var LIVE_OVERLAP_MIN_WORDS = 4;
3939
+ function normalizeLiveCaptionText(text) {
3940
+ return text.trim().replace(/\s+/g, " ").toLowerCase();
3941
+ }
3942
+ function lastSubseqIndex(haystack, needle) {
3943
+ if (needle.length === 0 || needle.length > haystack.length) return -1;
3944
+ let found = -1;
3945
+ for (let index = 0; index <= haystack.length - needle.length; index += 1) {
3946
+ let match = true;
3947
+ for (let offset = 0; offset < needle.length; offset += 1) {
3948
+ if (haystack[index + offset] !== needle[offset]) {
3949
+ match = false;
3950
+ break;
3951
+ }
3952
+ }
3953
+ if (match) found = index;
3954
+ }
3955
+ return found;
3956
+ }
3957
+ function remainderAfterOverlap(previous, next) {
3958
+ const prevWords = normalizeLiveCaptionText(previous).split(" ").filter(Boolean);
3959
+ const nextWords = next.trim().replace(/\s+/g, " ").split(" ").filter(Boolean);
3960
+ const nextNorm = nextWords.map((word) => word.toLowerCase());
3961
+ const maxN = Math.min(prevWords.length, nextNorm.length);
3962
+ for (let size = maxN; size >= LIVE_OVERLAP_MIN_WORDS; size -= 1) {
3963
+ for (let start = prevWords.length - size; start >= 0; start -= 1) {
3964
+ const gram = prevWords.slice(start, start + size);
3965
+ const index = lastSubseqIndex(nextNorm, gram);
3966
+ if (index !== -1) {
3967
+ return nextWords.slice(index + size).join(" ");
3968
+ }
3969
+ }
3970
+ }
3971
+ return null;
3972
+ }
3973
+ function coalesceLiveCaptionCues(cues) {
3974
+ const out = [];
3975
+ for (const cue of cues) {
3976
+ const text = cue.text.trim();
3977
+ if (!text) continue;
3978
+ const last = out[out.length - 1];
3979
+ if (!last) {
3980
+ out.push({ text, receivedAt: cue.receivedAt });
3981
+ continue;
3982
+ }
3983
+ const lastNorm = normalizeLiveCaptionText(last.text);
3984
+ const nextNorm = normalizeLiveCaptionText(text);
3985
+ if (lastNorm === nextNorm) continue;
3986
+ if (nextNorm.startsWith(lastNorm) || lastNorm.startsWith(nextNorm)) {
3987
+ out[out.length - 1] = {
3988
+ text: nextNorm.length >= lastNorm.length ? text : last.text,
3989
+ receivedAt: cue.receivedAt
3990
+ };
3991
+ continue;
3992
+ }
3993
+ const remainder = remainderAfterOverlap(last.text, text);
3994
+ if (remainder !== null) {
3995
+ if (!remainder) continue;
3996
+ out.push({ text: remainder, receivedAt: cue.receivedAt });
3997
+ continue;
3998
+ }
3999
+ out.push({ text, receivedAt: cue.receivedAt });
4000
+ }
4001
+ return out;
4002
+ }
3922
4003
  function mergeLiveCaptionCues(existing, incoming, cap = LIVE_STACK_MAX_CUES) {
3923
4004
  const map = /* @__PURE__ */ new Map();
3924
4005
  for (const cue of existing) {
@@ -3970,27 +4051,40 @@ var NEAR_BOTTOM_PX = 24;
3970
4051
  function LiveCaptionStack({ cues, latestLabel }) {
3971
4052
  const scrollerRef = React9.useRef(null);
3972
4053
  const followRef = React9.useRef(true);
4054
+ const ignoreScrollRef = React9.useRef(false);
3973
4055
  const [following, setFollowing] = React9.useState(true);
3974
4056
  const prefersReduced = useMediaQuery2("(prefers-reduced-motion: reduce)");
3975
4057
  const lastReceivedAt = cues[cues.length - 1]?.receivedAt;
3976
4058
  const scrollToLatest = React9.useCallback(() => {
3977
4059
  const scroller = scrollerRef.current;
3978
4060
  if (!scroller) return;
4061
+ ignoreScrollRef.current = true;
3979
4062
  scroller.scrollTo({
3980
4063
  top: scroller.scrollHeight,
3981
4064
  behavior: prefersReduced ? "auto" : "smooth"
3982
4065
  });
3983
4066
  followRef.current = true;
3984
4067
  setFollowing(true);
4068
+ window.requestAnimationFrame(() => {
4069
+ window.requestAnimationFrame(() => {
4070
+ ignoreScrollRef.current = false;
4071
+ });
4072
+ });
3985
4073
  }, [prefersReduced]);
3986
4074
  React9.useLayoutEffect(() => {
3987
4075
  if (!followRef.current) return;
3988
4076
  const scroller = scrollerRef.current;
3989
4077
  if (!scroller) return;
4078
+ ignoreScrollRef.current = true;
3990
4079
  scroller.scrollTo({
3991
4080
  top: scroller.scrollHeight,
3992
4081
  behavior: prefersReduced ? "auto" : "smooth"
3993
4082
  });
4083
+ window.requestAnimationFrame(() => {
4084
+ window.requestAnimationFrame(() => {
4085
+ ignoreScrollRef.current = false;
4086
+ });
4087
+ });
3994
4088
  }, [lastReceivedAt, prefersReduced, cues.length]);
3995
4089
  if (cues.length === 0) return null;
3996
4090
  return /* @__PURE__ */ jsx(
@@ -4014,31 +4108,40 @@ function LiveCaptionStack({ cues, latestLabel }) {
4014
4108
  ref: scrollerRef,
4015
4109
  role: "log",
4016
4110
  "aria-relevant": "additions",
4017
- 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",
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",
4018
4112
  onScroll: (event) => {
4113
+ if (ignoreScrollRef.current) return;
4019
4114
  const scroller = event.currentTarget;
4020
4115
  const nearBottom = scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight < NEAR_BOTTOM_PX;
4021
4116
  followRef.current = nearBottom;
4022
4117
  setFollowing(nearBottom);
4023
4118
  },
4024
- children: cues.map((cue, index) => {
4025
- const fromEnd = cues.length - 1 - index;
4026
- const opacity = fromEnd === 0 ? 1 : fromEnd === 1 ? 0.75 : 0.45;
4027
- const newest = fromEnd === 0;
4028
- return /* @__PURE__ */ jsx(
4029
- "p",
4030
- {
4031
- className: cn(
4032
- "leading-5 break-words",
4033
- !prefersReduced && "transition-opacity"
4034
- ),
4035
- style: { opacity },
4036
- "aria-live": newest ? "polite" : void 0,
4037
- children: cue.text
4038
- },
4039
- cue.receivedAt
4040
- );
4041
- })
4119
+ children: /* @__PURE__ */ jsx(
4120
+ "p",
4121
+ {
4122
+ className: cn(
4123
+ "text-justify [text-align-last:left] leading-5 break-words",
4124
+ !prefersReduced && "transition-opacity"
4125
+ ),
4126
+ children: cues.map((cue, index) => {
4127
+ const fromEnd = cues.length - 1 - index;
4128
+ const opacity = fromEnd === 0 ? 1 : fromEnd === 1 ? 0.75 : 0.45;
4129
+ const newest = fromEnd === 0;
4130
+ return /* @__PURE__ */ jsxs(
4131
+ "span",
4132
+ {
4133
+ style: { opacity },
4134
+ "aria-live": newest ? "polite" : void 0,
4135
+ children: [
4136
+ index > 0 ? " " : "",
4137
+ cue.text
4138
+ ]
4139
+ },
4140
+ cue.receivedAt
4141
+ );
4142
+ })
4143
+ }
4144
+ )
4042
4145
  }
4043
4146
  )
4044
4147
  ] })
@@ -4317,7 +4420,8 @@ function VideoPlayer({
4317
4420
  return;
4318
4421
  }
4319
4422
  const cues = await loadCaptionVtt(url);
4320
- if (!cancelled) setCaptionCues(cues);
4423
+ const next = isLivePlaylistSrc(src) ? zeroAnchorCaptionCues(cues) : cues;
4424
+ if (!cancelled) setCaptionCues(next);
4321
4425
  };
4322
4426
  void load();
4323
4427
  if (!liveCaptionsBaseUrl) return;
@@ -4327,7 +4431,9 @@ function VideoPlayer({
4327
4431
  window.clearInterval(timer);
4328
4432
  };
4329
4433
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4330
- const liveDueCues = liveCaptionsBaseUrl ? liveCuesDue(liveCaptionCues, Date.now(), liveLagMs) : [];
4434
+ const liveDueCues = liveCaptionsBaseUrl ? coalesceLiveCaptionCues(
4435
+ liveCuesDue(liveCaptionCues, Date.now(), liveLagMs)
4436
+ ) : [];
4331
4437
  const activeCaption = liveCaptionsBaseUrl ? null : captionAtTime(captionCues, currentTime);
4332
4438
  function togglePlay() {
4333
4439
  const video = videoRef.current;