koel/resources/assets/js/components/utils/hotkeys.vue

102 lines
2.1 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2022-04-15 17:00:08 +00:00
<GlobalEvents
@keydown.space="togglePlayback"
@keydown.j="playNext"
@keydown.k="playPrev"
@keydown.f="search"
@keydown.l="toggleLike"
@keydown.176="playPrev"
@keydown.177="playNext"
@keydown.179="togglePlayback"
2022-04-15 14:24:30 +00:00
/>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import { GlobalEvents } from 'vue-global-events'
import { $, eventBus } from '@/utils'
2022-04-15 14:24:30 +00:00
import { playback, socket } from '@/services'
2022-04-15 17:00:08 +00:00
import { favoriteStore, queueStore, songStore } from '@/stores'
const togglePlayback = (e: KeyboardEvent) => {
if (
!(e.target instanceof Document) &&
$.is(e.target as Element, 'input, textarea, button, select') &&
!$.is(e.target as Element, 'input[type=range]')
) {
return true
}
e.preventDefault()
playback.toggle()
return false
}
/**
* Play the previous song when user presses K.
*/
const playPrev = (e: KeyboardEvent) => {
if (!(e.target instanceof Document) && $.is(e.target as Element, 'input, select, textarea')) {
return true
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
e.preventDefault()
playback.playPrev()
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
return false
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
/**
* Play the next song when user presses J.
*/
const playNext = (e: KeyboardEvent) => {
if (!(e.target instanceof Document) && $.is(e.target as Element, 'input, select, textarea')) {
return true
}
e.preventDefault()
playback.playNext()
return false
2022-04-15 14:24:30 +00:00
}
/**
2022-04-15 17:00:08 +00:00
* Put focus into the search field when user presses F.
2022-04-15 14:24:30 +00:00
*/
2022-04-15 17:00:08 +00:00
const search = (e: KeyboardEvent) => {
if (
!(e.target instanceof Document) &&
($.is(e.target as Element, 'input, select, textarea') && !$.is(e.target as Element, 'input[type=range]'))
) {
return true
}
if (e.metaKey || e.ctrlKey) {
return true
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
e.preventDefault()
eventBus.emit('FOCUS_SEARCH_FIELD')
return false
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
/**
* Like/unlike the current song when use presses L.
*/
const toggleLike = (e: KeyboardEvent) => {
if (!(e.target instanceof Document) && $.is(e.target as Element, 'input, select, textarea')) {
return true
}
if (!queueStore.current) {
return false
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
favoriteStore.toggleOne(queueStore.current)
socket.broadcast('SOCKET_SONG', songStore.generateDataToBroadcast(queueStore.current))
return false
}
2022-04-15 14:24:30 +00:00
</script>