2024-05-30 10:54:37 -05:00
|
|
|
import { PenpotFile } from '@ui/lib/types/penpotFile';
|
2024-06-28 04:40:33 -05:00
|
|
|
import { Uuid } from '@ui/lib/types/utils/uuid';
|
2024-06-26 01:11:57 -05:00
|
|
|
import { components, parseFigmaId } from '@ui/parser';
|
2024-05-30 10:54:37 -05:00
|
|
|
import { ComponentInstance } from '@ui/types';
|
|
|
|
|
|
|
|
import { createArtboard } from '.';
|
|
|
|
|
2024-06-28 04:40:33 -05:00
|
|
|
let remoteFileId: Uuid | undefined = undefined;
|
|
|
|
|
2024-05-30 10:54:37 -05:00
|
|
|
export const createComponentInstance = (
|
|
|
|
file: PenpotFile,
|
2024-06-28 05:17:56 -05:00
|
|
|
{ type, mainComponentFigmaId, isComponentRoot, ...shape }: ComponentInstance
|
2024-05-30 10:54:37 -05:00
|
|
|
) => {
|
2024-06-18 04:20:51 -05:00
|
|
|
const uiComponent =
|
2024-06-25 10:12:20 -05:00
|
|
|
components.get(mainComponentFigmaId) ?? createUiComponent(file, mainComponentFigmaId);
|
2024-05-30 10:54:37 -05:00
|
|
|
|
|
|
|
if (!uiComponent) {
|
2024-06-18 04:20:51 -05:00
|
|
|
return;
|
2024-05-30 10:54:37 -05:00
|
|
|
}
|
|
|
|
|
2024-06-28 05:17:56 -05:00
|
|
|
if (!shape.figmaRelatedId) {
|
|
|
|
shape.shapeRef = uiComponent.mainInstanceId;
|
|
|
|
}
|
2024-06-28 04:40:33 -05:00
|
|
|
shape.componentFile = shape.isOrphan ? getRemoteFileId(file) : file.getId();
|
2024-06-18 04:20:51 -05:00
|
|
|
shape.componentRoot = isComponentRoot;
|
|
|
|
shape.componentId = uiComponent.componentId;
|
|
|
|
|
|
|
|
createArtboard(file, shape);
|
|
|
|
};
|
|
|
|
|
|
|
|
const createUiComponent = (file: PenpotFile, mainComponentFigmaId: string) => {
|
|
|
|
const mainInstanceId = parseFigmaId(file, mainComponentFigmaId);
|
|
|
|
if (!mainInstanceId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uiComponent = {
|
|
|
|
componentId: file.newId(),
|
|
|
|
componentFigmaId: mainComponentFigmaId,
|
|
|
|
mainInstanceId
|
|
|
|
};
|
|
|
|
|
2024-06-26 01:11:57 -05:00
|
|
|
components.set(mainComponentFigmaId, uiComponent);
|
2024-06-18 04:20:51 -05:00
|
|
|
|
|
|
|
return uiComponent;
|
2024-05-30 10:54:37 -05:00
|
|
|
};
|
2024-06-28 04:40:33 -05:00
|
|
|
|
|
|
|
const getRemoteFileId = (file: PenpotFile): Uuid => {
|
|
|
|
if (!remoteFileId) {
|
|
|
|
remoteFileId = file.newId();
|
|
|
|
}
|
|
|
|
|
|
|
|
return remoteFileId;
|
|
|
|
};
|