themeFormatting.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if (typeof value === 'object' && Object.keys(value).length){
  29. // 对于二级属性
  30. for (const subKey in value) {
  31. if (subKey.includes('color') || subKey.includes('Color') || subKey.includes('BGC')){
  32. theme[type][`${item}_${subKey}`] = value[subKey];
  33. }
  34. }
  35. }else{
  36. // 如果只有一级属性
  37. if (item.includes('color') || item.includes('Color') || item.includes('BGC')) {
  38. theme[type][item] = config.customize[item]
  39. }
  40. }
  41. }
  42. }
  43. }
  44. return theme
  45. } else {
  46. return {}
  47. }
  48. }
  49. // 将保存的theme主题设置(颜色)存放到chartList
  50. export function themeToSetting (chartList, type) {
  51. // 排除掉主题非暗黑非明亮的情况
  52. if (['dark', 'light'].includes(type)) {
  53. chartList.forEach(chart => {
  54. chart.option.theme = type
  55. if (chart.theme && chart.theme[type]) {
  56. // 如果是g2组件或者远程组件
  57. if (['customComponent', 'remoteComponent'].includes(chart.type)) {
  58. for (const item of chart.setting) {
  59. // 检查 obj 中是否存在与 item.field 相对应的属性
  60. if (Object.prototype.hasOwnProperty.call(chart.theme[type], item.field)) {
  61. // 更新 setting 中对应项的 value 值为 theme 中的属性值
  62. item.value = chart.theme[type][item.field]
  63. }
  64. }
  65. } else {
  66. // 如果是普通组件
  67. if (chart.customize && Object.keys(chart.customize).length) {
  68. for(let key in chart.theme[type]){
  69. const value = chart.theme[type][key];
  70. // 如果对应的是二级属性
  71. if (key.includes('_')){
  72. const [propertyName, subPropertyName] = key.split('_');
  73. if (!chart.customize[propertyName]) {
  74. chart.customize[propertyName] = {}
  75. }else{
  76. chart.customize[propertyName][subPropertyName] = value;
  77. }
  78. }else{
  79. // 对应的一级属性
  80. if (Object.prototype.hasOwnProperty.call(chart.customize, key)) {
  81. // 更新 customize 中对应项的值为 theme 中的属性值
  82. chart.customize[key] = chart.theme[type][key]
  83. }
  84. }
  85. }
  86. for (const item in chart.customize) {
  87. // 检查 obj 中是否存在与 customize 相对应的属性
  88. if (Object.prototype.hasOwnProperty.call(chart.theme[type], item)) {
  89. // 更新 customize 中对应项的值为 theme 中的属性值
  90. chart.customize[item] = chart.theme[type][item]
  91. }
  92. }
  93. }
  94. }
  95. }
  96. })
  97. }
  98. return chartList
  99. }