util.js 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import { getFieldValue, isArray } from '@cip/utils/util'
  2. import { sizeCellConfig } from './config'
  3. export const analyseData = (list = [], treeProps, count = 0, parentPath = []) => {
  4. return list.reduce((acc, item, index) => {
  5. const path = parentPath.concat(index)
  6. acc[count] = path
  7. count++
  8. const children = getFieldValue(item, treeProps.children)
  9. if (isArray(children)) {
  10. const indexed = analyseData(children, treeProps, count, path)
  11. acc = Object.assign(acc, indexed)
  12. count += Object.keys(indexed).length
  13. }
  14. return acc
  15. }, {})
  16. }
  17. export const getPropertyKeyByPath = (path, treeProps) => {
  18. const { children } = treeProps
  19. return path.join(`.${children}.`)
  20. }
  21. // 根据标准和当前大小计算转换系数
  22. export const calculateCurrentWidth = (size = 'default', standardSize = 'default', width) => {
  23. // 宽度 = 字体宽度 + padding
  24. if (size === standardSize) return width
  25. const standardConfig = sizeCellConfig[standardSize]
  26. const x = (width - standardConfig.padding * 2) / standardConfig.fontSize
  27. const currentConfig = sizeCellConfig[size]
  28. return x * currentConfig.fontSize + currentConfig.padding * 2
  29. }