editPlaylist.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { defineStore } from "pinia";
  2. import { Playlist } from "@/types/playlist";
  3. export const useEditPlaylistStore = ({ modalUuid }: { modalUuid: string }) =>
  4. defineStore(`editPlaylist-${modalUuid}`, {
  5. state: (): {
  6. tab: string;
  7. playlist: Playlist;
  8. } => ({
  9. tab: "settings",
  10. playlist: { songs: [] }
  11. }),
  12. actions: {
  13. showTab(tab) {
  14. this.tab = tab;
  15. },
  16. setPlaylist(playlist) {
  17. this.playlist = { ...playlist };
  18. this.playlist.songs.sort((a, b) => a.position - b.position);
  19. },
  20. clearPlaylist() {
  21. this.playlist = { songs: [] };
  22. },
  23. addSong(song) {
  24. this.playlist.songs.push(song);
  25. },
  26. removeSong(mediaSource) {
  27. this.playlist.songs = this.playlist.songs.filter(
  28. song => song.mediaSource !== mediaSource
  29. );
  30. },
  31. updatePlaylistSongs(playlistSongs) {
  32. this.playlist.songs = playlistSongs;
  33. },
  34. repositionedSong(song) {
  35. if (
  36. this.playlist.songs[song.newIndex] &&
  37. this.playlist.songs[song.newIndex].mediaSource ===
  38. song.mediaSource
  39. )
  40. return;
  41. this.playlist.songs.splice(
  42. song.newIndex,
  43. 0,
  44. this.playlist.songs.splice(song.oldIndex, 1)[0]
  45. );
  46. }
  47. }
  48. })();