feat: better context menu implementation

This commit is contained in:
Phan An 2022-07-08 12:32:44 +02:00
parent 2b09e1e855
commit fd1ef163dc
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
6 changed files with 62 additions and 62 deletions

View file

@ -15,13 +15,16 @@
</div> </div>
</template> </template>
<SongContextMenu ref="songContextMenu"/> <SongContextMenu/>
<AlbumContextMenu ref="albumContextMenu"/> <AlbumContextMenu/>
<ArtistContextMenu ref="artistContextMenu"/> <ArtistContextMenu/>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { defineAsyncComponent, nextTick, onMounted, ref } from 'vue' import { defineAsyncComponent, onMounted, ref } from 'vue'
import { $, eventBus, hideOverlay, showOverlay } from '@/utils'
import { commonStore, favoriteStore, preferenceStore as preferences, queueStore } from '@/stores'
import { authService, playbackService, socketService } from '@/services'
import AppHeader from '@/components/layout/AppHeader.vue' import AppHeader from '@/components/layout/AppHeader.vue'
import AppFooter from '@/components/layout/app-footer/index.vue' import AppFooter from '@/components/layout/app-footer/index.vue'
@ -30,20 +33,12 @@ import Hotkeys from '@/components/utils/HotkeyListener.vue'
import LoginForm from '@/components/auth/LoginForm.vue' import LoginForm from '@/components/auth/LoginForm.vue'
import MainWrapper from '@/components/layout/main-wrapper/index.vue' import MainWrapper from '@/components/layout/main-wrapper/index.vue'
import Overlay from '@/components/ui/Overlay.vue' import Overlay from '@/components/ui/Overlay.vue'
import AlbumContextMenu from '@/components/album/AlbumContextMenu.vue'
import ArtistContextMenu from '@/components/artist/ArtistContextMenu.vue'
import SongContextMenu from '@/components/song/SongContextMenu.vue'
import { $, arrayify, eventBus, hideOverlay, showOverlay } from '@/utils'
import { commonStore, favoriteStore, preferenceStore as preferences, queueStore } from '@/stores'
import { authService, playbackService, socketService } from '@/services'
const SongContextMenu = defineAsyncComponent(() => import('@/components/song/SongContextMenu.vue'))
const AlbumContextMenu = defineAsyncComponent(() => import('@/components/album/AlbumContextMenu.vue'))
const ArtistContextMenu = defineAsyncComponent(() => import('@/components/artist/ArtistContextMenu.vue'))
const SupportKoel = defineAsyncComponent(() => import('@/components/meta/SupportKoel.vue')) const SupportKoel = defineAsyncComponent(() => import('@/components/meta/SupportKoel.vue'))
const songContextMenu = ref<InstanceType<typeof SongContextMenu>>()
const albumContextMenu = ref<InstanceType<typeof AlbumContextMenu>>()
const artistContextMenu = ref<InstanceType<typeof ArtistContextMenu>>()
const authenticated = ref(false) const authenticated = ref(false)
/** /**
@ -80,21 +75,6 @@ onMounted(async () => {
$.addClass(document.documentElement, navigator.userAgent.includes('Mac') ? 'mac' : 'non-mac') $.addClass(document.documentElement, navigator.userAgent.includes('Mac') ? 'mac' : 'non-mac')
}) })
eventBus.on('SONG_CONTEXT_MENU_REQUESTED', async (e: MouseEvent, songs: Song | Song[]) => {
await nextTick()
songContextMenu.value?.open(e.pageY, e.pageX, { songs: arrayify(songs) })
})
eventBus.on('ALBUM_CONTEXT_MENU_REQUESTED', async (e: MouseEvent, album: Album) => {
await nextTick()
albumContextMenu.value?.open(e.pageY, e.pageX, { album })
})
eventBus.on('ARTIST_CONTEXT_MENU_REQUESTED', async (e: MouseEvent, artist: Artist) => {
await nextTick()
artistContextMenu.value?.open(e.pageY, e.pageX, { artist })
})
const init = async () => { const init = async () => {
showOverlay() showOverlay()
await socketService.init() await socketService.init()

View file

@ -1,50 +1,59 @@
import { fireEvent } from '@testing-library/vue' import { fireEvent } from '@testing-library/vue'
import { expect, it } from 'vitest' import { expect, it } from 'vitest'
import { downloadService } from '@/services' import { downloadService, playbackService } from '@/services'
import factory from '@/__tests__/factory' import factory from '@/__tests__/factory'
import UnitTestCase from '@/__tests__/UnitTestCase' import UnitTestCase from '@/__tests__/UnitTestCase'
import AlbumCard from './AlbumCard.vue' import AlbumCard from './AlbumCard.vue'
import { songStore } from '@/stores'
let album: Album let album: Album
new class extends UnitTestCase { new class extends UnitTestCase {
protected beforeEach () { private renderComponent () {
super.beforeEach(() => { album = factory<Album>('album', {
album = factory<Album>('album', { name: 'IV',
name: 'IV' play_count: 30,
}) song_count: 10,
length: 123
})
return this.render(AlbumCard, {
props: {
album
}
}) })
} }
protected test () { protected test () {
it('renders', () => { it('renders', () => {
const { getByText, getByTestId } = this.render(AlbumCard, { const { getByText, getByTestId } = this.renderComponent()
props: {
album
}
})
expect(getByTestId('name').textContent).toBe('IV') expect(getByTestId('name').textContent).toBe('IV')
getByText(/^10 songs.+0 plays$/) getByText(/^10 songs.+02:03.+30 plays$/)
getByTestId('shuffle-album') getByTestId('shuffle-album')
getByTestId('download-album') getByTestId('download-album')
}) })
it('downloads', async () => { it('downloads', async () => {
const mock = this.mock(downloadService, 'fromAlbum') const mock = this.mock(downloadService, 'fromAlbum')
const { getByTestId } = this.renderComponent()
const { getByTestId } = this.render(AlbumCard, {
props: {
album
}
})
await fireEvent.click(getByTestId('download-album')) await fireEvent.click(getByTestId('download-album'))
expect(mock).toHaveBeenCalledTimes(1) expect(mock).toHaveBeenCalledTimes(1)
}) })
it('shuffles', async () => { it('shuffles', async () => {
throw 'Unimplemented' const songs = factory<Song>('song', 10)
const fetchMock = this.mock(songStore, 'fetchForAlbum').mockResolvedValue(songs)
const shuffleMock = this.mock(playbackService, 'queueAndPlay').mockResolvedValue(void 0)
const { getByTestId } = this.renderComponent()
await fireEvent.click(getByTestId('shuffle-album'))
await this.tick()
expect(fetchMock).toHaveBeenCalledWith(album)
expect(shuffleMock).toHaveBeenCalledWith(songs, true)
}) })
} }
} }

View file

@ -15,15 +15,16 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, Ref, toRef } from 'vue' import { computed, ref, toRef } from 'vue'
import { albumStore, artistStore, commonStore, songStore } from '@/stores' import { albumStore, artistStore, commonStore, songStore } from '@/stores'
import { downloadService, playbackService } from '@/services' import { downloadService, playbackService } from '@/services'
import { useContextMenu } from '@/composables' import { useContextMenu } from '@/composables'
import router from '@/router' import router from '@/router'
import { eventBus } from '@/utils'
const { context, base, ContextMenuBase, open, trigger } = useContextMenu() const { context, base, ContextMenuBase, open, trigger } = useContextMenu()
const album = toRef(context, 'album') as Ref<Album> const album = ref<Album>()
const allowDownload = toRef(commonStore.state, 'allow_download') const allowDownload = toRef(commonStore.state, 'allow_download')
const isStandardAlbum = computed(() => !albumStore.isUnknown(album.value)) const isStandardAlbum = computed(() => !albumStore.isUnknown(album.value))
@ -42,5 +43,8 @@ const viewAlbumDetails = () => trigger(() => router.go(`album/${album.value.id}`
const viewArtistDetails = () => trigger(() => router.go(`artist/${album.value.artist_id}`)) const viewArtistDetails = () => trigger(() => router.go(`artist/${album.value.artist_id}`))
const download = () => trigger(() => downloadService.fromAlbum(album.value)) const download = () => trigger(() => downloadService.fromAlbum(album.value))
defineExpose({ open }) eventBus.on('ALBUM_CONTEXT_MENU_REQUESTED', async (e: MouseEvent, _album: Album) => {
album.value = _album
open(e.pageY, e.pageX, { album })
})
</script> </script>

View file

@ -16,15 +16,16 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, Ref, toRef } from 'vue' import { computed, ref, toRef } from 'vue'
import { artistStore, commonStore, songStore } from '@/stores' import { artistStore, commonStore, songStore } from '@/stores'
import { downloadService, playbackService } from '@/services' import { downloadService, playbackService } from '@/services'
import { useContextMenu } from '@/composables' import { useContextMenu } from '@/composables'
import router from '@/router' import router from '@/router'
import { eventBus } from '@/utils'
const { context, base, ContextMenuBase, open, trigger } = useContextMenu() const { context, base, ContextMenuBase, open, trigger } = useContextMenu()
const artist = toRef(context, 'artist') as Ref<Artist> const artist = ref<Artist>()
const allowDownload = toRef(commonStore.state, 'allow_download') const allowDownload = toRef(commonStore.state, 'allow_download')
const isStandardArtist = computed(() => const isStandardArtist = computed(() =>
@ -41,5 +42,8 @@ const shuffle = () => {
const viewArtistDetails = () => trigger(() => router.go(`artist/${artist.value.id}`)) const viewArtistDetails = () => trigger(() => router.go(`artist/${artist.value.id}`))
const download = () => trigger(() => downloadService.fromArtist(artist.value)) const download = () => trigger(() => downloadService.fromArtist(artist.value))
defineExpose({ open }) eventBus.on('ARTIST_CONTEXT_MENU_REQUESTED', async (e: MouseEvent, _artist: Artist) => {
artist.value = _artist
open(e.pageY, e.pageX, { _artist })
})
</script> </script>

View file

@ -42,8 +42,8 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, Ref, toRef } from 'vue' import { computed, ref, toRef } from 'vue'
import { alerts, copyText, eventBus, isClipboardSupported as copyable } from '@/utils' import { alerts, arrayify, copyText, eventBus, isClipboardSupported as copyable } from '@/utils'
import { commonStore, playlistStore, queueStore, songStore, userStore } from '@/stores' import { commonStore, playlistStore, queueStore, songStore, userStore } from '@/stores'
import { downloadService, playbackService } from '@/services' import { downloadService, playbackService } from '@/services'
import router from '@/router' import router from '@/router'
@ -51,7 +51,7 @@ import { useAuthorization, useContextMenu, useSongMenuMethods } from '@/composab
const { context, base, ContextMenuBase, open, close, trigger } = useContextMenu() const { context, base, ContextMenuBase, open, close, trigger } = useContextMenu()
const songs = toRef(context, 'songs') as Ref<Song[]> const songs = ref<Song[]>([])
const { const {
queueSongsAfterCurrent, queueSongsAfterCurrent,
@ -101,5 +101,8 @@ const copyUrl = () => trigger(() => {
alerts.success('URL copied to clipboard.') alerts.success('URL copied to clipboard.')
}) })
defineExpose({ open }) eventBus.on('SONG_CONTEXT_MENU_REQUESTED', async (e: MouseEvent, _songs: Song | Song[]) => {
songs.value = arrayify(_songs)
open(e.pageY, e.pageX, { songs: songs.value })
})
</script> </script>

View file

@ -21,11 +21,11 @@ export const useContextMenu = () => {
} }
return { return {
base,
ContextMenuBase, ContextMenuBase,
base,
context,
open, open,
close, close,
trigger, trigger
context
} }
} }