Skip to content

Commit

Permalink
Constraints translation (#142)
Browse files Browse the repository at this point in the history
* constraints translation

* fix lint

* fixes

* fix vector constraints

* fix lint
  • Loading branch information
Cenadros authored Jun 6, 2024
1 parent 12be821 commit 02fa336
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-elephants-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"penpot-exporter": minor
---

Constraints translation
1 change: 1 addition & 0 deletions plugin-src/transformers/partials/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './transformBlend';
export * from './transformChildren';
export * from './transformConstraints';
export * from './transformCornerRadius';
export * from './transformDimensionAndPosition';
export * from './transformEffects';
Expand Down
12 changes: 12 additions & 0 deletions plugin-src/transformers/partials/transformConstraints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { translateConstraintH, translateConstraintV } from '@plugin/translators';

import { ShapeAttributes } from '@ui/lib/types/shapes/shape';

export const transformConstraints = (
node: ConstraintMixin
): Pick<ShapeAttributes, 'constraintsH' | 'constraintsV'> => {
return {
constraintsH: translateConstraintH(node.constraints.horizontal),
constraintsV: translateConstraintV(node.constraints.vertical)
};
};
5 changes: 3 additions & 2 deletions plugin-src/transformers/partials/transformVectorPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import {
translateWindingRule
} from '@plugin/translators/vectors';

import { PathAttributes } from '@ui/lib/types/shapes/pathShape';
import { PathShape } from '@ui/lib/types/shapes/pathShape';
import { PathAttributes, PathShape } from '@ui/lib/types/shapes/pathShape';

export const transformVectorPathsAsContent = (
node: StarNode | LineNode | PolygonNode,
Expand Down Expand Up @@ -104,6 +103,8 @@ const transformVectorPath = (
svgAttrs: {
fillRule: translateWindingRule(vectorPath.windingRule)
},
constraintsH: 'scale',
constraintsV: 'scale',
...transformStrokesFromVector(node, normalizedPaths, vectorRegion),
...transformEffects(node),
...transformDimensionAndPositionFromVectorPath(vectorPath, baseX, baseY),
Expand Down
4 changes: 3 additions & 1 deletion plugin-src/transformers/transformComponentNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { componentsLibrary } from '@plugin/ComponentLibrary';
import {
transformBlend,
transformChildren,
transformConstraints,
transformCornerRadius,
transformDimensionAndPosition,
transformEffects,
Expand Down Expand Up @@ -33,7 +34,8 @@ export const transformComponentNode = async (
...transformProportion(node),
...transformCornerRadius(node),
...(await transformChildren(node, baseX + node.x, baseY + node.y)),
...transformDimensionAndPosition(node, baseX, baseY)
...transformDimensionAndPosition(node, baseX, baseY),
...transformConstraints(node)
});

return {
Expand Down
4 changes: 3 additions & 1 deletion plugin-src/transformers/transformEllipseNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
transformBlend,
transformConstraints,
transformDimension,
transformEffects,
transformFigmaIds,
Expand Down Expand Up @@ -28,6 +29,7 @@ export const transformEllipseNode = (
...transformRotationAndPosition(node, baseX, baseY),
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node)
...transformProportion(node),
...transformConstraints(node)
};
};
4 changes: 3 additions & 1 deletion plugin-src/transformers/transformFrameNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
transformBlend,
transformChildren,
transformConstraints,
transformCornerRadius,
transformDimensionAndPosition,
transformEffects,
Expand Down Expand Up @@ -34,7 +35,8 @@ export const transformFrameNode = async (
...transformBlend(node),
...transformProportion(node),
...transformCornerRadius(node),
...transformEffects(node)
...transformEffects(node),
...transformConstraints(node)
};
}

Expand Down
2 changes: 2 additions & 0 deletions plugin-src/transformers/transformInstanceNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { remoteComponentLibrary } from '@plugin/RemoteComponentLibrary';
import {
transformBlend,
transformChildren,
transformConstraints,
transformCornerRadius,
transformDimensionAndPosition,
transformEffects,
Expand Down Expand Up @@ -44,6 +45,7 @@ export const transformInstanceNode = async (
...transformProportion(node),
...transformCornerRadius(node),
...transformDimensionAndPosition(node, baseX, baseY),
...transformConstraints(node),
...(await transformChildren(node, baseX + node.x, baseY + node.y))
};
};
Expand Down
4 changes: 3 additions & 1 deletion plugin-src/transformers/transformPathNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
transformBlend,
transformConstraints,
transformDimensionAndPosition,
transformEffects,
transformFigmaIds,
Expand Down Expand Up @@ -32,6 +33,7 @@ export const transformPathNode = (
...transformDimensionAndPosition(node, baseX, baseY),
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node)
...transformProportion(node),
...transformConstraints(node)
};
};
4 changes: 3 additions & 1 deletion plugin-src/transformers/transformRectangleNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
transformBlend,
transformConstraints,
transformCornerRadius,
transformDimension,
transformEffects,
Expand Down Expand Up @@ -30,6 +31,7 @@ export const transformRectangleNode = (
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node),
...transformCornerRadius(node)
...transformCornerRadius(node),
...transformConstraints(node)
};
};
4 changes: 3 additions & 1 deletion plugin-src/transformers/transformTextNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
transformBlend,
transformConstraints,
transformDimensionAndPosition,
transformEffects,
transformFigmaIds,
Expand All @@ -22,6 +23,7 @@ export const transformTextNode = (node: TextNode, baseX: number, baseY: number):
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node),
...transformStrokes(node)
...transformStrokes(node),
...transformConstraints(node)
};
};
10 changes: 8 additions & 2 deletions plugin-src/transformers/transformVectorNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { transformFigmaIds, transformVectorPaths } from '@plugin/transformers/partials';
import {
transformConstraints,
transformFigmaIds,
transformVectorPaths
} from '@plugin/transformers/partials';

import { GroupShape } from '@ui/lib/types/shapes/groupShape';
import { PathShape } from '@ui/lib/types/shapes/pathShape';
Expand All @@ -22,13 +26,15 @@ export const transformVectorNode = (
return {
...children[0],
name: node.name,
...transformFigmaIds(node)
...transformFigmaIds(node),
...transformConstraints(node)
};
}

return {
...transformGroupNodeLike(node, baseX, baseY),
...transformFigmaIds(node),
...transformConstraints(node),
children
};
};
1 change: 1 addition & 0 deletions plugin-src/translators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from './translateBlendMode';
export * from './translateBlurEffects';
export * from './translateBoolType';
export * from './translateChildren';
export * from './translateConstraints';
export * from './translateShadowEffects';
export * from './translateStrokes';
31 changes: 31 additions & 0 deletions plugin-src/translators/translateConstraints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ConstraintH, ConstraintV } from '@ui/lib/types/shapes/shape';

export const translateConstraintH = (constraint: ConstraintType): ConstraintH => {
switch (constraint) {
case 'MAX':
return 'right';
case 'MIN':
return 'left';
case 'CENTER':
return 'center';
case 'SCALE':
return 'scale';
case 'STRETCH':
return 'leftright';
}
};

export const translateConstraintV = (constraint: ConstraintType): ConstraintV => {
switch (constraint) {
case 'MAX':
return 'bottom';
case 'MIN':
return 'top';
case 'CENTER':
return 'center';
case 'SCALE':
return 'scale';
case 'STRETCH':
return 'topbottom';
}
};
7 changes: 5 additions & 2 deletions ui-src/lib/types/shapes/shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export type ShapeAttributes = {
hideFillOnExport?: boolean;
proportion?: number;
proportionLock?: boolean;
constraintsH?: 'left' | 'right' | 'leftright' | 'center' | 'scale';
constraintsV?: 'top' | 'bottom' | 'topbottom' | 'center' | 'scale';
constraintsH?: ConstraintH;
constraintsV?: ConstraintV;
fixedScroll?: boolean;
rx?: number;
ry?: number;
Expand All @@ -84,3 +84,6 @@ export type ShapeGeomAttributes = {
};

export type GrowType = 'auto-width' | 'auto-height' | 'fixed';

export type ConstraintH = 'left' | 'right' | 'leftright' | 'center' | 'scale';
export type ConstraintV = 'top' | 'bottom' | 'topbottom' | 'center' | 'scale';

0 comments on commit 02fa336

Please sign in to comment.