UNPKG

4.33 kBJavaScriptView Raw
1import * as _vue from "vue";
2
3function _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; }
4
5function _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; }
6
7function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
8
9function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
10
11import Icon from './Icon';
12var customCache = new Set();
13
14function isValidCustomScriptUrl(scriptUrl) {
15 return typeof scriptUrl === 'string' && scriptUrl.length && !customCache.has(scriptUrl);
16}
17
18function createScriptUrlElements(scriptUrls) {
19 var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
20 var currentScriptUrl = scriptUrls[index];
21
22 if (isValidCustomScriptUrl(currentScriptUrl)) {
23 var script = document.createElement('script');
24 script.setAttribute('src', currentScriptUrl);
25 script.setAttribute('data-namespace', currentScriptUrl);
26
27 if (scriptUrls.length > index + 1) {
28 script.onload = function () {
29 createScriptUrlElements(scriptUrls, index + 1);
30 };
31
32 script.onerror = function () {
33 createScriptUrlElements(scriptUrls, index + 1);
34 };
35 }
36
37 customCache.add(currentScriptUrl);
38 document.body.appendChild(script);
39 }
40}
41
42export default function create() {
43 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
44 var scriptUrl = options.scriptUrl,
45 _options$extraCommonP = options.extraCommonProps,
46 extraCommonProps = _options$extraCommonP === void 0 ? {} : _options$extraCommonP;
47 /**
48 * DOM API required.
49 * Make sure in browser environment.
50 * The Custom Icon will create a <script/>
51 * that loads SVG symbols and insert the SVG Element into the document body.
52 */
53
54 if (typeof document !== 'undefined' && typeof window !== 'undefined' && typeof document.createElement === 'function') {
55 if (Array.isArray(scriptUrl)) {
56 // 因为iconfont资源会把svg插入before,所以前加载相同type会覆盖后加载,为了数组覆盖顺序,倒叙插入
57 createScriptUrlElements(scriptUrl.reverse());
58 } else {
59 createScriptUrlElements([scriptUrl]);
60 }
61 }
62
63 var Iconfont = function Iconfont(props, context) {
64 var attrs = context.attrs,
65 slots = context.slots;
66
67 var _props$attrs = _objectSpread({}, props, attrs),
68 type = _props$attrs.type,
69 restProps = _objectWithoutProperties(_props$attrs, ["type"]);
70
71 var children = slots["default"] && slots["default"](); // children > type
72
73 var content = null;
74
75 if (type) {
76 content = _vue.createVNode("use", {
77 "xlink:href": "#".concat(type)
78 }, null);
79 }
80
81 if (children && children.length) {
82 content = children;
83 }
84
85 var iconProps = _objectSpread({}, extraCommonProps, restProps);
86
87 return _vue.createVNode(Icon, iconProps, {
88 "default": function _default() {
89 return [content];
90 }
91 });
92 };
93
94 Iconfont.props = {
95 spin: Boolean,
96 rotate: Number,
97 type: String
98 };
99 Iconfont.inheritAttrs = false;
100 Iconfont.displayName = 'Iconfont';
101 return Iconfont;
102}
\No newline at end of file