0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 13:43:03 -05:00
penpot-exporter-figma-plugin/plugin-src/transformers/transformInstanceNode.ts
Alex Sánchez 90d6dfd95a
Filter missing component set (#131)
* filtering missing component sets

* fixes

* fixes

* fixes
2024-06-03 13:27:30 +02:00

72 lines
2.1 KiB
TypeScript

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();
if (!isNodeProcessable(node, mainComponent)) {
return;
}
return {
type: 'instance',
name: node.name,
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 isNodeProcessable = (node: SceneNode, mainComponent: ComponentNode | null): boolean => {
/**
* We do not want to process component instances in the following scenarios:
*
* 1. If the component does not have a main component.
* 2. If the component comes from an external design system.
* 3. If th component does not have a parent. (it's been removed)
* 4. Main component can be in a ComponentSet removed from the page or external design system.
*/
return !(
!mainComponent ||
mainComponent.remote ||
mainComponent.parent === null ||
(mainComponent.parent?.type === 'COMPONENT_SET' &&
(mainComponent.parent.parent === null || mainComponent.parent.remote))
);
};
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;
};