themeFormatting.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. for (const item in config.customize) {
  25. if (item.includes('color') || item.includes('Color') || item.includes('BGC')) {
  26. theme[type][item] = config.customize[item]
  27. }
  28. }
  29. }
  30. }
  31. return theme
  32. } else {
  33. return {}
  34. }
  35. }
  36. // 将保存的theme主题设置(颜色)存放到chartList
  37. export function themeToSetting (chartList, type) {
  38. // 排除掉主题非暗黑非明亮的情况
  39. if (['dark', 'light'].includes(type)) {
  40. chartList.forEach(chart => {
  41. chart.option.theme = type
  42. if (chart.theme && chart.theme[type]) {
  43. // 如果是g2组件或者远程组件
  44. if (['customComponent', 'remoteComponent'].includes(chart.type)) {
  45. for (const item of chart.setting) {
  46. // 检查 obj 中是否存在与 item.field 相对应的属性
  47. if (Object.prototype.hasOwnProperty.call(chart.theme[type], item.field)) {
  48. // 更新 setting 中对应项的 value 值为 theme 中的属性值
  49. item.value = chart.theme[type][item.field]
  50. }
  51. }
  52. } else {
  53. // 如果是普通组件
  54. if (chart.customize && Object.keys(chart.customize).length) {
  55. for (const item in chart.customize) {
  56. // 检查 obj 中是否存在与 customize 相对应的属性
  57. if (Object.prototype.hasOwnProperty.call(chart.theme[type], item)) {
  58. // 更新 customize 中对应项的值为 theme 中的属性值
  59. chart.customize[item] = chart.theme[type][item]
  60. }
  61. }
  62. }
  63. }
  64. }
  65. })
  66. }
  67. return chartList
  68. }