webrtc/public/weigets.js

319 lines
11 KiB
JavaScript
Raw Normal View History

2023-10-13 03:10:39 +08:00
export function createElement({ innerText, textContent, readOnly, children = [], dataset, style, classList = [], ...attributes }, tagName = 'div') {
2023-10-02 22:28:55 +08:00
const element = document.createElement(tagName)
2023-10-13 03:10:39 +08:00
for (const key in attributes) {
if (key.startsWith('on')) element[key] = attributes[key] // 如果是事件则直接赋值
else element.setAttribute(key, attributes[key]) // 否则是属性则使用setAttribute
}
2023-10-04 20:05:02 +08:00
if (style) Object.assign(element.style, style)
2023-10-04 06:14:38 +08:00
if (classList.length) element.classList.add(...classList)
2023-10-02 22:24:38 +08:00
if (textContent) element.textContent = textContent
2023-10-07 00:49:02 +08:00
if (innerText) element.innerText = innerText
if (readOnly) element.readOnly = readOnly
2023-10-07 00:49:02 +08:00
if (dataset) Object.assign(element.dataset, dataset)
2023-10-02 22:24:38 +08:00
if (children) children.forEach(child => element.appendChild(child))
return element
2023-09-28 15:20:02 +08:00
}
2023-10-02 22:28:55 +08:00
export function List(options) {
return createElement(options, 'ul')
2023-09-28 15:20:02 +08:00
}
2023-10-02 08:11:28 +08:00
2023-10-02 22:28:55 +08:00
export function ListItem(options) {
return createElement(options, 'li')
2023-10-02 22:24:38 +08:00
}
2023-10-02 22:28:55 +08:00
export function Span(options) {
return createElement(options, 'span')
}
export function Button(options) {
return createElement(options, 'button')
2023-10-02 08:11:28 +08:00
}
2023-10-04 17:34:15 +08:00
export function Input(options) {
return createElement(options, 'input')
}
export function Avatar(options) {
const element = createElement(options, 'img')
element.onerror = () => element.src = '/favicon.ico'
return element
}
2023-10-04 18:01:43 +08:00
2023-10-12 03:00:30 +08:00
export function UploadMusic(options) {
2023-10-13 01:28:33 +08:00
let dragStats = null
2023-10-13 01:07:36 +08:00
const drop = createElement({
2023-10-13 00:50:58 +08:00
textContent: '点击或拖拽音乐到此处共享您的音乐',
style: {
width: '100%',
color: '#999',
lineHeight: '5rem',
textAlign: 'center',
position: 'absolute',
top: 0,
display: 'none'
}
})
2023-10-12 03:00:30 +08:00
return createElement({
...options,
style: {
width: '100%',
height: '10rem',
backdropFilter: 'blur(5px)',
backgroundColor: '#fcfcfc',
borderRadius: '1em',
border: '1px solid #f1f1f1',
position: 'relative',
userSelect: 'none',
cursor: 'pointer',
...options.style
},
onclick: event => {
// 临时创建一个input触发input的点击事件
const input = Input({
type: 'file',
multiple: true,
accept: 'audio/*',
style: {
display: 'none',
},
onchange: event => {
const files = Array.from(event.target.files)
if (files.length === 0) return
options.onchange(files)
}
})
input.click()
},
2023-10-13 00:50:58 +08:00
onmouseenter: event => {
2023-10-13 01:07:36 +08:00
drop.style.display = 'block'
2023-10-13 00:50:58 +08:00
},
onmouseleave: event => {
2023-10-13 01:07:36 +08:00
drop.style.display = 'none'
},
ondragover: event => {
console.log('dragover')
event.preventDefault()
event.stopPropagation()
2023-10-13 01:28:33 +08:00
if(dragStats) return
2023-10-13 01:07:36 +08:00
event.dataTransfer.dropEffect = 'copy'
drop.style.display = 'block'
},
2023-10-13 01:28:33 +08:00
ondragleave: event => {
event.preventDefault()
event.stopPropagation()
if(dragStats) return
clearTimeout(dragStats)
dragStats = setTimeout(() => {
drop.style.display = 'none'
dragStats = null
}, 1000)
},
2023-10-13 01:07:36 +08:00
ondrop: event => {
console.log('drop')
event.preventDefault()
event.stopPropagation()
const files = Array.from(event.dataTransfer.files)
2023-10-13 01:40:31 +08:00
// 检查必须是音乐文件, 移除其它文件
for (let i = 0; i < files.length; i++) {
if (!files[i].type.startsWith('audio/')) {
files.splice(i, 1)
i--
}
}
2023-10-13 01:07:36 +08:00
if (files.length === 0) return console.log('没有文件')
console.log('files', files)
options.onchange(files)
},
2023-10-12 03:00:30 +08:00
children: [
2023-10-12 22:18:36 +08:00
// 绘制一个云朵上传图标(别放弃...还有我呢!)
2023-10-12 03:00:30 +08:00
createElement({
style: {
2023-10-13 00:50:58 +08:00
width: '82px',
height: '30px',
background: '#e7f4fd',
background: '-webkit-linear-gradient(top,#e7f4fd 5%,#ceedfd 100%)',
borderRadius: '25px',
position: 'relative',
margin: '33px auto 5px'
2023-10-12 03:00:30 +08:00
},
children: [
createElement({
style: {
2023-10-13 00:50:58 +08:00
width: '45px',
height: '45px',
top: '-22px',
right: '12px',
borderRadius: '50px',
borderRadius: '25px',
background: '#e7f4fd',
2023-10-12 03:00:30 +08:00
position: 'absolute',
2023-10-13 00:50:58 +08:00
zIndex: '-1'
2023-10-12 03:00:30 +08:00
}
2023-10-13 00:50:58 +08:00
}),
createElement({
style: {
width: '25px',
height: '25px',
top: '-12px',
left: '12px',
borderRadius: '25px',
background: '#e7f4fd',
position: 'absolute',
zIndex: '-1'
}
}),
Span({
width: '87px',
position: 'absolute',
bottom: '-2px',
background: '#000',
zIndex: '-1',
boxShadow: '0 0 6px 2px rgba(0,0,0,0.1)',
borderRadius: '50%'
2023-10-13 02:01:37 +08:00
}),
createElement({
textContent: '♫',
style: {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
fontSize: '3rem',
fontWeight: 'bold',
position: 'relative',
top: '-1.5rem',
color: '#e7f4fd',
textShadow: '0 1px 1px #fff'
//textShadow: '0 0 5px #000, 0 0 10px #000, 0 0 15px #000, 0 0 20px #000, 0 0 25px #000, 0 0 30px #000, 0 0 35px #000, 0 0 40px #000, 0 0 45px #000, 0 0 50px #000, 0 0 55px #000, 0 0 60px #000, 0 0 65px #000, 0 0 70px #000, 0 0 75px #000'
}
2023-10-12 03:00:30 +08:00
})
2023-10-13 00:50:58 +08:00
]
}),
2023-10-13 01:07:36 +08:00
drop
2023-10-12 03:00:30 +08:00
]
})
}
2023-10-04 18:01:43 +08:00
// 弹出窗口, 高斯模糊背景, 进入离开动画过渡
export function Dialog(options) {
2023-10-07 01:53:06 +08:00
const element = createElement({
tabIndex: 0,
style: {
position: 'fixed',
top: 0,
left: 0,
zIndex: 1000,
width: '100%',
height: '100%',
backdropFilter: 'blur(5px)',
duration: '0.5s',
transition: 'all 0.5s'
},
onclick: async event => {
// 判断必须是点击自身, 而不是子元素
if (event.target !== event.currentTarget) return
2023-10-07 01:53:06 +08:00
await event.target.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 100 }).finished
await event.target.remove()
},
onkeydown: async event => {
if (event.key !== 'Escape') return
await event.target.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 100 }).finished
await event.target.remove()
},
children: [
createElement({
...options,
style: {
position: 'fixed',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
backgroundColor: '#fff',
borderRadius: '150px',
boxShadow: '0 0 1em #ccc',
overflow: 'hidden',
display: 'flex',
justifyContent: 'center',
...options.style,
}
})
]
})
2023-10-05 14:03:51 +08:00
// 显示时自动聚焦
2023-10-07 01:53:06 +08:00
const observer = new MutationObserver(mutationsList => {
2023-10-05 14:03:51 +08:00
for (const mutation of mutationsList) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
element.focus()
2023-10-05 14:08:29 +08:00
element.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 100 }).finished
2023-10-07 01:53:06 +08:00
return observer.disconnect()
2023-10-05 14:03:51 +08:00
}
}
})
observer.observe(document.body, { childList: true, subtree: true })
2023-10-04 18:01:43 +08:00
return element
}
2023-10-07 00:38:09 +08:00
// 深度代理, 用于监听数据的变化
2023-10-07 22:29:23 +08:00
export function useProxy(data, callback, path = []) {
if (Array.isArray(data)) {
const array = class extends Array {
constructor(args) {
super(args)
}
push(args) {
super.push(args)
console.log('push', args)
this.__notify()
}
pop(...args) {
const result = super.pop(...args)
console.log('pop')
this.__notify()
return result
}
shift(...args) {
const result = super.shift(...args)
this.__notify()
return result
}
unshift(...args) {
super.unshift(...args)
this.__notify()
}
splice(...args) {
super.splice(...args)
this.__notify()
}
deleteProperty(...args) {
console.log('deleteProperty', ...args)
super.deleteProperty(...args)
this.__notify()
}
__notify() {
if (callback) callback()
2023-10-07 00:38:09 +08:00
}
}
2023-10-07 22:29:23 +08:00
//console.log('为数组每项递归创建代理')
data.forEach((item, index) => {
data[index] = useProxy(item, callback, [...path, index])
})
return new array(...data)
}
if (typeof data === 'object' && data !== null) {
//console.log('为对象属性递归创建代理')
Object.keys(data).forEach(key => {
if (typeof data[key] === 'object' && data[key] !== null) {
data[key] = useProxy(data[key], callback, [...path, key])
}
})
return new Proxy(data, {
deleteProperty(target, key) {
console.log('deleteProperty', key)
return delete target[key]
}
})
}
console.log('为其它类型直接返回')
return data
2023-10-07 00:38:09 +08:00
}