UNPKG

7.23 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", {
3 value: true
4});
5Object.defineProperty(exports, "default", {
6 enumerable: true,
7 get: function() {
8 return create;
9 }
10});
11var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
12var _Icon = /*#__PURE__*/ _interop_require_default(require("./Icon"));
13function _define_property(obj, key, value) {
14 if (key in obj) {
15 Object.defineProperty(obj, key, {
16 value: value,
17 enumerable: true,
18 configurable: true,
19 writable: true
20 });
21 } else {
22 obj[key] = value;
23 }
24 return obj;
25}
26function _interop_require_default(obj) {
27 return obj && obj.__esModule ? obj : {
28 default: obj
29 };
30}
31function _getRequireWildcardCache(nodeInterop) {
32 if (typeof WeakMap !== "function") return null;
33 var cacheBabelInterop = new WeakMap();
34 var cacheNodeInterop = new WeakMap();
35 return (_getRequireWildcardCache = function(nodeInterop) {
36 return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
37 })(nodeInterop);
38}
39function _interop_require_wildcard(obj, nodeInterop) {
40 if (!nodeInterop && obj && obj.__esModule) {
41 return obj;
42 }
43 if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
44 return {
45 default: obj
46 };
47 }
48 var cache = _getRequireWildcardCache(nodeInterop);
49 if (cache && cache.has(obj)) {
50 return cache.get(obj);
51 }
52 var newObj = {};
53 var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
54 for(var key in obj){
55 if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
56 var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
57 if (desc && (desc.get || desc.set)) {
58 Object.defineProperty(newObj, key, desc);
59 } else {
60 newObj[key] = obj[key];
61 }
62 }
63 }
64 newObj.default = obj;
65 if (cache) {
66 cache.set(obj, newObj);
67 }
68 return newObj;
69}
70function _object_spread(target) {
71 for(var i = 1; i < arguments.length; i++){
72 var source = arguments[i] != null ? arguments[i] : {};
73 var ownKeys = Object.keys(source);
74 if (typeof Object.getOwnPropertySymbols === "function") {
75 ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
76 return Object.getOwnPropertyDescriptor(source, sym).enumerable;
77 }));
78 }
79 ownKeys.forEach(function(key) {
80 _define_property(target, key, source[key]);
81 });
82 }
83 return target;
84}
85function ownKeys(object, enumerableOnly) {
86 var keys = Object.keys(object);
87 if (Object.getOwnPropertySymbols) {
88 var symbols = Object.getOwnPropertySymbols(object);
89 if (enumerableOnly) {
90 symbols = symbols.filter(function(sym) {
91 return Object.getOwnPropertyDescriptor(object, sym).enumerable;
92 });
93 }
94 keys.push.apply(keys, symbols);
95 }
96 return keys;
97}
98function _object_spread_props(target, source) {
99 source = source != null ? source : {};
100 if (Object.getOwnPropertyDescriptors) {
101 Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
102 } else {
103 ownKeys(Object(source)).forEach(function(key) {
104 Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
105 });
106 }
107 return target;
108}
109function _object_without_properties(source, excluded) {
110 if (source == null) return {};
111 var target = _object_without_properties_loose(source, excluded);
112 var key, i;
113 if (Object.getOwnPropertySymbols) {
114 var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
115 for(i = 0; i < sourceSymbolKeys.length; i++){
116 key = sourceSymbolKeys[i];
117 if (excluded.indexOf(key) >= 0) continue;
118 if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
119 target[key] = source[key];
120 }
121 }
122 return target;
123}
124function _object_without_properties_loose(source, excluded) {
125 if (source == null) return {};
126 var target = {};
127 var sourceKeys = Object.keys(source);
128 var key, i;
129 for(i = 0; i < sourceKeys.length; i++){
130 key = sourceKeys[i];
131 if (excluded.indexOf(key) >= 0) continue;
132 target[key] = source[key];
133 }
134 return target;
135}
136var customCache = new Set();
137function isValidCustomScriptUrl(scriptUrl) {
138 return Boolean(typeof scriptUrl === "string" && scriptUrl.length && !customCache.has(scriptUrl));
139}
140function createScriptUrlElements(scriptUrls) {
141 var index = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0;
142 var currentScriptUrl = scriptUrls[index];
143 if (isValidCustomScriptUrl(currentScriptUrl)) {
144 var script = document.createElement("script");
145 script.setAttribute("src", currentScriptUrl);
146 script.setAttribute("data-namespace", currentScriptUrl);
147 if (scriptUrls.length > index + 1) {
148 script.onload = function() {
149 createScriptUrlElements(scriptUrls, index + 1);
150 };
151 script.onerror = function() {
152 createScriptUrlElements(scriptUrls, index + 1);
153 };
154 }
155 customCache.add(currentScriptUrl);
156 document.body.appendChild(script);
157 }
158}
159function create() {
160 var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
161 var scriptUrl = options.scriptUrl, _options_extraCommonProps = options.extraCommonProps, extraCommonProps = _options_extraCommonProps === void 0 ? {} : _options_extraCommonProps;
162 /**
163 * DOM API required.
164 * Make sure in browser environment.
165 * The Custom Icon will create a <script/>
166 * that loads SVG symbols and insert the SVG Element into the document body.
167 */ if (scriptUrl && typeof document !== "undefined" && typeof window !== "undefined" && typeof document.createElement === "function") {
168 if (Array.isArray(scriptUrl)) {
169 // 因为iconfont资源会把svg插入before,所以前加载相同type会覆盖后加载,为了数组覆盖顺序,倒叙插入
170 createScriptUrlElements(scriptUrl.reverse());
171 } else {
172 createScriptUrlElements([
173 scriptUrl
174 ]);
175 }
176 }
177 var Iconfont = /*#__PURE__*/ _react.forwardRef(function(props, ref) {
178 var type = props.type, children = props.children, restProps = _object_without_properties(props, [
179 "type",
180 "children"
181 ]);
182 // children > type
183 var content = null;
184 if (props.type) {
185 content = /*#__PURE__*/ _react.createElement("use", {
186 xlinkHref: "#".concat(type)
187 });
188 }
189 if (children) {
190 content = children;
191 }
192 return /*#__PURE__*/ _react.createElement(_Icon.default, _object_spread_props(_object_spread({}, extraCommonProps, restProps), {
193 ref: ref
194 }), content);
195 });
196 Iconfont.displayName = "Iconfont";
197 return Iconfont;
198}