2024-04-16 09:08:39 -05:00
|
|
|
import { translateFill } from '@plugin/translators/translateFills';
|
|
|
|
|
2024-04-18 02:42:45 -05:00
|
|
|
import { Stroke, StrokeAlignment, StrokeCaps } from '@ui/lib/types/utils/stroke';
|
2024-04-16 09:08:39 -05:00
|
|
|
|
2024-04-17 03:53:38 -05:00
|
|
|
export const translateStrokes = (
|
2024-04-18 02:42:45 -05:00
|
|
|
nodeStrokes: MinimalStrokesMixin,
|
2024-04-17 03:53:38 -05:00
|
|
|
hasFillGeometry?: boolean,
|
2024-04-18 02:42:45 -05:00
|
|
|
vectorNetwork?: VectorNetwork,
|
|
|
|
individualStrokes?: IndividualStrokesMixin
|
2024-04-17 03:53:38 -05:00
|
|
|
): Stroke[] => {
|
2024-04-18 02:42:45 -05:00
|
|
|
return nodeStrokes.strokes.map((paint, index) => {
|
2024-04-17 03:53:38 -05:00
|
|
|
const fill = translateFill(paint, 0, 0);
|
|
|
|
const stroke: Stroke = {
|
2024-04-16 09:08:39 -05:00
|
|
|
strokeColor: fill?.fillColor,
|
|
|
|
strokeOpacity: fill?.fillOpacity,
|
2024-04-18 02:42:45 -05:00
|
|
|
strokeWidth: translateStrokeWeight(nodeStrokes.strokeWeight, individualStrokes),
|
|
|
|
strokeAlignment: translateStrokeAlignment(nodeStrokes.strokeAlign),
|
|
|
|
strokeStyle: nodeStrokes.dashPattern.length ? 'dashed' : 'solid'
|
2024-04-16 09:08:39 -05:00
|
|
|
};
|
2024-04-17 03:53:38 -05:00
|
|
|
|
2024-04-24 09:29:24 -05:00
|
|
|
if (!hasFillGeometry && index === 0 && vectorNetwork && vectorNetwork.vertices.length > 0) {
|
2024-04-17 03:53:38 -05:00
|
|
|
stroke.strokeCapStart = translateStrokeCap(vectorNetwork.vertices[0]);
|
|
|
|
stroke.strokeCapEnd = translateStrokeCap(
|
|
|
|
vectorNetwork.vertices[vectorNetwork.vertices.length - 1]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stroke;
|
2024-04-16 09:08:39 -05:00
|
|
|
});
|
|
|
|
};
|
2024-04-17 03:53:38 -05:00
|
|
|
|
2024-04-18 02:42:45 -05:00
|
|
|
const translateStrokeWeight = (
|
|
|
|
strokeWeight: number | typeof figma.mixed,
|
|
|
|
individualStrokes?: IndividualStrokesMixin
|
|
|
|
): number => {
|
|
|
|
if (strokeWeight !== figma.mixed) {
|
|
|
|
return strokeWeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!individualStrokes) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Math.max(
|
|
|
|
individualStrokes.strokeTopWeight,
|
|
|
|
individualStrokes.strokeRightWeight,
|
|
|
|
individualStrokes.strokeBottomWeight,
|
|
|
|
individualStrokes.strokeLeftWeight
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const translateStrokeAlignment = (
|
|
|
|
strokeAlign: 'CENTER' | 'INSIDE' | 'OUTSIDE'
|
|
|
|
): StrokeAlignment => {
|
|
|
|
switch (strokeAlign) {
|
|
|
|
case 'CENTER':
|
|
|
|
return 'center';
|
|
|
|
case 'INSIDE':
|
|
|
|
return 'inner';
|
|
|
|
case 'OUTSIDE':
|
|
|
|
return 'outer';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-04-17 03:53:38 -05:00
|
|
|
const translateStrokeCap = (vertex: VectorVertex): StrokeCaps | undefined => {
|
|
|
|
switch (vertex.strokeCap as StrokeCap | ConnectorStrokeCap) {
|
|
|
|
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':
|
|
|
|
return 'line-arrow';
|
2024-04-24 09:29:24 -05:00
|
|
|
case 'NONE':
|
|
|
|
default:
|
|
|
|
return;
|
2024-04-17 03:53:38 -05:00
|
|
|
}
|
|
|
|
};
|