mirror of
https://github.com/penpot/penpot-exporter-figma-plugin.git
synced 2024-12-22 05:33:02 -05:00
Text Variables Overrides (#167)
* wip * register text variables overrides * changeset * fixes * fixes
This commit is contained in:
parent
202e7f4fda
commit
14c9d02cc9
3 changed files with 78 additions and 23 deletions
5
.changeset/violet-otters-impress.md
Normal file
5
.changeset/violet-otters-impress.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"penpot-exporter": minor
|
||||
---
|
||||
|
||||
Register Text Variables as Instance override
|
|
@ -18,22 +18,28 @@ import {
|
|||
transformStrokes
|
||||
} from '@plugin/transformers/partials';
|
||||
|
||||
import { ComponentInstance } from '@ui/types';
|
||||
import { ComponentInstance, ComponentTextPropertyOverride } from '@ui/types';
|
||||
|
||||
export const transformInstanceNode = async (
|
||||
node: InstanceNode,
|
||||
baseRotation: number
|
||||
): Promise<ComponentInstance | undefined> => {
|
||||
const mainComponent = await node.getMainComponentAsync();
|
||||
|
||||
if (mainComponent === null || isUnprocessableComponent(mainComponent)) {
|
||||
if (mainComponent === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isExternalComponent(mainComponent)) {
|
||||
registerExternalComponents(mainComponent);
|
||||
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)
|
||||
|
@ -64,25 +70,67 @@ export const transformInstanceNode = async (
|
|||
};
|
||||
};
|
||||
|
||||
const registerExternalComponents = (mainComponent: ComponentNode): void => {
|
||||
let component: ComponentSetNode | ComponentNode = mainComponent;
|
||||
|
||||
if (component.parent?.type === 'COMPONENT_SET') {
|
||||
component = component.parent;
|
||||
const getPrimaryComponent = (mainComponent: ComponentNode): ComponentNode | ComponentSetNode => {
|
||||
if (mainComponent.parent?.type === 'COMPONENT_SET') {
|
||||
return mainComponent.parent;
|
||||
}
|
||||
|
||||
if (remoteComponentLibrary.get(component.id) !== undefined) {
|
||||
return mainComponent;
|
||||
};
|
||||
|
||||
const registerExternalComponents = (primaryComponent: ComponentNode | ComponentSetNode): void => {
|
||||
if (remoteComponentLibrary.get(primaryComponent.id) !== undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
remoteComponentLibrary.register(component.id, component);
|
||||
remoteComponentLibrary.register(primaryComponent.id, primaryComponent);
|
||||
};
|
||||
|
||||
const isExternalComponent = (mainComponent: ComponentNode): boolean => {
|
||||
return (
|
||||
mainComponent.remote ||
|
||||
(mainComponent.parent?.type === 'COMPONENT_SET' && mainComponent.parent.remote)
|
||||
const getComponentTextPropertyOverrides = (
|
||||
node: InstanceNode,
|
||||
primaryComponent: ComponentNode | ComponentSetNode
|
||||
): ComponentTextPropertyOverride[] => {
|
||||
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);
|
||||
};
|
||||
|
||||
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']);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -91,13 +139,8 @@ const isExternalComponent = (mainComponent: ComponentNode): boolean => {
|
|||
* 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).
|
||||
*/
|
||||
const isUnprocessableComponent = (mainComponent: ComponentNode): boolean => {
|
||||
return (
|
||||
(mainComponent.parent === null && !mainComponent.remote) ||
|
||||
(mainComponent.parent?.type === 'COMPONENT_SET' &&
|
||||
mainComponent.parent.parent === null &&
|
||||
!mainComponent.parent.remote)
|
||||
);
|
||||
const isUnprocessableComponent = (primaryComponent: ComponentNode | ComponentSetNode): boolean => {
|
||||
return primaryComponent.parent === null && !primaryComponent.remote;
|
||||
};
|
||||
|
||||
const isComponentRoot = (node: InstanceNode): boolean => {
|
||||
|
|
|
@ -7,6 +7,13 @@ export type ComponentRoot = {
|
|||
type: 'component';
|
||||
};
|
||||
|
||||
export type ComponentTextPropertyOverride = {
|
||||
id: string;
|
||||
type: 'TEXT';
|
||||
value: string;
|
||||
defaultValue: string;
|
||||
};
|
||||
|
||||
export type ComponentInstance = ShapeGeomAttributes &
|
||||
ShapeAttributes &
|
||||
LayoutAttributes &
|
||||
|
|
Loading…
Reference in a new issue