2024-05-29 17:33:29 +02:00
|
|
|
import { componentsLibrary } from '@plugin/ComponentLibrary';
|
|
|
|
|
2024-05-29 12:52:21 +02:00
|
|
|
import { PenpotFile } from '@ui/lib/types/penpotFile';
|
2024-05-29 17:33:29 +02:00
|
|
|
import { uiComponents } from '@ui/parser/libraries';
|
|
|
|
import { ComponentRoot } from '@ui/types';
|
2024-05-29 12:52:21 +02:00
|
|
|
|
|
|
|
import { createArtboard } from '.';
|
|
|
|
|
2024-05-29 17:33:29 +02:00
|
|
|
export const createComponent = (file: PenpotFile, { figmaId }: ComponentRoot) => {
|
|
|
|
const component = componentsLibrary.get(figmaId);
|
2024-06-18 11:20:51 +02:00
|
|
|
|
2024-05-29 17:33:29 +02:00
|
|
|
if (!component) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-18 11:20:51 +02:00
|
|
|
const componentId = getComponentId(file, figmaId);
|
|
|
|
const { type, ...shape } = component;
|
2024-05-30 17:54:37 +02:00
|
|
|
|
2024-06-18 11:20:51 +02:00
|
|
|
shape.componentFile = file.getId();
|
|
|
|
shape.componentId = componentId;
|
|
|
|
shape.componentRoot = true;
|
|
|
|
shape.mainInstance = true;
|
|
|
|
|
|
|
|
const frameId = createArtboard(file, shape);
|
2024-05-29 12:52:21 +02:00
|
|
|
|
2024-05-30 17:54:37 +02:00
|
|
|
if (!frameId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-29 17:33:29 +02:00
|
|
|
uiComponents.register(figmaId, {
|
|
|
|
componentId,
|
2024-05-29 12:52:21 +02:00
|
|
|
mainInstancePage: file.getCurrentPageId(),
|
2024-05-29 17:33:29 +02:00
|
|
|
componentFigmaId: figmaId,
|
|
|
|
mainInstanceId: frameId
|
2024-05-29 12:52:21 +02:00
|
|
|
});
|
|
|
|
};
|
2024-06-18 11:20:51 +02:00
|
|
|
|
|
|
|
const getComponentId = (file: PenpotFile, figmaId: string) => {
|
|
|
|
const uiComponent = uiComponents.get(figmaId);
|
|
|
|
|
|
|
|
return uiComponent?.componentId ?? file.newId();
|
|
|
|
};
|