diff --git a/public/music.js b/public/music.js index 41131d8..0219f17 100644 --- a/public/music.js +++ b/public/music.js @@ -17,7 +17,6 @@ export default class MusicList { //this.audio.addEventListener('timeupdate', () => { // console.log(this.audio.currentTime) //}) - // 本地添加音乐按钮 const input = document.createElement('input') input.type = 'file' @@ -73,10 +72,18 @@ export default class MusicList { this.EventListeners[name] = callback } add(item) { + // 将字节转换为可读的单位 + const bytesToSize = bytes => { + if (bytes === 0) return '0 B' + const k = 1024 + const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] + const i = Math.floor(Math.log(bytes) / Math.log(k)) + return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i] + } this.list.push(item) this.ul.appendChild(ListItem({ id: item.id, - innerText: `${item.name} - ${item.size} - ${item.type} - ${item.id}`, + innerText: `${item.name} - ${bytesToSize(item.size)}`, children: [ Button({ innerText: '播放', diff --git a/public/weigets.js b/public/weigets.js index 87f86c7..37edc0c 100644 --- a/public/weigets.js +++ b/public/weigets.js @@ -11,11 +11,12 @@ export function List({ children = [] }) { return ul } -export function ListItem({ innerText, onclick, id, children = [] }) { +export function ListItem({ innerText, onclick, id, children = [], dataset }) { const li = document.createElement('li') li.innerText = innerText li.onclick = onclick li.id = id + dataset && Object.keys(dataset).forEach(key => li.dataset[key] = dataset[key]) children.forEach(child => li.appendChild(child)) return li }