diff --git a/src/common/utils/common.ts b/src/common/utils/common.ts index 21797d3b..146bbad5 100644 --- a/src/common/utils/common.ts +++ b/src/common/utils/common.ts @@ -225,3 +225,23 @@ export const arrPushByPosition = (list: T[], newList: T[], position: number) } return list } + + +// https://stackoverflow.com/a/2450976 +export const arrShuffle = (array: T[]) => { + let currentIndex = array.length + let randomIndex + + // While there remain elements to shuffle. + while (currentIndex != 0) { + // Pick a remaining element. + randomIndex = Math.floor(Math.random() * currentIndex) + currentIndex--; + + // And swap it with the current element. + [array[currentIndex], array[randomIndex]] = [ + array[randomIndex], array[currentIndex]] + } + + return array +} diff --git a/src/lang/en-us.json b/src/lang/en-us.json index c1b9c95f..3a89b82b 100644 --- a/src/lang/en-us.json +++ b/src/lang/en-us.json @@ -125,6 +125,7 @@ "list_sort_modal_by_down": "Descending", "list_sort_modal_by_field": "Sort field", "list_sort_modal_by_name": "Song name", + "list_sort_modal_by_random": "Random", "list_sort_modal_by_singer": "Singer name", "list_sort_modal_by_source": "Song source", "list_sort_modal_by_time": "Duration", diff --git a/src/lang/zh-cn.json b/src/lang/zh-cn.json index e04a410e..e36c9e26 100644 --- a/src/lang/zh-cn.json +++ b/src/lang/zh-cn.json @@ -125,12 +125,12 @@ "list_sort_modal_by_down": "降序", "list_sort_modal_by_field": "排序字段", "list_sort_modal_by_name": "歌曲名", + "list_sort_modal_by_random": "随机乱序", "list_sort_modal_by_singer": "歌手名", "list_sort_modal_by_source": "歌曲源", "list_sort_modal_by_time": "时长", "list_sort_modal_by_type": "排序类别", "list_sort_modal_by_up": "升序", - "list_sort_modal_by_disorder": "随机乱序", "list_sort_modal_tip_confirm": "你确定要这么做吗?", "list_update_modal__auto_update": "自动更新", "list_update_modal__tips": "💡 每次启动软件时将会自动更新已勾选“自动更新”的列表", diff --git a/src/lang/zh-tw.json b/src/lang/zh-tw.json index 353422b7..0587da8e 100644 --- a/src/lang/zh-tw.json +++ b/src/lang/zh-tw.json @@ -125,6 +125,7 @@ "list_sort_modal_by_down": "降序", "list_sort_modal_by_field": "排序字段", "list_sort_modal_by_name": "歌曲名", + "list_sort_modal_by_random": "隨機亂序", "list_sort_modal_by_singer": "歌手名", "list_sort_modal_by_source": "歌曲源", "list_sort_modal_by_time": "時長", diff --git a/src/renderer/views/List/MyList/components/ListSortModal.vue b/src/renderer/views/List/MyList/components/ListSortModal.vue index 1c2430f1..1bcdd68a 100644 --- a/src/renderer/views/List/MyList/components/ListSortModal.vue +++ b/src/renderer/views/List/MyList/components/ListSortModal.vue @@ -10,31 +10,31 @@
  • @@ -56,8 +56,8 @@
  • @@ -102,7 +102,7 @@ export default { sortType.value = '' } const verify = () => { - return !!sortField.value && !!sortType.value + return !!sortType.value && (!!sortField.value || sortType.value == 'random') } const handleSort = async() => { if (!verify()) return @@ -131,9 +131,14 @@ export default { } }) + const disabledSortFislds = computed(() => { + return sortType.value == 'random' + }) + return { sortField, sortType, + disabledSortFislds, closeModal, handleSort, handleAfterLeave, diff --git a/src/renderer/worker/main/list.ts b/src/renderer/worker/main/list.ts index 1218293f..566442e6 100644 --- a/src/renderer/worker/main/list.ts +++ b/src/renderer/worker/main/list.ts @@ -1,6 +1,6 @@ // import { throttle } from '@common/utils' -import { filterFileName, sortInsert, similar, arrPushByPosition } from '@common/utils/common' +import { filterFileName, sortInsert, similar, arrPushByPosition, arrShuffle } from '@common/utils/common' import { joinPath, saveStrToFile } from '@common/utils/nodejs' import { createLocalMusicInfo } from '@renderer/utils/music' @@ -79,45 +79,14 @@ const getIntv = (musicInfo: LX.Music.MusicInfo) => { * @param localeId 排序语言 * @returns */ -export const sortListMusicInfo = async(list: LX.Music.MusicInfo[], sortType: 'up' | 'down' | 'disorder', fieldName: 'name' | 'singer' | 'albumName' | 'interval' | 'source', localeId: string) => { +export const sortListMusicInfo = async(list: LX.Music.MusicInfo[], sortType: 'up' | 'down' | 'random', fieldName: 'name' | 'singer' | 'albumName' | 'interval' | 'source', localeId: string) => { // console.log(sortType, fieldName, localeId) // const locale = new Intl.Locale(localeId) - if (sortType == 'disorder' && (fieldName =='name' || fieldName == 'singer' || fieldName =='albumName' || fieldName =='interval' || fieldName =='source')){ - - let change_num:number[]=[] - - for(let i:number = 0;i { if (a.interval == null) { @@ -144,7 +113,8 @@ export const sortListMusicInfo = async(list: LX.Music.MusicInfo[], sortType: 'up break } } - } else { + break + case 'down': if (fieldName == 'interval') { list.sort((a, b) => { if (a.interval == null) { @@ -171,11 +141,9 @@ export const sortListMusicInfo = async(list: LX.Music.MusicInfo[], sortType: 'up break } } - } - return list - + break } - + return list } const variantRxp = /(\(|().+(\)|))/g