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/ui-src/parser/creators/createComponent.ts

41 lines
1,013 B
TypeScript
Raw Permalink Normal View History

import { PenpotFile } from '@ui/lib/types/penpotFile';
2024-06-26 01:11:57 -05:00
import { componentShapes, components } from '@ui/parser';
import { ComponentRoot } from '@ui/types';
import { createArtboard } from '.';
export const createComponent = (file: PenpotFile, { figmaId }: ComponentRoot) => {
2024-06-26 01:11:57 -05:00
const componentShape = componentShapes.get(figmaId);
2024-06-26 01:11:57 -05:00
if (!componentShape) {
return;
}
const componentId = getComponentId(file, figmaId);
2024-06-26 01:11:57 -05:00
const { type, ...shape } = componentShape;
shape.componentFile = file.getId();
shape.componentId = componentId;
shape.componentRoot = true;
shape.mainInstance = true;
const frameId = createArtboard(file, shape);
if (!frameId) {
return;
}
2024-06-26 01:11:57 -05:00
components.set(figmaId, {
componentId,
mainInstancePage: file.getCurrentPageId(),
componentFigmaId: figmaId,
mainInstanceId: frameId
});
};
const getComponentId = (file: PenpotFile, figmaId: string) => {
2024-06-26 01:11:57 -05:00
const component = components.get(figmaId);
2024-06-26 01:11:57 -05:00
return component?.componentId ?? file.newId();
};