UNPKG

3.07 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports["default"] = create;
9
10var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
12var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
13
14var _react = _interopRequireDefault(require("react"));
15
16var _Icon = _interopRequireDefault(require("./Icon"));
17
18var customCache = new Set();
19
20function isValidCustomScriptUrl(scriptUrl) {
21 return Boolean(typeof scriptUrl === 'string' && scriptUrl.length && !customCache.has(scriptUrl));
22}
23
24function createScriptUrlElements(scriptUrls) {
25 var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
26 var currentScriptUrl = scriptUrls[index];
27
28 if (isValidCustomScriptUrl(currentScriptUrl)) {
29 var script = document.createElement('script');
30 script.setAttribute('src', currentScriptUrl);
31 script.setAttribute('data-namespace', currentScriptUrl);
32
33 if (scriptUrls.length > index + 1) {
34 script.onload = function () {
35 createScriptUrlElements(scriptUrls, index + 1);
36 };
37
38 script.onerror = function () {
39 createScriptUrlElements(scriptUrls, index + 1);
40 };
41 }
42
43 customCache.add(currentScriptUrl);
44 document.body.appendChild(script);
45 }
46}
47
48function create() {
49 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
50 var scriptUrl = options.scriptUrl,
51 _options$extraCommonP = options.extraCommonProps,
52 extraCommonProps = _options$extraCommonP === void 0 ? {} : _options$extraCommonP;
53 /**
54 * DOM API required.
55 * Make sure in browser environment.
56 * The Custom Icon will create a <script/>
57 * that loads SVG symbols and insert the SVG Element into the document body.
58 */
59
60 if (scriptUrl && typeof document !== 'undefined' && typeof window !== 'undefined' && typeof document.createElement === 'function') {
61 if (Array.isArray(scriptUrl)) {
62 // 因为iconfont资源会把svg插入before,所以前加载相同type会覆盖后加载,为了数组覆盖顺序,倒叙插入
63 createScriptUrlElements(scriptUrl.reverse());
64 } else {
65 createScriptUrlElements([scriptUrl]);
66 }
67 }
68
69 var Iconfont = _react["default"].forwardRef(function (props, ref) {
70 var type = props.type,
71 children = props.children,
72 restProps = (0, _objectWithoutProperties2["default"])(props, ["type", "children"]); // children > type
73
74 var content = null;
75
76 if (props.type) {
77 content = _react["default"].createElement("use", {
78 xlinkHref: "#".concat(type)
79 });
80 }
81
82 if (children) {
83 content = children;
84 }
85
86 return _react["default"].createElement(_Icon["default"], (0, _extends2["default"])({}, extraCommonProps, restProps, {
87 ref: ref
88 }), content);
89 });
90
91 Iconfont.displayName = 'SvgIcon';
92 Iconfont.__C7N_ICON = true;
93 return Iconfont;
94}
95//# sourceMappingURL=IconFont.js.map