IconFont.js 4.3 KB

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