IconFont.js 4.5 KB

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