2024-05-31 04:25:32 -05:00
|
|
|
import { isGoogleFont } from '@plugin/translators/text/font/gfonts';
|
|
|
|
import { isLocalFont } from '@plugin/translators/text/font/local';
|
|
|
|
import { sleep } from '@plugin/utils';
|
|
|
|
|
2024-05-09 05:56:45 -05:00
|
|
|
import { registerChange } from './registerChange';
|
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-05-31 04:25:32 -05:00
|
|
|
for (const node of page.children) {
|
|
|
|
if (node.type === 'TEXT') {
|
|
|
|
extractMissingFonts(node, fonts);
|
2024-05-03 06:43:07 -05:00
|
|
|
}
|
|
|
|
|
2024-06-13 08:17:17 -05:00
|
|
|
await sleep(0);
|
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-09 05:56:45 -05:00
|
|
|
figma.currentPage.once('nodechange', registerChange);
|
2024-05-03 06:43:07 -05:00
|
|
|
};
|
2024-05-31 04:25:32 -05:00
|
|
|
|
|
|
|
const extractMissingFonts = (node: TextNode, fonts: Set<string>) => {
|
|
|
|
const styledTextSegments = node.getStyledTextSegments(['fontName']);
|
|
|
|
|
|
|
|
styledTextSegments.forEach(segment => {
|
|
|
|
if (isGoogleFont(segment.fontName) || isLocalFont(segment.fontName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fonts.add(segment.fontName.family);
|
|
|
|
});
|
|
|
|
};
|