editSong.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { defineStore } from "pinia";
  2. import { Song } from "@/types/song";
  3. import { Report } from "@/types/report";
  4. export const useEditSongStore = props => {
  5. const { modalUuid } = props;
  6. if (!modalUuid) return null;
  7. return defineStore(`editSong-${modalUuid}`, {
  8. state: () => ({
  9. video: {
  10. player: null,
  11. paused: true,
  12. playerReady: false,
  13. autoPlayed: false,
  14. currentTime: 0,
  15. playbackRate: 1
  16. },
  17. youtubeId: null,
  18. song: <Song>{},
  19. originalSong: <Song>{},
  20. reports: <Report[]>[],
  21. tab: "discogs",
  22. newSong: false,
  23. prefillData: {},
  24. bulk: false,
  25. youtubeIds: [],
  26. songPrefillData: {}
  27. }),
  28. actions: {
  29. init({ song, songs }) {
  30. if (songs) {
  31. this.bulk = true;
  32. this.youtubeIds = songs.map(song => song.youtubeId);
  33. this.songPrefillData = Object.fromEntries(
  34. songs.map(song => [
  35. song.youtubeId,
  36. song.prefill ? song.prefill : {}
  37. ])
  38. );
  39. } else this.editSong(song);
  40. },
  41. showTab(tab) {
  42. this.tab = tab;
  43. },
  44. editSong(song) {
  45. this.newSong = !!song.newSong || !song._id;
  46. this.youtubeId = song.youtubeId || null;
  47. this.prefillData = song.prefill ? song.prefill : {};
  48. },
  49. setSong(song) {
  50. if (song.discogs === undefined) song.discogs = null;
  51. this.originalSong = JSON.parse(JSON.stringify(song));
  52. this.song = JSON.parse(JSON.stringify(song));
  53. this.newSong = !song._id;
  54. this.youtubeId = song.youtubeId;
  55. },
  56. updateOriginalSong(song) {
  57. this.originalSong = JSON.parse(JSON.stringify(song));
  58. },
  59. resetSong(youtubeId) {
  60. if (this.youtubeId === youtubeId) this.youtubeId = "";
  61. if (this.song && this.song.youtubeId === youtubeId)
  62. this.song = {};
  63. if (
  64. this.originalSong &&
  65. this.originalSong.youtubeId === youtubeId
  66. )
  67. this.originalSong = {};
  68. },
  69. stopVideo() {
  70. if (this.video.player && this.video.player.pauseVideo) {
  71. this.video.player.pauseVideo();
  72. this.video.player.seekTo(0);
  73. }
  74. },
  75. hardStopVideo() {
  76. if (this.video.player && this.video.player.stopVideo) {
  77. this.video.player.stopVideo();
  78. }
  79. },
  80. loadVideoById(id, skipDuration) {
  81. this.song.duration = -1;
  82. this.video.player.loadVideoById(id, skipDuration);
  83. },
  84. pauseVideo(status) {
  85. if (
  86. (this.video.player && this.video.player.pauseVideo) ||
  87. this.video.playVideo
  88. ) {
  89. if (status) this.video.player.pauseVideo();
  90. else this.video.player.playVideo();
  91. }
  92. this.video.paused = status;
  93. },
  94. updateSongField(data) {
  95. this.song[data.field] = data.value;
  96. },
  97. selectDiscogsInfo(discogsInfo) {
  98. this.song.discogs = discogsInfo;
  99. },
  100. updateReports(reports) {
  101. this.reports = reports;
  102. },
  103. resolveReport(reportId) {
  104. this.reports = this.reports.filter(
  105. report => report._id !== reportId
  106. );
  107. },
  108. updateYoutubeId(youtubeId) {
  109. this.song.youtubeId = youtubeId;
  110. this.loadVideoById(youtubeId, 0);
  111. },
  112. updateTitle(title) {
  113. this.song.title = title;
  114. },
  115. updateThumbnail(thumbnail) {
  116. this.song.thumbnail = thumbnail;
  117. },
  118. setPlaybackRate(rate) {
  119. if (rate) {
  120. this.video.playbackRate = rate;
  121. this.video.player.setPlaybackRate(rate);
  122. } else if (
  123. this.video.player.getPlaybackRate() !== undefined &&
  124. this.video.playbackRate !==
  125. this.video.player.getPlaybackRate()
  126. ) {
  127. this.video.player.setPlaybackRate(this.video.playbackRate);
  128. this.video.playbackRate =
  129. this.video.player.getPlaybackRate();
  130. }
  131. }
  132. }
  133. })();
  134. };