@braccato/core 1.21.4 → 1.21.5

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,16 +1,6 @@
1
1
  import type { AnimationEngineInstance } from "./instance.js";
2
2
  import { type LineScrollItem } from "./lineScroll.js";
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;
3
+ export declare function setupLineCullObserver(engine: AnimationEngineInstance): void;
14
4
  export declare function clearOffscreenLineCulling(engine: AnimationEngineInstance): void;
15
5
  export declare function updateVisibleLyricWillChange(engine: AnimationEngineInstance, lines: LineScrollItem[], fromScrollTop: number, toScrollTop: number, viewportHeight: number): void;
16
6
  export declare function clearVisibleLyricWillChange(engine: AnimationEngineInstance): void;
@@ -1,106 +1,41 @@
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
- // 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)
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)
15
12
  return;
16
13
  // The engine owns scrollTop; Firefox's scroll anchoring would answer cull height shifts with a scroll the
17
14
  // engine reads as the user's.
18
- scroller.style.setProperty("overflow-anchor", "none");
15
+ engine.host.getScrollElement()?.style.setProperty("overflow-anchor", "none");
19
16
  pinCulledLineHeights(engine);
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;
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
+ }
85
29
  }
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;
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;
100
34
  }
101
35
  /** A line that started while it was skipped starts over now that its words have boxes to place on. */
102
- function restartUnplacedLine(line) {
103
- if (hasUnplacedContextSweeps(line))
36
+ function restartUnplacedLine(engine, element) {
37
+ const line = engine.lines.find(candidate => candidate.lyricElement === element);
38
+ if (line && hasUnplacedContextSweeps(line))
104
39
  resetLineAnimationState(line);
105
40
  }
106
41
  export function clearOffscreenLineCulling(engine) {
@@ -6,7 +6,6 @@ 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";
10
9
  /**
11
10
  * Everything that belongs to a single rendered lyrics view: one scroll container, one selection,
12
11
  * one set of pending scroll work.
@@ -66,7 +65,7 @@ export interface AnimationEngineInstance extends AnimEngineViewState {
66
65
  visibleWillChangeElements: Set<HTMLElement>;
67
66
  culledLineElements: Set<HTMLElement>;
68
67
  waveAnimationPool: Animation[];
69
- lineCulling: LineCulling | null;
68
+ lineCullObserver: IntersectionObserver | null;
70
69
  cachedDurations: Map<string, number>;
71
70
  cachedCSSValues: Map<string, string>;
72
71
  cachedAnimationSettings: AnimationSettings | null;
@@ -1,7 +1,6 @@
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";
5
4
  import { liveEngines } from "./liveViews.js";
6
5
  import { stopPassiveScrollLoop } from "./passiveScroll.js";
7
6
  import { cancelLyricPositionUpdate } from "./tick.js";
@@ -50,7 +49,7 @@ export function createAnimationEngineInstance(engineDocument, engineWindow, host
50
49
  visibleWillChangeElements: new Set(),
51
50
  culledLineElements: new Set(),
52
51
  waveAnimationPool: [],
53
- lineCulling: null,
52
+ lineCullObserver: null,
54
53
  cachedDurations: new Map(),
55
54
  cachedCSSValues: new Map(),
56
55
  cachedAnimationSettings: null,
@@ -78,7 +77,8 @@ export function createAnimationEngineInstance(engineDocument, engineWindow, host
78
77
  engine.tabRendererResizeObserver?.disconnect();
79
78
  engine.tabRendererResizeObserver = null;
80
79
  engine.observedTabRenderer = null;
81
- stopLineCulling(engine);
80
+ engine.lineCullObserver?.disconnect();
81
+ engine.lineCullObserver = null;
82
82
  stopPassiveScrollLoop(engine);
83
83
  cancelLyricPositionUpdate(engine);
84
84
  },
@@ -1,5 +1,5 @@
1
1
  import { ANIMATING_CLASS, CURRENT_LYRICS_CLASS } from "../constants.js";
2
- import { clearVisibleLyricWillChange, stopLineCulling } from "./culling.js";
2
+ import { clearOffscreenLineCulling, clearVisibleLyricWillChange } 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,7 +28,9 @@ export function clearLyrics(engine) {
28
28
  dropPendingLineScroll(engine);
29
29
  stopAllElementMotion(engine);
30
30
  clearVisibleLyricWillChange(engine);
31
- stopLineCulling(engine);
31
+ engine.lineCullObserver?.disconnect();
32
+ engine.lineCullObserver = null;
33
+ clearOffscreenLineCulling(engine);
32
34
  for (const line of engine.lines) {
33
35
  resetLineAnimationState(line);
34
36
  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, setupLineCulling, updateLineCulling, updateVisibleLyricWillChange, } from "./culling.js";
8
+ import { clearOffscreenLineCulling, clearVisibleLyricWillChange, setupLineCullObserver, 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,15 +101,9 @@ function renderSyncedLyrics(engine, lyricsElement, playhead, options, now) {
101
101
  clearVisibleLyricWillChange(engine);
102
102
  return;
103
103
  }
104
- if (scroller) {
104
+ if (scroller && config.enabled.scroll) {
105
105
  const pendingScrollTop = engine.pendingLineScroll?.toScrollTop ?? scroller.scrollTop;
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
- }
106
+ updateVisibleLyricWillChange(engine, engine.lines, scroller.scrollTop, pendingScrollTop, scroller.height);
113
107
  }
114
108
  else {
115
109
  stopLineScrolling(engine);
@@ -216,7 +210,7 @@ export function relayout(engine, linesAreRendered) {
216
210
  const restarted = restartContextSweeps(engine);
217
211
  engine.cachedCreditsItem = measureTrailingItem(lyricsElement, CREDITS_CLASS);
218
212
  engine.cachedFooterItem = measureTrailingItem(lyricsElement, FOOTER_CLASS);
219
- setupLineCulling(engine);
213
+ setupLineCullObserver(engine);
220
214
  requestRescroll(engine);
221
215
  engine.host.debug?.resize();
222
216
  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 { setupLineCulling } from "./engine/culling.js";
20
+ import { setupLineCullObserver } 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
- setupLineCulling(engine);
234
+ setupLineCullObserver(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.4",
3
+ "version": "1.21.5",
4
4
  "description": "Synchronized lyrics renderer with word-by-word animations",
5
5
  "type": "module",
6
6
  "license": "MIT",