icon.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use client';
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __rest = (this && this.__rest) || function (s, e) {
  14. var t = {};
  15. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  16. t[p] = s[p];
  17. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  18. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  19. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  20. t[p[i]] = s[p[i]];
  21. }
  22. return t;
  23. };
  24. import { createElement, forwardRef, useRef, } from 'react';
  25. export var IconBase = forwardRef(function (props, ref) {
  26. var icon = props.icon, id = props.id, className = props.className, extend = props.extend, restProps = __rest(props, ["icon", "id", "className", "extend"]);
  27. var cls = "univerjs-icon univerjs-icon-".concat(id, " ").concat(className || '').trim();
  28. var idSuffix = useRef("_".concat(generateShortUuid()));
  29. return render(icon, "".concat(id), { defIds: icon.defIds, idSuffix: idSuffix.current }, __assign({ ref: ref, className: cls }, restProps), extend);
  30. });
  31. /**
  32. * use react createElement to render an IconElement with other props
  33. */
  34. function render(node, id, runtimeProps, rootProps, extend) {
  35. return createElement(node.tag, __assign(__assign({ key: id }, replaceRuntimeIdsAndExtInAttrs(node, runtimeProps, extend)), rootProps), (replaceRuntimeIdsInDefs(node, runtimeProps).children || []).map(function (child, index) {
  36. return render(child, "".concat(id, "-").concat(node.tag, "-").concat(index), runtimeProps, undefined, extend);
  37. }));
  38. }
  39. // Adds id-suffix to references, returns new attrs.
  40. function replaceRuntimeIdsAndExtInAttrs(node, runtimeProps, extend) {
  41. // replace extend colorChannel
  42. var attrs = __assign({}, node.attrs);
  43. if ((extend === null || extend === void 0 ? void 0 : extend.colorChannel1) && attrs['fill'] === 'colorChannel1') {
  44. attrs['fill'] = extend.colorChannel1;
  45. }
  46. // If `defIds` is empty, do nothing
  47. var defIds = runtimeProps.defIds;
  48. if (!defIds || defIds.length === 0) {
  49. return attrs;
  50. }
  51. // Adds suffix to references
  52. if (node.tag === 'use' && attrs['xlink:href']) {
  53. attrs['xlink:href'] = attrs['xlink:href'] + runtimeProps.idSuffix;
  54. }
  55. Object.entries(attrs).forEach(function (_a) {
  56. var key = _a[0], value = _a[1];
  57. if (typeof value === 'string') {
  58. attrs[key] = value.replace(/url\(#(.*)\)/, "url(#$1".concat(runtimeProps.idSuffix, ")"));
  59. }
  60. });
  61. return attrs;
  62. }
  63. // Adds id-suffix to definitions, returns new node.
  64. function replaceRuntimeIdsInDefs(node, runtimeProps) {
  65. var _a;
  66. // If `defIds` is empty, do nothing
  67. var defIds = runtimeProps.defIds;
  68. if (!defIds || defIds.length === 0) {
  69. return node;
  70. }
  71. // Adds suffix to definition
  72. if (node.tag === 'defs' && ((_a = node.children) === null || _a === void 0 ? void 0 : _a.length)) {
  73. return __assign(__assign({}, node), { children: node.children.map(function (child) {
  74. if (typeof child.attrs.id === 'string') {
  75. if (defIds && defIds.indexOf(child.attrs.id) > -1) {
  76. return __assign(__assign({}, child), { attrs: __assign(__assign({}, child.attrs), { id: child.attrs.id + runtimeProps.idSuffix }) });
  77. }
  78. }
  79. return child;
  80. }) });
  81. }
  82. return node;
  83. }
  84. function generateShortUuid() {
  85. return Math.random().toString(36).substring(2, 8);
  86. }
  87. IconBase.displayName = 'UniverIcon';
  88. //# sourceMappingURL=icon.js.map