0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 21:53:27 -05:00
penpot-exporter-figma-plugin/plugin-src/transformers/transformInstanceNode.ts

167 lines
4.8 KiB
TypeScript
Raw Permalink Normal View History

import { overridesLibrary } from '@plugin/OverridesLibrary';
2024-06-19 04:38:57 -05:00
import { nodeQueue } from '@plugin/Queue';
import { remoteComponentLibrary } from '@plugin/RemoteComponentLibrary';
import {
transformAutoLayout,
transformBlend,
transformChildren,
transformConstraints,
transformCornerRadius,
transformDimension,
transformEffects,
transformFigmaIds,
transformFills,
transformLayoutAttributes,
transformOverrides,
transformProportion,
transformRotationAndPosition,
transformSceneNode,
transformStrokes
} from '@plugin/transformers/partials';
import { ComponentInstance, ComponentTextPropertyOverride } from '@ui/types';
export const transformInstanceNode = async (
node: InstanceNode
): Promise<ComponentInstance | undefined> => {
const mainComponent = await node.getMainComponentAsync();
if (mainComponent === null) {
return;
}
const primaryComponent = getPrimaryComponent(mainComponent);
if (isUnprocessableComponent(primaryComponent)) {
return;
}
if (primaryComponent.remote) {
registerExternalComponents(primaryComponent);
}
registerTextVariableOverrides(node, primaryComponent);
if (node.overrides.length > 0) {
node.overrides.forEach(override =>
overridesLibrary.register(override.id, override.overriddenFields)
);
}
return {
type: 'instance',
2024-05-31 06:32:59 -05:00
name: node.name,
2024-06-03 06:52:50 -05:00
mainComponentFigmaId: mainComponent.id,
isComponentRoot: isComponentRoot(node),
showContent: !node.clipsContent,
...transformFigmaIds(node),
...transformFills(node),
...transformEffects(node),
...transformStrokes(node),
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node),
...transformLayoutAttributes(node, true),
...transformCornerRadius(node),
...transformDimension(node),
...transformRotationAndPosition(node),
...transformConstraints(node),
...transformAutoLayout(node),
...transformChildren(node),
...transformOverrides(node)
};
};
const getPrimaryComponent = (mainComponent: ComponentNode): ComponentNode | ComponentSetNode => {
if (mainComponent.parent?.type === 'COMPONENT_SET') {
return mainComponent.parent;
}
return mainComponent;
};
const registerExternalComponents = (primaryComponent: ComponentNode | ComponentSetNode): void => {
2024-06-19 04:38:57 -05:00
if (remoteComponentLibrary.has(primaryComponent.id)) {
return;
}
2024-06-19 04:38:57 -05:00
remoteComponentLibrary.register(primaryComponent.id);
nodeQueue.enqueue([primaryComponent, 0]).then(([penpotNode, _]) => {
if (penpotNode) remoteComponentLibrary.add(penpotNode);
});
};
const getComponentTextPropertyOverrides = (
node: InstanceNode,
primaryComponent: ComponentNode | ComponentSetNode
): ComponentTextPropertyOverride[] => {
try {
const componentPropertyDefinitions = Object.entries(
primaryComponent.componentPropertyDefinitions
).filter(([, value]) => value.type === 'TEXT');
const instanceComponentProperties = new Map(
Object.entries(node.componentProperties).filter(([, value]) => value.type === 'TEXT')
);
return componentPropertyDefinitions
.map(([key, value]) => {
const nodeValue = instanceComponentProperties.get(key);
return {
id: key,
...value,
value: nodeValue ? nodeValue.value : value.defaultValue
} as ComponentTextPropertyOverride;
})
.filter(({ value, defaultValue }) => value !== defaultValue);
} catch (error) {
return [];
}
};
const registerTextVariableOverrides = (
node: InstanceNode,
primaryComponent: ComponentNode | ComponentSetNode
) => {
const mergedOverridden = getComponentTextPropertyOverrides(node, primaryComponent);
if (mergedOverridden.length > 0) {
const textNodes = node
.findChildren(child => child.type === 'TEXT')
.filter(textNode => {
const componentPropertyReference = textNode.componentPropertyReferences?.characters;
return (
componentPropertyReference &&
mergedOverridden.some(property => property.id === componentPropertyReference)
);
});
textNodes.forEach(textNode => {
overridesLibrary.register(textNode.id, ['text']);
});
}
};
2024-06-03 06:52:50 -05:00
/**
* We do not want to process component instances in the following scenarios:
*
* 1. If the component does not have a parent. (it's been removed)
* 2. Main component can be in a ComponentSet (the same logic applies to the parent).
2024-06-03 06:52:50 -05:00
*/
const isUnprocessableComponent = (primaryComponent: ComponentNode | ComponentSetNode): boolean => {
return primaryComponent.parent === null && !primaryComponent.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;
};