Skip to content

Commit

Permalink
lines & arrows draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Cenadros committed Apr 16, 2024
1 parent 73ccf83 commit 1ffb224
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions plugin-src/transformers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './transformEllipseNode';
export * from './transformFrameNode';
export * from './transformGroupNode';
export * from './transformImageNode';
export * from './transformLineNode';
export * from './transformPageNode';
export * from './transformPolygonNode';
export * from './transformRectangleNode';
Expand Down
21 changes: 21 additions & 0 deletions plugin-src/transformers/transformLineNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
transformBlend,
transformDimensionAndPosition,
transformSceneNode
} from '@plugin/transformers/partials';
import { translateFills, translateStrokes, translateVectorPaths } from '@plugin/translators';

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

export const transformLineNode = (node: LineNode, baseX: number, baseY: number): PathShape => {
return {
type: 'path',
name: node.name,
fills: node.fillGeometry.length ? translateFills(node.fills, node.width, node.height) : [],
content: translateVectorPaths(node.strokeGeometry, baseX + node.x, baseY + node.y),
strokes: translateStrokes(node),
...transformDimensionAndPosition(node, baseX, baseY),
...transformSceneNode(node),
...transformBlend(node)
};
};
3 changes: 3 additions & 0 deletions plugin-src/transformers/transformSceneNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
transformFrameNode,
transformGroupNode,
transformImageNode,
transformLineNode,
transformPolygonNode,
transformRectangleNode,
transformTextNode,
Expand Down Expand Up @@ -47,6 +48,8 @@ export const transformSceneNode = async (
return transformPolygonNode(node, baseX, baseY);
case 'VECTOR':
return transformVectorNode(node, baseX, baseY);
case 'LINE':
return transformLineNode(node, baseX, baseY);
}

throw new Error(`Unsupported node type: ${node.type}`);
Expand Down
2 changes: 1 addition & 1 deletion plugin-src/transformers/transformVectorNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const transformVectorNode = (node: VectorNode, baseX: number, baseY: numb
name: node.name,
fills: node.fillGeometry.length ? translateFills(node.fills, node.width, node.height) : [],
content: translateVectorPaths(node.vectorPaths, baseX + node.x, baseY + node.y),
strokes: translateStrokes(node),
strokes: translateStrokes(node, node.vectorNetwork),
...transformDimensionAndPosition(node, baseX, baseY),
...transformSceneNode(node),
...transformBlend(node)
Expand Down
39 changes: 35 additions & 4 deletions plugin-src/translators/translateStrokes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import { translateFill } from '@plugin/translators/translateFills';

import { Stroke } from '@ui/lib/types/utils/stroke';
import { Stroke, StrokeCaps } from '@ui/lib/types/utils/stroke';

export const translateStrokes = (node: MinimalStrokesMixin): Stroke[] => {
return node.strokes.map(stroke => {
export const translateStrokes = (
node: MinimalStrokesMixin,
vectorNetwork?: VectorNetwork
): Stroke[] => {
const strokes = node.strokes.map(stroke => {
const fill = translateFill(stroke, 0, 0);
return {
strokeColor: fill?.fillColor,
strokeOpacity: fill?.fillOpacity,
strokeWidth: node.strokeWeight === figma.mixed ? 1 : node.strokeWeight
};
} as Stroke;
});

// if line check for arrow strokes
if (vectorNetwork && vectorNetwork.vertices.length === 2) {
strokes[0].strokeCapStart = translateStrokeCap(vectorNetwork.vertices[0]);
strokes[0].strokeCapEnd = translateStrokeCap(vectorNetwork.vertices[1]);
}

return strokes;
};

const translateStrokeCap = (vertex: VectorVertex): StrokeCaps | undefined => {
switch (vertex.strokeCap as StrokeCap | ConnectorStrokeCap) {
case 'NONE':
return;
case 'ROUND':
return 'round';
case 'ARROW_EQUILATERAL':
return 'triangle-arrow';
case 'SQUARE':
return 'square';
case 'CIRCLE_FILLED':
return 'circle-marker';
case 'DIAMOND_FILLED':
return 'diamond-marker';
case 'ARROW_LINES':
default:
return 'line-arrow';
}
};

0 comments on commit 1ffb224

Please sign in to comment.