Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Cenadros committed Apr 29, 2024
1 parent 709ea46 commit c9b2ffb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
11 changes: 4 additions & 7 deletions plugin-src/translators/translateFontId.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import slugify from 'slugify';

import fonts from '@plugin/gfonts.json';

import { Gfont } from '@ui/lib/types/utils/gfont';

const gfonts: Gfont[] = fonts.items;
import { loadGoogleFonts } from '@plugin/utils';

export const translateFontId = (fontName: FontName): string => {
// is gfont
if (isGfont(fontName.family)) {
return `gfont-${slugify(fontName.family.toLowerCase())}`;
}

// @TODO: check if source sans pro

// always send font name if not gfont or source sans pro
figma.ui.postMessage({ type: 'FONT_NAME', data: fontName.family });

// @TODO: check if source sans pro
// @TODO: custom font
return slugify(fontName.family.toLowerCase());
};

const isGfont = (fontFamily: string): boolean => {
const foundFamily = gfonts.find(gfont => gfont.family === fontFamily);
const foundFamily = loadGoogleFonts().find(gfont => gfont.family === fontFamily);

return foundFamily !== undefined;
};
7 changes: 2 additions & 5 deletions plugin-src/translators/translateFontVariantId.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import fonts from '@plugin/gfonts.json';
import { loadGoogleFonts } from '@plugin/utils';

import { Gfont } from '@ui/lib/types/utils/gfont';

const gfonts: Gfont[] = fonts.items;

export const translateFontVariantId = (fontName: FontName, fontWeight: number) => {
// Gfont
const variantId = translateGfontVariantId(fontName, fontWeight);
Expand All @@ -17,11 +15,10 @@ export const translateFontVariantId = (fontName: FontName, fontWeight: number) =
};

const getGfont = (fontFamily: string): Gfont | undefined => {
return gfonts.find(gfont => gfont.family === fontFamily);
return loadGoogleFonts().find(gfont => gfont.family === fontFamily);
};

const translateGfontVariantId = (fontName: FontName, fontWeight: number): string | undefined => {
// is gfont
const gfont = getGfont(fontName.family);
if (gfont === undefined) {
return;
Expand Down
1 change: 1 addition & 0 deletions plugin-src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from './applyMatrixToPoint';
export * from './calculateAdjustment';
export * from './calculateLinearGradient';
export * from './detectMimeType';
export * from './loadGoogleFonts';
export * from './matrixInvert';
export * from './rgbToHex';
7 changes: 7 additions & 0 deletions plugin-src/utils/loadGoogleFonts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fonts from '@plugin/gfonts.json';

import { Gfont } from '@ui/lib/types/utils/gfont';

export const loadGoogleFonts = (): Gfont[] => {
return fonts.items;
};

0 comments on commit c9b2ffb

Please sign in to comment.