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