themeFormatting.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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'].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. // 排除掉主题非暗黑非明亮的情况
  51. if (['dark', 'light'].includes(type)) {
  52. chartList.forEach(chart => {
  53. chart.option.theme = type
  54. if (chart.theme && chart.theme[type]) {
  55. // 如果是g2组件或者远程组件
  56. if (['customComponent', 'remoteComponent'].includes(chart.type)) {
  57. for (const item of chart.setting) {
  58. // 检查 obj 中是否存在与 item.field 相对应的属性
  59. if (Object.prototype.hasOwnProperty.call(chart.theme[type], item.field)) {
  60. // 更新 setting 中对应项的 value 值为 theme 中的属性值
  61. item.value = chart.theme[type][item.field]
  62. }
  63. }
  64. } else {
  65. // 如果是普通组件
  66. if (chart.customize && Object.keys(chart.customize).length) {
  67. for(let key in chart.theme[type]){
  68. const value = chart.theme[type][key];
  69. // 如果对应的是二级属性
  70. if (key.includes('_')){
  71. const [propertyName, subPropertyName] = key.split('_');
  72. if (!chart.customize[propertyName]) {
  73. chart.customize[propertyName] = {}
  74. }else{
  75. chart.customize[propertyName][subPropertyName] = value;
  76. }
  77. }else{
  78. // 对应的一级属性
  79. if (Object.prototype.hasOwnProperty.call(chart.customize, key)) {
  80. // 更新 customize 中对应项的值为 theme 中的属性值
  81. chart.customize[key] = chart.theme[type][key]
  82. }
  83. }
  84. }
  85. for (const item in chart.customize) {
  86. // 检查 obj 中是否存在与 customize 相对应的属性
  87. if (Object.prototype.hasOwnProperty.call(chart.theme[type], item)) {
  88. // 更新 customize 中对应项的值为 theme 中的属性值
  89. chart.customize[item] = chart.theme[type][item]
  90. }
  91. }
  92. }
  93. }
  94. }
  95. })
  96. }
  97. return chartList
  98. }