viewYoutubeVideo.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { defineStore } from "pinia";
  2. export const useViewYoutubeVideoStore = ({
  3. modalUuid
  4. }: {
  5. modalUuid: string;
  6. }) =>
  7. defineStore(`viewYoutubeVideo-${modalUuid}`, {
  8. state: (): {
  9. video: {
  10. _id: string;
  11. youtubeId: string;
  12. title: string;
  13. author: string;
  14. duration: number;
  15. uploadedAt?: Date;
  16. };
  17. player: {
  18. error: boolean;
  19. errorMessage: string;
  20. player: null;
  21. paused: boolean;
  22. playerReady: boolean;
  23. autoPlayed: boolean;
  24. duration: string;
  25. currentTime: number;
  26. playbackRate: number;
  27. videoNote: string;
  28. volume: number;
  29. muted: boolean;
  30. showRateDropdown: boolean;
  31. };
  32. } => ({
  33. video: {
  34. _id: null,
  35. youtubeId: null,
  36. title: null,
  37. author: null,
  38. duration: 0
  39. },
  40. player: {
  41. error: false,
  42. errorMessage: "",
  43. player: null,
  44. paused: true,
  45. playerReady: false,
  46. autoPlayed: false,
  47. duration: "0.000",
  48. currentTime: 0,
  49. playbackRate: 1,
  50. videoNote: "",
  51. volume: 0,
  52. muted: false,
  53. showRateDropdown: false
  54. }
  55. }),
  56. actions: {
  57. viewYoutubeVideo(video) {
  58. this.video = video;
  59. },
  60. updatePlayer(player) {
  61. this.player = Object.assign(this.player, player);
  62. },
  63. stopVideo() {
  64. if (this.player.player && this.player.player.pauseVideo) {
  65. this.player.player.pauseVideo();
  66. this.player.player.seekTo(0);
  67. }
  68. },
  69. loadVideoById(id) {
  70. this.player.player.loadVideoById(id);
  71. },
  72. pauseVideo(status: boolean) {
  73. if (
  74. (this.player.player && this.player.player.pauseVideo) ||
  75. this.player.playVideo
  76. ) {
  77. if (status) this.player.player.pauseVideo();
  78. else this.player.player.playVideo();
  79. }
  80. this.player.paused = status;
  81. },
  82. setPlaybackRate(rate?: number) {
  83. if (typeof rate === "number") {
  84. this.player.playbackRate = rate;
  85. this.player.player.setPlaybackRate(rate);
  86. } else if (
  87. this.player.player.getPlaybackRate() !== undefined &&
  88. this.player.playbackRate !==
  89. this.player.player.getPlaybackRate()
  90. ) {
  91. this.player.player.setPlaybackRate(
  92. this.player.playbackRate
  93. );
  94. this.player.playbackRate =
  95. this.player.player.getPlaybackRate();
  96. }
  97. }
  98. }
  99. })();