utils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.warn = warn;
  6. exports.warning = warning;
  7. exports.isIconDefinition = isIconDefinition;
  8. exports.normalizeAttrs = normalizeAttrs;
  9. exports.generate = generate;
  10. exports.getSecondaryColor = getSecondaryColor;
  11. exports.normalizeTwoToneColors = normalizeTwoToneColors;
  12. exports.useInsertStyles = exports.iconStyles = exports.svgBaseProps = void 0;
  13. var _vue = require("vue");
  14. var _colors = require("@ant-design/colors");
  15. var _Context = require("./components/Context");
  16. var _dynamicCSS = require("./dynamicCSS");
  17. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
  18. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  19. function warn(valid, message) {
  20. // Support uglify
  21. if (process.env.NODE_ENV !== 'production' && !valid && console !== undefined) {
  22. console.error("Warning: ".concat(message));
  23. }
  24. }
  25. function warning(valid, message) {
  26. warn(valid, "[@ant-design/icons-vue] ".concat(message));
  27. }
  28. function camelCase(input) {
  29. return input.replace(/-(.)/g, function (_match, g) {
  30. return g.toUpperCase();
  31. });
  32. } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  33. function isIconDefinition(target) {
  34. return typeof target === 'object' && typeof target.name === 'string' && typeof target.theme === 'string' && (typeof target.icon === 'object' || typeof target.icon === 'function');
  35. }
  36. function normalizeAttrs() {
  37. var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  38. return Object.keys(attrs).reduce(function (acc, key) {
  39. var val = attrs[key];
  40. switch (key) {
  41. case 'class':
  42. acc.className = val;
  43. delete acc["class"];
  44. break;
  45. default:
  46. delete acc[key];
  47. acc[camelCase(key)] = val;
  48. }
  49. return acc;
  50. }, {});
  51. }
  52. function generate(node, key, rootProps) {
  53. if (!rootProps) {
  54. return (0, _vue.h)(node.tag, _objectSpread({
  55. key: key
  56. }, node.attrs), (node.children || []).map(function (child, index) {
  57. return generate(child, "".concat(key, "-").concat(node.tag, "-").concat(index));
  58. }));
  59. }
  60. return (0, _vue.h)(node.tag, _objectSpread({
  61. key: key
  62. }, rootProps, node.attrs), (node.children || []).map(function (child, index) {
  63. return generate(child, "".concat(key, "-").concat(node.tag, "-").concat(index));
  64. }));
  65. }
  66. function getSecondaryColor(primaryColor) {
  67. // choose the second color
  68. return (0, _colors.generate)(primaryColor)[0];
  69. }
  70. function normalizeTwoToneColors(twoToneColor) {
  71. if (!twoToneColor) {
  72. return [];
  73. }
  74. return Array.isArray(twoToneColor) ? twoToneColor : [twoToneColor];
  75. } // These props make sure that the SVG behaviours like general text.
  76. // Reference: https://blog.prototypr.io/align-svg-icons-to-text-and-say-goodbye-to-font-icons-d44b3d7b26b4
  77. var svgBaseProps = {
  78. width: '1em',
  79. height: '1em',
  80. fill: 'currentColor',
  81. 'aria-hidden': 'true',
  82. focusable: 'false'
  83. };
  84. exports.svgBaseProps = svgBaseProps;
  85. var iconStyles = "\n.anticon {\n display: inline-block;\n color: inherit;\n font-style: normal;\n line-height: 0;\n text-align: center;\n text-transform: none;\n vertical-align: -0.125em;\n text-rendering: optimizeLegibility;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n}\n\n.anticon > * {\n line-height: 1;\n}\n\n.anticon svg {\n display: inline-block;\n}\n\n.anticon::before {\n display: none;\n}\n\n.anticon .anticon-icon {\n display: block;\n}\n\n.anticon[tabindex] {\n cursor: pointer;\n}\n\n.anticon-spin::before,\n.anticon-spin {\n display: inline-block;\n -webkit-animation: loadingCircle 1s infinite linear;\n animation: loadingCircle 1s infinite linear;\n}\n\n@-webkit-keyframes loadingCircle {\n 100% {\n -webkit-transform: rotate(360deg);\n transform: rotate(360deg);\n }\n}\n\n@keyframes loadingCircle {\n 100% {\n -webkit-transform: rotate(360deg);\n transform: rotate(360deg);\n }\n}\n";
  86. exports.iconStyles = iconStyles;
  87. function getRoot(ele) {
  88. return ele && ele.getRootNode && ele.getRootNode();
  89. }
  90. /**
  91. * Check if is in shadowRoot
  92. */
  93. function inShadow(ele) {
  94. if (!(0, _dynamicCSS.canUseDom)()) {
  95. return false;
  96. }
  97. return getRoot(ele) instanceof ShadowRoot;
  98. }
  99. /**
  100. * Return shadowRoot if possible
  101. */
  102. function getShadowRoot(ele) {
  103. return inShadow(ele) ? getRoot(ele) : null;
  104. }
  105. var useInsertStyles = function useInsertStyles() {
  106. var _useInjectIconContext = (0, _Context.useInjectIconContext)(),
  107. prefixCls = _useInjectIconContext.prefixCls,
  108. csp = _useInjectIconContext.csp;
  109. var instance = (0, _vue.getCurrentInstance)();
  110. var mergedStyleStr = iconStyles;
  111. if (prefixCls) {
  112. mergedStyleStr = mergedStyleStr.replace(/anticon/g, prefixCls.value);
  113. }
  114. (0, _vue.nextTick)(function () {
  115. if (!(0, _dynamicCSS.canUseDom)()) {
  116. return;
  117. }
  118. var ele = instance.vnode.el;
  119. var shadowRoot = getShadowRoot(ele);
  120. (0, _dynamicCSS.updateCSS)(mergedStyleStr, '@ant-design-vue-icons', {
  121. prepend: true,
  122. csp: csp.value,
  123. attachTo: shadowRoot
  124. });
  125. });
  126. };
  127. exports.useInsertStyles = useInsertStyles;