helpers.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. function renderIconDefinitionToSVGElement(icon, options) {
  15. if (options === void 0) { options = {}; }
  16. if (typeof icon.icon === 'function') {
  17. var placeholders = options.placeholders || {};
  18. return renderAbstractNodeToSVGElement(icon.icon(placeholders.primaryColor || '#333', placeholders.secondaryColor || '#E6E6E6'), options);
  19. }
  20. return renderAbstractNodeToSVGElement(icon.icon, options);
  21. }
  22. exports.renderIconDefinitionToSVGElement = renderIconDefinitionToSVGElement;
  23. function renderAbstractNodeToSVGElement(node, options) {
  24. var targetAttrs = node.tag === 'svg'
  25. ? __assign({}, node.attrs, (options.extraSVGAttrs || {})) : node.attrs;
  26. var attrs = Object.keys(targetAttrs).reduce(function (acc, nextKey) {
  27. var key = nextKey;
  28. var value = targetAttrs[key];
  29. var token = key + "=\"" + value + "\"";
  30. acc.push(token);
  31. return acc;
  32. }, []);
  33. var attrsToken = attrs.length ? ' ' + attrs.join(' ') : '';
  34. var container = [
  35. "<" + node.tag + attrsToken + ">",
  36. "</" + node.tag + ">"
  37. ];
  38. var children = (node.children || [])
  39. .map(function (child) { return renderAbstractNodeToSVGElement(child, options); })
  40. .join('');
  41. return "" + container[0] + children + container[1];
  42. }