style.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { __read, __spreadArray } from "tslib";
  2. import { addPrefix, removePrefix, toUppercaseFirstLetter } from './string';
  3. /**
  4. * 对给定HTML对象应用给定样式
  5. * @param style {[key: string]: Object}
  6. * 样式表参考结构
  7. * {
  8. * '.selector': {
  9. * 'attrName': 'attr',
  10. * 'padding': '0 0 0 0',
  11. * 'background-color': 'red'
  12. * }
  13. * }
  14. */
  15. export function applyStyleSheet(element, style) {
  16. Object.entries(style).forEach(function (_a) {
  17. var _b = __read(_a, 2), selector = _b[0], styleString = _b[1];
  18. // apply styles to element and children
  19. __spreadArray([element], __read(element.querySelectorAll(selector)), false).filter(function (el) { return el.matches(selector); })
  20. .forEach(function (target) {
  21. if (!target)
  22. return;
  23. var temp = target;
  24. temp.style.cssText += Object.entries(styleString).reduce(function (total, currVal) {
  25. return "".concat(total).concat(currVal.join(':'), ";");
  26. }, '');
  27. });
  28. });
  29. }
  30. export function subStyleProps(style, prefix, invert) {
  31. if (invert === void 0) { invert = false; }
  32. var result = {};
  33. Object.entries(style).forEach(function (_a) {
  34. var _b = __read(_a, 2), key = _b[0], value = _b[1];
  35. // never transfer class property
  36. if (key === 'className' || key === 'class') {
  37. // do nothing
  38. }
  39. // @example showHandle -> showHandle, showHandleLabel -> showLabel
  40. else if (key.startsWith('show') && removePrefix(key, 'show').startsWith(prefix) !== invert) {
  41. if (key === addPrefix(prefix, 'show'))
  42. result[key] = value;
  43. else
  44. result[key.replace(new RegExp(toUppercaseFirstLetter(prefix)), '')] = value;
  45. }
  46. // @example navFormatter -> formatter
  47. else if (!key.startsWith('show') && key.startsWith(prefix) !== invert) {
  48. var name_1 = removePrefix(key, prefix);
  49. // don't transfer filter if it represents “过滤器”
  50. if (name_1 === 'filter' && typeof value === 'function') {
  51. // do nothing
  52. }
  53. else
  54. result[name_1] = value;
  55. }
  56. });
  57. return result;
  58. }
  59. export function superStyleProps(style, prefix) {
  60. return Object.entries(style).reduce(function (acc, _a) {
  61. var _b = __read(_a, 2), key = _b[0], value = _b[1];
  62. if (key.startsWith('show'))
  63. acc["show".concat(prefix).concat(key.slice(4))] = value;
  64. else
  65. acc["".concat(prefix).concat(toUppercaseFirstLetter(key))] = value;
  66. return acc;
  67. }, {});
  68. }
  69. /**
  70. * extract group style from mixin style
  71. * @param style
  72. * @param ignoreStyleDict style will be ignore from style
  73. * @returns shape style and rest style
  74. */
  75. export function splitStyle(style, ignoreStyleDict) {
  76. if (ignoreStyleDict === void 0) { ignoreStyleDict = ['x', 'y', 'class', 'className']; }
  77. var groupStyleDict = [
  78. 'transform',
  79. 'transformOrigin',
  80. 'anchor',
  81. 'visibility',
  82. 'pointerEvents',
  83. 'zIndex',
  84. 'cursor',
  85. 'clipPath',
  86. 'clipPathTargets',
  87. 'offsetPath',
  88. 'offsetPathTargets',
  89. 'offsetDistance',
  90. 'draggable',
  91. 'droppable',
  92. ];
  93. var output = {};
  94. var groupStyle = {};
  95. Object.entries(style).forEach(function (_a) {
  96. var _b = __read(_a, 2), key = _b[0], val = _b[1];
  97. if (ignoreStyleDict.includes(key)) {
  98. // do nothing
  99. }
  100. else if (groupStyleDict.indexOf(key) !== -1)
  101. groupStyle[key] = val;
  102. else
  103. output[key] = val;
  104. });
  105. return [output, groupStyle];
  106. }
  107. //# sourceMappingURL=style.js.map