Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement rotation for remaining figures #163

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions plugin-src/transformers/partials/transformChildren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ const nodeActsAsMask = (node: SceneNode): boolean => {
export const transformChildren = async (
node: ChildrenMixin,
baseX: number = 0,
baseY: number = 0
baseY: number = 0,
baseRotation: number = 0
): Promise<Children> => {
const maskIndex = node.children.findIndex(nodeActsAsMask);
const containsMask = maskIndex !== -1;

return {
children: containsMask
? await translateMaskChildren(node.children, maskIndex, baseX, baseY)
: await translateChildren(node.children, baseX, baseY)
? await translateMaskChildren(node.children, maskIndex, baseX, baseY, baseRotation)
: await translateChildren(node.children, baseX, baseY, baseRotation)
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { ShapeBaseAttributes, ShapeGeomAttributes } from '@ui/lib/types/shapes/s
export const transformRotationAndPosition = (
node: LayoutMixin,
baseX: number,
baseY: number
baseY: number,
baseRotation: number
): Pick<ShapeBaseAttributes, 'transform' | 'transformInverse' | 'rotation'> &
Pick<ShapeGeomAttributes, 'x' | 'y'> => {
const rotation = node.rotation;
const rotation = node.rotation + baseRotation;
const x = node.x + baseX;
const y = node.y + baseY;

Expand All @@ -28,6 +29,8 @@ export const transformRotationAndPosition = (
node.absoluteBoundingBox
);

console.log(node.id, node.name, node.absoluteTransform);

return {
...referencePoint,
rotation: -rotation < 0 ? -rotation + 360 : -rotation,
Expand Down
5 changes: 3 additions & 2 deletions plugin-src/transformers/transformEllipseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { CircleShape } from '@ui/lib/types/shapes/circleShape';
export const transformEllipseNode = (
node: EllipseNode,
baseX: number,
baseY: number
baseY: number,
baseRotation: number
): CircleShape => {
return {
type: 'circle',
Expand All @@ -27,7 +28,7 @@ export const transformEllipseNode = (
...transformEffects(node),
...transformStrokes(node),
...transformDimension(node),
...transformRotationAndPosition(node, baseX, baseY),
...transformRotationAndPosition(node, baseX, baseY, baseRotation),
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node),
Expand Down
35 changes: 30 additions & 5 deletions plugin-src/transformers/transformFrameNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import {
transformChildren,
transformConstraints,
transformCornerRadius,
transformDimensionAndPosition,
transformDimension,
transformEffects,
transformFigmaIds,
transformFills,
transformLayoutAttributes,
transformProportion,
transformRotationAndPosition,
transformSceneNode,
transformStrokes
} from '@plugin/transformers/partials';

import { FrameShape } from '@ui/lib/types/shapes/frameShape';
import { Point } from '@ui/lib/types/utils/point';

const isSectionNode = (node: FrameNode | SectionNode | ComponentSetNode): node is SectionNode => {
return node.type === 'SECTION';
Expand All @@ -23,11 +25,24 @@ const isSectionNode = (node: FrameNode | SectionNode | ComponentSetNode): node i
export const transformFrameNode = async (
node: FrameNode | SectionNode | ComponentSetNode,
baseX: number,
baseY: number
baseY: number,
baseRotation: number
): Promise<FrameShape> => {
let frameSpecificAttributes: Partial<FrameShape> = {};
let referencePoint: Point = { x: node.x + baseX, y: node.y + baseY };
let rotation = baseRotation;

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

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.
frameSpecificAttributes = {
Expand All @@ -40,19 +55,29 @@ export const transformFrameNode = async (
...transformCornerRadius(node),
...transformEffects(node),
...transformConstraints(node),
...transformAutoLayout(node)
...transformAutoLayout(node),
...transformAndRotation
};
}

console.log(
node.id,
node.name,
{ x: node.x, y: node.y },
{ x: node.absoluteBoundingBox?.x, y: node.absoluteBoundingBox?.y },
referencePoint
);

return {
type: 'frame',
name: node.name,
showContent: isSectionNode(node) ? true : !node.clipsContent,
...referencePoint,
...transformFigmaIds(node),
...transformFills(node),
...frameSpecificAttributes,
...(await transformChildren(node, baseX + node.x, baseY + node.y)),
...transformDimensionAndPosition(node, baseX, baseY),
...transformDimension(node),
...(await transformChildren(node, referencePoint.x, referencePoint.y, rotation)),
...transformSceneNode(node)
};
};
9 changes: 7 additions & 2 deletions plugin-src/transformers/transformLineNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ import { PathShape } 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, baseX: number, baseY: number): PathShape => {
export const transformLineNode = (
node: LineNode,
baseX: number,
baseY: number,
baseRotation: number
): PathShape => {
return {
type: 'path',
name: node.name,
content: translateLineNode(node, baseX, baseY),
content: translateLineNode(node, baseX, baseY, baseRotation),
...transformFigmaIds(node),
...transformStrokes(node),
...transformEffects(node),
Expand Down
5 changes: 3 additions & 2 deletions plugin-src/transformers/transformRectangleNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { RectShape } from '@ui/lib/types/shapes/rectShape';
export const transformRectangleNode = (
node: RectangleNode,
baseX: number,
baseY: number
baseY: number,
baseRotation: number
): RectShape => {
return {
type: 'rect',
Expand All @@ -28,7 +29,7 @@ export const transformRectangleNode = (
...transformEffects(node),
...transformStrokes(node),
...transformDimension(node),
...transformRotationAndPosition(node, baseX, baseY),
...transformRotationAndPosition(node, baseX, baseY, baseRotation),
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node),
Expand Down
11 changes: 6 additions & 5 deletions plugin-src/transformers/transformSceneNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
export const transformSceneNode = async (
node: SceneNode,
baseX: number = 0,
baseY: number = 0
baseY: number = 0,
baseRotation: number = 0
): Promise<PenpotNode | undefined> => {
let penpotNode: PenpotNode | undefined;

Expand All @@ -28,15 +29,15 @@ export const transformSceneNode = async (

switch (node.type) {
case 'RECTANGLE':
penpotNode = transformRectangleNode(node, baseX, baseY);
penpotNode = transformRectangleNode(node, baseX, baseY, baseRotation);
break;
case 'ELLIPSE':
penpotNode = transformEllipseNode(node, baseX, baseY);
penpotNode = transformEllipseNode(node, baseX, baseY, baseRotation);
break;
case 'SECTION':
case 'FRAME':
case 'COMPONENT_SET':
penpotNode = await transformFrameNode(node, baseX, baseY);
penpotNode = await transformFrameNode(node, baseX, baseY, baseRotation);
break;
case 'GROUP':
penpotNode = await transformGroupNode(node, baseX, baseY);
Expand All @@ -48,7 +49,7 @@ export const transformSceneNode = async (
penpotNode = transformVectorNode(node, baseX, baseY);
break;
case 'LINE':
penpotNode = transformLineNode(node, baseX, baseY);
penpotNode = transformLineNode(node, baseX, baseY, baseRotation);
break;
case 'STAR':
case 'POLYGON':
Expand Down
22 changes: 17 additions & 5 deletions plugin-src/translators/translateChildren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ export const translateMaskChildren = async (
children: readonly SceneNode[],
maskIndex: number,
baseX: number,
baseY: number
baseY: number,
baseRotation: number
): Promise<PenpotNode[]> => {
const maskChild = children[maskIndex];
const unmaskedChildren = await translateChildren(children.slice(0, maskIndex), baseX, baseY);
const maskedChildren = await translateChildren(children.slice(maskIndex), baseX, baseY);
const unmaskedChildren = await translateChildren(
children.slice(0, maskIndex),
baseX,
baseY,
baseRotation
);
const maskedChildren = await translateChildren(
children.slice(maskIndex),
baseX,
baseY,
baseRotation
);

const maskGroup = {
...transformMaskFigmaIds(maskChild),
Expand All @@ -37,12 +48,13 @@ export const translateMaskChildren = async (
export const translateChildren = async (
children: readonly SceneNode[],
baseX: number = 0,
baseY: number = 0
baseY: number = 0,
baseRotation: number = 0
): Promise<PenpotNode[]> => {
const transformedChildren: PenpotNode[] = [];

for (const child of children) {
const penpotNode = await transformSceneNode(child, baseX, baseY);
const penpotNode = await transformSceneNode(child, baseX, baseY, baseRotation);

if (penpotNode) transformedChildren.push(penpotNode);

Expand Down
29 changes: 18 additions & 11 deletions plugin-src/translators/vectors/translateLineNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ import { Segment } from '@ui/lib/types/shapes/pathShape';

import { translateCommandsToSegments } from '.';

export const translateLineNode = (node: LineNode, baseX: number, baseY: number): Segment[] => {
if (!hasRotation(node.rotation) || !node.absoluteBoundingBox) {
export const translateLineNode = (
node: LineNode,
baseX: number,
baseY: number,
baseRotation: number
): Segment[] => {
const rotation = node.rotation + baseRotation;

if (!hasRotation(rotation) || !node.absoluteBoundingBox) {
return translateCommandsToSegments(
[
{
Expand All @@ -28,14 +35,20 @@ export const translateLineNode = (node: LineNode, baseX: number, baseY: number):
);
}

const referencePoint = applyInverseRotation(
{ x: node.x, y: node.y },
node.absoluteTransform,
node.absoluteBoundingBox
);

const startPoint = applyRotation(
{ x: 0, y: 0 },
{ x: referencePoint.x, y: referencePoint.y },
node.absoluteTransform,
node.absoluteBoundingBox
);

const endPoint = applyRotation(
{ x: node.width, y: 0 },
{ x: referencePoint.x + node.width, y: referencePoint.y },
node.absoluteTransform,
node.absoluteBoundingBox
);
Expand All @@ -55,11 +68,5 @@ export const translateLineNode = (node: LineNode, baseX: number, baseY: number):
}
];

const referencePoint = applyInverseRotation(
{ x: node.x, y: node.y },
node.absoluteTransform,
node.absoluteBoundingBox
);

return translateCommandsToSegments(commands, baseX + referencePoint.x, baseY + referencePoint.y);
return translateCommandsToSegments(commands, baseX, baseY);
};
Loading