-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlyricsScript.js
More file actions
138 lines (125 loc) · 4.33 KB
/
lyricsScript.js
File metadata and controls
138 lines (125 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
let lastLyricsHash = '';
let lastPickId = null;
let mobileMode = false;
let lyricsNotFound = [
"Looks like we don't have the lyrics for this song yet.",
"Looks like we don't have the lyrics for this song.",
"Hmm. We don't know the lyrics for this one.",
"You'll have to guess the lyrics for this one.",
];
function getRandomForLyricsNotFound() {
const randomIndex = Math.floor(Math.random() * lyricsNotFound.length);
return lyricsNotFound[randomIndex];
}
document.querySelector('.placeholder').textContent = getRandomForLyricsNotFound();
window.addEventListener('message', (event) => {
const { command, lyrics, pick, color, textColor, mobileMode: mobileModeFlag } = event.data;
const body = document.body;
if (color && body.style.backgroundColor !== color) {
body.style.backgroundColor = color;
}
if (mobileModeFlag !== undefined) {
mobileMode = mobileModeFlag;
}
if (command === 'addLyrics') {
const lyricsHash = JSON.stringify(lyrics);
if (lyricsHash !== lastLyricsHash) {
renderLyrics(lyrics, textColor);
lastLyricsHash = lyricsHash;
}
if (pick !== undefined && pick !== null) {
pickLyrics(pick, textColor);
}
} else if (command === 'clearLyrics') {
clearLyrics();
} else if (command === 'pickLyrics') {
pickLyrics(pick, textColor);
}
});
function renderLyrics(lyrics, textColor) {
const box = document.querySelector('.box');
box.innerHTML = '';
const allStrings = lyrics.every((item) => typeof item === 'string');
if (allStrings) {
const note = document.createElement('div');
note.className = 'lyrics-note';
note.style.color = textColor;
note.textContent = "These lyrics aren't synced to the song yet.";
box.appendChild(note);
lyrics.forEach((text) => {
const div = document.createElement('div');
div.className = 'line';
div.style.opacity = '1';
div.textContent = text;
box.appendChild(div);
});
} else {
lyrics.forEach((line) => {
const div = document.createElement('div');
if (mobileMode) {
div.style.color = '#000000';
} else {
div.style.color = textColor;
}
div.className = 'line future';
div.textContent = line.text || '♪';
div.dataset.lyricsId = line.id;
box.appendChild(div);
});
}
window.scrollTo(0, 0);
}
function clearLyrics() {
const placeholder = document.querySelector('.placeholder');
if (placeholder) {
return;
}
const box = document.querySelector('.box');
box.innerHTML = `<div class="placeholder">${getRandomForLyricsNotFound()}</div>`;
lastLyricsHash = '';
lastPickId = null;
}
function pickLyrics(id, textColor) {
if (id === lastPickId || id === null || id === undefined) {
return;
}
console.log(textColor);
lastPickId = id;
const lines = Array.from(document.querySelectorAll('.line'));
if (!lines.length) {
return;
}
const pickedIndex = lines.findIndex((line) => line.dataset.lyricsId === String(id));
if (pickedIndex === -1) {
return;
}
lines.forEach((line, index) => {
line.classList.remove('past', 'current', 'future');
if (mobileMode) {
if (index <= pickedIndex) {
line.classList.add('current');
line.style.color = '#ffffff';
line.style.opacity = '1';
} else {
line.classList.add('future');
line.style.color = '#000000';
line.style.opacity = '1';
}
} else {
if (index < pickedIndex) {
line.classList.add('past');
line.style.color = textColor;
line.style.opacity = '';
} else if (index === pickedIndex) {
line.classList.add('current');
line.style.color = 'white';
line.style.opacity = '';
} else {
line.classList.add('future');
line.style.color = textColor;
line.style.opacity = '';
}
}
});
lines[pickedIndex].scrollIntoView({ behavior: 'smooth', block: 'center' });
}