style.js 4.0 KB

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