Skip to content

Commit d5af9ed

Browse files
authored
Merge pull request #53 from mbaraa/feat/keyboard-shortcuts
Feat: Player's Shortcuts
2 parents 4be6ae0 + c64c18c commit d5af9ed

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

static/js/player.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ function setLoading(loading, fallback) {
174174
* @returns {[Function, Function, Function]}
175175
*/
176176
function playPauser(audioEl) {
177+
let startedPlaylist = false;
178+
177179
const __play = () => {
178180
audioEl.play();
179181
const songEl = document.getElementById(
@@ -191,6 +193,12 @@ function playPauser(audioEl) {
191193
setPlayerButtonIcon(playPauseToggleExapndedEl, Player.icons.play);
192194
};
193195
const __toggle = () => {
196+
const playPlaylistEl = document.getElementById("play-playlist-button");
197+
if (!!playPlaylistEl && !startedPlaylist) {
198+
playPlaylistEl.click();
199+
startedPlaylist = true;
200+
return;
201+
}
194202
if (audioEl.paused) {
195203
__play();
196204
} else {
@@ -213,7 +221,7 @@ function stopper(audioEl) {
213221
"song-" + playerState.playlist.songs[playerState.currentSongIdx].yt_id,
214222
);
215223
if (!!songEl) {
216-
songEl.style.backgroundColor = "var(--secondary-color-20)";
224+
songEl.style.backgroundColor = "#ffffff00";
217225
}
218226
setPlayerButtonIcon(playPauseToggleEl, Player.icons.play);
219227
setPlayerButtonIcon(playPauseToggleExapndedEl, Player.icons.play);
@@ -265,7 +273,7 @@ function playlister(state) {
265273
return;
266274
}
267275
await fetch(
268-
"/api/playlist/plays?" +
276+
"/api/song/playlist/plays?" +
269277
new URLSearchParams({
270278
"song-id": state.playlist.songs[state.currentSongIdx].yt_id,
271279
"playlist-id": state.playlist.public_id,
@@ -367,6 +375,36 @@ function playlister(state) {
367375
return [__next, __prev, __remove, __setSongInPlaylistStyle];
368376
}
369377

378+
function volumer() {
379+
let lastVolume = 1;
380+
const __setVolume = (level) => {
381+
if (level > 1) {
382+
level = 1;
383+
}
384+
if (level < 0) {
385+
level = 0;
386+
}
387+
audioPlayerEl.volume = level;
388+
if (volumeSeekBarEl) {
389+
volumeSeekBarEl.value = Math.floor(level * 100);
390+
}
391+
if (volumeSeekBarExpandedEl) {
392+
volumeSeekBarExpandedEl.value = Math.floor(level * 100);
393+
}
394+
};
395+
396+
const __muter = () => {
397+
if (audioPlayerEl.volume === 0) {
398+
__setVolume(lastVolume);
399+
} else {
400+
lastVolume = audioPlayerEl.volume;
401+
__setVolume(0);
402+
}
403+
};
404+
405+
return [__setVolume, __muter];
406+
}
407+
370408
/**
371409
* @param {string} songYtId
372410
*/
@@ -571,6 +609,7 @@ const [
571609
removeSongFromPlaylist,
572610
highlightSongInPlaylist,
573611
] = playlister(playerState);
612+
const [setVolume, mute] = volumer();
574613

575614
playPauseToggleEl.addEventListener("click", (event) => {
576615
event.stopImmediatePropagation();

static/js/player_shortcuts.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"use strict";
2+
3+
/**
4+
* Using YouTube's applicaple shortcuts: https://support.google.com/youtube/answer/7631406?hl=en
5+
*/
6+
const shortcuts = {
7+
" ": togglePP,
8+
k: togglePP,
9+
n: nextMuzikk,
10+
N: previousMuzikk,
11+
s: stopMuzikk,
12+
m: mute,
13+
0: () => (audioPlayerEl.currentTime = 0),
14+
1: () => (audioPlayerEl.currentTime = audioPlayerEl.duration * 0.1),
15+
2: () => (audioPlayerEl.currentTime = audioPlayerEl.duration * 0.2),
16+
3: () => (audioPlayerEl.currentTime = audioPlayerEl.duration * 0.3),
17+
4: () => (audioPlayerEl.currentTime = audioPlayerEl.duration * 0.4),
18+
5: () => (audioPlayerEl.currentTime = audioPlayerEl.duration * 0.5),
19+
6: () => (audioPlayerEl.currentTime = audioPlayerEl.duration * 0.6),
20+
7: () => (audioPlayerEl.currentTime = audioPlayerEl.duration * 0.7),
21+
8: () => (audioPlayerEl.currentTime = audioPlayerEl.duration * 0.8),
22+
9: () => (audioPlayerEl.currentTime = audioPlayerEl.duration * 0.9),
23+
$: () => (audioPlayerEl.currentTime = audioPlayerEl.duration),
24+
j: () => (audioPlayerEl.currentTime -= 10),
25+
l: () => (audioPlayerEl.currentTime += 10),
26+
ArrowRight: () => setVolume(audioPlayerEl.volume + 0.1),
27+
ArrowLeft: () => setVolume(audioPlayerEl.volume - 0.1),
28+
i: expand,
29+
I: collapse,
30+
"/": () => searchInputEl.focus(),
31+
};
32+
33+
/**
34+
* @param {KeyboardEvent} e
35+
*/
36+
document.addEventListener("keyup", (e) => {
37+
if (
38+
[searchFormEl, searchInputEl, searchSugEl].includes(document.activeElement)
39+
) {
40+
console.log("search suka");
41+
return;
42+
}
43+
const action = shortcuts[e.key];
44+
if (action) {
45+
e.stopImmediatePropagation();
46+
e.preventDefault();
47+
action();
48+
}
49+
console.log(e.key);
50+
});
51+
52+
/**
53+
* @param {KeyboardEvent} e
54+
*/
55+
document.addEventListener("keydown", (e) => {
56+
if (
57+
[searchFormEl, searchInputEl, searchSugEl].includes(document.activeElement)
58+
) {
59+
return;
60+
}
61+
const action = shortcuts[e.key];
62+
if (action) {
63+
e.stopImmediatePropagation();
64+
e.preventDefault();
65+
}
66+
});

views/components/player/player.templ

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ templ PlayerSticky() {
1818
///
1919
<script src="/static/js/player.js" lang="javascript"></script>
2020
<script defer src="/static/js/player_icons.js" lang="javascript"></script>
21+
<script defer src="/static/js/player_shortcuts.js" lang="javascript"></script>
2122
}

views/pages/playlist.templ

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ templ playlistHeader(pl entities.Playlist) {
4545
</div>
4646
if pl.Songs != nil && len(pl.Songs) > 0 {
4747
<button
48+
id="play-playlist-button"
4849
type="button"
4950
title="Play playlist"
5051
onClick={ playSongFromPlaylist(pl.Songs[0].YtId, pl) }

0 commit comments

Comments
 (0)