utils.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import deepmerge from 'deepmerge';
  2. import constant from '../../../i18n/theme-editor.json';
  3. export const filterConfigType = (name) => {
  4. switch (name) {
  5. case 'color':
  6. case 'typography':
  7. case 'border':
  8. return 'global';
  9. default:
  10. return 'local';
  11. }
  12. };
  13. export const filterGlobalValue = (defaultConfig, userConfig) => {
  14. const valueObject = {};
  15. const globalArr = ['color', 'typography', 'border'];
  16. globalArr.forEach((global) => {
  17. const configObj = {};
  18. defaultConfig
  19. .find(config => (config.name === global))
  20. .config.forEach(c => (configObj[c.key] = deepmerge({}, c)));
  21. valueObject[global] = configObj;
  22. Object.keys(configObj).forEach((c) => {
  23. if (userConfig.global[c]) {
  24. configObj[c].value = userConfig.global[c];
  25. }
  26. });
  27. });
  28. return valueObject;
  29. };
  30. export const getStyleDisplayValue = (displayValue, global) => {
  31. if (displayValue.startsWith('$')) {
  32. return global[displayValue].value;
  33. }
  34. return displayValue;
  35. };
  36. const getLang = () => {
  37. return location.hash.replace('#', '').split('/')[1] || 'zh-CN';
  38. };
  39. const getNameFromI18N = (name) => {
  40. const lang = getLang();
  41. return constant.filter(config => config.lang === lang)[0][name];
  42. };
  43. export const getStyleDisplayName = (config, componentName) => {
  44. const displayNameMap = getNameFromI18N('display-name');
  45. if (config.name !== '[]') {
  46. const langIndex = {'zh-CN': '0', 'es': 2, 'fr-FR': 3}[getLang()] || 1;
  47. const nameArr = config.name.replace(/\[?\]?/g, '').split(',');
  48. return nameArr[langIndex] || nameArr[1];
  49. }
  50. let displayName = config.key.replace(`$--${componentName}-`, '').replace();
  51. Object.keys(displayNameMap).forEach((name) => {
  52. displayName = displayName.replace(name, displayNameMap[name]);
  53. });
  54. displayName = displayName.replace(/-/g, ' ');
  55. return displayName.trim();
  56. };
  57. export const getActionDisplayName = (key) => {
  58. return getNameFromI18N('action')[key] || key;
  59. };
  60. export const getCategoryDisplayName = (key) => {
  61. return getNameFromI18N('category')[key] || key;
  62. };
  63. export const updateDomHeadStyle = (id, styleContent) => {
  64. let styleTag = document.getElementById(id);
  65. if (!styleTag) {
  66. styleTag = document.createElement('style');
  67. styleTag.setAttribute('id', id);
  68. document.head.appendChild(styleTag);
  69. }
  70. styleTag.innerText = styleContent.replace(/@font-face{[^}]+}/, '');
  71. };