utils.mjs 10 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { isDevMode } from '@angular/core';
  2. import { generate as generateColor } from '@ant-design/colors';
  3. export const ANT_ICON_ANGULAR_CONSOLE_PREFIX = '[@ant-design/icons-angular]:';
  4. export function error(message) {
  5. console.error(`${ANT_ICON_ANGULAR_CONSOLE_PREFIX} ${message}.`);
  6. }
  7. export function warn(message) {
  8. if (isDevMode()) {
  9. console.warn(`${ANT_ICON_ANGULAR_CONSOLE_PREFIX} ${message}.`);
  10. }
  11. }
  12. export function getSecondaryColor(primaryColor) {
  13. return generateColor(primaryColor)[0];
  14. }
  15. export function withSuffix(name, theme) {
  16. switch (theme) {
  17. case 'fill': return `${name}-fill`;
  18. case 'outline': return `${name}-o`;
  19. case 'twotone': return `${name}-twotone`;
  20. case undefined: return name;
  21. default: throw new Error(`${ANT_ICON_ANGULAR_CONSOLE_PREFIX}Theme "${theme}" is not a recognized theme!`);
  22. }
  23. }
  24. export function withSuffixAndColor(name, theme, pri, sec) {
  25. return `${withSuffix(name, theme)}-${pri}-${sec}`;
  26. }
  27. export function mapAbbrToTheme(abbr) {
  28. return abbr === 'o' ? 'outline' : abbr;
  29. }
  30. export function alreadyHasAThemeSuffix(name) {
  31. return name.endsWith('-fill') || name.endsWith('-o') || name.endsWith('-twotone');
  32. }
  33. export function isIconDefinition(target) {
  34. return (typeof target === 'object' &&
  35. typeof target.name === 'string' &&
  36. (typeof target.theme === 'string' || target.theme === undefined) &&
  37. typeof target.icon === 'string');
  38. }
  39. /**
  40. * Get an `IconDefinition` object from abbreviation type, like `account-book-fill`.
  41. * @param str
  42. */
  43. export function getIconDefinitionFromAbbr(str) {
  44. const arr = str.split('-');
  45. const theme = mapAbbrToTheme(arr.splice(arr.length - 1, 1)[0]);
  46. const name = arr.join('-');
  47. return {
  48. name,
  49. theme,
  50. icon: ''
  51. };
  52. }
  53. export function cloneSVG(svg) {
  54. return svg.cloneNode(true);
  55. }
  56. /**
  57. * Parse inline SVG string and replace colors with placeholders. For twotone icons only.
  58. */
  59. export function replaceFillColor(raw) {
  60. return raw
  61. .replace(/['"]#333['"]/g, '"primaryColor"')
  62. .replace(/['"]#E6E6E6['"]/g, '"secondaryColor"')
  63. .replace(/['"]#D9D9D9['"]/g, '"secondaryColor"')
  64. .replace(/['"]#D8D8D8['"]/g, '"secondaryColor"');
  65. }
  66. /**
  67. * Split a name with namespace in it into a tuple like [ name, namespace ].
  68. */
  69. export function getNameAndNamespace(type) {
  70. const split = type.split(':');
  71. switch (split.length) {
  72. case 1: return [type, ''];
  73. case 2: return [split[1], split[0]];
  74. default: throw new Error(`${ANT_ICON_ANGULAR_CONSOLE_PREFIX}The icon type ${type} is not valid!`);
  75. }
  76. }
  77. export function hasNamespace(type) {
  78. return getNameAndNamespace(type)[1] !== '';
  79. }
  80. //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFNBQVMsRUFBRSxNQUFNLGVBQWUsQ0FBQztBQUMxQyxPQUFPLEVBQUUsUUFBUSxJQUFJLGFBQWEsRUFBRSxNQUFNLG9CQUFvQixDQUFDO0FBSS9ELE1BQU0sQ0FBQyxNQUFNLCtCQUErQixHQUFHLDhCQUE4QixDQUFDO0FBRTlFLE1BQU0sVUFBVSxLQUFLLENBQUMsT0FBZTtJQUNuQyxPQUFPLENBQUMsS0FBSyxDQUFDLEdBQUcsK0JBQStCLElBQUksT0FBTyxHQUFHLENBQUMsQ0FBQztBQUNsRSxDQUFDO0FBRUQsTUFBTSxVQUFVLElBQUksQ0FBQyxPQUFlO0lBQ2xDLElBQUksU0FBUyxFQUFFLEVBQUU7UUFDZixPQUFPLENBQUMsSUFBSSxDQUFDLEdBQU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