-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathwatch-video-playlist.js
522 lines (458 loc) · 16.8 KB
/
watch-video-playlist.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import { defineComponent, nextTick } from 'vue'
import { mapMutations } from 'vuex'
import FtLoader from '../ft-loader/ft-loader.vue'
import FtCard from '../ft-card/ft-card.vue'
import FtListVideoNumbered from '../ft-list-video-numbered/ft-list-video-numbered.vue'
import { copyToClipboard, setPublishedTimestampsInvidious, showToast } from '../../helpers/utils'
import {
getLocalPlaylist,
parseLocalPlaylistVideo,
untilEndOfLocalPlayList,
} from '../../helpers/api/local'
import { invidiousGetPlaylistInfo } from '../../helpers/api/invidious'
export default defineComponent({
name: 'WatchVideoPlaylist',
components: {
'ft-loader': FtLoader,
'ft-card': FtCard,
'ft-list-video-numbered': FtListVideoNumbered
},
props: {
playlistId: {
type: String,
required: true,
},
playlistType: {
type: String,
default: null
},
videoId: {
type: String,
required: true,
},
playlistItemId: {
type: String,
default: null,
},
watchViewLoading: {
type: Boolean,
required: true,
},
},
data: function () {
return {
isLoading: true,
shuffleEnabled: false,
loopEnabled: false,
reversePlaylist: false,
pauseOnCurrentVideo: false,
channelName: '',
channelId: '',
playlistTitle: '',
playlistItems: [],
randomizedPlaylistItems: [],
getPlaylistInfoRun: false,
}
},
computed: {
backendPreference: function () {
return this.$store.getters.getBackendPreference
},
backendFallback: function () {
return this.$store.getters.getBackendFallback
},
isUserPlaylist: function () {
return this.playlistType === 'user'
},
userPlaylistsReady: function () {
return this.$store.getters.getPlaylistsReady
},
selectedUserPlaylist: function () {
if (this.playlistId == null || this.playlistId === '') { return null }
return this.$store.getters.getPlaylist(this.playlistId)
},
selectedUserPlaylistVideoCount () {
return this.selectedUserPlaylist?.videos?.length
},
selectedUserPlaylistLastUpdatedAt () {
return this.selectedUserPlaylist?.lastUpdatedAt
},
currentVideoIndexZeroBased: function () {
return this.playlistItems.findIndex((item) => {
if (item.playlistItemId != null && this.playlistItemId != null) {
return item.playlistItemId === this.playlistItemId
} else if (item.videoId != null) {
return item.videoId === this.videoId
} else {
return item.id === this.videoId
}
})
},
currentVideoIndexOneBased: function () {
return this.currentVideoIndexZeroBased + 1
},
currentVideo: function () {
return this.playlistItems[this.currentVideoIndexZeroBased]
},
playlistVideoCount: function () {
return this.playlistItems.length
},
shouldStopDueToPlaylistEnd: function () {
if (!this.videoIsLastInInPlaylistItems) { return false }
// Loop enabled = should not stop
return !this.loopEnabled
},
videoIsLastInInPlaylistItems: function () {
if (this.shuffleEnabled) {
return this.videoIndexInPlaylistItems === this.randomizedPlaylistItems.length - 1
} else {
return this.videoIndexInPlaylistItems === this.playlistItems.length - 1
}
},
videoIndexInPlaylistItems: function () {
const playlistItems = this.shuffleEnabled ? this.randomizedPlaylistItems : this.playlistItems
return playlistItems.findIndex((item) => {
if (item.playlistItemId != null && this.playlistItemId != null) {
return item.playlistItemId === this.playlistItemId
} else if (item.videoId != null) {
return item.videoId === this.videoId
} else {
return item.id === this.videoId
}
})
},
videoIsFirstPlaylistItem: function () {
return this.videoIndexInPlaylistItems === 0
},
videoIsLastPlaylistItem: function () {
return this.videoIndexInPlaylistItems === (this.playlistItems.length - 1)
},
videoIsNotPlaylistItem: function () {
return this.videoIndexInPlaylistItems === -1
},
playlistPageLinkTo() {
// For `router-link` attribute `to`
return {
path: `/playlist/${this.playlistId}`,
query: {
playlistType: this.isUserPlaylist ? 'user' : '',
},
}
},
},
watch: {
userPlaylistsReady: function() {
this.getPlaylistInfoWithDelay()
},
selectedUserPlaylistVideoCount () {
// Re-fetch from local store when current user playlist updated
this.parseUserPlaylist(this.selectedUserPlaylist, { allowPlayingVideoRemoval: true })
this.shufflePlaylistItems()
},
selectedUserPlaylistLastUpdatedAt () {
// Re-fetch from local store when current user playlist updated
this.parseUserPlaylist(this.selectedUserPlaylist, { allowPlayingVideoRemoval: true })
},
videoId: function (newId, oldId) {
// Check if next video is from the shuffled list or if the user clicked a different video
if (this.shuffleEnabled) {
const newVideoIndex = this.randomizedPlaylistItems.findIndex((item) => {
return item.videoId === newId
})
const oldVideoIndex = this.randomizedPlaylistItems.findIndex((item) => {
return item.videoId === oldId
})
if ((newVideoIndex - 1) !== oldVideoIndex) {
// User clicked a different video than expected. Re-shuffle the list
this.shufflePlaylistItems()
}
}
},
watchViewLoading: function (newVal, oldVal) {
// This component is loaded/rendered before watch view loaded
if (oldVal && !newVal) {
// Scroll after watch view loaded, otherwise doesn't work
// Mainly for Local API
// nextTick(() => this.scrollToCurrentVideo())
this.scrollToCurrentVideo()
}
},
isLoading: function (newVal, oldVal) {
// This component is loaded/rendered before watch view loaded
if (oldVal && !newVal) {
// Scroll after this component loaded, otherwise doesn't work
// Mainly for Invidious API
// `nextTick` is required (tested via reloading)
nextTick(() => this.scrollToCurrentVideo())
}
},
playlistId: function (newVal, oldVal) {
if (oldVal !== newVal) {
if (!process.env.SUPPORTS_LOCAL_API || this.backendPreference === 'invidious') {
this.getPlaylistInformationInvidious()
} else {
this.getPlaylistInformationLocal()
}
}
},
},
mounted: function () {
const cachedPlaylist = this.$store.getters.getCachedPlaylist
if (cachedPlaylist?.id === this.playlistId) {
this.loadCachedPlaylistInformation(cachedPlaylist)
} else {
this.getPlaylistInfoWithDelay()
}
if ('mediaSession' in navigator) {
navigator.mediaSession.setActionHandler('previoustrack', this.playPreviousVideo)
navigator.mediaSession.setActionHandler('nexttrack', this.playNextVideo)
}
},
beforeDestroy: function () {
if ('mediaSession' in navigator) {
navigator.mediaSession.setActionHandler('previoustrack', null)
navigator.mediaSession.setActionHandler('nexttrack', null)
}
},
methods: {
getPlaylistInfoWithDelay: function () {
if (this.getPlaylistInfoRun) { return }
this.isLoading = true
// `selectedUserPlaylist` result accuracy relies on data being ready
if (this.isUserPlaylist && !this.userPlaylistsReady) { return }
this.getPlaylistInfoRun = true
if (this.selectedUserPlaylist != null) {
this.parseUserPlaylist(this.selectedUserPlaylist)
} else if (!process.env.SUPPORTS_LOCAL_API || this.backendPreference === 'invidious') {
this.getPlaylistInformationInvidious()
} else {
this.getPlaylistInformationLocal()
}
},
toggleLoop: function () {
if (this.loopEnabled) {
this.loopEnabled = false
showToast(this.$t('Loop is now disabled'))
} else {
this.loopEnabled = true
showToast(this.$t('Loop is now enabled'))
}
},
toggleShuffle: function () {
if (this.shuffleEnabled) {
this.shuffleEnabled = false
showToast(this.$t('Shuffle is now disabled'))
} else {
this.shuffleEnabled = true
showToast(this.$t('Shuffle is now enabled'))
this.shufflePlaylistItems()
}
},
toggleReversePlaylist: function () {
this.isLoading = true
showToast(this.$t('The playlist has been reversed'))
this.reversePlaylist = !this.reversePlaylist
// Create a new array to avoid changing array in data store state
// it could be user playlist or cache playlist
this.playlistItems = this.playlistItems.toReversed()
setTimeout(() => {
this.isLoading = false
}, 1)
},
togglePauseOnCurrentVideo: function () {
if (this.pauseOnCurrentVideo) {
this.pauseOnCurrentVideo = false
showToast(this.$t('Playlist will not pause when current video is finished'))
} else {
this.pauseOnCurrentVideo = true
showToast(this.$t('Playlist will pause when current video is finished'))
}
},
playNextVideo: function () {
const playlistInfo = {
playlistId: this.playlistId,
playlistType: this.playlistType,
}
const videoIndex = this.videoIndexInPlaylistItems
const targetVideoIndex = (this.videoIsNotPlaylistItem || this.videoIsLastPlaylistItem) ? 0 : videoIndex + 1
if (this.shuffleEnabled) {
let doShufflePlaylistItems = false
if (this.videoIsLastPlaylistItem && !this.loopEnabled) {
showToast(this.$t('The playlist has ended. Enable loop to continue playing'))
return
}
// loopEnabled = true
if (this.videoIsLastPlaylistItem || this.videoIsNotPlaylistItem) { doShufflePlaylistItems = true }
const targetPlaylistItem = this.randomizedPlaylistItems[targetVideoIndex]
this.$router.push(
{
path: `/watch/${targetPlaylistItem.videoId}`,
query: Object.assign(playlistInfo, { playlistItemId: targetPlaylistItem.playlistItemId }),
}
)
showToast(this.$t('Playing Next Video'))
if (doShufflePlaylistItems) { this.shufflePlaylistItems() }
} else {
const targetPlaylistItem = this.playlistItems[targetVideoIndex]
const stopDueToLoopDisabled = this.videoIsLastPlaylistItem && !this.loopEnabled
if (stopDueToLoopDisabled) {
showToast(this.$t('The playlist has ended. Enable loop to continue playing'))
return
}
this.$router.push(
{
path: `/watch/${targetPlaylistItem.videoId}`,
query: Object.assign(playlistInfo, { playlistItemId: targetPlaylistItem.playlistItemId }),
}
)
showToast(this.$t('Playing Next Video'))
}
},
playPreviousVideo: function () {
showToast(this.$t('Playing Previous Video'))
const playlistInfo = {
playlistId: this.playlistId,
playlistType: this.playlistType,
}
const videoIndex = this.videoIndexInPlaylistItems
const targetVideoIndex = (this.videoIsFirstPlaylistItem || this.videoIsNotPlaylistItem) ? this.playlistItems.length - 1 : videoIndex - 1
if (this.shuffleEnabled) {
const targetPlaylistItem = this.randomizedPlaylistItems[targetVideoIndex]
this.$router.push(
{
path: `/watch/${targetPlaylistItem.videoId}`,
query: Object.assign(playlistInfo, { playlistItemId: targetPlaylistItem.playlistItemId }),
}
)
} else {
const targetPlaylistItem = this.playlistItems[targetVideoIndex]
this.$router.push(
{
path: `/watch/${targetPlaylistItem.videoId}`,
query: Object.assign(playlistInfo, { playlistItemId: targetPlaylistItem.playlistItemId }),
}
)
}
},
loadCachedPlaylistInformation: async function (cachedPlaylist) {
this.isLoading = true
this.getPlaylistInfoRun = true
this.setCachedPlaylist(null)
this.playlistTitle = cachedPlaylist.title
this.channelName = cachedPlaylist.channelName
this.channelId = cachedPlaylist.channelId
if (!process.env.SUPPORTS_LOCAL_API || this.backendPreference === 'invidious' || cachedPlaylist.continuationData === null) {
this.playlistItems = cachedPlaylist.items
} else {
const videos = cachedPlaylist.items
await untilEndOfLocalPlayList(cachedPlaylist.continuationData, (p) => {
videos.push(...p.items.map(parseLocalPlaylistVideo))
}, { runCallbackOnceFirst: false })
this.playlistItems = videos
}
this.isLoading = false
},
getPlaylistInformationLocal: async function () {
this.isLoading = true
try {
const playlist = await getLocalPlaylist(this.playlistId)
let channelName
if (playlist.info.author) {
channelName = playlist.info.author.name
} else {
const subtitle = playlist.info.subtitle.toString()
const index = subtitle.lastIndexOf('•')
channelName = subtitle.substring(0, index).trim()
}
this.playlistTitle = playlist.info.title
this.channelName = channelName
this.channelId = playlist.info.author?.id
const videos = []
await untilEndOfLocalPlayList(playlist, (p) => {
videos.push(...p.items.map(parseLocalPlaylistVideo))
})
this.playlistItems = videos
this.isLoading = false
} catch (err) {
console.error(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
showToast(`${errorMessage}: ${err}`, 10000, () => {
copyToClipboard(err)
})
if (this.backendPreference === 'local' && this.backendFallback) {
showToast(this.$t('Falling back to Invidious API'))
this.getPlaylistInformationInvidious()
} else {
this.isLoading = false
}
}
},
getPlaylistInformationInvidious: function () {
this.isLoading = true
invidiousGetPlaylistInfo(this.playlistId).then((result) => {
this.playlistTitle = result.title
this.channelName = result.author
this.channelId = result.authorId
setPublishedTimestampsInvidious(result.videos)
this.playlistItems = this.playlistItems.concat(result.videos)
this.isLoading = false
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Invidious API Error (Click to copy)')
showToast(`${errorMessage}: ${err}`, 10000, () => {
copyToClipboard(err)
})
if (process.env.SUPPORTS_LOCAL_API && this.backendPreference === 'invidious' && this.backendFallback) {
showToast(this.$t('Falling back to Local API'))
this.getPlaylistInformationLocal()
} else {
this.isLoading = false
// TODO: Show toast with error message
}
})
},
parseUserPlaylist: function (playlist, { allowPlayingVideoRemoval = true } = {}) {
this.playlistTitle = playlist.playlistName
this.channelName = ''
this.channelId = ''
if (this.playlistItems.length === 0 || allowPlayingVideoRemoval) {
this.playlistItems = playlist.videos
} else {
// `this.currentVideo` relies on `playlistItems`
const latestPlaylistContainsCurrentVideo = playlist.videos.some(v => v.playlistItemId === this.playlistItemId)
// Only update list of videos if latest video list still contains currently playing video
if (latestPlaylistContainsCurrentVideo) {
this.playlistItems = playlist.videos
}
}
if (this.reversePlaylist) {
this.playlistItems = this.playlistItems.toReversed()
}
this.isLoading = false
},
shufflePlaylistItems: function () {
// Prevents the array from affecting the original object
const remainingItems = [].concat(this.playlistItems)
const items = []
items.push(this.currentVideo)
remainingItems.splice(this.currentVideoIndexZeroBased, 1)
while (remainingItems.length > 0) {
const randomInt = Math.floor(Math.random() * remainingItems.length)
items.push(remainingItems[randomInt])
remainingItems.splice(randomInt, 1)
}
this.randomizedPlaylistItems = items
},
scrollToCurrentVideo: function () {
const container = this.$refs.playlistItems
const currentVideoItem = (this.$refs.currentVideoItem || [])[0]
if (container != null && currentVideoItem != null) {
// Watch view can be ready sooner than this component
container.scrollTop = currentVideoItem.$el.offsetTop - container.offsetTop
}
},
...mapMutations([
'setCachedPlaylist'
])
}
})