editSong.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. mediaSource: string;
  17. song: Song;
  18. reports: Report[];
  19. tab: "discogs" | "reports" | "youtube" | "musare-songs";
  20. newSong: boolean;
  21. prefillData: any;
  22. bulk: boolean;
  23. mediaSources: 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. mediaSource: null,
  57. song: {},
  58. reports: [],
  59. tab: "discogs",
  60. newSong: false,
  61. prefillData: {},
  62. bulk: false,
  63. mediaSources: [],
  64. songPrefillData: {},
  65. form: {}
  66. }),
  67. actions: {
  68. init({ song, songs }) {
  69. console.log(12357878, song, songs);
  70. if (songs) {
  71. this.bulk = true;
  72. this.mediaSources = songs.map(song => song.mediaSource);
  73. this.songPrefillData = Object.fromEntries(
  74. songs.map(song => [
  75. song.mediaSource,
  76. song.prefill ? song.prefill : {}
  77. ])
  78. );
  79. } else this.editSong(song);
  80. },
  81. showTab(tab) {
  82. this.tab = tab;
  83. },
  84. editSong(song) {
  85. this.newSong = !!song.newSong || !song._id;
  86. this.mediaSource = song.mediaSource || null;
  87. this.prefillData = song.prefill ? song.prefill : {};
  88. },
  89. setSong(song, reset?: boolean) {
  90. if (song.discogs === undefined) song.discogs = null;
  91. this.song = JSON.parse(JSON.stringify(song));
  92. this.newSong = !song._id;
  93. this.mediaSource = song.mediaSource;
  94. const formSong = {
  95. title: song.title,
  96. duration: song.duration,
  97. skipDuration: song.skipDuration,
  98. thumbnail: song.thumbnail,
  99. mediaSource: song.mediaSource,
  100. verified: song.verified,
  101. addArtist: "",
  102. artists: song.artists,
  103. addGenre: "",
  104. genres: song.genres,
  105. addTag: "",
  106. tags: song.tags,
  107. discogs: song.discogs
  108. };
  109. if (reset) this.form.setValue(formSong, true);
  110. else this.form.setOriginalValue(formSong);
  111. },
  112. resetSong(mediaSource) {
  113. if (this.mediaSource === mediaSource) this.mediaSource = "";
  114. if (this.song && this.song.mediaSource === mediaSource) {
  115. this.song = {};
  116. if (this.form.setValue)
  117. this.form.setValue(
  118. {
  119. title: "",
  120. duration: 0,
  121. skipDuration: 0,
  122. thumbnail: "",
  123. mediaSource: "",
  124. verified: false,
  125. addArtist: "",
  126. artists: [],
  127. addGenre: "",
  128. genres: [],
  129. addTag: "",
  130. tags: [],
  131. discogs: {}
  132. },
  133. true
  134. );
  135. }
  136. },
  137. stopVideo() {
  138. if (this.video.player && this.video.player.pauseVideo) {
  139. this.video.player.pauseVideo();
  140. this.video.player.seekTo(0);
  141. }
  142. },
  143. hardStopVideo() {
  144. if (this.video.player && this.video.player.stopVideo) {
  145. this.video.player.stopVideo();
  146. }
  147. },
  148. loadVideoById(id, skipDuration) {
  149. this.form.setValue({ duration: -1 });
  150. this.video.player.loadVideoById(id, skipDuration);
  151. },
  152. pauseVideo(status) {
  153. if (
  154. (this.video.player && this.video.player.pauseVideo) ||
  155. this.video.playVideo
  156. ) {
  157. if (status) this.video.player.pauseVideo();
  158. else this.video.player.playVideo();
  159. }
  160. this.video.paused = status;
  161. },
  162. selectDiscogsInfo(discogsInfo) {
  163. this.form.setValue({ discogs: discogsInfo });
  164. },
  165. updateReports(reports) {
  166. this.reports = reports;
  167. },
  168. resolveReport(reportId) {
  169. this.reports = this.reports.filter(
  170. report => report._id !== reportId
  171. );
  172. },
  173. updateYoutubeId(youtubeId) {
  174. this.form.setValue({ mediaSource: `youtube:${youtubeId}` });
  175. // TODO support spotify
  176. this.loadVideoById(youtubeId, 0);
  177. },
  178. setPlaybackRate(rate) {
  179. if (rate) {
  180. this.video.playbackRate = rate;
  181. this.video.player.setPlaybackRate(rate);
  182. } else if (
  183. this.video.player.getPlaybackRate() !== undefined &&
  184. this.video.playbackRate !==
  185. this.video.player.getPlaybackRate()
  186. ) {
  187. this.video.player.setPlaybackRate(this.video.playbackRate);
  188. this.video.playbackRate =
  189. this.video.player.getPlaybackRate();
  190. }
  191. }
  192. }
  193. })();