Skip to content

Commit

Permalink
[guess works] Fetch tracks from the correct MB object for folded medi…
Browse files Browse the repository at this point in the history
…ums (close #80)
  • Loading branch information
loujine committed Mar 12, 2023
1 parent 133030e commit 5b9f725
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 8 additions & 4 deletions mb-reledit-guess_works.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// @name MusicBrainz relation editor: Guess related works in batch
// @namespace mbz-loujine
// @author loujine
// @version 2023.3.6
// @version 2023.3.12
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-guess_works.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-guess_works.user.js
// @supportURL https://github.com/loujine/musicbrainz-scripts
Expand All @@ -28,8 +28,12 @@ const repeatHelp = `Ways to associate subworks SW1, SW2, SW3... with selected tr

const setWork = async (recording, work, partial) => {
const medium = MB.relationshipEditor.state.mediumsByRecordingId.get(recording.id)[0];
const mediumIdx = medium.position - 1;
const trackIdx = medium.tracks.filter(t => t.recording.id === recording.id)[0].position -1;
const tracks = medium.tracks
// if medium was unfolded manually, medium.tracks stays empty
// but relEditor.state.loadedTracks has the new data
? medium.tracks
: MB.relationshipEditor.state.loadedTracks.get(medium.position);
const track = tracks.filter(t => t.recording.id === recording.id)[0];

await helper.waitFor(() => !MB.relationshipEditor.relationshipDialogDispatch, 1);
MB.relationshipEditor.dispatch({
Expand All @@ -38,7 +42,7 @@ const setWork = async (recording, work, partial) => {
batchSelection: false,
source: recording,
targetType: 'work',
track: MB.relationshipEditor.state.entity.mediums[mediumIdx].tracks[trackIdx],
track: track,
},
});
await helper.waitFor(() => !!MB.relationshipEditor.relationshipDialogDispatch, 1);
Expand Down
8 changes: 7 additions & 1 deletion mbz-loujine-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// @name mbz-loujine-common
// @namespace mbz-loujine
// @author loujine
// @version 2023.3.9
// @version 2023.3.12
// @description musicbrainz.org: common functions
// @compatible firefox+greasemonkey
// @license MIT
Expand Down Expand Up @@ -882,7 +882,13 @@ class RelationshipEditor {

// sort recordings by order in tracklist to avoid having the dialog jump everywhere
const recOrder = MB.getSourceEntityInstance().mediums.flatMap(
// tracks on mediums 1-10 loaded by default
m => m.tracks
).concat(
// tracks on unfolded mediums
Array.from(MB.relationshipEditor.state.loadedTracks.keys()).sort().flatMap(
k => MB.relationshipEditor.state.loadedTracks.get(k)
)
).map(t => t.recording.id);
recordings.sort((r1, r2) => recOrder.indexOf(r1.id) - recOrder.indexOf(r2.id));
return recordings;
Expand Down

0 comments on commit 5b9f725

Please sign in to comment.