designToken.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { extend, map } from '../core/util.js';
  2. var DesignTokenManager = (function () {
  3. function DesignTokenManager() {
  4. this._designTokens = {};
  5. this._resolvedTokens = {};
  6. }
  7. DesignTokenManager.prototype.registerTokens = function (tokens) {
  8. this._designTokens = extend({}, tokens);
  9. this._resolveTokens();
  10. };
  11. DesignTokenManager.prototype.getTokenValue = function (token) {
  12. var _a;
  13. if (typeof token !== 'string' || !token.startsWith('@')) {
  14. return token;
  15. }
  16. var key = token.slice(1);
  17. return (_a = this._resolvedTokens[key]) !== null && _a !== void 0 ? _a : token;
  18. };
  19. DesignTokenManager.prototype.resolveColor = function (color) {
  20. var _this = this;
  21. if (!color) {
  22. return color;
  23. }
  24. if (typeof color === 'string') {
  25. return this.getTokenValue(color);
  26. }
  27. else if (color.colorStops) {
  28. var gradient = extend({}, color);
  29. gradient.colorStops = map(gradient.colorStops, function (stop) {
  30. var newStop = extend({}, stop);
  31. newStop.color = _this.getTokenValue(stop.color);
  32. return newStop;
  33. });
  34. return gradient;
  35. }
  36. return color;
  37. };
  38. DesignTokenManager.prototype.getPaintStyle = function (style) {
  39. if (!style) {
  40. return style;
  41. }
  42. var paintStyle = extend({}, style);
  43. if (style.fill != undefined) {
  44. paintStyle.fill = this.resolveColor(style.fill);
  45. }
  46. if (style.stroke != undefined) {
  47. paintStyle.stroke = this.resolveColor(style.stroke);
  48. }
  49. return paintStyle;
  50. };
  51. DesignTokenManager.prototype.resolveStyle = function (style) {
  52. var resolvedStyle = extend({}, style);
  53. if (style.fill) {
  54. resolvedStyle.fill = this.resolveColor(style.fill);
  55. }
  56. if (style.stroke) {
  57. resolvedStyle.stroke = this.resolveColor(style.stroke);
  58. }
  59. return resolvedStyle;
  60. };
  61. DesignTokenManager.prototype._resolveTokens = function () {
  62. var _this = this;
  63. this._resolvedTokens = {};
  64. Object.keys(this._designTokens).forEach(function (category) {
  65. var tokens = _this._designTokens[category];
  66. Object.keys(tokens).forEach(function (key) {
  67. var value = tokens[key];
  68. _this._resolvedTokens[key] = _this._resolveTokenValue(value);
  69. });
  70. });
  71. };
  72. DesignTokenManager.prototype._resolveTokenValue = function (value) {
  73. if (typeof value !== 'string' || !value.startsWith('@')) {
  74. return value;
  75. }
  76. var tokenKey = value.slice(1);
  77. for (var _i = 0, _a = Object.values(this._designTokens); _i < _a.length; _i++) {
  78. var category = _a[_i];
  79. if (tokenKey in category) {
  80. var referencedValue = category[tokenKey];
  81. return this._resolveTokenValue(referencedValue);
  82. }
  83. }
  84. return value;
  85. };
  86. return DesignTokenManager;
  87. }());
  88. export { DesignTokenManager };