diff --git a/plugin-src/transformers/partials/transformChildren.ts b/plugin-src/transformers/partials/transformChildren.ts index 5de12951..05b98069 100644 --- a/plugin-src/transformers/partials/transformChildren.ts +++ b/plugin-src/transformers/partials/transformChildren.ts @@ -8,8 +8,6 @@ const nodeActsAsMask = (node: SceneNode): boolean => { export const transformChildren = async ( node: ChildrenMixin, - baseX: number = 0, - baseY: number = 0, baseRotation: number = 0 ): Promise => { const maskIndex = node.children.findIndex(nodeActsAsMask); @@ -17,7 +15,7 @@ export const transformChildren = async ( return { children: containsMask - ? await translateMaskChildren(node.children, maskIndex, baseX, baseY, baseRotation) - : await translateChildren(node.children, baseX, baseY, baseRotation) + ? await translateMaskChildren(node.children, maskIndex, baseRotation) + : await translateChildren(node.children, baseRotation) }; }; diff --git a/plugin-src/transformers/partials/transformDimensionAndPosition.ts b/plugin-src/transformers/partials/transformDimensionAndPosition.ts index c7fd3e75..529e78be 100644 --- a/plugin-src/transformers/partials/transformDimensionAndPosition.ts +++ b/plugin-src/transformers/partials/transformDimensionAndPosition.ts @@ -1,5 +1,3 @@ -import { getBoundingBox } from '@plugin/utils'; - import { ShapeGeomAttributes } from '@ui/lib/types/shapes/shape'; export const transformDimension = ( @@ -11,41 +9,13 @@ export const transformDimension = ( }; }; -export const transformPosition = ( - node: DimensionAndPositionMixin, - baseX: number, - baseY: number -): Pick => { - return { - x: node.x + baseX, - y: node.y + baseY - }; -}; - export const transformDimensionAndPosition = ( - node: DimensionAndPositionMixin, - baseX: number, - baseY: number + node: DimensionAndPositionMixin ): ShapeGeomAttributes => { return { - x: node.x + baseX, - y: node.y + baseY, + x: node.absoluteTransform[0][2], + y: node.absoluteTransform[1][2], width: node.width, height: node.height }; }; - -export const transformDimensionAndPositionFromVectorPath = ( - vectorPath: VectorPath, - baseX: number, - baseY: number -): ShapeGeomAttributes => { - const boundingBox = getBoundingBox(vectorPath); - - return { - x: boundingBox.x1 + baseX, - y: boundingBox.y1 + baseY, - width: boundingBox.x2 - boundingBox.x1, - height: boundingBox.y2 - boundingBox.y1 - }; -}; diff --git a/plugin-src/transformers/partials/transformRotationAndPosition.ts b/plugin-src/transformers/partials/transformRotationAndPosition.ts index 456a341e..e934a9f2 100644 --- a/plugin-src/transformers/partials/transformRotationAndPosition.ts +++ b/plugin-src/transformers/partials/transformRotationAndPosition.ts @@ -4,14 +4,12 @@ import { ShapeBaseAttributes, ShapeGeomAttributes } from '@ui/lib/types/shapes/s export const transformRotationAndPosition = ( node: LayoutMixin, - baseX: number, - baseY: number, baseRotation: number ): Pick & Pick => { const rotation = node.rotation + baseRotation; - const x = node.x + baseX; - const y = node.y + baseY; + const x = node.absoluteTransform[0][2]; + const y = node.absoluteTransform[1][2]; if (!hasRotation(rotation) || !node.absoluteBoundingBox) { return { @@ -29,8 +27,6 @@ export const transformRotationAndPosition = ( node.absoluteBoundingBox ); - console.log(node.id, node.name, node.absoluteTransform); - return { ...referencePoint, rotation: -rotation < 0 ? -rotation + 360 : -rotation, diff --git a/plugin-src/transformers/partials/transformVectorPaths.ts b/plugin-src/transformers/partials/transformVectorPaths.ts index 32c2608d..5b156b7b 100644 --- a/plugin-src/transformers/partials/transformVectorPaths.ts +++ b/plugin-src/transformers/partials/transformVectorPaths.ts @@ -2,7 +2,6 @@ import { parseSVG } from 'svg-path-parser'; import { transformBlend, - transformDimensionAndPositionFromVectorPath, transformEffects, transformLayoutAttributes, transformProportion, @@ -14,11 +13,7 @@ import { translateCommandsToSegments, translateWindingRule } from '@plugin/trans import { PathShape } from '@ui/lib/types/shapes/pathShape'; -export const transformVectorPaths = ( - node: VectorNode, - baseX: number, - baseY: number -): PathShape[] => { +export const transformVectorPaths = (node: VectorNode): PathShape[] => { const pathShapes = node.vectorPaths .filter((vectorPath, index) => { return ( @@ -27,7 +22,7 @@ export const transformVectorPaths = ( ); }) .map((vectorPath, index) => - transformVectorPath(node, vectorPath, (node.vectorNetwork.regions ?? [])[index], baseX, baseY) + transformVectorPath(node, vectorPath, (node.vectorNetwork.regions ?? [])[index]) ); const geometryShapes = node.fillGeometry @@ -37,7 +32,7 @@ export const transformVectorPaths = ( vectorPath => normalizePath(vectorPath.data) === normalizePath(geometry.data) ) ) - .map(geometry => transformVectorPath(node, geometry, undefined, baseX, baseY)); + .map(geometry => transformVectorPath(node, geometry, undefined)); return [...geometryShapes, ...pathShapes]; }; @@ -63,16 +58,18 @@ const nodeHasFills = ( const transformVectorPath = ( node: VectorNode, vectorPath: VectorPath, - vectorRegion: VectorRegion | undefined, - baseX: number, - baseY: number + vectorRegion: VectorRegion | undefined ): PathShape => { const normalizedPaths = parseSVG(vectorPath.data); return { type: 'path', name: 'svg-path', - content: translateCommandsToSegments(normalizedPaths, baseX + node.x, baseY + node.y), + content: translateCommandsToSegments( + normalizedPaths, + node.absoluteTransform[0][2], + node.absoluteTransform[1][2] + ), fills: vectorPath.windingRule === 'NONE' ? [] : translateFills(vectorRegion?.fills ?? node.fills), svgAttrs: { @@ -82,7 +79,6 @@ const transformVectorPath = ( constraintsV: 'scale', ...transformStrokesFromVector(node, normalizedPaths, vectorRegion), ...transformEffects(node), - ...transformDimensionAndPositionFromVectorPath(vectorPath, baseX, baseY), ...transformSceneNode(node), ...transformBlend(node), ...transformProportion(node), diff --git a/plugin-src/transformers/transformBooleanNode.ts b/plugin-src/transformers/transformBooleanNode.ts index b85d260a..47bebbe3 100644 --- a/plugin-src/transformers/transformBooleanNode.ts +++ b/plugin-src/transformers/transformBooleanNode.ts @@ -1,12 +1,13 @@ import { transformBlend, transformChildren, - transformDimensionAndPosition, + transformDimension, transformEffects, transformFigmaIds, transformFills, transformLayoutAttributes, transformProportion, + transformRotationAndPosition, transformSceneNode, transformStrokes } from '@plugin/transformers/partials'; @@ -16,19 +17,19 @@ import { BoolShape } from '@ui/lib/types/shapes/boolShape'; export const transformBooleanNode = async ( node: BooleanOperationNode, - baseX: number, - baseY: number + baseRotation: number ): Promise => { return { type: 'bool', name: node.name, boolType: translateBoolType(node.booleanOperation), ...transformFigmaIds(node), - ...(await transformChildren(node, baseX, baseY)), + ...(await transformChildren(node, baseRotation)), ...transformFills(node), ...transformEffects(node), ...transformStrokes(node), - ...transformDimensionAndPosition(node, baseX, baseY), + ...transformDimension(node), + ...transformRotationAndPosition(node, baseRotation), ...transformSceneNode(node), ...transformBlend(node), ...transformProportion(node), diff --git a/plugin-src/transformers/transformComponentNode.ts b/plugin-src/transformers/transformComponentNode.ts index 703cf74a..c19ed123 100644 --- a/plugin-src/transformers/transformComponentNode.ts +++ b/plugin-src/transformers/transformComponentNode.ts @@ -5,12 +5,13 @@ import { transformChildren, transformConstraints, transformCornerRadius, - transformDimensionAndPosition, + transformDimension, transformEffects, transformFigmaIds, transformFills, transformLayoutAttributes, transformProportion, + transformRotationAndPosition, transformSceneNode, transformStrokes } from '@plugin/transformers/partials'; @@ -19,8 +20,7 @@ import { ComponentRoot } from '@ui/types'; export const transformComponentNode = async ( node: ComponentNode, - baseX: number, - baseY: number + baseRotation: number ): Promise => { componentsLibrary.register(node.id, { type: 'component', @@ -36,8 +36,9 @@ export const transformComponentNode = async ( ...transformProportion(node), ...transformLayoutAttributes(node), ...transformCornerRadius(node), - ...(await transformChildren(node, baseX + node.x, baseY + node.y)), - ...transformDimensionAndPosition(node, baseX, baseY), + ...(await transformChildren(node, node.rotation + baseRotation)), + ...transformDimension(node), + ...transformRotationAndPosition(node, baseRotation), ...transformConstraints(node), ...transformAutoLayout(node) }); diff --git a/plugin-src/transformers/transformEllipseNode.ts b/plugin-src/transformers/transformEllipseNode.ts index be02cee6..608b5c83 100644 --- a/plugin-src/transformers/transformEllipseNode.ts +++ b/plugin-src/transformers/transformEllipseNode.ts @@ -14,12 +14,7 @@ import { import { CircleShape } from '@ui/lib/types/shapes/circleShape'; -export const transformEllipseNode = ( - node: EllipseNode, - baseX: number, - baseY: number, - baseRotation: number -): CircleShape => { +export const transformEllipseNode = (node: EllipseNode, baseRotation: number): CircleShape => { return { type: 'circle', name: node.name, @@ -28,7 +23,7 @@ export const transformEllipseNode = ( ...transformEffects(node), ...transformStrokes(node), ...transformDimension(node), - ...transformRotationAndPosition(node, baseX, baseY, baseRotation), + ...transformRotationAndPosition(node, baseRotation), ...transformSceneNode(node), ...transformBlend(node), ...transformProportion(node), diff --git a/plugin-src/transformers/transformFrameNode.ts b/plugin-src/transformers/transformFrameNode.ts index 1a741389..c47e7ccd 100644 --- a/plugin-src/transformers/transformFrameNode.ts +++ b/plugin-src/transformers/transformFrameNode.ts @@ -24,21 +24,14 @@ const isSectionNode = (node: FrameNode | SectionNode | ComponentSetNode): node i export const transformFrameNode = async ( node: FrameNode | SectionNode | ComponentSetNode, - baseX: number, - baseY: number, baseRotation: number ): Promise => { let frameSpecificAttributes: Partial = {}; - let referencePoint: Point = { x: node.x + baseX, y: node.y + baseY }; + 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, - baseX, - baseY, - baseRotation - ); + const { x, y, ...transformAndRotation } = transformRotationAndPosition(node, baseRotation); referencePoint = { x, y }; rotation += node.rotation; @@ -60,24 +53,16 @@ export const transformFrameNode = async ( }; } - 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), + ...referencePoint, ...frameSpecificAttributes, ...transformDimension(node), - ...(await transformChildren(node, referencePoint.x, referencePoint.y, rotation)), + ...(await transformChildren(node, rotation)), ...transformSceneNode(node) }; }; diff --git a/plugin-src/transformers/transformGroupNode.ts b/plugin-src/transformers/transformGroupNode.ts index 2968ccd1..bdb82ead 100644 --- a/plugin-src/transformers/transformGroupNode.ts +++ b/plugin-src/transformers/transformGroupNode.ts @@ -1,8 +1,9 @@ import { transformBlend, - transformDimensionAndPosition, + transformDimension, transformEffects, transformFigmaIds, + transformRotationAndPosition, transformSceneNode } from '@plugin/transformers/partials'; import { transformChildren } from '@plugin/transformers/partials'; @@ -11,27 +12,26 @@ import { GroupShape } from '@ui/lib/types/shapes/groupShape'; export const transformGroupNode = async ( node: GroupNode, - baseX: number, - baseY: number + baseRotation: number ): Promise => { return { ...transformFigmaIds(node), - ...transformGroupNodeLike(node, baseX, baseY), + ...transformGroupNodeLike(node, baseRotation), ...transformEffects(node), ...transformBlend(node), - ...(await transformChildren(node, baseX, baseY)) + ...(await transformChildren(node, baseRotation)) }; }; export const transformGroupNodeLike = ( - node: BaseNodeMixin & DimensionAndPositionMixin & SceneNodeMixin, - baseX: number, - baseY: number + node: BaseNodeMixin & LayoutMixin & SceneNodeMixin, + baseRotation: number ): GroupShape => { return { type: 'group', name: node.name, - ...transformDimensionAndPosition(node, baseX, baseY), + ...transformDimension(node), + ...transformRotationAndPosition(node, baseRotation), ...transformSceneNode(node) }; }; diff --git a/plugin-src/transformers/transformInstanceNode.ts b/plugin-src/transformers/transformInstanceNode.ts index 5ec49363..a6104475 100644 --- a/plugin-src/transformers/transformInstanceNode.ts +++ b/plugin-src/transformers/transformInstanceNode.ts @@ -5,12 +5,13 @@ import { transformChildren, transformConstraints, transformCornerRadius, - transformDimensionAndPosition, + transformDimension, transformEffects, transformFigmaIds, transformFills, transformLayoutAttributes, transformProportion, + transformRotationAndPosition, transformSceneNode, transformStrokes } from '@plugin/transformers/partials'; @@ -19,8 +20,7 @@ import { ComponentInstance } from '@ui/types'; export const transformInstanceNode = async ( node: InstanceNode, - baseX: number, - baseY: number + baseRotation: number ): Promise => { const mainComponent = await node.getMainComponentAsync(); @@ -47,10 +47,11 @@ export const transformInstanceNode = async ( ...transformProportion(node), ...transformLayoutAttributes(node), ...transformCornerRadius(node), - ...transformDimensionAndPosition(node, baseX, baseY), + ...transformDimension(node), + ...transformRotationAndPosition(node, baseRotation), ...transformConstraints(node), ...transformAutoLayout(node), - ...(await transformChildren(node, baseX + node.x, baseY + node.y)) + ...(await transformChildren(node, node.rotation + baseRotation)) }; }; diff --git a/plugin-src/transformers/transformLineNode.ts b/plugin-src/transformers/transformLineNode.ts index c87042b0..aa8b6532 100644 --- a/plugin-src/transformers/transformLineNode.ts +++ b/plugin-src/transformers/transformLineNode.ts @@ -4,7 +4,6 @@ import { transformEffects, transformFigmaIds, transformLayoutAttributes, - transformPosition, transformProportion, transformSceneNode, transformStrokes @@ -19,20 +18,14 @@ 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, - baseRotation: number -): PathShape => { +export const transformLineNode = (node: LineNode, baseRotation: number): PathShape => { return { type: 'path', name: node.name, - content: translateLineNode(node, baseX, baseY, baseRotation), + content: translateLineNode(node, baseRotation), ...transformFigmaIds(node), ...transformStrokes(node), ...transformEffects(node), - ...transformPosition(node, baseX, baseY), ...transformSceneNode(node), ...transformBlend(node), ...transformProportion(node), diff --git a/plugin-src/transformers/transformPathNode.ts b/plugin-src/transformers/transformPathNode.ts index 1c2e3879..f47586ab 100644 --- a/plugin-src/transformers/transformPathNode.ts +++ b/plugin-src/transformers/transformPathNode.ts @@ -1,7 +1,6 @@ import { transformBlend, transformConstraints, - transformDimensionAndPosition, transformEffects, transformFigmaIds, transformFills, @@ -14,20 +13,19 @@ import { translateVectorPaths } from '@plugin/translators/vectors'; import { PathShape } from '@ui/lib/types/shapes/pathShape'; -export const transformPathNode = ( - node: StarNode | PolygonNode, - baseX: number, - baseY: number -): PathShape => { +export const transformPathNode = (node: StarNode | PolygonNode): PathShape => { return { type: 'path', name: node.name, - content: translateVectorPaths(node.fillGeometry, baseX + node.x, baseY + node.y), + content: translateVectorPaths( + node.fillGeometry, + node.absoluteTransform[0][2], + node.absoluteTransform[1][2] + ), ...transformFigmaIds(node), ...transformFills(node), ...transformStrokes(node), ...transformEffects(node), - ...transformDimensionAndPosition(node, baseX, baseY), ...transformSceneNode(node), ...transformBlend(node), ...transformProportion(node), diff --git a/plugin-src/transformers/transformRectangleNode.ts b/plugin-src/transformers/transformRectangleNode.ts index 1d7c0e58..74a4f88a 100644 --- a/plugin-src/transformers/transformRectangleNode.ts +++ b/plugin-src/transformers/transformRectangleNode.ts @@ -15,12 +15,7 @@ import { import { RectShape } from '@ui/lib/types/shapes/rectShape'; -export const transformRectangleNode = ( - node: RectangleNode, - baseX: number, - baseY: number, - baseRotation: number -): RectShape => { +export const transformRectangleNode = (node: RectangleNode, baseRotation: number): RectShape => { return { type: 'rect', name: node.name, @@ -29,7 +24,7 @@ export const transformRectangleNode = ( ...transformEffects(node), ...transformStrokes(node), ...transformDimension(node), - ...transformRotationAndPosition(node, baseX, baseY, baseRotation), + ...transformRotationAndPosition(node, baseRotation), ...transformSceneNode(node), ...transformBlend(node), ...transformProportion(node), diff --git a/plugin-src/transformers/transformSceneNode.ts b/plugin-src/transformers/transformSceneNode.ts index 16c82783..68856d89 100644 --- a/plugin-src/transformers/transformSceneNode.ts +++ b/plugin-src/transformers/transformSceneNode.ts @@ -16,8 +16,6 @@ import { export const transformSceneNode = async ( node: SceneNode, - baseX: number = 0, - baseY: number = 0, baseRotation: number = 0 ): Promise => { let penpotNode: PenpotNode | undefined; @@ -29,40 +27,40 @@ export const transformSceneNode = async ( switch (node.type) { case 'RECTANGLE': - penpotNode = transformRectangleNode(node, baseX, baseY, baseRotation); + penpotNode = transformRectangleNode(node, baseRotation); break; case 'ELLIPSE': - penpotNode = transformEllipseNode(node, baseX, baseY, baseRotation); + penpotNode = transformEllipseNode(node, baseRotation); break; case 'SECTION': case 'FRAME': case 'COMPONENT_SET': - penpotNode = await transformFrameNode(node, baseX, baseY, baseRotation); + penpotNode = await transformFrameNode(node, baseRotation); break; case 'GROUP': - penpotNode = await transformGroupNode(node, baseX, baseY); + penpotNode = await transformGroupNode(node, baseRotation); break; case 'TEXT': - penpotNode = transformTextNode(node, baseX, baseY); + penpotNode = transformTextNode(node); break; case 'VECTOR': - penpotNode = transformVectorNode(node, baseX, baseY); + penpotNode = transformVectorNode(node, baseRotation); break; case 'LINE': - penpotNode = transformLineNode(node, baseX, baseY, baseRotation); + penpotNode = transformLineNode(node, baseRotation); break; case 'STAR': case 'POLYGON': - penpotNode = transformPathNode(node, baseX, baseY); + penpotNode = transformPathNode(node); break; case 'BOOLEAN_OPERATION': - penpotNode = await transformBooleanNode(node, baseX, baseY); + penpotNode = await transformBooleanNode(node, baseRotation); break; case 'COMPONENT': - penpotNode = await transformComponentNode(node, baseX, baseY); + penpotNode = await transformComponentNode(node, baseRotation); break; case 'INSTANCE': - penpotNode = await transformInstanceNode(node, baseX, baseY); + penpotNode = await transformInstanceNode(node, baseRotation); break; } diff --git a/plugin-src/transformers/transformTextNode.ts b/plugin-src/transformers/transformTextNode.ts index 49e65f85..1e59a03f 100644 --- a/plugin-src/transformers/transformTextNode.ts +++ b/plugin-src/transformers/transformTextNode.ts @@ -13,13 +13,13 @@ import { import { TextShape } from '@ui/lib/types/shapes/textShape'; -export const transformTextNode = (node: TextNode, baseX: number, baseY: number): TextShape => { +export const transformTextNode = (node: TextNode): TextShape => { return { type: 'text', name: node.name, ...transformFigmaIds(node), ...transformText(node), - ...transformDimensionAndPosition(node, baseX, baseY), + ...transformDimensionAndPosition(node), ...transformEffects(node), ...transformSceneNode(node), ...transformBlend(node), diff --git a/plugin-src/transformers/transformVectorNode.ts b/plugin-src/transformers/transformVectorNode.ts index 82a2c268..61f839be 100644 --- a/plugin-src/transformers/transformVectorNode.ts +++ b/plugin-src/transformers/transformVectorNode.ts @@ -17,10 +17,9 @@ import { transformGroupNodeLike } from '.'; */ export const transformVectorNode = ( node: VectorNode, - baseX: number, - baseY: number + baseRotation: number ): GroupShape | PathShape => { - const children = transformVectorPaths(node, baseX, baseY); + const children = transformVectorPaths(node); if (children.length === 1) { return { @@ -32,7 +31,7 @@ export const transformVectorNode = ( } return { - ...transformGroupNodeLike(node, baseX, baseY), + ...transformGroupNodeLike(node, baseRotation), ...transformFigmaIds(node), ...transformConstraints(node), children diff --git a/plugin-src/translators/translateChildren.ts b/plugin-src/translators/translateChildren.ts index 31a84ba8..529f2222 100644 --- a/plugin-src/translators/translateChildren.ts +++ b/plugin-src/translators/translateChildren.ts @@ -17,27 +17,31 @@ import { PenpotNode } from '@ui/types'; export const translateMaskChildren = async ( children: readonly SceneNode[], maskIndex: number, - baseX: number, - baseY: number, baseRotation: number ): Promise => { const maskChild = children[maskIndex]; - const unmaskedChildren = await translateChildren( - children.slice(0, maskIndex), - baseX, - baseY, - baseRotation - ); - const maskedChildren = await translateChildren( - children.slice(maskIndex), - baseX, - baseY, - baseRotation - ); + + const unmaskedChildren = await translateChildren(children.slice(0, maskIndex), baseRotation); + const maskedChildren = await translateChildren(children.slice(maskIndex), baseRotation); + + if ( + maskChild.type === 'STICKY' || + maskChild.type === 'CONNECTOR' || + maskChild.type === 'CODE_BLOCK' || + maskChild.type === 'WIDGET' || + maskChild.type === 'EMBED' || + maskChild.type === 'LINK_UNFURL' || + maskChild.type === 'MEDIA' || + maskChild.type === 'SECTION' || + maskChild.type === 'TABLE' || + maskChild.type === 'SHAPE_WITH_TEXT' + ) { + return [...unmaskedChildren, ...maskedChildren]; + } const maskGroup = { ...transformMaskFigmaIds(maskChild), - ...transformGroupNodeLike(maskChild, baseX, baseY), + ...transformGroupNodeLike(maskChild, baseRotation), children: maskedChildren, maskedGroup: true }; @@ -47,14 +51,12 @@ export const translateMaskChildren = async ( export const translateChildren = async ( children: readonly SceneNode[], - baseX: number = 0, - baseY: number = 0, baseRotation: number = 0 ): Promise => { const transformedChildren: PenpotNode[] = []; for (const child of children) { - const penpotNode = await transformSceneNode(child, baseX, baseY, baseRotation); + const penpotNode = await transformSceneNode(child, baseRotation); if (penpotNode) transformedChildren.push(penpotNode); diff --git a/plugin-src/translators/vectors/translateCommandsToSegments.ts b/plugin-src/translators/vectors/translateCommandsToSegments.ts index 607e15cb..2bcea90d 100644 --- a/plugin-src/translators/vectors/translateCommandsToSegments.ts +++ b/plugin-src/translators/vectors/translateCommandsToSegments.ts @@ -4,8 +4,8 @@ import { Segment } from '@ui/lib/types/shapes/pathShape'; export const translateCommandsToSegments = ( commands: Command[], - baseX: number, - baseY: number + baseX: number = 0, + baseY: number = 0 ): Segment[] => { return commands.map(command => { switch (command.command) { diff --git a/plugin-src/translators/vectors/translateLineNode.ts b/plugin-src/translators/vectors/translateLineNode.ts index 8ea53554..843d9a2d 100644 --- a/plugin-src/translators/vectors/translateLineNode.ts +++ b/plugin-src/translators/vectors/translateLineNode.ts @@ -6,13 +6,10 @@ import { Segment } from '@ui/lib/types/shapes/pathShape'; import { translateCommandsToSegments } from '.'; -export const translateLineNode = ( - node: LineNode, - baseX: number, - baseY: number, - baseRotation: number -): Segment[] => { +export const translateLineNode = (node: LineNode, baseRotation: number): Segment[] => { const rotation = node.rotation + baseRotation; + const x = node.absoluteTransform[0][2]; + const y = node.absoluteTransform[1][2]; if (!hasRotation(rotation) || !node.absoluteBoundingBox) { return translateCommandsToSegments( @@ -30,19 +27,13 @@ export const translateLineNode = ( code: 'L' } ], - baseX + node.x, - baseY + node.y + x, + y ); } const referencePoint = applyInverseRotation( - { x: node.x, y: node.y }, - node.absoluteTransform, - node.absoluteBoundingBox - ); - - const startPoint = applyRotation( - { x: referencePoint.x, y: referencePoint.y }, + { x, y }, node.absoluteTransform, node.absoluteBoundingBox ); @@ -55,8 +46,8 @@ export const translateLineNode = ( const commands: Command[] = [ { - x: startPoint.x, - y: startPoint.y, + x, + y, command: 'moveto', code: 'M' }, @@ -68,5 +59,5 @@ export const translateLineNode = ( } ]; - return translateCommandsToSegments(commands, baseX, baseY); + return translateCommandsToSegments(commands); }; diff --git a/plugin-src/utils/getBoundingBox.ts b/plugin-src/utils/getBoundingBox.ts deleted file mode 100644 index 70fa7411..00000000 --- a/plugin-src/utils/getBoundingBox.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { parseSVG } from 'svg-path-parser'; - -type BoundingBox = { x1: number; y1: number; x2: number; y2: number }; - -export const getBoundingBox = (vectorPath: VectorPath): BoundingBox => { - const path = parseSVG(vectorPath.data); - - if (!path.length) return { x1: 0, y1: 0, x2: 0, y2: 0 }; - - const bounds = { x1: Infinity, y1: Infinity, x2: -Infinity, y2: -Infinity }; - - for (const points of path) { - switch (points.code) { - case 'M': - case 'L': - case 'C': - bounds.x1 = Math.min(bounds.x1, points.x); - bounds.y1 = Math.min(bounds.y1, points.y); - bounds.x2 = Math.max(bounds.x2, points.x); - bounds.y2 = Math.max(bounds.y2, points.y); - } - } - - return bounds; -}; diff --git a/plugin-src/utils/index.ts b/plugin-src/utils/index.ts index 425aa915..f0a3ec4f 100644 --- a/plugin-src/utils/index.ts +++ b/plugin-src/utils/index.ts @@ -3,7 +3,6 @@ export * from './applyRotation'; export * from './calculateAdjustment'; export * from './calculateLinearGradient'; export * from './calculateRadialGradient'; -export * from './getBoundingBox'; export * from './matrixInvert'; export * from './rgbToHex'; export * from './sleep';