mirror of
https://github.com/penpot/penpot-exporter-figma-plugin.git
synced 2024-12-22 21:53:27 -05:00
5d7263bdbf
* Allow rotation for Ellipses, Rectangles and Texts * undo rotations for texts * Translate rotations for ellipses and rectangles
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { Command } from 'svg-path-parser';
|
|
|
|
import { translateStrokeCap, translateStrokes } from '@plugin/translators';
|
|
|
|
import { ShapeAttributes } from '@ui/lib/types/shapes/shape';
|
|
import { Stroke } from '@ui/lib/types/utils/stroke';
|
|
|
|
const isVectorLike = (node: GeometryMixin | VectorLikeMixin): node is VectorLikeMixin => {
|
|
return 'vectorNetwork' in node;
|
|
};
|
|
|
|
const hasFillGeometry = (node: GeometryMixin): boolean => {
|
|
return node.fillGeometry.length > 0;
|
|
};
|
|
|
|
export const transformStrokes = async (
|
|
node: GeometryMixin | (GeometryMixin & IndividualStrokesMixin)
|
|
): Promise<Pick<ShapeAttributes, 'strokes'>> => {
|
|
const vectorNetwork = isVectorLike(node) ? node.vectorNetwork : undefined;
|
|
|
|
const strokeCaps = (stroke: Stroke) => {
|
|
if (!hasFillGeometry(node) && vectorNetwork && vectorNetwork.vertices.length > 0) {
|
|
stroke.strokeCapStart = translateStrokeCap(vectorNetwork.vertices[0]);
|
|
stroke.strokeCapEnd = translateStrokeCap(
|
|
vectorNetwork.vertices[vectorNetwork.vertices.length - 1]
|
|
);
|
|
}
|
|
|
|
return stroke;
|
|
};
|
|
|
|
return {
|
|
strokes: await translateStrokes(node, strokeCaps)
|
|
};
|
|
};
|
|
|
|
export const transformStrokesFromVector = async (
|
|
node: VectorNode,
|
|
vector: Command[],
|
|
vectorRegion: VectorRegion | undefined
|
|
): Promise<Pick<ShapeAttributes, 'strokes'>> => {
|
|
const strokeCaps = (stroke: Stroke) => {
|
|
if (vectorRegion !== undefined) return stroke;
|
|
|
|
const startVertex = findVertex(node.vectorNetwork.vertices, vector[0]);
|
|
const endVertex = findVertex(node.vectorNetwork.vertices, vector[vector.length - 1]);
|
|
|
|
if (!startVertex || !endVertex) return stroke;
|
|
|
|
stroke.strokeCapStart = translateStrokeCap(startVertex);
|
|
stroke.strokeCapEnd = translateStrokeCap(endVertex);
|
|
|
|
return stroke;
|
|
};
|
|
|
|
return {
|
|
strokes: await translateStrokes(node, strokeCaps)
|
|
};
|
|
};
|
|
|
|
const findVertex = (
|
|
vertexs: readonly VectorVertex[],
|
|
command: Command
|
|
): VectorVertex | undefined => {
|
|
if (command.command !== 'moveto' && command.command !== 'lineto' && command.command !== 'curveto')
|
|
return;
|
|
|
|
return vertexs.find(vertex => vertex.x === command.x && vertex.y === command.y);
|
|
};
|