@braccato/core 1.21.0 → 1.21.2

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.
@@ -1,6 +1,16 @@
1
1
  import type { AnimationEngineInstance } from "./instance.js";
2
2
  import { type LineScrollItem } from "./lineScroll.js";
3
- export declare function setupLineCullObserver(engine: AnimationEngineInstance): void;
3
+ export interface LineCulling {
4
+ scroller: HTMLElement;
5
+ contentOffset: number;
6
+ followReaderScroll: () => void;
7
+ startObserver: IntersectionObserver | null;
8
+ ready: boolean;
9
+ }
10
+ export declare function setupLineCulling(engine: AnimationEngineInstance): void;
11
+ export declare function stopLineCulling(engine: AnimationEngineInstance): void;
12
+ export declare function updateLineCulling(engine: AnimationEngineInstance, fromScrollTop: number, toScrollTop: number, viewportHeight: number): void;
13
+ export declare function measureContentOffset(scroller: HTMLElement, container: HTMLElement): number;
4
14
  export declare function clearOffscreenLineCulling(engine: AnimationEngineInstance): void;
5
15
  export declare function updateVisibleLyricWillChange(engine: AnimationEngineInstance, lines: LineScrollItem[], fromScrollTop: number, toScrollTop: number, viewportHeight: number): void;
6
16
  export declare function clearVisibleLyricWillChange(engine: AnimationEngineInstance): void;
@@ -1,41 +1,106 @@
1
1
  import { hasUnplacedContextSweeps } from "./contextSweep.js";
2
2
  import { resetLineAnimationState } from "./lineAnimationState.js";
3
3
  import { isLineVisibleDuringScroll, LINE_SCROLL_WILL_CHANGE_VALUE } from "./lineScroll.js";
4
- // Use screen intersections because transformed scrolling decouples layout coordinates from visibility.
5
- const ONE_VIEWPORT_OVERSCAN = "100% 0px 100% 0px";
6
- export function setupLineCullObserver(engine) {
7
- engine.lineCullObserver?.disconnect();
8
- engine.lineCullObserver = null;
9
- clearOffscreenLineCulling(engine);
10
- const Observer = engine.window.IntersectionObserver;
11
- if (engine.layout === "stage" || typeof Observer !== "function" || engine.lines.length === 0)
4
+ // A skipped line keeps the height it last rendered at, so none is skipped before two rendering updates
5
+ // have laid every line out. An intersection observation is delivered as its own task after one has, so starting
6
+ // from it never adds a resize observation to a rendering update that is already delivering them.
7
+ const RENDERING_UPDATES_BEFORE_SKIPPING = 2;
8
+ export function setupLineCulling(engine) {
9
+ stopLineCulling(engine);
10
+ const container = engine.lyricsContainer;
11
+ if (engine.layout === "stage" || !container || engine.lines.length === 0)
12
+ return;
13
+ const scroller = engine.host.getScrollElement();
14
+ if (!scroller?.isConnected)
12
15
  return;
13
16
  // The engine owns scrollTop; Firefox's scroll anchoring would answer cull height shifts with a scroll the
14
17
  // engine reads as the user's.
15
- engine.host.getScrollElement()?.style.setProperty("overflow-anchor", "none");
18
+ scroller.style.setProperty("overflow-anchor", "none");
16
19
  pinCulledLineHeights(engine);
17
- const observer = new Observer(entries => {
18
- for (const entry of entries) {
19
- const element = entry.target;
20
- if (entry.isIntersecting) {
21
- if (engine.culledLineElements.delete(element))
22
- uncullLine(element);
23
- restartUnplacedLine(engine, element);
24
- }
25
- else if (!engine.culledLineElements.has(element)) {
26
- cullLine(element);
27
- engine.culledLineElements.add(element);
28
- }
20
+ const culling = {
21
+ scroller,
22
+ contentOffset: measureContentOffset(scroller, container),
23
+ followReaderScroll: () => cullAroundScroller(engine, culling),
24
+ startObserver: null,
25
+ ready: false,
26
+ };
27
+ engine.lineCulling = culling;
28
+ scroller.addEventListener("scroll", culling.followReaderScroll, { passive: true });
29
+ startCullingAfterRenderingUpdates(engine, culling, container, RENDERING_UPDATES_BEFORE_SKIPPING);
30
+ for (const line of engine.lines) {
31
+ if (!engine.culledLineElements.has(line.lyricElement))
32
+ restartUnplacedLine(line);
33
+ }
34
+ }
35
+ export function stopLineCulling(engine) {
36
+ const culling = engine.lineCulling;
37
+ if (culling) {
38
+ culling.scroller.removeEventListener("scroll", culling.followReaderScroll);
39
+ culling.startObserver?.disconnect();
40
+ }
41
+ engine.lineCulling = null;
42
+ clearOffscreenLineCulling(engine);
43
+ }
44
+ function cullAroundScroller(engine, culling) {
45
+ if (!culling.scroller.isConnected)
46
+ return;
47
+ const scrollTop = culling.scroller.scrollTop;
48
+ const viewportHeight = engine.cachedTabRendererHeight ?? culling.scroller.clientHeight;
49
+ updateLineCulling(engine, scrollTop, engine.pendingLineScroll?.toScrollTop ?? scrollTop, viewportHeight);
50
+ }
51
+ // A line scroll's transforms stay between the two scroll positions; one viewport either side is the margin.
52
+ export function updateLineCulling(engine, fromScrollTop, toScrollTop, viewportHeight) {
53
+ const culling = engine.lineCulling;
54
+ if (!culling?.ready)
55
+ return;
56
+ const top = Math.min(fromScrollTop, toScrollTop) - viewportHeight - culling.contentOffset;
57
+ const bottom = Math.max(fromScrollTop, toScrollTop) + 2 * viewportHeight - culling.contentOffset;
58
+ for (const line of engine.lines) {
59
+ const element = line.lyricElement;
60
+ const rendered = !isMeasured(line) || (line.position + line.height >= top && line.position <= bottom);
61
+ if (rendered) {
62
+ if (!engine.culledLineElements.delete(element))
63
+ continue;
64
+ uncullLine(element);
65
+ restartUnplacedLine(line);
66
+ }
67
+ else if (!engine.culledLineElements.has(element)) {
68
+ cullLine(element);
69
+ engine.culledLineElements.add(element);
70
+ }
71
+ }
72
+ }
73
+ function startCullingAfterRenderingUpdates(engine, culling, container, updates) {
74
+ const Observer = engine.window.IntersectionObserver;
75
+ if (typeof Observer !== "function")
76
+ return;
77
+ const observer = new Observer(() => {
78
+ observer.disconnect();
79
+ culling.startObserver = null;
80
+ if (engine.lineCulling !== culling)
81
+ return;
82
+ if (updates > 1) {
83
+ startCullingAfterRenderingUpdates(engine, culling, container, updates - 1);
84
+ return;
29
85
  }
30
- }, { root: null, rootMargin: ONE_VIEWPORT_OVERSCAN, threshold: 0 });
31
- for (const line of engine.lines)
32
- observer.observe(line.lyricElement);
33
- engine.lineCullObserver = observer;
86
+ culling.ready = true;
87
+ cullAroundScroller(engine, culling);
88
+ });
89
+ culling.startObserver = observer;
90
+ observer.observe(container);
91
+ }
92
+ function isMeasured(line) {
93
+ return line.position >= 0 && line.height >= 0;
94
+ }
95
+ // The document's scrolling element moves with the page, so its content starts at the viewport's top edge.
96
+ export function measureContentOffset(scroller, container) {
97
+ const scrollsDocument = scroller === scroller.ownerDocument?.scrollingElement;
98
+ const scrollerTop = scrollsDocument ? 0 : scroller.getBoundingClientRect().y + scroller.clientTop;
99
+ return container.getBoundingClientRect().y - scrollerTop + scroller.scrollTop;
34
100
  }
35
101
  /** A line that started while it was skipped starts over now that its words have boxes to place on. */
36
- function restartUnplacedLine(engine, element) {
37
- const line = engine.lines.find(candidate => candidate.lyricElement === element);
38
- if (line && hasUnplacedContextSweeps(line))
102
+ function restartUnplacedLine(line) {
103
+ if (hasUnplacedContextSweeps(line))
39
104
  resetLineAnimationState(line);
40
105
  }
41
106
  export function clearOffscreenLineCulling(engine) {
@@ -6,6 +6,7 @@ import type { ElementMotion } from "./elementMotion.js";
6
6
  import type { LineScrollItem, PendingLineScroll, RunningLineScroll } from "./lineScroll.js";
7
7
  import type { LineScrollTiming } from "./lineScrollStyle.js";
8
8
  import type { AnimationSettings } from "./animationConfig.js";
9
+ import { type LineCulling } from "./culling.js";
9
10
  /**
10
11
  * Everything that belongs to a single rendered lyrics view: one scroll container, one selection,
11
12
  * one set of pending scroll work.
@@ -65,7 +66,7 @@ export interface AnimationEngineInstance extends AnimEngineViewState {
65
66
  visibleWillChangeElements: Set<HTMLElement>;
66
67
  culledLineElements: Set<HTMLElement>;
67
68
  waveAnimationPool: Animation[];
68
- lineCullObserver: IntersectionObserver | null;
69
+ lineCulling: LineCulling | null;
69
70
  cachedDurations: Map<string, number>;
70
71
  cachedCSSValues: Map<string, string>;
71
72
  cachedAnimationSettings: AnimationSettings | null;
@@ -1,6 +1,7 @@
1
1
  import { NO_SCROLL_INSETS } from "./scrollViewport.js";
2
2
  import { REDUCED_MOTION_QUERY } from "./motionPreference.js";
3
3
  import { clearStyleCaches } from "./cssValues.js";
4
+ import { stopLineCulling } from "./culling.js";
4
5
  import { liveEngines } from "./liveViews.js";
5
6
  import { stopPassiveScrollLoop } from "./passiveScroll.js";
6
7
  import { cancelLyricPositionUpdate } from "./tick.js";
@@ -49,7 +50,7 @@ export function createAnimationEngineInstance(engineDocument, engineWindow, host
49
50
  visibleWillChangeElements: new Set(),
50
51
  culledLineElements: new Set(),
51
52
  waveAnimationPool: [],
52
- lineCullObserver: null,
53
+ lineCulling: null,
53
54
  cachedDurations: new Map(),
54
55
  cachedCSSValues: new Map(),
55
56
  cachedAnimationSettings: null,
@@ -77,8 +78,7 @@ export function createAnimationEngineInstance(engineDocument, engineWindow, host
77
78
  engine.tabRendererResizeObserver?.disconnect();
78
79
  engine.tabRendererResizeObserver = null;
79
80
  engine.observedTabRenderer = null;
80
- engine.lineCullObserver?.disconnect();
81
- engine.lineCullObserver = null;
81
+ stopLineCulling(engine);
82
82
  stopPassiveScrollLoop(engine);
83
83
  cancelLyricPositionUpdate(engine);
84
84
  },
@@ -23,7 +23,7 @@ function resetLineAnimations(lineData) {
23
23
  resetPartAnimations(part);
24
24
  }
25
25
  export function hasLineAnimations(lineData) {
26
- return [lineData, ...lineData.parts].some(part => part.animations.length > 0);
26
+ return lineData.animations.length > 0 || lineData.parts.some(part => part.animations.length > 0);
27
27
  }
28
28
  export function markLineAnimationsStopped(lineData) {
29
29
  lineData.isAnimating = false;
@@ -1,5 +1,5 @@
1
1
  import { ANIMATING_CLASS, CURRENT_LYRICS_CLASS } from "../constants.js";
2
- import { clearOffscreenLineCulling, clearVisibleLyricWillChange } from "./culling.js";
2
+ import { clearVisibleLyricWillChange, stopLineCulling } from "./culling.js";
3
3
  import { DECORATION_MIN_SHIFT_PX, decorationSlideAllowed, slideElementBy } from "./decorationSlides.js";
4
4
  import { stopAllElementMotion, stopElementMotion } from "./elementMotion.js";
5
5
  import { resetLineAnimationState, togglePartClass } from "./lineAnimationState.js";
@@ -28,9 +28,7 @@ export function clearLyrics(engine) {
28
28
  dropPendingLineScroll(engine);
29
29
  stopAllElementMotion(engine);
30
30
  clearVisibleLyricWillChange(engine);
31
- engine.lineCullObserver?.disconnect();
32
- engine.lineCullObserver = null;
33
- clearOffscreenLineCulling(engine);
31
+ stopLineCulling(engine);
34
32
  for (const line of engine.lines) {
35
33
  resetLineAnimationState(line);
36
34
  line.isSelected = false;
@@ -5,7 +5,7 @@ import { applyPlaybackRateToRunningAnimations, correctedScrollTimeS } from "./an
5
5
  import { followFocusedLines, lastSungLine } from "./autoScroll.js";
6
6
  import { advancePlaybackClock, lyricClockShiftS, NO_PLAYER_SNAPSHOT, playbackClock } from "./clock.js";
7
7
  import { getCSSDurationInMs } from "./cssValues.js";
8
- import { clearOffscreenLineCulling, clearVisibleLyricWillChange, setupLineCullObserver, updateVisibleLyricWillChange, } from "./culling.js";
8
+ import { clearOffscreenLineCulling, clearVisibleLyricWillChange, setupLineCulling, updateLineCulling, updateVisibleLyricWillChange, } from "./culling.js";
9
9
  import { captureDecorationSlide, lineDecorators } from "./decorationSlides.js";
10
10
  import { resetLineAnimationState } from "./lineAnimationState.js";
11
11
  import { driveLineAnimations } from "./lineLifecycle.js";
@@ -101,9 +101,15 @@ function renderSyncedLyrics(engine, lyricsElement, playhead, options, now) {
101
101
  clearVisibleLyricWillChange(engine);
102
102
  return;
103
103
  }
104
- if (scroller && config.enabled.scroll) {
104
+ if (scroller) {
105
105
  const pendingScrollTop = engine.pendingLineScroll?.toScrollTop ?? scroller.scrollTop;
106
- updateVisibleLyricWillChange(engine, engine.lines, scroller.scrollTop, pendingScrollTop, scroller.height);
106
+ updateLineCulling(engine, scroller.scrollTop, pendingScrollTop, scroller.height);
107
+ if (config.enabled.scroll) {
108
+ updateVisibleLyricWillChange(engine, engine.lines, scroller.scrollTop, pendingScrollTop, scroller.height);
109
+ }
110
+ else {
111
+ stopLineScrolling(engine);
112
+ }
107
113
  }
108
114
  else {
109
115
  stopLineScrolling(engine);
@@ -210,7 +216,7 @@ export function relayout(engine, linesAreRendered) {
210
216
  const restarted = restartContextSweeps(engine);
211
217
  engine.cachedCreditsItem = measureTrailingItem(lyricsElement, CREDITS_CLASS);
212
218
  engine.cachedFooterItem = measureTrailingItem(lyricsElement, FOOTER_CLASS);
213
- setupLineCullObserver(engine);
219
+ setupLineCulling(engine);
214
220
  requestRescroll(engine);
215
221
  engine.host.debug?.resize();
216
222
  return restarted;
package/dist/view.js CHANGED
@@ -17,7 +17,7 @@ import {} from "./engine/instance.js";
17
17
  import { elementMotionOffsetPx } from "./engine/elementMotion.js";
18
18
  import { replaceRenderedLines } from "./engine/renderedLines.js";
19
19
  import { playerTimeForLyricTime } from "./engine/clock.js";
20
- import { setupLineCullObserver } from "./engine/culling.js";
20
+ import { setupLineCulling } from "./engine/culling.js";
21
21
  import { addSeekHandler, applyDirection, buildLineSyncedParts, createLyricsLine, deriveSyncType, disableRichsync, findNearestAgent, hideCredits, isNearestLyricRtl, moveDecorations, newLineData, } from "./inject.js";
22
22
  import { refreshInstrumentalImages } from "./imageHighlights.js";
23
23
  import { createInstrumentalElement } from "./instrumental.js";
@@ -231,7 +231,7 @@ export function setLyrics(engine, mount, lyrics, options) {
231
231
  // sizes it by.
232
232
  engine.lyricWidth = container.clientWidth;
233
233
  engine.lyricHeight = container.clientHeight;
234
- setupLineCullObserver(engine);
234
+ setupLineCulling(engine);
235
235
  }
236
236
  /**
237
237
  * Pairs each new line with an old one that builds the same, keeping their order, so a kept element is
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@braccato/core",
3
- "version": "1.21.0",
3
+ "version": "1.21.2",
4
4
  "description": "Synchronized lyrics renderer with word-by-word animations",
5
5
  "type": "module",
6
6
  "license": "MIT",