themables.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { Codicon } from './codicons.js';
  6. export var ThemeColor;
  7. (function (ThemeColor) {
  8. function isThemeColor(obj) {
  9. return obj && typeof obj === 'object' && typeof obj.id === 'string';
  10. }
  11. ThemeColor.isThemeColor = isThemeColor;
  12. })(ThemeColor || (ThemeColor = {}));
  13. export var ThemeIcon;
  14. (function (ThemeIcon) {
  15. ThemeIcon.iconNameSegment = '[A-Za-z0-9]+';
  16. ThemeIcon.iconNameExpression = '[A-Za-z0-9-]+';
  17. ThemeIcon.iconModifierExpression = '~[A-Za-z]+';
  18. ThemeIcon.iconNameCharacter = '[A-Za-z0-9~-]';
  19. const ThemeIconIdRegex = new RegExp(`^(${ThemeIcon.iconNameExpression})(${ThemeIcon.iconModifierExpression})?$`);
  20. function asClassNameArray(icon) {
  21. const match = ThemeIconIdRegex.exec(icon.id);
  22. if (!match) {
  23. return asClassNameArray(Codicon.error);
  24. }
  25. const [, id, modifier] = match;
  26. const classNames = ['codicon', 'codicon-' + id];
  27. if (modifier) {
  28. classNames.push('codicon-modifier-' + modifier.substring(1));
  29. }
  30. return classNames;
  31. }
  32. ThemeIcon.asClassNameArray = asClassNameArray;
  33. function asClassName(icon) {
  34. return asClassNameArray(icon).join(' ');
  35. }
  36. ThemeIcon.asClassName = asClassName;
  37. function asCSSSelector(icon) {
  38. return '.' + asClassNameArray(icon).join('.');
  39. }
  40. ThemeIcon.asCSSSelector = asCSSSelector;
  41. function isThemeIcon(obj) {
  42. return obj && typeof obj === 'object' && typeof obj.id === 'string' && (typeof obj.color === 'undefined' || ThemeColor.isThemeColor(obj.color));
  43. }
  44. ThemeIcon.isThemeIcon = isThemeIcon;
  45. const _regexFromString = new RegExp(`^\\$\\((${ThemeIcon.iconNameExpression}(?:${ThemeIcon.iconModifierExpression})?)\\)$`);
  46. function fromString(str) {
  47. const match = _regexFromString.exec(str);
  48. if (!match) {
  49. return undefined;
  50. }
  51. const [, name] = match;
  52. return { id: name };
  53. }
  54. ThemeIcon.fromString = fromString;
  55. function fromId(id) {
  56. return { id };
  57. }
  58. ThemeIcon.fromId = fromId;
  59. function modify(icon, modifier) {
  60. let id = icon.id;
  61. const tildeIndex = id.lastIndexOf('~');
  62. if (tildeIndex !== -1) {
  63. id = id.substring(0, tildeIndex);
  64. }
  65. if (modifier) {
  66. id = `${id}~${modifier}`;
  67. }
  68. return { id };
  69. }
  70. ThemeIcon.modify = modify;
  71. function getModifier(icon) {
  72. const tildeIndex = icon.id.lastIndexOf('~');
  73. if (tildeIndex !== -1) {
  74. return icon.id.substring(tildeIndex + 1);
  75. }
  76. return undefined;
  77. }
  78. ThemeIcon.getModifier = getModifier;
  79. function isEqual(ti1, ti2) {
  80. var _a, _b;
  81. return ti1.id === ti2.id && ((_a = ti1.color) === null || _a === void 0 ? void 0 : _a.id) === ((_b = ti2.color) === null || _b === void 0 ? void 0 : _b.id);
  82. }
  83. ThemeIcon.isEqual = isEqual;
  84. })(ThemeIcon || (ThemeIcon = {}));