2024-05-31 04:25:32 -05:00
|
|
|
import { isGoogleFont } from '@plugin/translators/text/font/gfonts';
|
|
|
|
import { isLocalFont } from '@plugin/translators/text/font/local';
|
|
|
|
|
2024-05-03 06:43:07 -05:00
|
|
|
export const findAllTextNodes = async () => {
|
|
|
|
const fonts = new Set<string>();
|
|
|
|
|
2024-05-31 04:25:32 -05:00
|
|
|
for (const page of figma.root.children) {
|
|
|
|
await page.loadAsync();
|
2024-05-03 06:43:07 -05:00
|
|
|
|
2024-06-17 09:33:01 -05:00
|
|
|
const nodes = page.findAll(node => node.type === 'TEXT') as TextNode[];
|
2024-05-03 06:43:07 -05:00
|
|
|
|
2024-06-17 09:33:01 -05:00
|
|
|
for (const node of nodes) {
|
|
|
|
extractMissingFonts(node, fonts);
|
2024-05-31 04:25:32 -05:00
|
|
|
}
|
|
|
|
}
|
2024-05-03 06:43:07 -05:00
|
|
|
|
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'CUSTOM_FONTS',
|
|
|
|
data: Array.from(fonts)
|
|
|
|
});
|
|
|
|
};
|
2024-05-31 04:25:32 -05:00
|
|
|
|
2024-06-18 06:20:09 -05:00
|
|
|
export const findMissingFonts = (node: TextNode): FontName[] => {
|
|
|
|
if (node.fontName !== figma.mixed) {
|
|
|
|
return isKnownFont(node.fontName) ? [] : [node.fontName];
|
|
|
|
}
|
|
|
|
|
|
|
|
const missingFonts: FontName[] = [];
|
2024-05-31 04:25:32 -05:00
|
|
|
const styledTextSegments = node.getStyledTextSegments(['fontName']);
|
|
|
|
|
2024-06-18 06:20:09 -05:00
|
|
|
styledTextSegments.map(segment => {
|
|
|
|
if (isKnownFont(segment.fontName)) return;
|
|
|
|
|
|
|
|
missingFonts.push(segment.fontName);
|
|
|
|
});
|
|
|
|
|
|
|
|
return missingFonts;
|
|
|
|
};
|
2024-05-31 04:25:32 -05:00
|
|
|
|
2024-06-18 06:20:09 -05:00
|
|
|
const extractMissingFonts = (node: TextNode, fonts: Set<string>) => {
|
|
|
|
const missingFonts = findMissingFonts(node);
|
|
|
|
|
|
|
|
missingFonts.forEach(font => {
|
|
|
|
fonts.add(font.family);
|
2024-05-31 04:25:32 -05:00
|
|
|
});
|
|
|
|
};
|
2024-06-18 06:20:09 -05:00
|
|
|
|
|
|
|
const isKnownFont = (fontName: FontName): boolean => {
|
|
|
|
return isGoogleFont(fontName) || isLocalFont(fontName);
|
|
|
|
};
|