mirror of
https://github.com/penpot/penpot-exporter-figma-plugin.git
synced 2024-12-22 13:43:03 -05:00
Strokes (#44)
* strokes * adjust a few types * simplify --------- Co-authored-by: Jordi Sala Morales <jordism91@gmail.com>
This commit is contained in:
parent
798e50a69c
commit
c114bd0b98
2 changed files with 52 additions and 11 deletions
|
@ -6,19 +6,25 @@ const isVectorLike = (node: GeometryMixin | VectorLikeMixin): node is VectorLike
|
||||||
return 'vectorNetwork' in node;
|
return 'vectorNetwork' in node;
|
||||||
};
|
};
|
||||||
|
|
||||||
const hasFillGeometry = (node: GeometryMixin | (GeometryMixin & VectorLikeMixin)): boolean => {
|
const isIndividualStrokes = (
|
||||||
|
node: GeometryMixin | IndividualStrokesMixin
|
||||||
|
): node is IndividualStrokesMixin => {
|
||||||
|
return 'strokeTopWeight' in node;
|
||||||
|
};
|
||||||
|
|
||||||
|
const hasFillGeometry = (node: GeometryMixin): boolean => {
|
||||||
return node.fillGeometry.length > 0;
|
return node.fillGeometry.length > 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const transformStrokes = (
|
export const transformStrokes = (
|
||||||
node: GeometryMixin | (GeometryMixin & VectorLikeMixin)
|
node: GeometryMixin | (GeometryMixin & IndividualStrokesMixin)
|
||||||
): Partial<ShapeAttributes> => {
|
): Partial<ShapeAttributes> => {
|
||||||
return {
|
return {
|
||||||
strokes: translateStrokes(
|
strokes: translateStrokes(
|
||||||
node.strokes,
|
node,
|
||||||
node.strokeWeight,
|
|
||||||
hasFillGeometry(node),
|
hasFillGeometry(node),
|
||||||
isVectorLike(node) ? node.vectorNetwork : undefined
|
isVectorLike(node) ? node.vectorNetwork : undefined,
|
||||||
|
isIndividualStrokes(node) ? node : undefined
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,19 +1,21 @@
|
||||||
import { translateFill } from '@plugin/translators/translateFills';
|
import { translateFill } from '@plugin/translators/translateFills';
|
||||||
|
|
||||||
import { Stroke, StrokeCaps } from '@ui/lib/types/utils/stroke';
|
import { Stroke, StrokeAlignment, StrokeCaps } from '@ui/lib/types/utils/stroke';
|
||||||
|
|
||||||
export const translateStrokes = (
|
export const translateStrokes = (
|
||||||
paints: readonly Paint[],
|
nodeStrokes: MinimalStrokesMixin,
|
||||||
strokeWeight: number | typeof figma.mixed,
|
|
||||||
hasFillGeometry?: boolean,
|
hasFillGeometry?: boolean,
|
||||||
vectorNetwork?: VectorNetwork
|
vectorNetwork?: VectorNetwork,
|
||||||
|
individualStrokes?: IndividualStrokesMixin
|
||||||
): Stroke[] => {
|
): Stroke[] => {
|
||||||
return paints.map((paint, index) => {
|
return nodeStrokes.strokes.map((paint, index) => {
|
||||||
const fill = translateFill(paint, 0, 0);
|
const fill = translateFill(paint, 0, 0);
|
||||||
const stroke: Stroke = {
|
const stroke: Stroke = {
|
||||||
strokeColor: fill?.fillColor,
|
strokeColor: fill?.fillColor,
|
||||||
strokeOpacity: fill?.fillOpacity,
|
strokeOpacity: fill?.fillOpacity,
|
||||||
strokeWidth: strokeWeight === figma.mixed ? 1 : strokeWeight
|
strokeWidth: translateStrokeWeight(nodeStrokes.strokeWeight, individualStrokes),
|
||||||
|
strokeAlignment: translateStrokeAlignment(nodeStrokes.strokeAlign),
|
||||||
|
strokeStyle: nodeStrokes.dashPattern.length ? 'dashed' : 'solid'
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!hasFillGeometry && index === 0 && vectorNetwork && vectorNetwork.vertices.length) {
|
if (!hasFillGeometry && index === 0 && vectorNetwork && vectorNetwork.vertices.length) {
|
||||||
|
@ -27,6 +29,39 @@ export const translateStrokes = (
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const translateStrokeCap = (vertex: VectorVertex): StrokeCaps | undefined => {
|
const translateStrokeCap = (vertex: VectorVertex): StrokeCaps | undefined => {
|
||||||
switch (vertex.strokeCap as StrokeCap | ConnectorStrokeCap) {
|
switch (vertex.strokeCap as StrokeCap | ConnectorStrokeCap) {
|
||||||
case 'NONE':
|
case 'NONE':
|
||||||
|
|
Loading…
Reference in a new issue