editSong.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { defineStore } from "pinia";
  2. import { ComputedRef, Ref } from "vue";
  3. import { Song } from "@/types/song";
  4. import { Report } from "@/types/report";
  5. export const useEditSongStore = ({ modalUuid }: { modalUuid: string }) =>
  6. defineStore(`editSong-${modalUuid}`, {
  7. state: (): {
  8. video: {
  9. player: any;
  10. paused: boolean;
  11. playerReady: boolean;
  12. autoPlayed: boolean;
  13. currentTime: number;
  14. playbackRate: 0.5 | 1 | 2;
  15. };
  16. youtubeId: string;
  17. song: Song;
  18. reports: Report[];
  19. tab: "discogs" | "reports" | "youtube" | "musare-songs";
  20. newSong: boolean;
  21. prefillData: any;
  22. bulk: boolean;
  23. youtubeIds: string[];
  24. songPrefillData: any;
  25. form: {
  26. inputs: Ref<
  27. Record<
  28. string,
  29. | {
  30. value: any;
  31. originalValue: any;
  32. validate?: (value: any) => boolean | string;
  33. errors: string[];
  34. ref: Ref;
  35. sourceChanged: boolean;
  36. required: boolean;
  37. ignoreUnsaved: boolean;
  38. }
  39. | any
  40. >
  41. >;
  42. unsavedChanges: ComputedRef<string[]>;
  43. save: (saveCb?: () => void) => void;
  44. setValue: (value: Record<string, any>, reset?: boolean) => void;
  45. setOriginalValue: (value: Record<string, any>) => void;
  46. };
  47. } => ({
  48. video: {
  49. player: null,
  50. paused: true,
  51. playerReady: false,
  52. autoPlayed: false,
  53. currentTime: 0,
  54. playbackRate: 1
  55. },
  56. youtubeId: null,
  57. song: {},
  58. reports: [],
  59. tab: "discogs",
  60. newSong: false,
  61. prefillData: {},
  62. bulk: false,
  63. youtubeIds: [],
  64. songPrefillData: {},
  65. form: {}
  66. }),
  67. actions: {
  68. init({ song, songs }) {
  69. if (songs) {
  70. this.bulk = true;
  71. this.youtubeIds = songs.map(song => song.youtubeId);
  72. this.songPrefillData = Object.fromEntries(
  73. songs.map(song => [
  74. song.youtubeId,
  75. song.prefill ? song.prefill : {}
  76. ])
  77. );
  78. } else this.editSong(song);
  79. },
  80. showTab(tab) {
  81. this.tab = tab;
  82. },
  83. editSong(song) {
  84. this.newSong = !!song.newSong || !song._id;
  85. this.youtubeId = song.youtubeId || null;
  86. this.prefillData = song.prefill ? song.prefill : {};
  87. },
  88. setSong(song, reset?: boolean) {
  89. if (song.discogs === undefined) song.discogs = null;
  90. this.song = JSON.parse(JSON.stringify(song));
  91. this.newSong = !song._id;
  92. this.youtubeId = song.youtubeId;
  93. const formSong = {
  94. title: song.title,
  95. duration: song.duration,
  96. skipDuration: song.skipDuration,
  97. thumbnail: song.thumbnail,
  98. youtubeId: song.youtubeId,
  99. verified: song.verified,
  100. addArtist: "",
  101. artists: song.artists,
  102. addGenre: "",
  103. genres: song.genres,
  104. addTag: "",
  105. tags: song.tags,
  106. discogs: song.discogs
  107. };
  108. if (reset) this.form.setValue(formSong, true);
  109. else this.form.setOriginalValue(formSong);
  110. },
  111. resetSong(youtubeId) {
  112. if (this.youtubeId === youtubeId) this.youtubeId = "";
  113. if (this.song && this.song.youtubeId === youtubeId) {
  114. this.song = {};
  115. if (this.form.setValue)
  116. this.form.setValue(
  117. {
  118. title: "",
  119. duration: 0,
  120. skipDuration: 0,
  121. thumbnail: "",
  122. youtubeId: "",
  123. verified: false,
  124. addArtist: "",
  125. artists: [],
  126. addGenre: "",
  127. genres: [],
  128. addTag: "",
  129. tags: [],
  130. discogs: {}
  131. },
  132. true
  133. );
  134. }
  135. },
  136. stopVideo() {
  137. if (this.video.player && this.video.player.pauseVideo) {
  138. this.video.player.pauseVideo();
  139. this.video.player.seekTo(0);
  140. }
  141. },
  142. hardStopVideo() {
  143. if (this.video.player && this.video.player.stopVideo) {
  144. this.video.player.stopVideo();
  145. }
  146. },
  147. loadVideoById(id, skipDuration) {
  148. this.form.setValue({ duration: -1 });
  149. this.video.player.loadVideoById(id, skipDuration);
  150. },
  151. pauseVideo(status) {
  152. if (
  153. (this.video.player && this.video.player.pauseVideo) ||
  154. this.video.playVideo
  155. ) {
  156. if (status) this.video.player.pauseVideo();
  157. else this.video.player.playVideo();
  158. }
  159. this.video.paused = status;
  160. },
  161. selectDiscogsInfo(discogsInfo) {
  162. this.form.setValue({ discogs: discogsInfo });
  163. },
  164. updateReports(reports) {
  165. this.reports = reports;
  166. },
  167. resolveReport(reportId) {
  168. this.reports = this.reports.filter(
  169. report => report._id !== reportId
  170. );
  171. },
  172. updateYoutubeId(youtubeId) {
  173. this.form.setValue({ youtubeId });
  174. this.loadVideoById(youtubeId, 0);
  175. },
  176. setPlaybackRate(rate) {
  177. if (rate) {
  178. this.video.playbackRate = rate;
  179. this.video.player.setPlaybackRate(rate);
  180. } else if (
  181. this.video.player.getPlaybackRate() !== undefined &&
  182. this.video.playbackRate !==
  183. this.video.player.getPlaybackRate()
  184. ) {
  185. this.video.player.setPlaybackRate(this.video.playbackRate);
  186. this.video.playbackRate =
  187. this.video.player.getPlaybackRate();
  188. }
  189. }
  190. }
  191. })();