editPlaylist.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. replaceSong({ song, oldMediaSource }) {
  32. this.playlist.songs = this.playlist.songs.map(_song =>
  33. _song.mediaSource === oldMediaSource ? song : _song
  34. );
  35. },
  36. updatePlaylistSongs(playlistSongs) {
  37. this.playlist.songs = playlistSongs;
  38. },
  39. repositionedSong(song) {
  40. if (
  41. this.playlist.songs[song.newIndex] &&
  42. this.playlist.songs[song.newIndex].mediaSource ===
  43. song.mediaSource
  44. )
  45. return;
  46. this.playlist.songs.splice(
  47. song.newIndex,
  48. 0,
  49. this.playlist.songs.splice(song.oldIndex, 1)[0]
  50. );
  51. }
  52. }
  53. })();