UNPKG

@flatbiz/antd

Version:
231 lines (217 loc) 7.49 kB
/* eslint-disable */ import './index.css'; /*! @flatjs/forge MIT @flatbiz/antd */ import { createContext, useContext, useRef, useEffect, useMemo } from 'react'; import { b as _objectSpread2, c as _toConsumableArray } from '../_rollupPluginBabelHelpers-BspM60Sw.js'; import { useUpdate, useMemoizedFn } from 'ahooks'; import { toArray, trim, getUuid } from '@flatbiz/utils'; import { u as useResponsivePoint } from '../use-responsive-point-Bp3D3lZT.js'; import { theme } from 'antd'; /** * A helper to create a Context and Provider with no upfront default value, and * without having to check for undefined all the time. */ function createCtx(name, defaultValue) { var ctx = /*#__PURE__*/createContext(defaultValue); ctx.displayName = name; function useCtx() { var c = useContext(ctx); if (c === undefined) { throw new Error('useCtx must be inside a Provider with a value'); } return c; } return [useCtx, ctx.Provider]; // 'as const' makes TypeScript infer a tuple } var useArrayChange = function useArrayChange(defautDataList) { var forceUpdate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; var changeListRef = useRef(defautDataList); var update = useUpdate(); var arrayOperate = { add: useMemoizedFn(function (dataItem, isUnshift) { if (isUnshift) { var targetList = toArray(dataItem); changeListRef.current = [].concat(_toConsumableArray(targetList), _toConsumableArray(changeListRef.current)); } else { changeListRef.current = changeListRef.current.concat(dataItem); } if (forceUpdate) { update(); } }), update: useMemoizedFn(function (index, dataItem) { var target = changeListRef.current[index]; if (target) { changeListRef.current[index] = _objectSpread2(_objectSpread2({}, target), dataItem); } if (forceUpdate) { update(); } }), delete: useMemoizedFn(function (index) { var deleteItem = changeListRef.current.splice(index, 1); if (forceUpdate) { update(); } return deleteItem; }), resetList: useMemoizedFn(function (dataList) { changeListRef.current = dataList; if (forceUpdate) { update(); } }), getList: useMemoizedFn(function () { return changeListRef.current; }) }; return [changeListRef.current, arrayOperate]; }; var useEffectCustom = function useEffectCustom(fn, deps) { return useEffect(fn, deps); }; var innerIgnoreClass = ['ace_editor', 'tox-tinymce', 'cancel-flatbiz-antd-copy']; /** * 移除复制文本中前后空格 * ``` * 1. target 设置监听复制范围,如果不设置则监听全局,例如 * export const Demo = () => { * const ref = useRef<any>(); * useCopyRemoveSpace({ * target: () => ref.current, * }); * return ( * <div ref={ref}> * <QueryTable /> * </div> * ); * }; * 2. 某些控件(例如:ace编辑器、富文本等)内部自定义处理copy逻辑,此处不能进行拦截,通过配置class属性来忽略 * 3. ignoreClass包括操作目标class、以及层层父节点class,通过目标节点层层父节点的class匹配成功后,取消后续copy文案处理逻辑 * 3. ignoreClass 内置有 ['ace_editor', 'tox-tinymce', 'cancel-flatbiz-antd-copy'] * ``` */ var useCopyRemoveSpace = function useCopyRemoveSpace(props) { var _props$target; var ignoreClass = innerIgnoreClass.concat((props === null || props === void 0 ? void 0 : props.ignoreClass) || []); var target = (props === null || props === void 0 || (_props$target = props.target) === null || _props$target === void 0 ? void 0 : _props$target.call(props)) || window.document; useEffectCustom(function () { var handle = function handle(oEvent) { try { var parentNode = oEvent.target; var isIgnore = false; while (parentNode != null) { var _target = ignoreClass.find(function (item) { var _parentNode$classList; return (_parentNode$classList = parentNode.classList) === null || _parentNode$classList === void 0 ? void 0 : _parentNode$classList.contains(item); }); if (_target) { isIgnore = true; break; } parentNode = parentNode.parentNode; } if (isIgnore) return; var copyInfo = ''; // 获取复制信息 var _window = window; try { copyInfo = window['document']['selection'].createRange().text; } catch (_ex) { copyInfo = _window.getSelection().toString(); } var clipboardData = _window.clipboardData; // for IE if (!clipboardData) { clipboardData = oEvent['clipboardData']; } var nextValue = trim(copyInfo, true); // 修改剪贴板中的内容 clipboardData.setData('Text', nextValue); // 必须禁用原copy方法才能生效 oEvent.preventDefault(); if (nextValue !== copyInfo) { console.log('useCopyRemoveSpace 已操作移除复制文本中前后空格'); } } catch (error) { console.warn(error === null || error === void 0 ? void 0 : error.message); } }; target.addEventListener('copy', handle); return function () { target.removeEventListener('copy', handle); }; }, [JSON.stringify(ignoreClass)]); }; var useEffectCustomAsync = function useEffectCustomAsync(fn, deps) { useEffect(function () { function asyncFunction() { return new Promise(function ($return, $error) { return Promise.resolve(fn()).then(function ($await_1) { try { return $return(); } catch ($boundEx) { return $error($boundEx); } }, $error); }); } void asyncFunction(); }, deps); }; /** * 只执行一次的 effect,后续依赖项变化不会再次执行 */ var useEffectOnce = function useEffectOnce(effect, dep) { var executed = useRef(false); useEffect(function () { if (dep && !executed.current) { executed.current = true; return effect(); } }, [dep]); }; /** * 自定义控制 useMemo 依赖 */ var useMemoCustom = function useMemoCustom(fn, deps) { return useMemo(fn, deps); }; var defaultShouldUpdate = function defaultShouldUpdate(a, b) { return !Object.is(a, b); }; function usePrevious(state) { var shouldUpdate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultShouldUpdate; var prevRef = useRef(); var curRef = useRef(); if (shouldUpdate(curRef.current, state)) { prevRef.current = curRef.current; curRef.current = state; } return prevRef.current; } var useThemeToken = function useThemeToken() { var _theme$useToken = theme.useToken(), token = _theme$useToken.token; return token; }; var useUuid = function useUuid() { var id = useMemo(function () { return getUuid(); }, []); return id; }; var fbaHooks = { useEffectCustom: useEffectCustom, useEffectCustomAsync: useEffectCustomAsync, useThemeToken: useThemeToken, useArrayChange: useArrayChange, usePrevious: usePrevious, useResponsivePoint: useResponsivePoint, useMemoCustom: useMemoCustom, useCopyRemoveSpace: useCopyRemoveSpace, useUuid: useUuid, createCtx: createCtx, useEffectOnce: useEffectOnce }; export { fbaHooks }; //# sourceMappingURL=index.js.map