Allow pasting Playlist URLs in search to navigate straight to playlist page

This commit is contained in:
Preston 2021-01-15 11:34:44 -05:00
parent d5304cfcdd
commit 890d5b4a09
2 changed files with 46 additions and 18 deletions

View File

@ -77,7 +77,7 @@ export default Vue.extend({
this.debounceSearchResults = debounce(this.getSearchSuggestions, 200) this.debounceSearchResults = debounce(this.getSearchSuggestions, 200)
}, },
methods: { methods: {
goToSearch: function (query) { goToSearch: async function (query) {
const appWidth = $(window).width() const appWidth = $(window).width()
if (appWidth <= 680) { if (appWidth <= 680) {
@ -89,23 +89,30 @@ export default Vue.extend({
searchInput.blur() searchInput.blur()
} }
this.$store.dispatch('getVideoIdFromUrl', query).then((result) => { const videoId = await this.$store.dispatch('getVideoIdFromUrl', query)
if (result) { const playlistId = await this.$store.dispatch('getPlaylistIdFromUrl', query)
this.$router.push({
path: `/watch/${result}` console.log(playlistId)
})
} else { if (videoId) {
router.push({ this.$router.push({
path: `/search/${encodeURIComponent(query)}`, path: `/watch/${videoId}`
query: { })
sortBy: this.searchSettings.sortBy, } else if (playlistId) {
time: this.searchSettings.time, this.$router.push({
type: this.searchSettings.type, path: `/playlist/${playlistId}`
duration: this.searchSettings.duration })
} } else {
}) router.push({
} path: `/search/${encodeURIComponent(query)}`,
}) query: {
sortBy: this.searchSettings.sortBy,
time: this.searchSettings.time,
type: this.searchSettings.type,
duration: this.searchSettings.duration
}
})
}
this.showFilters = false this.showFilters = false
}, },

View File

@ -229,6 +229,27 @@ const actions = {
return extractors.reduce((a, c) => a || c(), null) || false return extractors.reduce((a, c) => a || c(), null) || false
}, },
getPlaylistIdFromUrl (_, url) {
/** @type {URL} */
let urlObject
try {
urlObject = new URL(url)
} catch (e) {
return false
}
const extractors = [
// anything with /playlist?list=
function() {
if (urlObject.pathname === '/playlist' && urlObject.searchParams.has('list')) {
return urlObject.searchParams.get('list')
}
}
]
return extractors.reduce((a, c) => a || c(), null) || false
},
padNumberWithLeadingZeros(_, payload) { padNumberWithLeadingZeros(_, payload) {
let numberString = payload.number.toString() let numberString = payload.number.toString()
while (numberString.length < payload.length) { while (numberString.length < payload.length) {