themeFormatting.js 3.9 KB

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