UNPKG

vantui-edit

Version:

一套适用于Taro3及React的vantui组件库

230 lines (203 loc) 6.35 kB
import _typeof from "@babel/runtime/helpers/typeof"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import { useCallback, useRef, useState } from 'react'; import { nextTick, createSelectorQuery } from '@tarojs/taro'; import { isNumber, isObject, isString } from './type'; export function parse(str) { var decode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; var params = {}; if (!isString(str)) { return params; } var trimStr = str.trim(); if (trimStr === '') { return params; } var newStr = trimStr.split('&'); for (var i = 0; i < newStr.length; i++) { var _split = newStr[i].split('='), _split2 = _slicedToArray(_split, 2), key = _split2[0], value = _split2[1]; if (decode) { var kkey = decodeURIComponent(key); var vvalue = decodeURIComponent(value); if (isString(vvalue)) { try { params[kkey] = JSON.parse(vvalue); if (isNumber(params[kkey]) && params[kkey] + '' !== vvalue + '') { params[kkey] = vvalue; } } catch (error) { params[kkey] = vvalue; } } else { params[kkey] = vvalue; } } else { params[key] = value; } } return params; } export function useFadeIn(ref) { return useCallback(function () { if (ref.current) { ref.current.style.cssText = 'display: block;opacity: 0'; } nextTick(function () { if (ref.current) { ref.current.style.cssText = 'transition: opacity .2s linear;opacity: 1;display: block;'; } }); }, [ref]); } export function useFadeOut(ref) { return useCallback(function () { if (ref.current) { ref.current.style.cssText = 'transition: opacity .2s linear;opacity: 0;display: block;'; } setTimeout(function () { if (ref.current) { ref.current.style.cssText = 'display: none;opacity: 0;'; } }, 200); }, [ref]); } export function useMask(ref) { var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), isShowMask = _useState2[0], setIsShowMask = _useState2[1]; var maskRef = useRef(); var maskfadeOut = useFadeOut(maskRef); var maskfadeIn = useFadeIn(maskRef); var actionRef = useRef({ show: function show() { setIsShowMask(true); maskfadeIn(); }, hide: function hide() { setIsShowMask(false); maskfadeOut(); } }); ref.current = actionRef.current; return { maskRef: maskRef, isShowMask: isShowMask }; } export function delay() { var delayTime = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 25; return new Promise(function (resolve) { setTimeout(function () { resolve(); }, delayTime); }); } export function delayQuerySelector(selectorStr) { var delayTime = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 500; return new Promise(function (resolve) { var selector = createSelectorQuery(); delay(delayTime).then(function () { selector.select(selectorStr).boundingClientRect().exec(function (res) { resolve(res); }); }); }); } export function pxTransform(size) { if (!size) return ''; var designWidth = 750; var deviceRatio = { 640: 2.34 / 2, 750: 1, 828: 1.81 / 2 }; return "".concat(size / deviceRatio[designWidth], "rpx"); } function objectToString(style) { if (style && _typeof(style) === 'object') { var styleStr = ''; Object.keys(style).forEach(function (key) { var _key$replace; var lowerCaseKey = (_key$replace = key.replace(/([A-Z])/g, '-$1')) === null || _key$replace === void 0 ? void 0 : _key$replace.toLowerCase(); styleStr += "".concat(lowerCaseKey, ":").concat(style[key], ";"); }); return styleStr; } else if (style && typeof style === 'string') { return style; } return ''; } export function mergeStyle(style1, style2) { if (style1 && _typeof(style1) === 'object' && style2 && _typeof(style2) === 'object') { return Object.assign({}, style1, style2); } return objectToString(style1) + objectToString(style2); } export function uuid() { var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 8; var radix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 16; var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); var value = []; var i = 0; radix = radix || chars.length; if (len) { // Compact form for (i = 0; i < len; i++) { value[i] = chars[0 | Math.random() * radix]; } } else { // rfc4122, version 4 form var r; // rfc4122 requires these characters /* eslint-disable-next-line */ value[8] = value[13] = value[18] = value[23] = '-'; value[14] = '4'; // Fill in random data. At i==19 set the high bits of clock sequence as // per rfc4122, sec. 4.1.5 for (i = 0; i < 36; i++) { if (!value[i]) { r = 0 | Math.random() * 16; value[i] = chars[i === 19 ? r & 0x3 | 0x8 : r]; } } } return value.join(''); } export function getRootScrollTop() { return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0; } export function setRootScrollTop(value) { setScrollTop(window, value); setScrollTop(document.body, value); } export function setScrollTop(el, value) { if ('scrollTop' in el) { el.scrollTop = value; } else { el.scrollTo(el.scrollX, value); } } export function resizeTextarea(input, autosize) { var scrollTop = getRootScrollTop(); input.style.height = 'auto'; input.style.lineHeight = 'inherit'; input.rows = 1; var height = input.scrollHeight; console.info(height); if (isObject(autosize)) { var maxHeight = autosize.maxHeight, minHeight = autosize.minHeight; if (maxHeight !== undefined) { height = Math.min(height, typeof maxHeight === 'number' ? maxHeight : Number(maxHeight.replace('px', ''))); } if (minHeight !== undefined) { height = Math.max(height, typeof minHeight === 'number' ? minHeight : Number(minHeight.replace('px', ''))); } } if (height) { input.style.height = "".concat(height, "px"); // https://github.com/youzan/vant/issues/9178 setRootScrollTop(scrollTop); } }