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 { 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);
|
2024-06-25 10:12:20 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const processPaintStyles = async (): Promise<Record<string, FillStyle>> => {
|
2024-06-26 01:11:57 -05:00
|
|
|
const stylesToFetch = toArray(paintStyles);
|
2024-06-25 10:12:20 -05:00
|
|
|
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;
|
|
|
|
};
|