From f50c1fe7e7d5f29e9b943f9843663a3800949b18 Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 19 Dec 2018 22:05:59 -0400 Subject: Revive the old shuffle-groups feature! Cool! This was one of my favorite tiny features of http-music. --- playlist-utils.js | 14 ++++++++++++++ ui.js | 13 +++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/playlist-utils.js b/playlist-utils.js index 64c1df4..ac6a929 100644 --- a/playlist-utils.js +++ b/playlist-utils.js @@ -6,6 +6,8 @@ const fs = require('fs') const { promisify } = require('util') const unlink = promisify(fs.unlink) +const { shuffleArray } = require('./general-util') + const parentSymbol = Symbol('Parent group') function updatePlaylistFormat(playlist) { @@ -188,6 +190,17 @@ function countTotalItems(grouplike) { }).reduce((a, b) => a + b, 0) } +function shuffleOrderOfGroups(grouplike) { + if (isGroup(grouplike) && grouplike.items.every(isGroup)) { + const items = grouplike.items.map(shuffleOrderOfGroups) + const shuffled = shuffleArray(items) + + return Object.assign({}, grouplike, {items: shuffled}) + } else { + return grouplike + } +} + function collectGrouplikeChildren(grouplike, filter = null) { // Collects all descendants of a grouplike into a single flat array. // Can be passed a filter function, which will decide whether or not to add @@ -506,6 +519,7 @@ module.exports = { cloneGrouplike, filterTracks, flattenGrouplike, countTotalItems, + shuffleOrderOfGroups, partiallyFlattenGrouplike, collapseGrouplike, filterGrouplikeByProperty, filterPlaylistByPathString, filterGrouplikeByPath, diff --git a/ui.js b/ui.js index 175bf73..6f8b07a 100644 --- a/ui.js +++ b/ui.js @@ -1,7 +1,7 @@ const { getAllCrawlersForArg } = require('./crawlers') const { getDownloaderFor } = require('./downloaders') const { getPlayer } = require('./players') -const { parentSymbol, isGroup, isTrack, getItemPath, getItemPathString, flattenGrouplike, countTotalItems, cloneGrouplike } = require('./playlist-utils') +const { parentSymbol, isGroup, isTrack, getItemPath, getItemPathString, flattenGrouplike, countTotalItems, shuffleOrderOfGroups, cloneGrouplike } = require('./playlist-utils') const { shuffleArray } = require('./general-util') const processSmartPlaylist = require('./smart-playlist') const UndoManager = require('./undo-manager') @@ -110,8 +110,12 @@ class AppElement extends FocusElement { grouplikeListing.on('menu', (item, opts) => this.menu.show(opts)) grouplikeListing.on('queue', (item, {where = 'end', order = 'normal', play = false} = {}) => { - if (isGroup(item) && order === 'shuffle') { - item = {items: shuffleArray(flattenGrouplike(item).items)} + if (isGroup(item)) { + if (order === 'shuffle') { + item = {items: shuffleArray(flattenGrouplike(item).items)} + } else if (order === 'shuffle-groups') { + item = shuffleOrderOfGroups(item) + } } let afterItem = null @@ -1177,7 +1181,8 @@ class InteractiveGrouplikeItemElement extends BasicGrouplikeItemElement { ]) this.orderControl = new InlineListPickerElement('Order?', [ - {value: 'shuffle', label: 'Shuffle'}, + {value: 'shuffle', label: 'Shuffle all'}, + {value: 'shuffle-groups', label: 'Shuffle order of groups'}, {value: 'normal', label: 'In order'} ]) } -- cgit 1.3.0-6-gf8a5