index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * This is just a simple version of deep copy
  3. * Has a lot of edge cases bug
  4. * If you want to use a perfect deep copy, use lodash's _.cloneDeep
  5. * @param {Object} source
  6. * @returns {Object}
  7. */
  8. export const deepClone = (source: any) => {
  9. if (!source && typeof source !== 'object') {
  10. throw new Error('error arguments', 'deepClone' as any);
  11. }
  12. const targetObj: any = source.constructor === Array ? [] : {};
  13. Object.keys(source).forEach((keys) => {
  14. if (source[keys] && typeof source[keys] === 'object') {
  15. targetObj[keys] = deepClone(source[keys]);
  16. } else {
  17. targetObj[keys] = source[keys];
  18. }
  19. });
  20. return targetObj;
  21. };
  22. export const getRgba = (rgbaString) => {
  23. // 使用正则表达式匹配rgba的各个部分
  24. const match = rgbaString.match(/^rgba\(\s*(\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\s*\)$/);
  25. if (!match) {
  26. return rgbaString;
  27. }
  28. // 提取出红、绿、蓝和原始透明度
  29. const [r, g, b, opacity] = match.slice(1).map(Number);
  30. // 返回新的RGBA字符串,其中透明度被设置为1
  31. return {
  32. color: 'rgb(' + r + ',' + g + ',' + b + ')',
  33. opacity: opacity
  34. }
  35. }
  36. // 通用table样式
  37. const getTableRowClass = ({rowIndex}) => {
  38. return rowIndex % 2 === 0 ? '' : 'common-table-tr';
  39. }
  40. export const hexToRgba = (hex, alpha = 1) => {
  41. if (!hex) return '';
  42. // 确保输入是一个有效的十六进制颜色值,并以 # 开头
  43. hex = hex.replace(/^#/, '');
  44. // 如果十六进制颜色值是3位的简写形式(如 #f80),则扩展为6位
  45. if (hex.length === 3) {
  46. hex = hex
  47. .split('')
  48. .map((x) => x + x)
  49. .join('');
  50. }
  51. // 如果十六进制颜色值的长度不是6位,则抛出错误
  52. if (hex.length !== 6) {
  53. return hex;
  54. }
  55. // 解析红、绿、蓝分量
  56. const bigint = parseInt(hex, 16);
  57. const r = (bigint >> 16) & 255;
  58. const g = (bigint >> 8) & 255;
  59. const b = bigint & 255;
  60. // 返回 RGBA 字符串
  61. return `rgba(${r}, ${g}, ${b}, ${alpha || 1})`;
  62. };
  63. export const rgbToRgba = (rgbString, alpha) => {
  64. if (!rgbString) return '';
  65. // 去掉字符串两端的空格
  66. rgbString = rgbString.trim();
  67. // 使用正则表达式匹配 rgb 格式的数字部分
  68. const rgbMatch = rgbString.match(/\d+/g);
  69. // 如果没有匹配到 rgb 格式,返回 null
  70. if (!rgbMatch || rgbMatch.length !== 3) {
  71. return null;
  72. }
  73. // 将匹配到的数字部分转换为整数
  74. const r = parseInt(rgbMatch[0], 10);
  75. const g = parseInt(rgbMatch[1], 10);
  76. const b = parseInt(rgbMatch[2], 10);
  77. // 拼接 rgba 字符串
  78. const rgbaString = `rgba(${r}, ${g}, ${b}, ${alpha})`;
  79. return rgbaString;
  80. };
  81. export const initDrag = (el, scaleX = 1, scaleY = 1) => {
  82. if (!el) return;
  83. el.style.position = 'fixed';
  84. el.style.cursor = 'move';
  85. const dragDom = el;
  86. let disX = 0; // 鼠标按下时,鼠标与拖拽元素的左边距
  87. let disY = 0; // 鼠标按下时,鼠标与拖拽元素的上边距
  88. dragDom.onmousedown = (e) => {
  89. // 鼠标按下,计算当前元素距离可视区的距离
  90. disX = e.clientX / scaleX - dragDom.offsetLeft;
  91. disY = e.clientY / scaleY - dragDom.offsetTop;
  92. // 鼠标移动事件
  93. document.onmousemove = function (e) {
  94. // 通过事件委托,计算移动的距离
  95. const l = e.clientX / scaleX - disX;
  96. const t = e.clientY / scaleY - disY;
  97. // 移动当前元素
  98. dragDom.style.left = `${l + 'px'}`;
  99. dragDom.style.top = `${t + 'px'}`;
  100. };
  101. // 鼠标松开事件
  102. document.onmouseup = function () {
  103. document.onmousemove = null;
  104. document.onmouseup = null;
  105. };
  106. };
  107. };