editSong.ts 3.0 KB

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