0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 13:43:03 -05:00
penpot-exporter-figma-plugin/plugin-src/translators/translateStrokes.ts
Alex Sánchez c9f8a0dcd2
Vectors - Line & Arrow (#37)
* lines & arrows draft

* simplify vectors

* try to refactor

* remove comments

* add more clarity to code

* fix translate strokes

* minor style fix

* fix for vectors without geometry

* reduce code

---------

Co-authored-by: Alex Sánchez <alejandro@runroom.com>
Co-authored-by: Jordi Sala Morales <jordism91@gmail.com>
2024-04-17 10:53:38 +02:00

49 lines
1.4 KiB
TypeScript

import { translateFill } from '@plugin/translators/translateFills';
import { Stroke, StrokeCaps } from '@ui/lib/types/utils/stroke';
export const translateStrokes = (
paints: readonly Paint[],
strokeWeight: number | typeof figma.mixed,
hasFillGeometry?: boolean,
vectorNetwork?: VectorNetwork
): Stroke[] => {
return paints.map((paint, index) => {
const fill = translateFill(paint, 0, 0);
const stroke: Stroke = {
strokeColor: fill?.fillColor,
strokeOpacity: fill?.fillOpacity,
strokeWidth: strokeWeight === figma.mixed ? 1 : strokeWeight
};
if (!hasFillGeometry && index === 0 && vectorNetwork && vectorNetwork.vertices.length) {
stroke.strokeCapStart = translateStrokeCap(vectorNetwork.vertices[0]);
stroke.strokeCapEnd = translateStrokeCap(
vectorNetwork.vertices[vectorNetwork.vertices.length - 1]
);
}
return stroke;
});
};
const translateStrokeCap = (vertex: VectorVertex): StrokeCaps | undefined => {
switch (vertex.strokeCap as StrokeCap | ConnectorStrokeCap) {
case 'NONE':
return;
case 'ROUND':
return 'round';
case 'ARROW_EQUILATERAL':
case 'TRIANGLE_FILLED':
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';
}
};