@braccato/core 1.20.0 → 1.21.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.
Files changed (85) hide show
  1. package/README.md +40 -37
  2. package/dist/README.md +40 -37
  3. package/dist/constants.d.ts +10 -0
  4. package/dist/constants.js +10 -0
  5. package/dist/engine/animationConfig.d.ts +83 -0
  6. package/dist/engine/animationConfig.js +93 -0
  7. package/dist/engine/animationDrift.d.ts +11 -0
  8. package/dist/engine/animationDrift.js +43 -0
  9. package/dist/engine/animationTiming.d.ts +26 -0
  10. package/dist/engine/animationTiming.js +120 -0
  11. package/dist/engine/autoScroll.d.ts +24 -0
  12. package/dist/engine/autoScroll.js +103 -0
  13. package/dist/engine/clock.d.ts +29 -0
  14. package/dist/engine/clock.js +70 -0
  15. package/dist/engine/contextSweep.d.ts +10 -0
  16. package/dist/engine/contextSweep.js +102 -0
  17. package/dist/engine/cssValues.d.ts +13 -0
  18. package/dist/engine/cssValues.js +47 -0
  19. package/dist/engine/culling.d.ts +6 -0
  20. package/dist/engine/culling.js +76 -0
  21. package/dist/engine/debugOverlay.d.ts +14 -0
  22. package/dist/engine/debugOverlay.js +58 -0
  23. package/dist/engine/decorationSlides.d.ts +6 -0
  24. package/dist/engine/decorationSlides.js +53 -0
  25. package/dist/engine/elementMotion.d.ts +17 -0
  26. package/dist/engine/elementMotion.js +98 -0
  27. package/dist/engine/glow.d.ts +7 -0
  28. package/dist/engine/glow.js +59 -0
  29. package/dist/engine/instance.d.ts +105 -0
  30. package/dist/engine/instance.js +89 -0
  31. package/dist/engine/instrumentalAnimations.d.ts +7 -0
  32. package/dist/engine/instrumentalAnimations.js +59 -0
  33. package/dist/engine/letterPlans.d.ts +31 -0
  34. package/dist/engine/letterPlans.js +62 -0
  35. package/dist/engine/lineAnimationState.d.ts +10 -0
  36. package/dist/engine/lineAnimationState.js +82 -0
  37. package/dist/engine/lineAnimations.d.ts +5 -0
  38. package/dist/engine/lineAnimations.js +63 -0
  39. package/dist/engine/lineLifecycle.d.ts +12 -0
  40. package/dist/engine/lineLifecycle.js +92 -0
  41. package/dist/engine/lineScroll.d.ts +47 -0
  42. package/dist/engine/lineScroll.js +184 -0
  43. package/dist/engine/lineScrollStyle.d.ts +32 -0
  44. package/dist/engine/lineScrollStyle.js +133 -0
  45. package/dist/engine/liveViews.d.ts +3 -0
  46. package/dist/engine/liveViews.js +5 -0
  47. package/dist/engine/motionCurves.d.ts +19 -0
  48. package/dist/engine/motionCurves.js +85 -0
  49. package/dist/engine/motionPreference.d.ts +1 -0
  50. package/dist/engine/motionPreference.js +1 -0
  51. package/dist/engine/passiveScroll.d.ts +10 -0
  52. package/dist/engine/passiveScroll.js +93 -0
  53. package/dist/engine/renderedLines.d.ts +16 -0
  54. package/dist/engine/renderedLines.js +130 -0
  55. package/dist/engine/scrollViewport.d.ts +36 -0
  56. package/dist/engine/scrollViewport.js +104 -0
  57. package/dist/engine/stageMotion.d.ts +7 -0
  58. package/dist/engine/stageMotion.js +319 -0
  59. package/dist/engine/sweepPlans.d.ts +36 -0
  60. package/dist/engine/sweepPlans.js +59 -0
  61. package/dist/engine/tick.d.ts +16 -0
  62. package/dist/engine/tick.js +270 -0
  63. package/dist/engine/timingLogs.d.ts +7 -0
  64. package/dist/engine/timingLogs.js +81 -0
  65. package/dist/engine/userScroll.d.ts +12 -0
  66. package/dist/engine/userScroll.js +62 -0
  67. package/dist/engine/wavePool.d.ts +7 -0
  68. package/dist/engine/wavePool.js +22 -0
  69. package/dist/engine/wordAnimations.d.ts +5 -0
  70. package/dist/engine/wordAnimations.js +238 -0
  71. package/dist/imageHighlights.d.ts +0 -11
  72. package/dist/imageHighlights.js +2 -48
  73. package/dist/index.d.ts +3 -2
  74. package/dist/index.js +2 -1
  75. package/dist/inject.d.ts +41 -2
  76. package/dist/inject.js +73 -28
  77. package/dist/renderer.js +10 -3
  78. package/dist/styles/image-highlights.css +1 -59
  79. package/dist/styles/lyrics.css +128 -57
  80. package/dist/types.d.ts +1 -1
  81. package/dist/view.d.ts +2 -2
  82. package/dist/view.js +7 -3
  83. package/package.json +1 -1
  84. package/dist/engine.d.ts +0 -471
  85. package/dist/engine.js +0 -3133
@@ -0,0 +1,5 @@
1
+ export const liveEngines = new Set();
2
+ export function forEveryLiveView(runOperation) {
3
+ for (const engine of liveEngines)
4
+ runOperation(engine);
5
+ }
@@ -0,0 +1,19 @@
1
+ export interface MotionCurve {
2
+ startMs: number;
3
+ durationMs: number;
4
+ fromPx: number;
5
+ toPx: number;
6
+ easing: string;
7
+ endEasing: string;
8
+ }
9
+ export type Easing = (progress: number) => number;
10
+ export type EasingLookup = (easing: string) => Easing;
11
+ export interface BakedMotion {
12
+ keyframes: Keyframe[];
13
+ startMs: number;
14
+ durationMs: number;
15
+ }
16
+ export declare function curveEndMs(curve: MotionCurve): number;
17
+ export declare function isRunningAt(curve: MotionCurve, timeMs: number): boolean;
18
+ export declare function offsetAt(curves: readonly MotionCurve[], timeMs: number, easingOf: EasingLookup): number;
19
+ export declare function bakeMotion(curves: readonly MotionCurve[], nowMs: number, easingOf: EasingLookup): BakedMotion;
@@ -0,0 +1,85 @@
1
+ const TOLERANCE_PX = 0.1;
2
+ const LONGEST_STOP_SPAN_MS = 100;
3
+ const SHORTEST_STOP_SPAN_MS = 1;
4
+ export function curveEndMs(curve) {
5
+ return curve.startMs + curve.durationMs;
6
+ }
7
+ export function isRunningAt(curve, timeMs) {
8
+ return curve.startMs <= timeMs && timeMs < curveEndMs(curve);
9
+ }
10
+ export function offsetAt(curves, timeMs, easingOf) {
11
+ return curves
12
+ .filter(curve => isRunningAt(curve, timeMs))
13
+ .reduce((sum, curve) => sum + easedOffset(curve, easingOf(curve.easing), timeMs), 0);
14
+ }
15
+ export function bakeMotion(curves, nowMs, easingOf) {
16
+ if (curves.length === 1)
17
+ return exactly(curves[0]);
18
+ const endsMs = [...new Set(curves.map(curveEndMs))].sort((a, b) => a - b);
19
+ const stops = [];
20
+ let fromMs = nowMs;
21
+ for (const toMs of endsMs) {
22
+ const running = curves
23
+ .filter(curve => curve.startMs <= fromMs && curveEndMs(curve) >= toMs)
24
+ .map(curve => ({ curve, easing: easingOf(curve.easing) }));
25
+ const path = timeMs => {
26
+ let offsetPx = 0;
27
+ for (const { curve, easing } of running)
28
+ offsetPx += easedOffset(curve, easing, timeMs);
29
+ return offsetPx;
30
+ };
31
+ const fromPx = path(fromMs);
32
+ if (stops.at(-1)?.offsetPx !== fromPx)
33
+ stops.push({ timeMs: fromMs, offsetPx: fromPx });
34
+ const spans = Math.ceil((toMs - fromMs) / LONGEST_STOP_SPAN_MS);
35
+ for (let span = 1; span <= spans; span++) {
36
+ const spanEndMs = fromMs + ((toMs - fromMs) * span) / spans;
37
+ appendStops(path, stops[stops.length - 1], { timeMs: spanEndMs, offsetPx: path(spanEndMs) }, stops);
38
+ }
39
+ fromMs = toMs;
40
+ }
41
+ const durationMs = endsMs[endsMs.length - 1] - nowMs;
42
+ const fullScalePx = Math.max(...stops.map(stop => Math.abs(stop.offsetPx))) || 1;
43
+ const easing = `linear(${stops
44
+ .map(({ timeMs, offsetPx }) => `${rounded(offsetPx / fullScalePx)} ${rounded((100 * (timeMs - nowMs)) / durationMs)}%`)
45
+ .join(", ")})`;
46
+ return {
47
+ keyframes: [
48
+ { translate: translateOf(0), easing },
49
+ { translate: translateOf(fullScalePx), easing: "linear" },
50
+ ],
51
+ startMs: nowMs,
52
+ durationMs,
53
+ };
54
+ }
55
+ function appendStops(path, from, to, stops) {
56
+ const middleMs = (from.timeMs + to.timeMs) / 2;
57
+ const middle = { timeMs: middleMs, offsetPx: path(middleMs) };
58
+ const chordPx = (from.offsetPx + to.offsetPx) / 2;
59
+ if (Math.abs(middle.offsetPx - chordPx) <= TOLERANCE_PX || to.timeMs - from.timeMs <= SHORTEST_STOP_SPAN_MS) {
60
+ stops.push(to);
61
+ return;
62
+ }
63
+ appendStops(path, from, middle, stops);
64
+ appendStops(path, middle, to, stops);
65
+ }
66
+ function exactly(curve) {
67
+ return {
68
+ keyframes: [
69
+ { translate: translateOf(curve.fromPx), easing: curve.easing },
70
+ { translate: translateOf(curve.toPx), easing: curve.endEasing },
71
+ ],
72
+ startMs: curve.startMs,
73
+ durationMs: curve.durationMs,
74
+ };
75
+ }
76
+ function easedOffset(curve, easing, timeMs) {
77
+ const progress = curve.durationMs > 0 ? Math.min(1, (timeMs - curve.startMs) / curve.durationMs) : 1;
78
+ return curve.fromPx + (curve.toPx - curve.fromPx) * easing(progress);
79
+ }
80
+ function translateOf(offsetPx) {
81
+ return `0px ${Math.round(offsetPx * 1000) / 1000}px`;
82
+ }
83
+ function rounded(value) {
84
+ return Math.round(value * 1e6) / 1e6;
85
+ }
@@ -0,0 +1 @@
1
+ export declare const REDUCED_MOTION_QUERY = "(prefers-reduced-motion: reduce)";
@@ -0,0 +1 @@
1
+ export const REDUCED_MOTION_QUERY = "(prefers-reduced-motion: reduce)";
@@ -0,0 +1,10 @@
1
+ import type { AnimationEngineInstance } from "./instance.js";
2
+ export declare function hasNoLyricsPlaceholder(engine: AnimationEngineInstance): boolean;
3
+ export declare function hasUnsyncedLyrics(engine: AnimationEngineInstance): boolean;
4
+ export declare function startPassiveScrollLoop(engine: AnimationEngineInstance): void;
5
+ export declare function stopPassiveScrollLoop(engine: AnimationEngineInstance): void;
6
+ /**
7
+ * The drift writes the scroll every frame, and a browser folds that scroll and the reader's into one
8
+ * event, so the drift's own scrolls are told apart by where they land rather than by counting them.
9
+ */
10
+ export declare function isPassiveDriftScroll(engine: AnimationEngineInstance): boolean;
@@ -0,0 +1,93 @@
1
+ import { USER_SCROLLING_CLASS } from "../constants.js";
2
+ import { registerThemeSetting } from "../themeSettings.js";
3
+ import { playbackClock } from "./clock.js";
4
+ const PASSIVE_SCROLL_ENABLED = registerThemeSetting("blyrics-passive-scroll-enabled", true);
5
+ const SECONDS_PER_LINE = registerThemeSetting("blyrics-passive-scroll-seconds-per-line", 3.5);
6
+ const BOTTOM_PAUSE_S = registerThemeSetting("blyrics-passive-scroll-bottom-pause-s", 1.5);
7
+ const RESET_DURATION_S = registerThemeSetting("blyrics-passive-scroll-reset-duration-s", 0.6);
8
+ const TOP_PAUSE_S = registerThemeSetting("blyrics-passive-scroll-top-pause-s", 0.8);
9
+ const MAX_FRAME_STEP_S = 0.5;
10
+ const DRIFT_SCROLL_TOLERANCE_PX = 1;
11
+ export function hasNoLyricsPlaceholder(engine) {
12
+ return engine.lyricsContainer?.dataset.noLyrics === "true";
13
+ }
14
+ export function hasUnsyncedLyrics(engine) {
15
+ return engine.lyricsContainer !== null && engine.syncType === "none" && !hasNoLyricsPlaceholder(engine);
16
+ }
17
+ export function startPassiveScrollLoop(engine) {
18
+ if (engine.passiveRAFId !== null)
19
+ return;
20
+ engine.passiveRAFId = engine.window.requestAnimationFrame(() => passiveScrollFrame(engine));
21
+ }
22
+ export function stopPassiveScrollLoop(engine) {
23
+ if (engine.passiveRAFId === null)
24
+ return;
25
+ engine.window.cancelAnimationFrame(engine.passiveRAFId);
26
+ engine.passiveRAFId = null;
27
+ }
28
+ function passiveScrollFrame(engine) {
29
+ engine.passiveRAFId = null;
30
+ if (!engine.passiveScrollEnabled || !PASSIVE_SCROLL_ENABLED.getBooleanValue() || !hasUnsyncedLyrics(engine))
31
+ return;
32
+ advancePassiveScroll(engine, playbackClock.lastPlayState);
33
+ engine.passiveRAFId = engine.window.requestAnimationFrame(() => passiveScrollFrame(engine));
34
+ }
35
+ function advancePassiveScroll(engine, isPlaying) {
36
+ if (!engine.host.isViewVisible() || engine.host.isLoaderActive())
37
+ return;
38
+ const scroller = engine.host.getScrollElement();
39
+ if (!scroller)
40
+ return;
41
+ const now = Date.now();
42
+ if (engine.passiveLastWallTime > 0 && isPlaying) {
43
+ engine.passiveScrollAccumulatedTime += Math.min((now - engine.passiveLastWallTime) / 1000, MAX_FRAME_STEP_S);
44
+ }
45
+ engine.passiveLastWallTime = now;
46
+ if (engine.scrollResumeTime > now)
47
+ return;
48
+ if (engine.wasUserScrolling)
49
+ resumeFromUserScroll(engine, scroller);
50
+ const scrollDurationS = engine.lines.length * SECONDS_PER_LINE.getNumberValue();
51
+ const maxScrollTop = scroller.scrollHeight - scroller.clientHeight;
52
+ if (engine.lines.length === 0 || maxScrollTop <= 0)
53
+ return;
54
+ const scrollTop = passiveCycleScrollTop(engine.passiveScrollAccumulatedTime, scrollDurationS, maxScrollTop);
55
+ scroller.scrollTop = scrollTop;
56
+ engine.passiveScrollTop = scroller.scrollTop;
57
+ }
58
+ /**
59
+ * The drift writes the scroll every frame, and a browser folds that scroll and the reader's into one
60
+ * event, so the drift's own scrolls are told apart by where they land rather than by counting them.
61
+ */
62
+ export function isPassiveDriftScroll(engine) {
63
+ const scrollTop = engine.host.getScrollElement()?.scrollTop;
64
+ return (engine.passiveScrollTop !== null &&
65
+ scrollTop !== undefined &&
66
+ Math.abs(scrollTop - engine.passiveScrollTop) < DRIFT_SCROLL_TOLERANCE_PX);
67
+ }
68
+ function resumeFromUserScroll(engine, scroller) {
69
+ engine.host.setResumeAffordanceVisible(false);
70
+ engine.lyricsContainer?.classList.remove(USER_SCROLLING_CLASS);
71
+ engine.wasUserScrolling = false;
72
+ const maxScrollTop = scroller.scrollHeight - scroller.clientHeight;
73
+ if (maxScrollTop <= 0)
74
+ return;
75
+ const scrollDurationS = engine.lines.length * SECONDS_PER_LINE.getNumberValue();
76
+ engine.passiveScrollAccumulatedTime = (scroller.scrollTop / maxScrollTop) * scrollDurationS;
77
+ }
78
+ function passiveCycleScrollTop(playedS, scrollDurationS, maxScrollTop) {
79
+ const reachesBottomS = scrollDurationS;
80
+ const leavesBottomS = reachesBottomS + BOTTOM_PAUSE_S.getNumberValue();
81
+ const resetDurationS = RESET_DURATION_S.getNumberValue();
82
+ const reachesTopS = leavesBottomS + resetDurationS;
83
+ const cycleS = playedS % (reachesTopS + TOP_PAUSE_S.getNumberValue());
84
+ if (cycleS < reachesBottomS)
85
+ return (cycleS / scrollDurationS) * maxScrollTop;
86
+ if (cycleS < leavesBottomS)
87
+ return maxScrollTop;
88
+ if (cycleS < reachesTopS) {
89
+ const remaining = 1 - (cycleS - leavesBottomS) / resetDurationS;
90
+ return maxScrollTop * remaining * remaining;
91
+ }
92
+ return 0;
93
+ }
@@ -0,0 +1,16 @@
1
+ import type { LineData } from "../inject.js";
2
+ import type { LyricSyncType } from "../types.js";
3
+ import type { AnimationEngineInstance } from "./instance.js";
4
+ type LinePlacement = {
5
+ position: number;
6
+ height: number;
7
+ motionOffset: number;
8
+ };
9
+ export declare function clearOnScreenLyrics(engine: AnimationEngineInstance): boolean;
10
+ export declare function hasRenderedLines(engine: AnimationEngineInstance): boolean;
11
+ export declare function getRenderedLines(engine: AnimationEngineInstance): LineData[];
12
+ export declare function getRenderedSyncType(engine: AnimationEngineInstance): LyricSyncType;
13
+ export declare function clearLyrics(engine: AnimationEngineInstance): void;
14
+ export declare function replaceRenderedLines(engine: AnimationEngineInstance, lines: LineData[], dropped: readonly LineData[], previousOf: ReadonlyMap<LineData, LineData>): void;
15
+ export declare function settleSwappedLines(engine: AnimationEngineInstance, before: ReadonlyMap<LineData, LinePlacement>, scrollTopBefore: number): void;
16
+ export {};
@@ -0,0 +1,130 @@
1
+ import { ANIMATING_CLASS, CURRENT_LYRICS_CLASS } from "../constants.js";
2
+ import { clearOffscreenLineCulling, clearVisibleLyricWillChange } from "./culling.js";
3
+ import { DECORATION_MIN_SHIFT_PX, decorationSlideAllowed, slideElementBy } from "./decorationSlides.js";
4
+ import { stopAllElementMotion, stopElementMotion } from "./elementMotion.js";
5
+ import { resetLineAnimationState, togglePartClass } from "./lineAnimationState.js";
6
+ import { dropPendingLineScroll } from "./lineScroll.js";
7
+ import { stopPassiveScrollLoop } from "./passiveScroll.js";
8
+ import { replaceStageElements, resetStage } from "./stageMotion.js";
9
+ import { noteEngineScrollEvent } from "./userScroll.js";
10
+ export function clearOnScreenLyrics(engine) {
11
+ if (!engine.lyricsContainer)
12
+ return false;
13
+ engine.lyricsContainer.replaceChildren();
14
+ resetStage(engine);
15
+ return true;
16
+ }
17
+ export function hasRenderedLines(engine) {
18
+ return engine.lines.length > 0;
19
+ }
20
+ export function getRenderedLines(engine) {
21
+ return engine.lines;
22
+ }
23
+ export function getRenderedSyncType(engine) {
24
+ return engine.syncType;
25
+ }
26
+ export function clearLyrics(engine) {
27
+ engine.scrollPos = -1;
28
+ dropPendingLineScroll(engine);
29
+ stopAllElementMotion(engine);
30
+ clearVisibleLyricWillChange(engine);
31
+ engine.lineCullObserver?.disconnect();
32
+ engine.lineCullObserver = null;
33
+ clearOffscreenLineCulling(engine);
34
+ for (const line of engine.lines) {
35
+ resetLineAnimationState(line);
36
+ line.isSelected = false;
37
+ }
38
+ engine.skipScrollsDecayTimes = [];
39
+ engine.lastScrollElements = [];
40
+ engine.pinnedScrollLine = null;
41
+ engine.lastScrollDebugContext.activeElms = [];
42
+ engine.lastScrollDebugContext.centers = [];
43
+ engine.passiveScrollAccumulatedTime = 0;
44
+ engine.passiveLastWallTime = 0;
45
+ engine.passiveScrollTop = null;
46
+ stopPassiveScrollLoop(engine);
47
+ engine.lines = [];
48
+ engine.cachedFooterItem = null;
49
+ engine.cachedCreditsItem = null;
50
+ engine.creditsFocused = false;
51
+ engine.lyricsContainer = null;
52
+ engine.waveAnimationPool.length = 0;
53
+ resetStage(engine);
54
+ }
55
+ export function replaceRenderedLines(engine, lines, dropped, previousOf) {
56
+ for (const [line, previous] of previousOf)
57
+ carrySelection(line, previous);
58
+ dropPendingLineScroll(engine);
59
+ const droppedElements = new Set(dropped.map(line => line.lyricElement));
60
+ for (const element of droppedElements)
61
+ stopElementMotion(engine, element);
62
+ for (const line of dropped) {
63
+ resetLineAnimationState(line);
64
+ line.isSelected = false;
65
+ engine.visibleWillChangeElements.delete(line.lyricElement);
66
+ engine.culledLineElements.delete(line.lyricElement);
67
+ }
68
+ const successorOf = new Map();
69
+ for (const [line, previous] of previousOf)
70
+ successorOf.set(previous, line);
71
+ const carry = (line) => successorOf.get(line) ?? (droppedElements.has(line.lyricElement) ? null : line);
72
+ const carryAll = (from) => from.map(carry).filter((line) => line !== null);
73
+ engine.lastScrollElements = carryAll(engine.lastScrollElements);
74
+ engine.lastScrollDebugContext.activeElms = carryAll(engine.lastScrollDebugContext.activeElms);
75
+ engine.pinnedScrollLine = engine.pinnedScrollLine === null ? null : carry(engine.pinnedScrollLine);
76
+ engine.lines = lines;
77
+ if (engine.layout === "stage")
78
+ replaceStageElements(engine, dropped, previousOf);
79
+ }
80
+ function carrySelection(line, previous) {
81
+ line.isScrolled = previous.isScrolled;
82
+ line.isSelected = previous.isSelected;
83
+ togglePartClass(line, CURRENT_LYRICS_CLASS, previous.lyricElement.classList.contains(CURRENT_LYRICS_CLASS));
84
+ togglePartClass(line, ANIMATING_CLASS, previous.lyricElement.classList.contains(ANIMATING_CLASS));
85
+ }
86
+ export function settleSwappedLines(engine, before, scrollTopBefore) {
87
+ const container = engine.lyricsContainer;
88
+ const scroller = engine.host.getScrollElement();
89
+ if (container === null || scroller === null || engine.layout === "stage")
90
+ return;
91
+ const now = Date.now();
92
+ const followed = followedLine(engine, before, scrollTopBefore, now);
93
+ const followedWas = followed === undefined ? undefined : before.get(followed)?.position;
94
+ if (followed !== undefined && followedWas !== undefined && isMeasured(followedWas) && isMeasured(followed.position)) {
95
+ const moved = followed.position - followedWas;
96
+ if (Math.abs(moved) >= DECORATION_MIN_SHIFT_PX)
97
+ scroller.scrollTop = scrollTopBefore + moved;
98
+ }
99
+ // Layout or this write may clamp scrolling; count an engine scroll event only when the actual position changed.
100
+ const appliedShift = scroller.scrollTop - scrollTopBefore;
101
+ if (appliedShift !== 0) {
102
+ noteEngineScrollEvent(engine, now);
103
+ if (engine.scrollPos >= 0)
104
+ engine.scrollPos += appliedShift;
105
+ }
106
+ if (!decorationSlideAllowed(engine, container))
107
+ return;
108
+ for (const line of engine.lines) {
109
+ const was = before.get(line);
110
+ if (was === undefined || !isMeasured(was.position) || !isMeasured(line.position))
111
+ continue;
112
+ const onScreenShift = was.position + was.motionOffset - line.position + appliedShift;
113
+ if (Math.abs(onScreenShift) >= DECORATION_MIN_SHIFT_PX)
114
+ slideElementBy(engine, line.lyricElement, onScreenShift);
115
+ }
116
+ }
117
+ function followedLine(engine, before, scrollTopBefore, now) {
118
+ const readerOwnsScroll = engine.scrollResumeTime > now;
119
+ if (readerOwnsScroll) {
120
+ return engine.lines.find(line => {
121
+ const was = before.get(line);
122
+ return was !== undefined && was.position + was.height > scrollTopBefore;
123
+ });
124
+ }
125
+ return (engine.lastScrollElements.findLast(line => before.has(line)) ??
126
+ engine.lines.find(line => before.has(line) && line.isScrolled));
127
+ }
128
+ function isMeasured(position) {
129
+ return position >= 0;
130
+ }
@@ -0,0 +1,36 @@
1
+ import type { AnimationEngineInstance } from "./instance.js";
2
+ export interface ScrollInsets {
3
+ readonly top: number;
4
+ readonly bottom: number;
5
+ }
6
+ export declare const NO_SCROLL_INSETS: ScrollInsets;
7
+ interface ScrollTargetLine {
8
+ readonly time: number;
9
+ readonly duration: number;
10
+ readonly position: number;
11
+ readonly height: number;
12
+ }
13
+ interface ScrollPaddingMeasurements {
14
+ viewportHeight: number;
15
+ targetScrollRatio: number;
16
+ contentHeightWithoutBottomPadding: number;
17
+ firstLineHeight: number;
18
+ lastLineCentre: number | null;
19
+ lastLineHeight: number;
20
+ footerHeight: number;
21
+ }
22
+ export declare function getTargetScrollRatio(engine: AnimationEngineInstance, lyricsElement: HTMLElement): number;
23
+ export declare function setupTabRendererObserver(engine: AnimationEngineInstance, element: HTMLElement): void;
24
+ export declare function measureScrollViewport(engine: AnimationEngineInstance, element: HTMLElement): void;
25
+ export declare function resolveScrollInsets(scrollPaddingTop: string, scrollPaddingBottom: string, viewportHeight: number): ScrollInsets;
26
+ export declare function findScrollAnchor<Line extends ScrollTargetLine>(sungLines: readonly Line[], scrollTime: number): Line;
27
+ export declare function computeActiveLinesScrollTop(sungLines: readonly ScrollTargetLine[], anchor: ScrollTargetLine, viewportHeight: number, insets: ScrollInsets, targetOffset: number): {
28
+ scrollTop: number;
29
+ overflowsBand: boolean;
30
+ };
31
+ export declare function computeScrollPadding(measurements: ScrollPaddingMeasurements): {
32
+ top: number;
33
+ bottom: number;
34
+ };
35
+ export declare function applyScrollPadding(engine: AnimationEngineInstance): void;
36
+ export {};
@@ -0,0 +1,104 @@
1
+ import { CREDITS_CLASS, FOOTER_CLASS, LINE_CLASS } from "../constants.js";
2
+ import { registerThemeSetting } from "../themeSettings.js";
3
+ import { clamp, getRelativeLayoutBounds } from "../util.js";
4
+ import { getCSSValue } from "./cssValues.js";
5
+ import { dropPendingLineScroll } from "./lineScroll.js";
6
+ import { hasNoLyricsPlaceholder } from "./passiveScroll.js";
7
+ const TARGET_SCROLL_POSITION_RATIO = registerThemeSetting("blyrics-target-scroll-pos-ratio", 0.37);
8
+ export const NO_SCROLL_INSETS = { top: 0, bottom: 0 };
9
+ export function getTargetScrollRatio(engine, lyricsElement) {
10
+ const themed = Number.parseFloat(getCSSValue(engine, lyricsElement, "--blyrics-target-scroll-pos-ratio", ""));
11
+ return Number.isFinite(themed) ? clamp(themed, 0, 1) : TARGET_SCROLL_POSITION_RATIO.getNumberValue();
12
+ }
13
+ export function setupTabRendererObserver(engine, element) {
14
+ engine.tabRendererResizeObserver?.disconnect();
15
+ engine.tabRendererResizeObserver = new engine.window.ResizeObserver(() => {
16
+ dropPendingLineScroll(engine);
17
+ if (element.isConnected)
18
+ measureScrollViewport(engine, element);
19
+ });
20
+ engine.tabRendererResizeObserver.observe(element);
21
+ engine.observedTabRenderer = element;
22
+ measureScrollViewport(engine, element);
23
+ }
24
+ export function measureScrollViewport(engine, element) {
25
+ const height = element.getBoundingClientRect().height;
26
+ const { scrollPaddingTop, scrollPaddingBottom } = engine.window.getComputedStyle(element);
27
+ engine.cachedTabRendererHeight = height;
28
+ engine.cachedScrollInsets = resolveScrollInsets(scrollPaddingTop, scrollPaddingBottom, height);
29
+ }
30
+ export function resolveScrollInsets(scrollPaddingTop, scrollPaddingBottom, viewportHeight) {
31
+ const top = resolveScrollInset(scrollPaddingTop, viewportHeight);
32
+ const bottom = resolveScrollInset(scrollPaddingBottom, viewportHeight);
33
+ const leavesABand = top + bottom < viewportHeight;
34
+ return leavesABand ? { top, bottom } : NO_SCROLL_INSETS;
35
+ }
36
+ function resolveScrollInset(value, viewportHeight) {
37
+ const amount = Number.parseFloat(value);
38
+ if (!Number.isFinite(amount) || amount <= 0)
39
+ return 0;
40
+ return value.trim().endsWith("%") ? (viewportHeight * amount) / 100 : amount;
41
+ }
42
+ export function findScrollAnchor(sungLines, scrollTime) {
43
+ const latestRunning = sungLines.findLast(line => line.time <= scrollTime && scrollTime < line.time + line.duration);
44
+ return latestRunning ?? sungLines.findLast(line => line.time <= scrollTime) ?? sungLines[0];
45
+ }
46
+ export function computeActiveLinesScrollTop(sungLines, anchor, viewportHeight, insets, targetOffset) {
47
+ const first = sungLines[0];
48
+ const last = sungLines[sungLines.length - 1];
49
+ const visibleBottom = viewportHeight - insets.bottom;
50
+ const clampToLineBounds = (scrollTop, line) => Math.min(Math.max(scrollTop, line.position + line.height - visibleBottom), line.position - insets.top);
51
+ const averageCentre = sungLines.reduce((total, line) => total + line.position + line.height / 2, 0) / sungLines.length;
52
+ const showingLast = clampToLineBounds(averageCentre - targetOffset, last);
53
+ const showingFirstTop = Math.min(showingLast, first.position - insets.top);
54
+ return {
55
+ scrollTop: clampToLineBounds(showingFirstTop, anchor),
56
+ overflowsBand: last.position + last.height - first.position > visibleBottom - insets.top,
57
+ };
58
+ }
59
+ export function computeScrollPadding(measurements) {
60
+ const { viewportHeight, targetScrollRatio, lastLineCentre } = measurements;
61
+ const contentHeight = measurements.contentHeightWithoutBottomPadding;
62
+ const belowTarget = viewportHeight * (1 - targetScrollRatio);
63
+ const top = Math.max(0, viewportHeight * targetScrollRatio - measurements.firstLineHeight / 2);
64
+ const lastLineAtTarget = (lastLineCentre === null ? viewportHeight : lastLineCentre + belowTarget) - contentHeight;
65
+ const fillsViewport = viewportHeight - contentHeight;
66
+ const unrenderedFloor = belowTarget - (measurements.footerHeight + measurements.lastLineHeight / 2);
67
+ const bottom = Math.max(lastLineAtTarget, fillsViewport, unrenderedFloor, 0);
68
+ return { top, bottom: Math.ceil(bottom) };
69
+ }
70
+ // These published properties must remain document-scoped for external stylesheets and themes. A second
71
+ // renderer in the same document overwrites them.
72
+ export function applyScrollPadding(engine) {
73
+ const lyricsElement = engine.lyricsContainer;
74
+ const scroller = engine.host.getScrollElement();
75
+ if (!lyricsElement || !scroller)
76
+ return;
77
+ if (hasNoLyricsPlaceholder(engine)) {
78
+ setScrollPadding(engine, lyricsElement, { top: 0, bottom: 0 });
79
+ return;
80
+ }
81
+ const bounds = (element) => (element ? getRelativeLayoutBounds(lyricsElement, element) : null);
82
+ const child = (className) => lyricsElement.querySelector(`:scope > .${className}`);
83
+ const lines = lyricsElement.querySelectorAll(`:scope > .${LINE_CLASS}`);
84
+ const creditsBounds = bounds(child(CREDITS_CLASS));
85
+ const lastTargetBounds = creditsBounds && creditsBounds.height > 0 ? creditsBounds : bounds(lines[lines.length - 1] ?? null);
86
+ const currentBottomPadding = Number.parseFloat(engine.window.getComputedStyle(lyricsElement).paddingBottom) || 0;
87
+ setScrollPadding(engine, lyricsElement, computeScrollPadding({
88
+ viewportHeight: scroller.getBoundingClientRect().height,
89
+ targetScrollRatio: getTargetScrollRatio(engine, lyricsElement),
90
+ contentHeightWithoutBottomPadding: Math.max(0, lyricsElement.scrollHeight - currentBottomPadding),
91
+ firstLineHeight: bounds(lines[0] ?? null)?.height ?? 0,
92
+ lastLineCentre: lastTargetBounds ? lastTargetBounds.y + lastTargetBounds.height / 2 : null,
93
+ lastLineHeight: lastTargetBounds?.height ?? 0,
94
+ footerHeight: bounds(child(FOOTER_CLASS))?.height ?? 0,
95
+ }));
96
+ }
97
+ function setScrollPadding(engine, lyricsElement, { top, bottom }) {
98
+ const rootStyle = engine.document.documentElement.style;
99
+ rootStyle.setProperty("--blyrics-padding-top", `${top}px`);
100
+ rootStyle.setProperty("--blyrics-padding-bottom", `${bottom}px`);
101
+ // Inline, because a theme's `.blyrics-container { padding }` comes after the package stylesheet at equal
102
+ // specificity and would otherwise take this room away.
103
+ lyricsElement.style.setProperty("padding-bottom", `${bottom}px`);
104
+ }
@@ -0,0 +1,7 @@
1
+ import type { LineData } from "../inject.js";
2
+ import type { AnimationEngineInstance } from "./instance.js";
3
+ export declare function applyStage(engine: AnimationEngineInstance, timeS: number, instant: boolean): void;
4
+ export declare function measureStage(engine: AnimationEngineInstance): void;
5
+ export declare function replaceStageElements(engine: AnimationEngineInstance, dropped: readonly LineData[], previousOf: ReadonlyMap<LineData, LineData>): void;
6
+ export declare function resetStage(engine: AnimationEngineInstance): void;
7
+ export declare function currentTranslateY(engine: AnimationEngineInstance, element: HTMLElement, fallback: number): number;