2024-06-26 01:11:57 -05:00
|
|
|
import { toArray } from '@common/map';
|
|
|
|
import { sleep } from '@common/sleep';
|
|
|
|
|
2024-06-25 10:12:20 -05:00
|
|
|
import { textStyles } from '@plugin/libraries';
|
|
|
|
import { translateTextStyle } from '@plugin/translators/styles';
|
|
|
|
|
|
|
|
import { TypographyStyle } from '@ui/lib/types/shapes/textShape';
|
|
|
|
|
|
|
|
const isTextStyle = (style: BaseStyle): style is TextStyle => {
|
|
|
|
return style.type === 'TEXT';
|
|
|
|
};
|
|
|
|
|
|
|
|
export const registerTextStyles = async () => {
|
|
|
|
const localTextStyles = await figma.getLocalTextStylesAsync();
|
|
|
|
localTextStyles.forEach(style => {
|
2024-06-26 01:11:57 -05:00
|
|
|
textStyles.set(style.id, style);
|
2024-06-25 10:12:20 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const processTextStyles = async (): Promise<Record<string, TypographyStyle>> => {
|
2024-06-26 01:11:57 -05:00
|
|
|
const stylesToFetch = toArray(textStyles);
|
2024-06-25 10:12:20 -05:00
|
|
|
const styles: Record<string, TypographyStyle> = {};
|
|
|
|
|
|
|
|
if (stylesToFetch.length === 0) return styles;
|
|
|
|
|
|
|
|
let currentStyle = 1;
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_TOTAL_ITEMS',
|
|
|
|
data: stylesToFetch.length
|
|
|
|
});
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_STEP',
|
|
|
|
data: 'typographies'
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const [styleId, style] of stylesToFetch) {
|
|
|
|
const figmaStyle = style ?? (await figma.getStyleByIdAsync(styleId));
|
|
|
|
if (figmaStyle && isTextStyle(figmaStyle)) {
|
|
|
|
styles[styleId] = translateTextStyle(figmaStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_PROCESSED_ITEMS',
|
|
|
|
data: currentStyle++
|
|
|
|
});
|
|
|
|
|
|
|
|
await sleep(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
await sleep(20);
|
|
|
|
|
|
|
|
return styles;
|
|
|
|
};
|