Skip to content

Commit

Permalink
remove base rotation and calculate it instead (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 authored Jun 19, 2024
1 parent 8eb2b1a commit 4e5d01a
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 139 deletions.
9 changes: 3 additions & 6 deletions plugin-src/transformers/partials/transformChildren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ const nodeActsAsMask = (node: SceneNode): boolean => {
return 'isMask' in node && node.isMask;
};

export const transformChildren = async (
node: ChildrenMixin,
baseRotation: number = 0
): Promise<Children> => {
export const transformChildren = async (node: ChildrenMixin): Promise<Children> => {
const maskIndex = node.children.findIndex(nodeActsAsMask);
const containsMask = maskIndex !== -1;

return {
children: containsMask
? await translateMaskChildren(node.children, maskIndex, baseRotation)
: await translateChildren(node.children, baseRotation)
? await translateMaskChildren(node.children, maskIndex)
: await translateChildren(node.children)
};
};
12 changes: 5 additions & 7 deletions plugin-src/transformers/partials/transformRotationAndPosition.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { translateRotation, translateZeroRotation } from '@plugin/translators';
import { applyInverseRotation, hasRotation } from '@plugin/utils';
import { applyInverseRotation, getRotation, hasRotation } from '@plugin/utils';

import { ShapeBaseAttributes, ShapeGeomAttributes } from '@ui/lib/types/shapes/shape';

export const transformRotation = (
node: LayoutMixin,
baseRotation: number
node: LayoutMixin
): Pick<ShapeBaseAttributes, 'transform' | 'transformInverse' | 'rotation'> => {
const rotation = node.rotation + baseRotation;
const rotation = getRotation(node.absoluteTransform);

if (!hasRotation(rotation)) {
return translateZeroRotation();
Expand All @@ -17,13 +16,12 @@ export const transformRotation = (
};

export const transformRotationAndPosition = (
node: LayoutMixin,
baseRotation: number
node: LayoutMixin
): Pick<ShapeBaseAttributes, 'transform' | 'transformInverse' | 'rotation'> &
Pick<ShapeGeomAttributes, 'x' | 'y'> => {
const rotation = node.rotation + baseRotation;
const x = node.absoluteTransform[0][2];
const y = node.absoluteTransform[1][2];
const rotation = getRotation(node.absoluteTransform);

if (!hasRotation(rotation) || !node.absoluteBoundingBox) {
return {
Expand Down
11 changes: 5 additions & 6 deletions plugin-src/transformers/partials/transformVectorPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { translateCommands, translateWindingRule } from '@plugin/translators/vec

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

export const transformVectorPaths = (node: VectorNode, baseRotation: number): PathShape[] => {
export const transformVectorPaths = (node: VectorNode): PathShape[] => {
const pathShapes = node.vectorPaths
.filter((vectorPath, index) => {
return (
Expand All @@ -22,7 +22,7 @@ export const transformVectorPaths = (node: VectorNode, baseRotation: number): Pa
);
})
.map((vectorPath, index) =>
transformVectorPath(node, vectorPath, (node.vectorNetwork.regions ?? [])[index], baseRotation)
transformVectorPath(node, vectorPath, (node.vectorNetwork.regions ?? [])[index])
);

const geometryShapes = node.fillGeometry
Expand All @@ -32,7 +32,7 @@ export const transformVectorPaths = (node: VectorNode, baseRotation: number): Pa
vectorPath => normalizePath(vectorPath.data) === normalizePath(geometry.data)
)
)
.map(geometry => transformVectorPath(node, geometry, undefined, baseRotation));
.map(geometry => transformVectorPath(node, geometry, undefined));

return [...geometryShapes, ...pathShapes];
};
Expand All @@ -58,15 +58,14 @@ const nodeHasFills = (
const transformVectorPath = (
node: VectorNode,
vectorPath: VectorPath,
vectorRegion: VectorRegion | undefined,
baseRotation: number
vectorRegion: VectorRegion | undefined
): PathShape => {
const normalizedPaths = parseSVG(vectorPath.data);

return {
type: 'path',
name: 'svg-path',
content: translateCommands(node, normalizedPaths, baseRotation),
content: translateCommands(node, normalizedPaths),
fills:
vectorPath.windingRule === 'NONE' ? [] : translateFills(vectorRegion?.fills ?? node.fills),
svgAttrs: {
Expand Down
9 changes: 3 additions & 6 deletions plugin-src/transformers/transformBooleanNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ import { translateBoolType } from '@plugin/translators';

import { BoolShape } from '@ui/lib/types/shapes/boolShape';

export const transformBooleanNode = async (
node: BooleanOperationNode,
baseRotation: number
): Promise<BoolShape> => {
export const transformBooleanNode = async (node: BooleanOperationNode): Promise<BoolShape> => {
return {
type: 'bool',
name: node.name,
boolType: translateBoolType(node.booleanOperation),
...transformFigmaIds(node),
...(await transformChildren(node, baseRotation)),
...(await transformChildren(node)),
...transformFills(node),
...transformEffects(node),
...transformStrokes(node),
...transformDimension(node),
...transformRotationAndPosition(node, baseRotation),
...transformRotationAndPosition(node),
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node),
Expand Down
9 changes: 3 additions & 6 deletions plugin-src/transformers/transformComponentNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ import {

import { ComponentRoot } from '@ui/types';

export const transformComponentNode = async (
node: ComponentNode,
baseRotation: number
): Promise<ComponentRoot> => {
export const transformComponentNode = async (node: ComponentNode): Promise<ComponentRoot> => {
componentsLibrary.register(node.id, {
type: 'component',
name: node.name,
Expand All @@ -36,9 +33,9 @@ export const transformComponentNode = async (
...transformProportion(node),
...transformLayoutAttributes(node, true),
...transformCornerRadius(node),
...(await transformChildren(node, node.rotation + baseRotation)),
...(await transformChildren(node)),
...transformDimension(node),
...transformRotationAndPosition(node, baseRotation),
...transformRotationAndPosition(node),
...transformConstraints(node),
...transformAutoLayout(node)
});
Expand Down
4 changes: 2 additions & 2 deletions plugin-src/transformers/transformEllipseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import { CircleShape } from '@ui/lib/types/shapes/circleShape';

export const transformEllipseNode = (node: EllipseNode, baseRotation: number): CircleShape => {
export const transformEllipseNode = (node: EllipseNode): CircleShape => {
return {
type: 'circle',
name: node.name,
Expand All @@ -24,7 +24,7 @@ export const transformEllipseNode = (node: EllipseNode, baseRotation: number): C
...transformEffects(node),
...transformStrokes(node),
...transformDimension(node),
...transformRotationAndPosition(node, baseRotation),
...transformRotationAndPosition(node),
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node),
Expand Down
9 changes: 3 additions & 6 deletions plugin-src/transformers/transformFrameNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@ const isSectionNode = (node: FrameNode | SectionNode | ComponentSetNode): node i
};

export const transformFrameNode = async (
node: FrameNode | SectionNode | ComponentSetNode,
baseRotation: number
node: FrameNode | SectionNode | ComponentSetNode
): Promise<FrameShape> => {
let frameSpecificAttributes: Partial<FrameShape> = {};
let referencePoint: Point = { x: node.absoluteTransform[0][2], y: node.absoluteTransform[1][2] };
let rotation = baseRotation;

if (!isSectionNode(node)) {
const { x, y, ...transformAndRotation } = transformRotationAndPosition(node, baseRotation);
const { x, y, ...transformAndRotation } = transformRotationAndPosition(node);

referencePoint = { x, y };
rotation += node.rotation;

// Figma API does not expose strokes, blend modes, corner radius, or constraint proportions for sections,
// they plan to add it in the future. Refactor this when available.
Expand Down Expand Up @@ -63,7 +60,7 @@ export const transformFrameNode = async (
...referencePoint,
...frameSpecificAttributes,
...transformDimension(node),
...(await transformChildren(node, rotation)),
...(await transformChildren(node)),
...transformSceneNode(node),
...transformOverrides(node)
};
Expand Down
14 changes: 5 additions & 9 deletions plugin-src/transformers/transformGroupNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,25 @@ import { transformChildren } from '@plugin/transformers/partials';

import { GroupShape } from '@ui/lib/types/shapes/groupShape';

export const transformGroupNode = async (
node: GroupNode,
baseRotation: number
): Promise<GroupShape> => {
export const transformGroupNode = async (node: GroupNode): Promise<GroupShape> => {
return {
...transformFigmaIds(node),
...transformGroupNodeLike(node, baseRotation),
...transformGroupNodeLike(node),
...transformEffects(node),
...transformBlend(node),
...(await transformChildren(node, baseRotation)),
...(await transformChildren(node)),
...transformOverrides(node)
};
};

export const transformGroupNodeLike = (
node: BaseNodeMixin & LayoutMixin & SceneNodeMixin,
baseRotation: number
node: BaseNodeMixin & LayoutMixin & SceneNodeMixin
): GroupShape => {
return {
type: 'group',
name: node.name,
...transformDimension(node),
...transformRotationAndPosition(node, baseRotation),
...transformRotationAndPosition(node),
...transformSceneNode(node)
};
};
7 changes: 3 additions & 4 deletions plugin-src/transformers/transformInstanceNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import {
import { ComponentInstance, ComponentTextPropertyOverride } from '@ui/types';

export const transformInstanceNode = async (
node: InstanceNode,
baseRotation: number
node: InstanceNode
): Promise<ComponentInstance | undefined> => {
const mainComponent = await node.getMainComponentAsync();
if (mainComponent === null) {
Expand Down Expand Up @@ -62,10 +61,10 @@ export const transformInstanceNode = async (
...transformLayoutAttributes(node, true),
...transformCornerRadius(node),
...transformDimension(node),
...transformRotationAndPosition(node, baseRotation),
...transformRotationAndPosition(node),
...transformConstraints(node),
...transformAutoLayout(node),
...(await transformChildren(node, node.rotation + baseRotation)),
...(await transformChildren(node)),
...transformOverrides(node)
};
};
Expand Down
38 changes: 17 additions & 21 deletions plugin-src/transformers/transformLineNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { Segment } from '@ui/lib/types/shapes/pathShape';
*
* To represent the line rotated we do take into account the rotation of the line, but only in its content.
*/
export const transformLineNode = (node: LineNode, baseRotation: number): PathShape => {
export const transformLineNode = (node: LineNode): PathShape => {
return {
type: 'path',
name: node.name,
content: translateLineNode(node, baseRotation),
content: translateLineNode(node),
...transformFigmaIds(node),
...transformStrokes(node),
...transformEffects(node),
Expand All @@ -37,23 +37,19 @@ export const transformLineNode = (node: LineNode, baseRotation: number): PathSha
};
};

const translateLineNode = (node: LineNode, baseRotation: number): Segment[] => {
return translateCommands(
node,
[
{
x: 0,
y: 0,
command: 'moveto',
code: 'M'
},
{
x: node.width,
y: 0,
command: 'lineto',
code: 'L'
}
],
baseRotation
);
const translateLineNode = (node: LineNode): Segment[] => {
return translateCommands(node, [
{
x: 0,
y: 0,
command: 'moveto',
code: 'M'
},
{
x: node.width,
y: 0,
command: 'lineto',
code: 'L'
}
]);
};
13 changes: 5 additions & 8 deletions plugin-src/transformers/transformPathNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,24 @@ import { translateCommands } from '@plugin/translators/vectors';

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

export const transformPathNode = (
node: StarNode | PolygonNode,
baseRotation: number
): PathShape => {
export const transformPathNode = (node: StarNode | PolygonNode): PathShape => {
return {
type: 'path',
name: node.name,
content: translatePathNode(node, baseRotation),
content: translatePathNode(node),
...transformFigmaIds(node),
...transformFills(node),
...transformStrokes(node),
...transformEffects(node),
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node),
...transformRotation(node, baseRotation),
...transformRotation(node),
...transformLayoutAttributes(node),
...transformConstraints(node),
...transformOverrides(node)
};
};

const translatePathNode = (node: StarNode | PolygonNode, baseRotation: number): Segment[] =>
translateCommands(node, parseSVG(node.fillGeometry[0].data), baseRotation);
const translatePathNode = (node: StarNode | PolygonNode): Segment[] =>
translateCommands(node, parseSVG(node.fillGeometry[0].data));
4 changes: 2 additions & 2 deletions plugin-src/transformers/transformRectangleNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {

import { RectShape } from '@ui/lib/types/shapes/rectShape';

export const transformRectangleNode = (node: RectangleNode, baseRotation: number): RectShape => {
export const transformRectangleNode = (node: RectangleNode): RectShape => {
return {
type: 'rect',
name: node.name,
Expand All @@ -25,7 +25,7 @@ export const transformRectangleNode = (node: RectangleNode, baseRotation: number
...transformEffects(node),
...transformStrokes(node),
...transformDimension(node),
...transformRotationAndPosition(node, baseRotation),
...transformRotationAndPosition(node),
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node),
Expand Down
Loading

0 comments on commit 4e5d01a

Please sign in to comment.