1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| const animationConf = { loading: { keyframe: 'loading { to { background-position-x: -20%; } }', needDelay: false, styles: { backgroundColor: '#ecf0f2', background: 'linear-gradient( 100deg, rgba(255, 255, 255, 0) 40%, rgba(255, 255, 255, .5) 50%, rgba(255, 255, 255, 0) 60% ) #ecf0f2', backgroundSize: '200% 100%', backgroundPositionX: '180%', animation: '2s loading ease-in-out infinite' }, }, flash: { keyframe: 'flash {from,50%,to{opacity: 1;}25%,75% {opacity: 0.5;}}', needDelay: true, styles: { backgroundColor: '#ecf0f2', animation: 'flash 3s infinite', }, }, }
export const getDefault = () => { return { include: ['audio', 'button', 'canvas', 'code', 'img', 'input', 'pre', 'svg', 'textarea', 'video'], w: window.innerWidth, h: window.innerHeight, boxId: 'bone', boxStyle: `position: fixed;width: 100%;height: 100%;background-color: #fff;top: 0;z-index: 100000;`, delay: 0.3, beforeCreateOption: (() => false), styles: { position: 'fixed', zIndex: 1000000, background: '#ecf0f2', animation: 'flash 3s infinite' } } } export const useAnimation = (animation, config) => { const animate = animationConf[animation] config.keyframe = animate.keyframe return { ...config.styles, ...animate.styles } }
export const getStyle = el => { if(el.nodeType !== 1) return () => '' const computedStyle = getComputedStyle(el) return attr => computedStyle[attr] }
export const percent = (x, y) => parseFloat(x / y * 100).toFixed(3)
export const isHidden = (style, node) => style('display') === 'none' || style('visibility') === 'hidden' || style('opacity') == 0 || node.hidden;
export const hasBackgroundUrl = style => !!(style('backgroundImage').match(/url\(.+?\)/) || []).length
export const hasBackgroundColor = style => !!(style('backgroundColor'))
export const hasBorderRadius = style => !!(-style('border-radius').replace('px', ''))
export const getBorderRadius = style => !hasBorderRadius(style) ? `5px` : style('border-radius')
export const getRect = node => node ? node.getBoundingClientRect() : {}
export const isTextNode = node => node.textContent.trim().length && node.nodeType === 1 && !node.children.length
export const hasChildText = node => [...node.children].some(item => isTextNode(item)) && node.children.length === 1
export const inWhite = (els, node) => els.includes(node.tagName.toLocaleLowerCase())
export const getRootNode = (el) => { el = el || document.body return typeof el === 'object' ? el : (typeof el === 'string' ? document.querySelector(el): null); }
export const getPadding = style => { return { paddingTop: parseInt(style('paddingTop')), paddingLeft: parseInt(style('paddingLeft')), paddingBottom: parseInt(style('paddingBottom')), paddingRight: parseInt(style('paddingRight')) } }
export const transformStyle = (str, replace='-') => { var temp = str.replace(/[A-Z]/g, (match) => { return replace + match.toLowerCase(); }); if(temp.slice(0,1) === replace){ temp = temp.slice(1); } return temp; }
export const drawBlock = (styles = {}) => Object.entries(styles).reduce((prev,[key, value]) => `${prev}${transformStyle(key)}:${value};`, '<div style="') + `"></div>`
export const margeOptions = (option, useOption, node) => { const diy = useOption(node, option) if(diy === null || JSON.stringify(diy) === '{}') return null const options = typeof diy === 'object' ? diy : option return options }
|