0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 13:43:03 -05:00
penpot-exporter-figma-plugin/ui-src/parser/creators/createComponentsLibrary.ts
Jordi Sala Morales 1ebdf3e59d
Fix components (#193)
* Fix components

* fix lint
2024-06-26 11:38:42 +02:00

62 lines
1.6 KiB
TypeScript

import { toArray } from '@common/map';
import { sleep } from '@common/sleep';
import { sendMessage } from '@ui/context';
import { PenpotFile } from '@ui/lib/types/penpotFile';
import { componentShapes, components as uiComponents } from '@ui/parser';
import { symbolFills, symbolStrokes } from '@ui/parser/creators/symbols';
import { UiComponent } from '@ui/types';
import { createItems } from '.';
export const createComponentsLibrary = async (file: PenpotFile) => {
let componentsBuilt = 1;
const components = toArray(uiComponents);
sendMessage({
type: 'PROGRESS_TOTAL_ITEMS',
data: components.length
});
sendMessage({
type: 'PROGRESS_STEP',
data: 'components'
});
for (const [_, uiComponent] of components) {
createComponentLibrary(file, uiComponent);
sendMessage({
type: 'PROGRESS_PROCESSED_ITEMS',
data: componentsBuilt++
});
await sleep(0);
}
};
const createComponentLibrary = (file: PenpotFile, uiComponent: UiComponent) => {
const componentShape = componentShapes.get(uiComponent.componentFigmaId);
if (!componentShape) {
return;
}
const { children = [], ...shape } = componentShape;
shape.fills = symbolFills(shape.fillStyleId, shape.fills);
shape.strokes = symbolStrokes(shape.strokes);
shape.id = uiComponent.componentId;
shape.componentId = uiComponent.componentId;
shape.mainInstancePage = uiComponent.mainInstancePage;
shape.mainInstanceId = uiComponent.mainInstanceId;
shape.componentRoot = true;
shape.mainInstance = true;
shape.componentFile = file.getId();
file.startComponent(shape);
createItems(file, children);
file.finishComponent();
};