2024-06-25 10:12:20 -05:00
|
|
|
import { overrides, remoteComponents } from '@plugin/libraries';
|
2024-05-30 10:54:37 -05:00
|
|
|
import {
|
2024-06-14 03:18:34 -05:00
|
|
|
transformAutoLayout,
|
2024-05-30 10:54:37 -05:00
|
|
|
transformBlend,
|
|
|
|
transformChildren,
|
2024-06-06 05:17:17 -05:00
|
|
|
transformConstraints,
|
2024-05-30 10:54:37 -05:00
|
|
|
transformCornerRadius,
|
2024-06-14 09:28:01 -05:00
|
|
|
transformDimension,
|
2024-05-30 10:54:37 -05:00
|
|
|
transformEffects,
|
|
|
|
transformFigmaIds,
|
|
|
|
transformFills,
|
2024-06-14 03:18:34 -05:00
|
|
|
transformLayoutAttributes,
|
2024-06-17 02:58:23 -05:00
|
|
|
transformOverrides,
|
2024-05-30 10:54:37 -05:00
|
|
|
transformProportion,
|
2024-06-14 09:28:01 -05:00
|
|
|
transformRotationAndPosition,
|
2024-05-30 10:54:37 -05:00
|
|
|
transformSceneNode,
|
|
|
|
transformStrokes
|
|
|
|
} from '@plugin/transformers/partials';
|
|
|
|
|
2024-06-17 05:19:53 -05:00
|
|
|
import { ComponentInstance, ComponentTextPropertyOverride } from '@ui/types';
|
2024-05-30 10:54:37 -05:00
|
|
|
|
|
|
|
export const transformInstanceNode = async (
|
2024-06-19 01:10:20 -05:00
|
|
|
node: InstanceNode
|
2024-05-30 10:54:37 -05:00
|
|
|
): Promise<ComponentInstance | undefined> => {
|
|
|
|
const mainComponent = await node.getMainComponentAsync();
|
2024-06-17 05:19:53 -05:00
|
|
|
if (mainComponent === null) {
|
|
|
|
return;
|
|
|
|
}
|
2024-05-30 10:54:37 -05:00
|
|
|
|
2024-06-17 05:19:53 -05:00
|
|
|
const primaryComponent = getPrimaryComponent(mainComponent);
|
|
|
|
if (isUnprocessableComponent(primaryComponent)) {
|
2024-05-30 10:54:37 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-17 05:19:53 -05:00
|
|
|
if (primaryComponent.remote) {
|
|
|
|
registerExternalComponents(primaryComponent);
|
2024-06-06 02:37:35 -05:00
|
|
|
}
|
|
|
|
|
2024-06-17 05:19:53 -05:00
|
|
|
registerTextVariableOverrides(node, primaryComponent);
|
|
|
|
|
2024-06-17 02:58:23 -05:00
|
|
|
if (node.overrides.length > 0) {
|
2024-06-26 01:11:57 -05:00
|
|
|
node.overrides.forEach(override => overrides.set(override.id, override.overriddenFields));
|
2024-06-17 02:58:23 -05:00
|
|
|
}
|
|
|
|
|
2024-05-30 10:54:37 -05:00
|
|
|
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,
|
2024-05-30 10:54:37 -05:00
|
|
|
isComponentRoot: isComponentRoot(node),
|
2024-06-06 02:37:35 -05:00
|
|
|
showContent: !node.clipsContent,
|
2024-05-30 10:54:37 -05:00
|
|
|
...transformFigmaIds(node),
|
2024-06-05 05:36:49 -05:00
|
|
|
...transformFills(node),
|
2024-05-30 10:54:37 -05:00
|
|
|
...transformEffects(node),
|
2024-06-05 05:36:49 -05:00
|
|
|
...transformStrokes(node),
|
2024-05-30 10:54:37 -05:00
|
|
|
...transformSceneNode(node),
|
|
|
|
...transformBlend(node),
|
|
|
|
...transformProportion(node),
|
2024-06-18 06:10:30 -05:00
|
|
|
...transformLayoutAttributes(node, true),
|
2024-05-30 10:54:37 -05:00
|
|
|
...transformCornerRadius(node),
|
2024-06-14 09:28:01 -05:00
|
|
|
...transformDimension(node),
|
2024-06-19 01:10:20 -05:00
|
|
|
...transformRotationAndPosition(node),
|
2024-06-06 05:17:17 -05:00
|
|
|
...transformConstraints(node),
|
2024-06-14 03:18:34 -05:00
|
|
|
...transformAutoLayout(node),
|
2024-06-19 01:10:20 -05:00
|
|
|
...(await transformChildren(node)),
|
2024-06-17 02:58:23 -05:00
|
|
|
...transformOverrides(node)
|
2024-05-30 10:54:37 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-06-17 05:19:53 -05:00
|
|
|
const getPrimaryComponent = (mainComponent: ComponentNode): ComponentNode | ComponentSetNode => {
|
|
|
|
if (mainComponent.parent?.type === 'COMPONENT_SET') {
|
|
|
|
return mainComponent.parent;
|
2024-06-06 02:37:35 -05:00
|
|
|
}
|
|
|
|
|
2024-06-17 05:19:53 -05:00
|
|
|
return mainComponent;
|
|
|
|
};
|
|
|
|
|
|
|
|
const registerExternalComponents = (primaryComponent: ComponentNode | ComponentSetNode): void => {
|
2024-06-25 10:12:20 -05:00
|
|
|
if (remoteComponents.has(primaryComponent.id)) {
|
2024-06-06 02:37:35 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-25 10:12:20 -05:00
|
|
|
remoteComponents.register(primaryComponent.id, primaryComponent);
|
2024-06-06 02:37:35 -05:00
|
|
|
};
|
|
|
|
|
2024-06-17 05:19:53 -05:00
|
|
|
const getComponentTextPropertyOverrides = (
|
|
|
|
node: InstanceNode,
|
|
|
|
primaryComponent: ComponentNode | ComponentSetNode
|
|
|
|
): ComponentTextPropertyOverride[] => {
|
2024-06-17 05:46:10 -05:00
|
|
|
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 [];
|
|
|
|
}
|
2024-06-17 05:19:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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 => {
|
2024-06-26 01:11:57 -05:00
|
|
|
overrides.set(textNode.id, ['text']);
|
2024-06-17 05:19:53 -05:00
|
|
|
});
|
|
|
|
}
|
2024-06-06 02:37:35 -05:00
|
|
|
};
|
|
|
|
|
2024-06-03 06:52:50 -05:00
|
|
|
/**
|
|
|
|
* We do not want to process component instances in the following scenarios:
|
|
|
|
*
|
2024-06-06 02:37:35 -05:00
|
|
|
* 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
|
|
|
*/
|
2024-06-17 05:19:53 -05:00
|
|
|
const isUnprocessableComponent = (primaryComponent: ComponentNode | ComponentSetNode): boolean => {
|
|
|
|
return primaryComponent.parent === null && !primaryComponent.remote;
|
2024-06-03 06:27:30 -05:00
|
|
|
};
|
|
|
|
|
2024-05-30 10:54:37 -05:00
|
|
|
const isComponentRoot = (node: InstanceNode): boolean => {
|
|
|
|
let parent = node.parent;
|
2024-06-05 05:36:49 -05:00
|
|
|
|
2024-05-30 10:54:37 -05:00
|
|
|
while (parent !== null) {
|
|
|
|
if (parent.type === 'COMPONENT' || parent.type === 'INSTANCE') {
|
|
|
|
return false;
|
|
|
|
}
|
2024-06-05 05:36:49 -05:00
|
|
|
|
2024-05-30 10:54:37 -05:00
|
|
|
parent = parent.parent;
|
|
|
|
}
|
2024-06-05 05:36:49 -05:00
|
|
|
|
2024-05-30 10:54:37 -05:00
|
|
|
return true;
|
|
|
|
};
|