Facebook
From Toxic Eider, 4 Months ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 82
  1. async loadMissingFonts () {
  2.     let fontNames: FontName[];
  3.     if (FigmaUtils.isMixedValue(this.baseNode.fontName)) {
  4.         fontNames = Array.from(this.baseNode.characters).reduce(
  5.             (p, _, i) => {
  6.                 // as FontName - for single character the is no possibility to have multiple font families set
  7.                 const characterFontName = this.baseNode.getRangeFontName(i, i+1) as FontName;
  8.                 const fontAlreadyInArray = p.find(
  9.                     fontName => fontName.family === characterFontName.family &&
  10.                     fontName.style === characterFontName.style
  11.                 );
  12.                 if (!fontAlreadyInArray) {
  13.                     p.push(characterFontName);
  14.                 }
  15.                 return p;
  16.             },
  17.             [] as FontName[]
  18.         );
  19.     } else {
  20.         fontNames = [this.baseNode.fontName];
  21.     }
  22.  
  23.     const loadFontResults = await Promise.all(fontNames.map(
  24.         fontName => figma.loadFontAsync(fontName)
  25.             .catch(e => {
  26.                 figma.notify(e);
  27.                 return false;
  28.             })
  29.     ));
  30.    
  31.     return !loadFontResults.includes(false); // return false if any font loading have failed
  32. }