0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 21:53:27 -05:00
penpot-exporter-figma-plugin/plugin-src/processors/processPaintStyles.ts

56 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-06-26 01:11:57 -05:00
import { toArray } from '@common/map';
import { sleep } from '@common/sleep';
import { paintStyles } from '@plugin/libraries';
import { translatePaintStyle } from '@plugin/translators/styles';
import { FillStyle } from '@ui/lib/types/utils/fill';
const isPaintStyle = (style: BaseStyle): style is PaintStyle => {
return style.type === 'PAINT';
};
export const registerPaintStyles = async () => {
const localPaintStyles = await figma.getLocalPaintStylesAsync();
localPaintStyles.forEach(style => {
2024-06-26 01:11:57 -05:00
paintStyles.set(style.id, style);
});
};
export const processPaintStyles = async (): Promise<Record<string, FillStyle>> => {
2024-06-26 01:11:57 -05:00
const stylesToFetch = toArray(paintStyles);
const styles: Record<string, FillStyle> = {};
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: 'fills'
});
for (const [styleId, paintStyle] of stylesToFetch) {
const figmaStyle = paintStyle ?? (await figma.getStyleByIdAsync(styleId));
if (figmaStyle && isPaintStyle(figmaStyle)) {
styles[styleId] = translatePaintStyle(figmaStyle);
}
figma.ui.postMessage({
type: 'PROGRESS_PROCESSED_ITEMS',
data: currentStyle++
});
await sleep(0);
}
await sleep(20);
return styles;
};