@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.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,71 @@ 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
+ var LIVE_OVERLAP_MIN_WORDS = 4;
3965
+ function normalizeLiveCaptionText(text) {
3966
+ return text.trim().replace(/\s+/g, " ").toLowerCase();
3967
+ }
3968
+ function lastSubseqIndex(haystack, needle) {
3969
+ if (needle.length === 0 || needle.length > haystack.length) return -1;
3970
+ let found = -1;
3971
+ for (let index = 0; index <= haystack.length - needle.length; index += 1) {
3972
+ let match = true;
3973
+ for (let offset = 0; offset < needle.length; offset += 1) {
3974
+ if (haystack[index + offset] !== needle[offset]) {
3975
+ match = false;
3976
+ break;
3977
+ }
3978
+ }
3979
+ if (match) found = index;
3980
+ }
3981
+ return found;
3982
+ }
3983
+ function remainderAfterOverlap(previous, next) {
3984
+ const prevWords = normalizeLiveCaptionText(previous).split(" ").filter(Boolean);
3985
+ const nextWords = next.trim().replace(/\s+/g, " ").split(" ").filter(Boolean);
3986
+ const nextNorm = nextWords.map((word) => word.toLowerCase());
3987
+ const maxN = Math.min(prevWords.length, nextNorm.length);
3988
+ for (let size = maxN; size >= LIVE_OVERLAP_MIN_WORDS; size -= 1) {
3989
+ for (let start = prevWords.length - size; start >= 0; start -= 1) {
3990
+ const gram = prevWords.slice(start, start + size);
3991
+ const index = lastSubseqIndex(nextNorm, gram);
3992
+ if (index !== -1) {
3993
+ return nextWords.slice(index + size).join(" ");
3994
+ }
3995
+ }
3996
+ }
3997
+ return null;
3998
+ }
3999
+ function coalesceLiveCaptionCues(cues) {
4000
+ const out = [];
4001
+ for (const cue of cues) {
4002
+ const text = cue.text.trim();
4003
+ if (!text) continue;
4004
+ const last = out[out.length - 1];
4005
+ if (!last) {
4006
+ out.push({ text, receivedAt: cue.receivedAt });
4007
+ continue;
4008
+ }
4009
+ const lastNorm = normalizeLiveCaptionText(last.text);
4010
+ const nextNorm = normalizeLiveCaptionText(text);
4011
+ if (lastNorm === nextNorm) continue;
4012
+ if (nextNorm.startsWith(lastNorm) || lastNorm.startsWith(nextNorm)) {
4013
+ out[out.length - 1] = {
4014
+ text: nextNorm.length >= lastNorm.length ? text : last.text,
4015
+ receivedAt: cue.receivedAt
4016
+ };
4017
+ continue;
4018
+ }
4019
+ const remainder = remainderAfterOverlap(last.text, text);
4020
+ if (remainder !== null) {
4021
+ if (!remainder) continue;
4022
+ out.push({ text: remainder, receivedAt: cue.receivedAt });
4023
+ continue;
4024
+ }
4025
+ out.push({ text, receivedAt: cue.receivedAt });
4026
+ }
4027
+ return out;
4028
+ }
3948
4029
  function mergeLiveCaptionCues(existing, incoming, cap = LIVE_STACK_MAX_CUES) {
3949
4030
  const map = /* @__PURE__ */ new Map();
3950
4031
  for (const cue of existing) {
@@ -3996,27 +4077,40 @@ var NEAR_BOTTOM_PX = 24;
3996
4077
  function LiveCaptionStack({ cues, latestLabel }) {
3997
4078
  const scrollerRef = React9__namespace.useRef(null);
3998
4079
  const followRef = React9__namespace.useRef(true);
4080
+ const ignoreScrollRef = React9__namespace.useRef(false);
3999
4081
  const [following, setFollowing] = React9__namespace.useState(true);
4000
4082
  const prefersReduced = useMediaQuery2("(prefers-reduced-motion: reduce)");
4001
4083
  const lastReceivedAt = cues[cues.length - 1]?.receivedAt;
4002
4084
  const scrollToLatest = React9__namespace.useCallback(() => {
4003
4085
  const scroller = scrollerRef.current;
4004
4086
  if (!scroller) return;
4087
+ ignoreScrollRef.current = true;
4005
4088
  scroller.scrollTo({
4006
4089
  top: scroller.scrollHeight,
4007
4090
  behavior: prefersReduced ? "auto" : "smooth"
4008
4091
  });
4009
4092
  followRef.current = true;
4010
4093
  setFollowing(true);
4094
+ window.requestAnimationFrame(() => {
4095
+ window.requestAnimationFrame(() => {
4096
+ ignoreScrollRef.current = false;
4097
+ });
4098
+ });
4011
4099
  }, [prefersReduced]);
4012
4100
  React9__namespace.useLayoutEffect(() => {
4013
4101
  if (!followRef.current) return;
4014
4102
  const scroller = scrollerRef.current;
4015
4103
  if (!scroller) return;
4104
+ ignoreScrollRef.current = true;
4016
4105
  scroller.scrollTo({
4017
4106
  top: scroller.scrollHeight,
4018
4107
  behavior: prefersReduced ? "auto" : "smooth"
4019
4108
  });
4109
+ window.requestAnimationFrame(() => {
4110
+ window.requestAnimationFrame(() => {
4111
+ ignoreScrollRef.current = false;
4112
+ });
4113
+ });
4020
4114
  }, [lastReceivedAt, prefersReduced, cues.length]);
4021
4115
  if (cues.length === 0) return null;
4022
4116
  return /* @__PURE__ */ jsxRuntime.jsx(
@@ -4040,31 +4134,40 @@ function LiveCaptionStack({ cues, latestLabel }) {
4040
4134
  ref: scrollerRef,
4041
4135
  role: "log",
4042
4136
  "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",
4137
+ 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
4138
  onScroll: (event) => {
4139
+ if (ignoreScrollRef.current) return;
4045
4140
  const scroller = event.currentTarget;
4046
4141
  const nearBottom = scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight < NEAR_BOTTOM_PX;
4047
4142
  followRef.current = nearBottom;
4048
4143
  setFollowing(nearBottom);
4049
4144
  },
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
- })
4145
+ children: /* @__PURE__ */ jsxRuntime.jsx(
4146
+ "p",
4147
+ {
4148
+ className: cn(
4149
+ "text-justify [text-align-last:left] leading-5 break-words",
4150
+ !prefersReduced && "transition-opacity"
4151
+ ),
4152
+ children: cues.map((cue, index) => {
4153
+ const fromEnd = cues.length - 1 - index;
4154
+ const opacity = fromEnd === 0 ? 1 : fromEnd === 1 ? 0.75 : 0.45;
4155
+ const newest = fromEnd === 0;
4156
+ return /* @__PURE__ */ jsxRuntime.jsxs(
4157
+ "span",
4158
+ {
4159
+ style: { opacity },
4160
+ "aria-live": newest ? "polite" : void 0,
4161
+ children: [
4162
+ index > 0 ? " " : "",
4163
+ cue.text
4164
+ ]
4165
+ },
4166
+ cue.receivedAt
4167
+ );
4168
+ })
4169
+ }
4170
+ )
4068
4171
  }
4069
4172
  )
4070
4173
  ] })
@@ -4343,7 +4446,8 @@ function VideoPlayer({
4343
4446
  return;
4344
4447
  }
4345
4448
  const cues = await loadCaptionVtt(url);
4346
- if (!cancelled) setCaptionCues(cues);
4449
+ const next = isLivePlaylistSrc(src) ? zeroAnchorCaptionCues(cues) : cues;
4450
+ if (!cancelled) setCaptionCues(next);
4347
4451
  };
4348
4452
  void load();
4349
4453
  if (!liveCaptionsBaseUrl) return;
@@ -4353,7 +4457,9 @@ function VideoPlayer({
4353
4457
  window.clearInterval(timer);
4354
4458
  };
4355
4459
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4356
- const liveDueCues = liveCaptionsBaseUrl ? liveCuesDue(liveCaptionCues, Date.now(), liveLagMs) : [];
4460
+ const liveDueCues = liveCaptionsBaseUrl ? coalesceLiveCaptionCues(
4461
+ liveCuesDue(liveCaptionCues, Date.now(), liveLagMs)
4462
+ ) : [];
4357
4463
  const activeCaption = liveCaptionsBaseUrl ? null : captionAtTime(captionCues, currentTime);
4358
4464
  function togglePlay() {
4359
4465
  const video = videoRef.current;