modern.mjs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { iconToSVG, isUnsetKeyword } from '../svg/build.mjs';
  2. import { getIconData } from '../icon-set/get-icon.mjs';
  3. import { calculateSize } from '../svg/size.mjs';
  4. import { mergeIconProps } from './utils.mjs';
  5. import createDebugger from 'debug';
  6. import { defaultIconCustomisations } from '../customisations/defaults.mjs';
  7. import '../icon/defaults.mjs';
  8. import '../svg/defs.mjs';
  9. import '../icon/merge.mjs';
  10. import '../icon/transformations.mjs';
  11. import '../icon-set/tree.mjs';
  12. const debug = createDebugger("@iconify-loader:icon");
  13. async function searchForIcon(iconSet, collection, ids, options) {
  14. let iconData;
  15. const { customize } = options?.customizations ?? {};
  16. for (const id of ids) {
  17. iconData = getIconData(iconSet, id);
  18. if (iconData) {
  19. debug(`${collection}:${id}`);
  20. let defaultCustomizations = {
  21. ...defaultIconCustomisations
  22. };
  23. if (typeof customize === "function") {
  24. iconData = Object.assign({}, iconData);
  25. defaultCustomizations = customize(
  26. defaultCustomizations,
  27. iconData,
  28. `${collection}:${id}`
  29. ) ?? defaultCustomizations;
  30. }
  31. const {
  32. attributes: { width, height, ...restAttributes },
  33. body
  34. } = iconToSVG(iconData, defaultCustomizations);
  35. const scale = options?.scale;
  36. return await mergeIconProps(
  37. // DON'T remove space on <svg >
  38. `<svg >${body}</svg>`,
  39. collection,
  40. id,
  41. options,
  42. () => {
  43. return { ...restAttributes };
  44. },
  45. (props) => {
  46. const check = (prop, defaultValue) => {
  47. const propValue = props[prop];
  48. let value;
  49. if (!isUnsetKeyword(propValue)) {
  50. if (propValue) {
  51. return;
  52. }
  53. if (typeof scale === "number") {
  54. if (scale) {
  55. value = calculateSize(
  56. // Base on result from iconToSVG() or 1em
  57. defaultValue ?? "1em",
  58. scale
  59. );
  60. }
  61. } else {
  62. value = defaultValue;
  63. }
  64. }
  65. if (!value) {
  66. delete props[prop];
  67. } else {
  68. props[prop] = value;
  69. }
  70. };
  71. check("width", width);
  72. check("height", height);
  73. }
  74. );
  75. }
  76. }
  77. }
  78. export { searchForIcon };