installed plugin AudioIgniter version 1.7.3

This commit is contained in:
2022-04-17 00:39:10 +00:00
committed by Gitium
parent dbc3ae04ae
commit 55ea6cf8dd
56 changed files with 13860 additions and 0 deletions

View File

@ -0,0 +1,14 @@
/**
* Shifts an array to right / left by n positions.
*
* @param {Array} arr The array.
* @param {number} direction The direction - 0 for left 1 for right.
* @param {number} n Number of positions to shift by.
* @returns {any[]}
*/
const arrayShift = (arr, direction, n) => {
const times = n > arr.length ? n % arr.length : n;
return arr.concat(arr.splice(0, direction > 0 ? arr.length - times : times));
};
export default arrayShift;

View File

@ -0,0 +1,24 @@
/**
* Shuffles an array.
* Copied from https://github.com/sindresorhus/array-shuffle
*
* @param {Array} array The array to be shuffled.
* @returns {*[]|*}
*/
const arrayShuffle = array => {
if (!Array.isArray(array)) {
return array;
}
const clone = [...array];
// eslint-disable-next-line no-plusplus
for (let index = clone.length - 1; index > 0; index--) {
const newIndex = Math.floor(Math.random() * (index + 1));
[clone[index], clone[newIndex]] = [clone[newIndex], clone[index]];
}
return clone;
};
export default arrayShuffle;

View File

@ -0,0 +1,76 @@
import arrayShuffle from './array-shuffle';
import arrayShift from './array-shift';
/**
* Fetches the initial track index.
*
* @param {Object} options The options.
* @param {Array} options.tracks The tracks.
* @param {number} [options.initialTrack] The initial track index.
* @param {boolean} options.reverseTrackOrder Whether the track order is reversed.
* @returns {number}
*/
export const getInitialTrackIndex = ({
tracks = [],
initialTrack = 1,
reverseTrackOrder = false,
}) => {
// The user provides a 1-index value.
const initialTrackIndex = initialTrack - 1;
if (!tracks.length || !initialTrack || initialTrack > tracks.length) {
return 0;
}
if (reverseTrackOrder) {
return Math.max(tracks.length - initialTrack, 0);
}
return initialTrackIndex;
};
/**
* Fetches the initial track index and the initial track queue.
*
* @param {Object} options The options.
* @param {Array} options.tracks The tracks.
* @param {Number} options.initialTrack The initial track number (1-indexed).
* @param {Boolean} reverseTrackOrder Whether the track order is reversed.
* @param {Boolean} shuffle Whether the track queue is shuffled.
* @returns {{activeIndex: number, trackQueue: (*[]|*)}|{activeIndex: number, trackQueue: *}}
*/
export const getInitialTrackQueueAndIndex = ({
tracks = [],
initialTrack = 1,
reverseTrackOrder = false,
shuffle = false,
}) => {
const activeIndex = getInitialTrackIndex({
tracks,
initialTrack,
reverseTrackOrder,
});
const orderedTrackIndexes = tracks.map((_, index) => index);
if (!shuffle) {
const shiftAmount = orderedTrackIndexes.indexOf(activeIndex);
return {
activeIndex,
trackQueue: arrayShift(orderedTrackIndexes, 0, shiftAmount),
};
}
const shuffledQueue = arrayShuffle(orderedTrackIndexes);
// Always bring the initial track (activeIndex) to the front of the queue.
shuffledQueue.splice(shuffledQueue.indexOf(activeIndex), 1);
shuffledQueue.unshift(activeIndex);
return {
activeIndex,
trackQueue: shuffledQueue,
};
};
export default getInitialTrackIndex;

View File

@ -0,0 +1,8 @@
const multiSoundDisabled = () => {
return (
window.ai_pro_front_scripts &&
!!window.ai_pro_front_scripts.multi_sound_disabled
);
};
export default multiSoundDisabled;

View File

@ -0,0 +1,88 @@
export default class SoundCloud {
constructor(clientId) {
if (!clientId) {
throw new Error('SoundCloud client ID is required');
}
this.clientId = clientId;
this.baseUrl = 'https://api.soundcloud.com';
}
/**
* Checks if a URL is from SoundCloud
*
* @param {string} url - URL to be checked
*
* @returns {boolean}
*/
static isSoundCloudUrl(url) {
return url.indexOf('soundcloud.com') > -1;
}
/**
* Resolves a SoundCloud URL into a track object
*
* @param {string} url - URL to be resolved
*
* @returns {Promise.<*>}
*/
resolve(url) {
/*
* Tell the SoundCloud API not to serve a redirect. This is to get around
* CORS issues on Safari 7+, which likes to send pre-flight requests
* before following redirects, which has problems.
*
* https://github.com/soundcloud/soundcloud-javascript/issues/27
*/
const statusCodeMap = encodeURIComponent('_status_code_map[302]=200');
return fetch(
`${this.baseUrl}/resolve?url=${url}&client_id=${
this.clientId
}&${statusCodeMap}`,
)
.then(res => res.json())
.then(res => fetch(res.location))
.then(res => res.json());
}
/**
* Resolves and fetches SoundCloud track objects
*
* @param {Object[]} tracks - Tracks object
*
* @returns {Promise.<*>}
*/
fetchSoundCloudStreams(tracks) {
const scTracks = tracks
.filter(track => SoundCloud.isSoundCloudUrl(track.audio))
.map(track => this.resolve(track.audio));
return Promise.all(scTracks);
}
/**
* Maps a SoundCloud tracks object into an AudioIgniter one
* by replacing `track.audio` with `sctrack.stream_url`.
*
* Works *in order* of appearance in the `tracks` object.
*
* @param {Object[]} tracks - AudioIgniter tracks object
* @param {Object[]} scTracks - SoundCloud tracks object
*
* @returns {Object[]}
*/
mapStreamsToTracks(tracks, scTracks) {
let i = 0;
return tracks.map(track => {
if (SoundCloud.isSoundCloudUrl(track.audio)) {
// eslint-disable-next-line no-param-reassign
track.audio = `${scTracks[i].stream_url}?client_id=${this.clientId}`;
i++; // eslint-disable-line no-plusplus
}
return track;
});
}
}

View File

@ -0,0 +1,8 @@
const typographyDisabled = () => {
return (
window.ai_pro_front_scripts &&
!!window.ai_pro_front_scripts.typography_disabled
);
};
export default typographyDisabled;