themeFormatting.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @Description: 主题设置格式化
  3. * @author liu.shiyi
  4. * @date 2023/8/17 14:47
  5. */
  6. // 将组件中的setting里面的主题设置(颜色)存放到theme里面
  7. export function settingToTheme (config, type) {
  8. // 考虑与上一次保存过的主题进行合并
  9. // 排除掉主题非暗黑非明亮的情况
  10. if (['dark', 'light'].includes(type)) {
  11. const theme = { dark: { ...config?.theme?.dark }, light: { ...config?.theme?.light } }
  12. // 根据组件的type来判断主题的转化方式
  13. // 如果是g2组件或者远程组件
  14. if (['customComponent', 'remoteComponent', 'echartsComponent'].includes(config.type)) {
  15. config.setting.forEach((setItem) => {
  16. if (['gradual', 'colorPicker', 'colorSelect'].includes(setItem.type)) {
  17. theme[type][setItem.field] = setItem.value
  18. }
  19. })
  20. } else {
  21. // 如果是普通组件
  22. if (config.customize && Object.keys(config.customize).length) {
  23. // customize属性存在层级
  24. for (const item in config.customize) {
  25. const value = config.customize[item]
  26. // 如果包含二级属性
  27. if (value && typeof value === 'object' && Object.keys(value).length) {
  28. // 对于二级属性
  29. for (const subKey in value) {
  30. if (subKey.includes('color') || subKey.includes('Color') || subKey.includes('BGC')) {
  31. theme[type][`${item}_${subKey}`] = value[subKey]
  32. }
  33. }
  34. } else {
  35. // 如果只有一级属性
  36. if (item.includes('color') || item.includes('Color') || item.includes('BGC')) {
  37. theme[type][item] = config.customize[item]
  38. }
  39. }
  40. }
  41. }
  42. }
  43. return theme
  44. } else {
  45. return config?.theme || { dark: {}, light: {} }
  46. }
  47. }
  48. // 将保存的theme主题设置(颜色)存放到chartList
  49. export function themeToSetting (chartList, type) {
  50. let modifiedChartList = chartList
  51. let finalChartList = chartList
  52. // 排除掉主题非暗黑非明亮的情况
  53. if (['dark', 'light'].includes(type)) {
  54. modifiedChartList = chartList.map((item) => {
  55. // 使用 map 方法遍历 chartList 数组并执行操作chartThemeToSetting
  56. return chartThemeToSetting(item, type)
  57. })
  58. finalChartList = modifiedChartList.map((item) => {
  59. // 如果当前项的 type 为 'chartTab',遍历其 tabList 数组并执行操作chartThemeToSetting
  60. if (item.type === 'chartTab' && Array.isArray(item.customize.tabList) && item.customize.tabList.length) {
  61. const modifiedChildren = item.customize.tabList.map((child) => {
  62. return { ...child, chart: chartThemeToSetting(child.chart, type) }
  63. })
  64. return { ...item, customize: { ...item.customize, tabList: modifiedChildren } }
  65. }
  66. return item
  67. })
  68. }
  69. return finalChartList
  70. }
  71. // 对单个组件进行主题设置(从theme到Setting)
  72. function chartThemeToSetting (chart, type) {
  73. if (chart.option && chart.option.theme) {
  74. chart.option.theme = type === 'dark' ? 'transparent' : 'light'
  75. }
  76. if (chart.theme && chart.theme[type]) {
  77. // 如果是g2组件或者远程组件
  78. if (['customComponent', 'remoteComponent', 'echartsComponent'].includes(chart.type)) {
  79. for (const item of chart.setting) {
  80. // 检查 obj 中是否存在与 item.field 相对应的属性
  81. if (Object.prototype.hasOwnProperty.call(chart.theme[type], item.field)) {
  82. // 更新 setting 中对应项的 value 值为 theme 中的属性值
  83. item.value = chart.theme[type][item.field]
  84. }
  85. }
  86. } else {
  87. // 如果是普通组件
  88. if (chart.customize && Object.keys(chart.customize).length) {
  89. for (const key in chart.theme[type]) {
  90. const value = chart.theme[type][key]
  91. // 如果对应的是二级属性
  92. if (key.includes('_')) {
  93. const [propertyName, subPropertyName] = key.split('_')
  94. if (!chart.customize[propertyName]) {
  95. chart.customize[propertyName] = {}
  96. } else {
  97. chart.customize[propertyName][subPropertyName] = value
  98. }
  99. } else {
  100. // 对应的一级属性
  101. if (Object.prototype.hasOwnProperty.call(chart.customize, key)) {
  102. // 更新 customize 中对应项的值为 theme 中的属性值
  103. chart.customize[key] = chart.theme[type][key]
  104. }
  105. }
  106. }
  107. for (const item in chart.customize) {
  108. // 检查 obj 中是否存在与 customize 相对应的属性
  109. if (Object.prototype.hasOwnProperty.call(chart.theme[type], item)) {
  110. // 更新 customize 中对应项的值为 theme 中的属性值
  111. chart.customize[item] = chart.theme[type][item]
  112. }
  113. }
  114. }
  115. }
  116. }
  117. return chart
  118. }