Skip to content

Commit

Permalink
- Selection manipulation\Group: new tool to group tracks by TF withou…
Browse files Browse the repository at this point in the history
…t respecting the original sorting. It may be used to listen to all tracks of a random album played in a shuffled order, then all of another album (chosen randomly), ...
  • Loading branch information
regorxxx committed Jul 21, 2023
1 parent 067ff2a commit fc45796
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
7 changes: 7 additions & 0 deletions helpers/readme/group_by_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Reorders selection to group consecutive tracks with the same configurable
tag. Can be used to group by album, artist, ...

Note it doesn't perform any sorting, so it respects the original one.

i.e. it may be used to listen to all tracks of a random album played in a
shuffled order, then all of another album (chosen randomly), ...
75 changes: 75 additions & 0 deletions main/sort/group_by_tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';
//21/07/23

include('..\\..\\helpers\\helpers_xxx_basic_js.js');
include('..\\..\\helpers\\helpers_xxx_prototypes.js');

/*
groupByTags
-----------------------------------
Reorders selection to group consecutive tracks with the same value by tags ('tagName').
Respects source sorting, contrary to sorting tools which also group items by values.
Output is sent to active playlist or as a handle list by setting 'bSendToActivePls'.
*/

function groupByTags({
tagName = ['ALBUM'],
selItems = plman.ActivePlaylist !== -1 ? plman.GetPlaylistSelectedItems(plman.ActivePlaylist) : null,
bSendToActivePls = true
} = {}) {
// Safety checks
if (!tagName.length) {return;}
if (!selItems || selItems.Count <= 2) {return;}
// Convert input
const totalTracks = selItems.Count;
let selItemsArray = selItems.Clone().Convert();
// Get tag values
const tagValues = getTagsValuesV3(selItems, tagName, true);
const valuesMap = new Map();
for (let i = 0; i < totalTracks; i++) {
const tagValue_i = tagValues[i].filter(Boolean).map((item) => {return item.toLowerCase();}).join(', ');
if (valuesMap.has(tagValue_i)) {
valuesMap.get(tagValue_i).Add(selItemsArray[i]);
} else {
valuesMap.set(tagValue_i, new FbMetadbHandleList([selItemsArray[i]]));
}
}
// And output
selItemsArray = [...valuesMap.values()].reduce((acc, val) => {acc.AddRange(val); return acc;}, new FbMetadbHandleList());
if (bSendToActivePls) {
// 'Hack' Inserts on focus (may be at any place of selection), but then removes the original selection,
// so inserted tracks get sent to the right position. Only works for contiguous selections!
const focusIdx = plman.GetPlaylistFocusItemIndex(plman.ActivePlaylist);
plman.UndoBackup(plman.ActivePlaylist);
plman.InsertPlaylistItems(plman.ActivePlaylist, plman.GetPlaylistFocusItemIndex(plman.ActivePlaylist), selItemsArray);
plman.RemovePlaylistSelection(plman.ActivePlaylist);
// Try to restore prev. selection
let idx = [];
if (focusIdx === 0) {
idx = range(0, totalTracks - 1, 1);
} else {
const clone = selItemsArray.Clone();
clone.Sort();
const plsItems = plman.GetPlaylistItems(plman.ActivePlaylist).Convert();
const end = focusIdx - totalTracks + 1;
if (end >= 0) {
const plsItemsBelow = new FbMetadbHandleList(plsItems.slice(end, end + totalTracks));
plsItemsBelow.Sort();
plsItemsBelow.MakeIntersection(clone);
if (plsItemsBelow.Count === totalTracks) {idx = range(end, focusIdx, 1);}
}
if (end < 0 || !idx.lenth) {
const plsItemsOver = new FbMetadbHandleList(plsItems.slice(focusIdx, focusIdx + totalTracks));
plsItemsOver.Sort();
plsItemsOver.MakeIntersection(clone);
if (plsItemsOver.Count === totalTracks) {idx = range(focusIdx, focusIdx + totalTracks - 1, 1);}
}
}
if (idx.length === totalTracks) {
plman.SetPlaylistSelection(plman.ActivePlaylist, idx, true);
plman.SetPlaylistFocusItem(plman.ActivePlaylist, focusIdx);
}
console.log('Selection grouped by tag(s) \'' + tagName.join(', ') + '\' on playlist: ' + plman.GetPlaylistName(plman.ActivePlaylist));
}
return selItemsArray;
}

0 comments on commit fc45796

Please sign in to comment.