2024-05-30 10:54:37 -05:00
|
|
|
import {
|
|
|
|
transformBlend,
|
|
|
|
transformChildren,
|
|
|
|
transformCornerRadius,
|
|
|
|
transformDimensionAndPosition,
|
|
|
|
transformEffects,
|
|
|
|
transformFigmaIds,
|
|
|
|
transformFills,
|
|
|
|
transformProportion,
|
|
|
|
transformSceneNode,
|
|
|
|
transformStrokes
|
|
|
|
} from '@plugin/transformers/partials';
|
|
|
|
|
|
|
|
import { ComponentInstance } from '@ui/types';
|
|
|
|
|
|
|
|
export const transformInstanceNode = async (
|
|
|
|
node: InstanceNode,
|
|
|
|
baseX: number,
|
|
|
|
baseY: number
|
|
|
|
): Promise<ComponentInstance | undefined> => {
|
|
|
|
const mainComponent = await node.getMainComponentAsync();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We do not want to process component instances in the following scenarios:
|
|
|
|
*
|
|
|
|
* 1. If the component does not have a main component.
|
2024-06-03 03:06:18 -05:00
|
|
|
* 2. If the component comes from an external design system.
|
|
|
|
* 3. If th component does not have a parent. (it's been removed)
|
2024-05-30 10:54:37 -05:00
|
|
|
*/
|
2024-06-03 03:06:18 -05:00
|
|
|
if (!mainComponent || mainComponent.remote || mainComponent.parent === null) {
|
2024-05-30 10:54:37 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'instance',
|
2024-05-31 06:32:59 -05:00
|
|
|
name: node.name,
|
2024-05-30 10:54:37 -05:00
|
|
|
mainComponentFigmaId: mainComponent.id,
|
|
|
|
isComponentRoot: isComponentRoot(node),
|
|
|
|
...transformFigmaIds(node),
|
|
|
|
...(await transformFills(node)),
|
|
|
|
...transformEffects(node),
|
|
|
|
...(await transformStrokes(node)),
|
|
|
|
...transformSceneNode(node),
|
|
|
|
...transformBlend(node),
|
|
|
|
...transformProportion(node),
|
|
|
|
...transformCornerRadius(node),
|
|
|
|
...transformDimensionAndPosition(node, baseX, baseY),
|
|
|
|
...(await transformChildren(node, baseX + node.x, baseY + node.y))
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const isComponentRoot = (node: InstanceNode): boolean => {
|
|
|
|
let parent = node.parent;
|
|
|
|
while (parent !== null) {
|
|
|
|
if (parent.type === 'COMPONENT' || parent.type === 'INSTANCE') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
parent = parent.parent;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|