themeFormatting.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. config.setting.forEach((setItem) => {
  13. if (['gradual', 'colorPicker'].includes(setItem.type)) {
  14. theme[type][setItem.field] = setItem.value
  15. }
  16. })
  17. return theme
  18. } else {
  19. return {}
  20. }
  21. }
  22. // 将保存的theme主题设置(颜色)存放到chartList
  23. export function themeToSetting (chartList, type) {
  24. // 排除掉主题非暗黑非明亮的情况
  25. if (['dark', 'light'].includes(type)) {
  26. chartList.forEach(chart => {
  27. chart.option.theme = type
  28. chart.key = new Date().getTime()
  29. if (chart.theme && chart.theme[type]) {
  30. for (const item of chart.setting) {
  31. // 检查 obj 中是否存在与 item.field 相对应的属性
  32. if (Object.prototype.hasOwnProperty.call(chart.theme[type], item.field)) {
  33. // 更新 setting 中对应项的 value 值为 theme 中的属性值
  34. item.value = chart.theme[type][item.field]
  35. }
  36. }
  37. }
  38. })
  39. }
  40. return chartList
  41. }