widgets/main.js

761 lines
20 KiB
JavaScript
Raw Normal View History

2024-06-27 14:44:29 +08:00
export function createElement({ innerText, innerHTML, textContent, readOnly, children = [], dataset, style, classList = [], ...attributes }, tagName = 'div', __name = '') {
2024-01-12 04:38:20 +08:00
const element = document.createElement(tagName)
for (const key in attributes) {
if (key.startsWith('on')) element[key] = attributes[key] // 如果是事件则直接赋值
else element.setAttribute(key, attributes[key]) // 否则是属性则使用setAttribute
}
if (dataset) Object.assign(element.dataset, dataset)
if (style) Object.assign(element.style, style)
if (classList.length) element.classList.add(...classList)
if (textContent) element.textContent = textContent
if (innerText) element.innerText = innerText
if (innerHTML) element.innerHTML = innerHTML
if (readOnly) element.readOnly = readOnly
if (children) children.forEach(child => element.appendChild(child))
2024-05-16 05:00:08 +08:00
2024-06-27 14:44:29 +08:00
// 生成样式
console.log('name:', __name)
// 分解类名, 生成样式, 直接将样式赋予元素
__name.split('.').map(name => name.split('_')).forEach(item => {
console.log('item:', item)
// 两个参数的情况下, 第一个参数是样式名, 第二个参数是样式值
if (item.length === 2) {
const [key, value] = item
if (key === 'cursor') {
element.style.cursor = value
return
}
if (key === 'overflow') {
element.style.overflow = value
return
}
if (key === 'bg') {
element.style.backgroundColor = value
return
}
const styleMap = {
'w': ['width'],
'h': ['height'],
'p': ['padding'],
'm': ['margin'],
'pt': ['paddingTop'],
'pl': ['paddingLeft'],
'pr': ['paddingRight'],
'pb': ['paddingBottom'],
'mt': ['marginTop'],
'ml': ['marginLeft'],
'mr': ['marginRight'],
'mb': ['marginBottom'],
'px': ['paddingLeft', 'paddingRight'],
'py': ['paddingTop', 'paddingBottom'],
'mx': ['marginLeft', 'marginRight'],
'my': ['marginTop', 'marginBottom']
}
if (styleMap.hasOwnProperty(key)) {
styleMap[key].forEach(style => {
element.style[style] = isNaN(value) ? value : `${value}px`
})
return
}
if (key === 'radius') {
element.style.borderRadius = value
return
}
if (key === 'font') {
element.style.font = value
return
}
}
// 三个参数的情况下, 第一个参数是样式名, 第二个参数是样式, 第三个参数是数值
if (item.length === 3) {
if (item[0] === 'color') {
element.style.color = item[1]
return
}
}
// px py mx my
// p-1 p-2 p-3 p-4
// m-1 m-2 m-3 m-4
// w h z bg
})
//const className = __name.split('.').map(name => name.replace(/_/g, '-')).join(' ')
//console.log('className:', className, element.classList)
//if (className) element.classList.add(...className.split(' '))
//// 获取全局样式表, 如果不存在则添加一个
//if (!document.styleSheets.length) {
// document.head.appendChild(document.createElement('style'))
//}
//// 检查全局是否已经存在 cursor-pointer, 如果不存在则添加
//const rules = Array.from(document.styleSheets[0].cssRules)
//const styleSheet = document.styleSheets[0]
//if (!rules.some(rule => rule.selectorText === '.cursor-pointer')) {
// styleSheet.insertRule('.cursor-pointer { cursor: pointer; }')
// console.log('insertRule:', styleSheet.cssRules)
//}
2024-05-16 20:52:00 +08:00
// 为元素添加链式操作方法
2024-05-16 05:00:08 +08:00
element.w = (width) => { element.style.width = width; return element }
element.h = (height) => { element.style.height = height; return element }
2024-06-27 14:44:29 +08:00
element.x = (left) => { element.style.left = left; return element } // 以居中线对齐(flex)
element.y = (top) => { element.style.top = top; return element } // 以居中线对齐(flex)
element.z = (zIndex) => { element.style.zIndex = zIndex; return element }
element.cursor = (cursor) => { element.style.cursor = cursor; return element }
2024-05-16 05:00:08 +08:00
element.text = (text) => { element.textContent = text; return element }
element.html = (html) => { element.innerHTML = html; return element }
element.childs = (childs) => { childs.forEach(child => element.appendChild(child)); return element }
element.bg = (color) => { element.style.backgroundColor = color; return element }
2024-05-16 05:24:43 +08:00
element.grid = (options) => { element.style.display = 'grid'; Object.assign(element.style, options); return element }
element.flex = (options) => { element.style.display = 'flex'; Object.assign(element.style, options); return element }
2024-05-16 20:52:00 +08:00
element.radius = (radius) => { element.style.borderRadius = radius; return element }
2024-05-16 06:46:41 +08:00
element.p = (padding) => applyStyle(element, 'padding', padding)
element.m = (margin) => applyStyle(element, 'margin', margin)
2024-05-16 19:24:21 +08:00
element.mx = (value) => { element.style.marginLeft = value; element.style.marginRight = value; return element }
element.my = (value) => { element.style.marginTop = value; element.style.marginBottom = value; return element }
element.px = (value) => { element.style.paddingLeft = value; element.style.paddingRight = value; return element }
element.py = (value) => { element.style.paddingTop = value; element.style.paddingBottom = value; return element }
2024-05-17 00:30:42 +08:00
element.font = (font) => { element.style.font = font; return element }
2024-01-12 04:38:20 +08:00
return element
}
2024-05-16 06:46:41 +08:00
function applyStyle(element, styleName, value) {
if (typeof value === 'string') {
element.style[styleName] = value;
} else if (typeof value === 'object') {
const { left = 0, right = 0, top = 0, bottom = 0 } = value;
element.style[`${styleName}Left`] = left;
element.style[`${styleName}Right`] = right;
element.style[`${styleName}Top`] = top;
element.style[`${styleName}Bottom`] = bottom;
}
return element;
}
2024-05-14 16:28:24 +08:00
/* HTML5 语义标签 */
2024-05-16 05:00:08 +08:00
export class BaseElement {
constructor(options) {
return createElement(options, this.constructor.name)
}
static text(text) {
return createElement({ textContent: text }, this.name)
}
static html(html) {
return createElement({ innerHTML: html }, this.name)
}
static childs(childs) {
return createElement({ children: childs }, this.name)
}
2024-06-27 14:44:29 +08:00
static w(width, __name) {
2024-05-16 19:24:21 +08:00
if (typeof width === 'number') width = width + 'px'
2024-06-27 14:44:29 +08:00
return createElement({ style: { width } }, this.name, __name)
2024-05-16 05:00:08 +08:00
}
static h(height) {
2024-05-16 19:24:21 +08:00
if (typeof height === 'number') height = height + 'px'
2024-05-16 05:00:08 +08:00
return createElement({ style: { height } }, this.name)
}
static bg(color) {
return createElement({ style: { backgroundColor: color } }, this.name)
}
2024-05-16 06:46:41 +08:00
static p(...args) {
if (args.length === 1 && typeof args[0] === 'string') {
return createElement({ style: { padding: args[0] } }, this.name)
}
if (args.length === 1 && typeof args[0] === 'object') {
const { left = 0, right = 0, top = 0, bottom = 0 } = args[0]
const style = { paddingLeft: left, paddingRight: right, paddingTop: top, paddingBottom: bottom }
return createElement({ style }, this.name)
}
2024-05-16 19:24:21 +08:00
if (args.length === 4) {
const [top, right, bottom, left] = args
const style = { paddingTop: top, paddingRight: right, paddingBottom: bottom, paddingLeft: left }
return createElement({ style }, this.name)
}
2024-05-16 06:46:41 +08:00
}
static m(...args) {
if (args.length === 1 && typeof args[0] === 'string') {
return createElement({ style: { margin: args[0] } }, this.name)
}
if (args.length === 1 && typeof args[0] === 'object') {
const { left = 0, right = 0, top = 0, bottom = 0 } = args[0]
const style = { marginLeft: left, marginRight: right, marginTop: top, marginBottom: bottom }
return createElement({ style }, this.name)
}
2024-05-16 19:24:21 +08:00
if (args.length === 4) {
const [top, right, bottom, left] = args
const style = { marginTop: top, marginRight: right, marginBottom: bottom, marginLeft: left }
return createElement({ style }, this.name)
}
}
static mx(value) {
if (typeof value === 'number') value = value + 'px'
return createElement({ style: { marginLeft: value, marginRight: value } }, this.name)
}
static my(value) {
if (typeof value === 'number') value = value + 'px'
return createElement({ style: { marginTop: value, marginBottom: value } }, this.name)
}
static px(value) {
if (typeof value === 'number') value = value + 'px'
return createElement({ style: { paddingLeft: value, paddingRight: value } }, this.name)
}
static py(value) {
if (typeof value === 'number') value = value + 'px'
return createElement({ style: { paddingTop: value, paddingBottom: value } }, this.name)
2024-05-16 05:24:43 +08:00
}
static grid(options) {
return createElement({ style: { display: 'grid', ...options } }, this.name)
}
static flex(options) {
return createElement({ style: { display: 'flex', ...options } }, this.name)
}
2024-05-16 06:46:41 +08:00
static radius(radius) {
return createElement({ style: { borderRadius: radius } }, this.name)
}
2024-05-17 00:30:42 +08:00
static font(font) {
return createElement({ style: { font } }, this.name)
}
2024-05-16 05:00:08 +08:00
}
2024-06-27 14:44:29 +08:00
export const proxy = new Proxy(BaseElement, {
get(target, property) {
if (property in target) {
// 如果调用的是静态方法则在结尾附上 __name 参数
const __name = this.__name || ''
if (typeof target[property] === 'function') {
return function (...args) {
return target[property](...args, __name)
}
}
return target[property]
} else {
const __name = this.__name ? this.__name + '.' + property : property
console.log('__name:', __name)
return new Proxy(target, { __name, get: this.get })
}
}
})
export class div extends proxy {
2024-05-16 05:00:08 +08:00
constructor(options) {
super(options)
}
}
export class span extends BaseElement {
constructor(options) {
super(options)
}
}
export class button extends BaseElement {
constructor(options) {
super({ ...options, style: { cursor: 'pointer', ...options.style } })
}
}
export class img extends BaseElement {
constructor(options) {
super(options)
}
}
export class pre extends BaseElement {
constructor(options) {
super(options)
}
}
export class input extends BaseElement {
constructor(options) {
super(options)
}
}
export class textarea extends BaseElement {
constructor(options) {
super(options)
}
}
export class select extends BaseElement {
constructor(options) {
super(options)
}
}
export class option extends BaseElement {
constructor(options) {
super(options)
}
}
export class nav extends BaseElement {
constructor(options) {
super(options)
}
}
export class main extends BaseElement {
constructor(options) {
super(options)
}
}
export class article extends BaseElement {
constructor(options) {
super(options)
}
}
export class section extends BaseElement {
constructor(options) {
super(options)
}
}
export class aside extends BaseElement {
constructor(options) {
super(options)
}
}
export class header extends BaseElement {
constructor(options) {
super(options)
}
}
export class footer extends BaseElement {
constructor(options) {
super(options)
}
}
export class h1 extends BaseElement {
constructor(options) {
super(options)
}
}
export class h2 extends BaseElement {
constructor(options) {
super(options)
}
}
export class h3 extends BaseElement {
constructor(options) {
super(options)
}
}
export class h4 extends BaseElement {
constructor(options) {
super(options)
}
}
export class h5 extends BaseElement {
constructor(options) {
super(options)
}
}
export class h6 extends BaseElement {
constructor(options) {
super(options)
}
}
export class p extends BaseElement {
constructor(options) {
super(options)
}
}
export class a extends BaseElement {
constructor(options) {
super(options)
}
}
export class ul extends BaseElement {
constructor(options) {
super(options)
}
}
export class ol extends BaseElement {
constructor(options) {
super(options)
}
}
export class li extends BaseElement {
constructor(options) {
super(options)
}
}
export class dl extends BaseElement {
constructor(options) {
super(options)
}
}
export class dt extends BaseElement {
constructor(options) {
super(options)
}
}
export class dd extends BaseElement {
constructor(options) {
super(options)
}
}
export class table extends BaseElement {
constructor(options) {
super(options)
}
}
export class caption extends BaseElement {
constructor(options) {
super(options)
}
}
export class thead extends BaseElement {
constructor(options) {
super(options)
}
}
export class tbody extends BaseElement {
constructor(options) {
super(options)
}
}
export class tfoot extends BaseElement {
constructor(options) {
super(options)
}
}
export class tr extends BaseElement {
constructor(options) {
super(options)
}
}
export class th extends BaseElement {
constructor(options) {
super(options)
}
}
export class td extends BaseElement {
constructor(options) {
super(options)
}
}
export class colgroup extends BaseElement {
constructor(options) {
super(options)
}
}
export class col extends BaseElement {
constructor(options) {
super(options)
}
}
export class form extends BaseElement {
constructor(options) {
super(options)
}
}
export class fieldset extends BaseElement {
constructor(options) {
super(options)
}
}
export class legend extends BaseElement {
constructor(options) {
super(options)
}
}
export class label extends BaseElement {
constructor(options) {
super(options)
}
}
export class strong extends BaseElement {
constructor(options) {
super(options)
}
}
export class small extends BaseElement {
constructor(options) {
super(options)
}
}
export class em extends BaseElement {
constructor(options) {
super(options)
}
}
export class mark extends BaseElement {
constructor(options) {
super(options)
}
}
export class del extends BaseElement {
constructor(options) {
super(options)
}
}
export class ins extends BaseElement {
constructor(options) {
super(options)
}
}
export class code extends BaseElement {
constructor(options) {
super(options)
}
}
export class sub extends BaseElement {
constructor(options) {
super(options)
}
}
export class sup extends BaseElement {
constructor(options) {
super(options)
}
}
export class i extends BaseElement {
constructor(options) {
super(options)
}
}
export class b extends BaseElement {
constructor(options) {
super(options)
}
}
export class u extends BaseElement {
constructor(options) {
super(options)
}
}
export class s extends BaseElement {
constructor(options) {
super(options)
}
}
export class cite extends BaseElement {
constructor(options) {
super(options)
}
}
export class q extends BaseElement {
constructor(options) {
super(options)
}
}
export class blockquote extends BaseElement {
constructor(options) {
super(options)
}
}
export class address extends BaseElement {
constructor(options) {
super(options)
}
}
export class time extends BaseElement {
constructor(options) {
super(options)
}
}
export class dfn extends BaseElement {
constructor(options) {
super(options)
}
}
export class abbr extends BaseElement {
constructor(options) {
super(options)
}
}
export class ruby extends BaseElement {
constructor(options) {
super(options)
}
}
export class rt extends BaseElement {
constructor(options) {
super(options)
}
}
export class rp extends BaseElement {
constructor(options) {
super(options)
}
}
export class bdi extends BaseElement {
constructor(options) {
super(options)
}
}
export class bdo extends BaseElement {
constructor(options) {
super(options)
}
}
export class hr extends BaseElement {
constructor(options) {
super(options)
}
}
export class br extends BaseElement {
constructor(options) {
super(options)
}
}
export class figure extends BaseElement {
constructor(options) {
super(options)
}
}
export class figcaption extends BaseElement {
constructor(options) {
super(options)
}
}
export class video extends BaseElement {
constructor(options) {
super(options)
}
}
export class audio extends BaseElement {
constructor(options) {
super(options)
}
}
export class canvas extends BaseElement {
constructor(options) {
super(options)
}
}
2024-01-12 04:38:20 +08:00
export function Avatar(options) {
const element = createElement(options, 'img')
element.onerror = () => element.src = '/favicon.ico'
return element
}
export function Dialog(options) {
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
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,
}
})
]
})
const observer = new MutationObserver(mutationsList => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
element.focus()
element.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 100 }).finished
return observer.disconnect()
}
}
})
observer.observe(document.body, { childList: true, subtree: true })
return element
}