Skip to content

Commit 749d65a

Browse files
committed
refactor(synced-lyrics): extract scrolling constants and helpers
1 parent d94c96b commit 749d65a

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

src/plugins/synced-lyrics/renderer/renderer.tsx

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from './components';
2929

3030
import { currentLyrics } from './store';
31+
import { clamp, SCROLL_DURATION, LEAD_IN_TIME_MS } from './scrolling';
3132

3233
import type { LineLyrics, SyncedLyricsPluginConfig } from '../types';
3334

@@ -391,6 +392,8 @@ export const LyricsRenderer = () => {
391392
const targetIndex = scrollTargetIndex();
392393
const maxIndex = untrack(statuses).length - 1;
393394
const scrollerInstance = scroller();
395+
const lineEffect = config()?.lineEffect;
396+
const isEnhanced = lineEffect === 'enhanced';
394397

395398
if (!visible || !scrollerInstance || !current.data?.lines) return;
396399

@@ -403,27 +406,33 @@ export const LyricsRenderer = () => {
403406
jumpSize: number,
404407
fast: boolean,
405408
) => {
406-
// fast scroll for others
407409
if (fast) {
408-
const d = 260 + distance * 0.28;
409-
return Math.min(680, Math.max(240, d));
410+
return clamp(
411+
SCROLL_DURATION.FAST_BASE + distance * SCROLL_DURATION.FAST_MULT,
412+
SCROLL_DURATION.FAST_MIN,
413+
SCROLL_DURATION.FAST_MAX,
414+
);
410415
}
411416

412-
let minDuration = 850;
413-
let maxDuration = 1650;
414-
let duration = 550 + distance * 0.7;
417+
let base: number = SCROLL_DURATION.NORMAL_BASE;
418+
let mult: number = SCROLL_DURATION.NORMAL_MULT;
419+
let min: number = SCROLL_DURATION.NORMAL_MIN;
420+
let max: number = SCROLL_DURATION.NORMAL_MAX;
415421

416422
if (jumpSize === 1) {
417-
minDuration = 1000;
418-
maxDuration = 1800;
419-
duration = 700 + distance * 0.8;
423+
base = SCROLL_DURATION.JUMP1_BASE;
424+
mult = SCROLL_DURATION.JUMP1_MULT;
425+
min = SCROLL_DURATION.JUMP1_MIN;
426+
max = SCROLL_DURATION.JUMP1_MAX;
420427
} else if (jumpSize > 3) {
421-
minDuration = 600;
422-
maxDuration = 1400;
423-
duration = 400 + distance * 0.6;
428+
base = SCROLL_DURATION.JUMP4_BASE;
429+
mult = SCROLL_DURATION.JUMP4_MULT;
430+
min = SCROLL_DURATION.JUMP4_MIN;
431+
max = SCROLL_DURATION.JUMP4_MAX;
424432
}
425433

426-
return Math.min(maxDuration, Math.max(minDuration, duration));
434+
const duration = base + distance * mult;
435+
return clamp(duration, min, max);
427436
};
428437

429438
// easing function
@@ -452,7 +461,7 @@ export const LyricsRenderer = () => {
452461
const itemCenter = itemSize / 2;
453462
const centerOffset = itemOffset - viewportCenter + itemCenter;
454463

455-
return Math.max(0, Math.min(centerOffset, maxScroll));
464+
return clamp(centerOffset, 0, maxScroll);
456465
};
457466

458467
// enhanced scroll animation
@@ -527,7 +536,7 @@ export const LyricsRenderer = () => {
527536

528537
// wait for scroller ready
529538
const waitForReady = (tries = 0) => {
530-
const nonEnhanced = config()?.lineEffect !== 'enhanced';
539+
const nonEnhanced = !isEnhanced;
531540
const scrollerReady = isScrollerReady(scrollerInstance, scrollIndex);
532541
const hasCurrentIndex = !nonEnhanced || currentIndex() >= 0;
533542

@@ -543,7 +552,7 @@ export const LyricsRenderer = () => {
543552
const inFastWindow = now < fastScrollUntil();
544553
const suppressed = now < suppressFastUntil();
545554

546-
if (config()?.lineEffect !== 'enhanced') {
555+
if (!isEnhanced) {
547556
scrollerInstance.scrollToIndex(scrollIndex, {
548557
smooth: true,
549558
align: 'center',
@@ -628,10 +637,9 @@ export const LyricsRenderer = () => {
628637

629638
if (nextLine) {
630639
// start scroll early
631-
const leadInTimeMs = 130;
632640
const timeUntilNextLine = nextLine.timeInMs - currentTimeMs;
633641

634-
if (timeUntilNextLine <= leadInTimeMs) {
642+
if (timeUntilNextLine <= LEAD_IN_TIME_MS) {
635643
setScrollTargetIndex(nextIdx);
636644
prevIndexForFast = effIdx;
637645
return;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export const clamp = (n: number, min: number, max: number) =>
2+
Math.max(min, Math.min(max, n));
3+
4+
export const SCROLL_DURATION = {
5+
FAST_BASE: 260,
6+
FAST_MULT: 0.28,
7+
FAST_MIN: 240,
8+
FAST_MAX: 680,
9+
NORMAL_BASE: 550,
10+
NORMAL_MULT: 0.7,
11+
NORMAL_MIN: 850,
12+
NORMAL_MAX: 1650,
13+
JUMP1_BASE: 700,
14+
JUMP1_MULT: 0.8,
15+
JUMP1_MIN: 1000,
16+
JUMP1_MAX: 1800,
17+
JUMP4_BASE: 400,
18+
JUMP4_MULT: 0.6,
19+
JUMP4_MIN: 600,
20+
JUMP4_MAX: 1400,
21+
} as const;
22+
23+
export const LEAD_IN_TIME_MS = 130;

0 commit comments

Comments
 (0)