| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 'use client';
- var __assign = (this && this.__assign) || function () {
- __assign = Object.assign || function(t) {
- for (var s, i = 1, n = arguments.length; i < n; i++) {
- s = arguments[i];
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
- t[p] = s[p];
- }
- return t;
- };
- return __assign.apply(this, arguments);
- };
- var __rest = (this && this.__rest) || function (s, e) {
- var t = {};
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
- t[p] = s[p];
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
- t[p[i]] = s[p[i]];
- }
- return t;
- };
- import { createElement, forwardRef, useRef, } from 'react';
- export var IconBase = forwardRef(function (props, ref) {
- var icon = props.icon, id = props.id, className = props.className, extend = props.extend, restProps = __rest(props, ["icon", "id", "className", "extend"]);
- var cls = "univerjs-icon univerjs-icon-".concat(id, " ").concat(className || '').trim();
- var idSuffix = useRef("_".concat(generateShortUuid()));
- return render(icon, "".concat(id), { defIds: icon.defIds, idSuffix: idSuffix.current }, __assign({ ref: ref, className: cls }, restProps), extend);
- });
- /**
- * use react createElement to render an IconElement with other props
- */
- function render(node, id, runtimeProps, rootProps, extend) {
- return createElement(node.tag, __assign(__assign({ key: id }, replaceRuntimeIdsAndExtInAttrs(node, runtimeProps, extend)), rootProps), (replaceRuntimeIdsInDefs(node, runtimeProps).children || []).map(function (child, index) {
- return render(child, "".concat(id, "-").concat(node.tag, "-").concat(index), runtimeProps, undefined, extend);
- }));
- }
- // Adds id-suffix to references, returns new attrs.
- function replaceRuntimeIdsAndExtInAttrs(node, runtimeProps, extend) {
- // replace extend colorChannel
- var attrs = __assign({}, node.attrs);
- if ((extend === null || extend === void 0 ? void 0 : extend.colorChannel1) && attrs['fill'] === 'colorChannel1') {
- attrs['fill'] = extend.colorChannel1;
- }
- // If `defIds` is empty, do nothing
- var defIds = runtimeProps.defIds;
- if (!defIds || defIds.length === 0) {
- return attrs;
- }
- // Adds suffix to references
- if (node.tag === 'use' && attrs['xlink:href']) {
- attrs['xlink:href'] = attrs['xlink:href'] + runtimeProps.idSuffix;
- }
- Object.entries(attrs).forEach(function (_a) {
- var key = _a[0], value = _a[1];
- if (typeof value === 'string') {
- attrs[key] = value.replace(/url\(#(.*)\)/, "url(#$1".concat(runtimeProps.idSuffix, ")"));
- }
- });
- return attrs;
- }
- // Adds id-suffix to definitions, returns new node.
- function replaceRuntimeIdsInDefs(node, runtimeProps) {
- var _a;
- // If `defIds` is empty, do nothing
- var defIds = runtimeProps.defIds;
- if (!defIds || defIds.length === 0) {
- return node;
- }
- // Adds suffix to definition
- if (node.tag === 'defs' && ((_a = node.children) === null || _a === void 0 ? void 0 : _a.length)) {
- return __assign(__assign({}, node), { children: node.children.map(function (child) {
- if (typeof child.attrs.id === 'string') {
- if (defIds && defIds.indexOf(child.attrs.id) > -1) {
- return __assign(__assign({}, child), { attrs: __assign(__assign({}, child.attrs), { id: child.attrs.id + runtimeProps.idSuffix }) });
- }
- }
- return child;
- }) });
- }
- return node;
- }
- function generateShortUuid() {
- return Math.random().toString(36).substring(2, 8);
- }
- IconBase.displayName = 'UniverIcon';
- //# sourceMappingURL=icon.js.map
|