Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectors - Pen & pencil #35

Merged
merged 12 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"dependencies": {
"react": "^18.2",
"react-dom": "^18.2",
"slugify": "^1.6"
"slugify": "^1.6",
"svg-path-commander": "^2.0"
},
"devDependencies": {
"@figma/eslint-plugin-figma-plugins": "^0.15",
Expand Down
4 changes: 3 additions & 1 deletion plugin-src/transformers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ export * from './transformDocumentNode';
export * from './transformEllipseNode';
export * from './transformFrameNode';
export * from './transformGroupNode';
export * from './transformImageNode';
export * from './transformPageNode';
export * from './transformPolygonNode';
export * from './transformRectangleNode';
export * from './transformSceneNode';
export * from './transformImageNode';
export * from './transformTextNode';
export * from './transformVectorNode';
3 changes: 2 additions & 1 deletion plugin-src/transformers/transformEllipseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
transformDimensionAndPosition,
transformSceneNode
} from '@plugin/transformers/partials';
import { translateFills } from '@plugin/translators';
import { translateFills, translateStrokes } from '@plugin/translators';

import { CircleShape } from '@ui/lib/types/circle/circleShape';

Expand All @@ -16,6 +16,7 @@ export const transformEllipseNode = (
type: 'circle',
name: node.name,
fills: translateFills(node.fills, node.width, node.height),
strokes: translateStrokes(node),
...transformDimensionAndPosition(node, baseX, baseY),
...transformSceneNode(node),
...transformBlend(node)
Expand Down
3 changes: 2 additions & 1 deletion plugin-src/transformers/transformFrameNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { transformDimensionAndPosition, transformSceneNode } from '@plugin/transformers/partials';
import { transformChildren } from '@plugin/transformers/partials';
import { translateFills } from '@plugin/translators';
import { translateFills, translateStrokes } from '@plugin/translators';

import { FrameShape } from '@ui/lib/types/frame/frameShape';

Expand All @@ -13,6 +13,7 @@ export const transformFrameNode = async (
type: 'frame',
name: node.name,
fills: translateFills(node.fills, node.width, node.height),
strokes: translateStrokes(node),
...(await transformChildren(node, baseX, baseY)),
...transformDimensionAndPosition(node, baseX, baseY),
...transformSceneNode(node)
Expand Down
25 changes: 25 additions & 0 deletions plugin-src/transformers/transformPolygonNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 transformPolygonNode = (
node: DefaultShapeMixin,
baseX: number,
baseY: number
): PathShape => {
return {
type: 'path',
name: node.name,
content: translateVectorPaths(node.fillGeometry, baseX + node.x, baseY + node.y),
strokes: translateStrokes(node),
fills: translateFills(node.fills, node.width, node.height),
...transformDimensionAndPosition(node, baseX, baseY),
...transformSceneNode(node),
...transformBlend(node)
};
};
3 changes: 2 additions & 1 deletion plugin-src/transformers/transformRectangleNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
transformDimensionAndPosition,
transformSceneNode
} from '@plugin/transformers/partials';
import { translateFills } from '@plugin/translators';
import { translateFills, translateStrokes } from '@plugin/translators';

import { RectShape } from '@ui/lib/types/rect/rectShape';

Expand All @@ -16,6 +16,7 @@ export const transformRectangleNode = (
type: 'rect',
name: node.name,
fills: translateFills(node.fills, node.width, node.height),
strokes: translateStrokes(node),
...transformDimensionAndPosition(node, baseX, baseY),
...transformSceneNode(node),
...transformBlend(node)
Expand Down
9 changes: 8 additions & 1 deletion plugin-src/transformers/transformSceneNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
transformFrameNode,
transformGroupNode,
transformImageNode,
transformPolygonNode,
transformRectangleNode,
transformTextNode
transformTextNode,
transformVectorNode
} from '.';

export const transformSceneNode = async (
Expand Down Expand Up @@ -40,6 +42,11 @@ export const transformSceneNode = async (
return await transformGroupNode(node, baseX, baseY);
case 'TEXT':
return transformTextNode(node, baseX, baseY);
case 'STAR':
case 'POLYGON':
return transformPolygonNode(node, baseX, baseY);
case 'VECTOR':
return transformVectorNode(node, baseX, baseY);
}

throw new Error(`Unsupported node type: ${node.type}`);
Expand Down
21 changes: 21 additions & 0 deletions plugin-src/transformers/transformVectorNode.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 transformVectorNode = (node: VectorNode, 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.vectorPaths, baseX + node.x, baseY + node.y),
strokes: translateStrokes(node),
...transformDimensionAndPosition(node, baseX, baseY),
...transformSceneNode(node),
...transformBlend(node)
};
};
4 changes: 3 additions & 1 deletion plugin-src/translators/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from './translateBlendMode';
export * from './translateFills';
export * from './translateGradientLinearFill';
export * from './translateSolidFill';
export * from './translateStrokes';
export * from './translateTextDecoration';
export * from './translateTextTransform';
export * from './translateBlendMode';
export * from './translateVectorPaths';
2 changes: 1 addition & 1 deletion plugin-src/translators/translateFills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Fill } from '@ui/lib/types/utils/fill';
import { translateGradientLinearFill } from './translateGradientLinearFill';
import { translateSolidFill } from './translateSolidFill';

const translateFill = (fill: Paint, width: number, height: number): Fill | undefined => {
export const translateFill = (fill: Paint, width: number, height: number): Fill | undefined => {
switch (fill.type) {
case 'SOLID':
return translateSolidFill(fill);
Expand Down
14 changes: 14 additions & 0 deletions plugin-src/translators/translateStrokes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { translateFill } from '@plugin/translators/translateFills';

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

export const translateStrokes = (node: MinimalStrokesMixin): Stroke[] => {
return 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
};
});
};
60 changes: 60 additions & 0 deletions plugin-src/translators/translateVectorPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import SVGPathCommander from 'svg-path-commander';

import { Segment } from '@ui/lib/types/path/PathContent';

export const translateVectorPaths = (
paths: VectorPaths,
baseX: number,
baseY: number
): Segment[] => {
let segments: Segment[] = [];

for (const path of paths) {
segments = [...segments, ...translateVectorPath(path, baseX, baseY)];
}

return segments;
};

const translateVectorPath = (path: VectorPath, baseX: number, baseY: number): Segment[] => {
const segments: Segment[] = [];

const normalizedPath = SVGPathCommander.normalizePath(path.data);

for (const [command, ...rest] of normalizedPath) {
switch (command) {
case 'M':
segments.push({
command: 'move-to',
params: { x: (rest[0] ?? 0) + baseX, y: (rest[1] ?? 0) + baseY }
});
break;
case 'L':
segments.push({
command: 'line-to',
params: { x: (rest[0] ?? 0) + baseX, y: (rest[1] ?? 0) + baseY }
});
break;
case 'C':
segments.push({
command: 'curve-to',
params: {
c1x: (rest[0] ?? 0) + baseX,
c1y: (rest[1] ?? 0) + baseY,
c2x: (rest[2] ?? 0) + baseX,
c2y: (rest[3] ?? 0) + baseY,
x: (rest[4] ?? 0) + baseX,
y: (rest[5] ?? 0) + baseY
}
});
break;
case 'Z':
segments.push({
command: 'close-path'
});
break;
}
}

return segments;
};
3 changes: 3 additions & 0 deletions ui-src/converters/createPenpotItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createPenpotCircle,
createPenpotGroup,
createPenpotImage,
createPenpotPath,
createPenpotRectangle,
createPenpotText
} from '.';
Expand All @@ -22,6 +23,8 @@ export const createPenpotItem = (file: PenpotFile, node: PenpotNode) => {
return createPenpotGroup(file, node);
case 'image':
return createPenpotImage(file, node);
case 'path':
return createPenpotPath(file, node);
case 'text':
return createPenpotText(file, node);
}
Expand Down
21 changes: 21 additions & 0 deletions ui-src/converters/createPenpotPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { PenpotFile } from '@ui/lib/penpot';
import { PATH_TYPE } from '@ui/lib/types/path/pathAttributes';
import { PathShape } from '@ui/lib/types/path/pathShape';
import {
translateFillGradients,
translatePathContent,
translateUiBlendMode
} from '@ui/translators';

export const createPenpotPath = (
file: PenpotFile,
{ type, fills, blendMode, content, ...rest }: PathShape
) => {
file.createPath({
type: PATH_TYPE,
fills: translateFillGradients(fills),
blendMode: translateUiBlendMode(blendMode),
content: translatePathContent(content),
...rest
});
};
1 change: 1 addition & 0 deletions ui-src/converters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export * from './createPenpotGroup';
export * from './createPenpotImage';
export * from './createPenpotItem';
export * from './createPenpotPage';
export * from './createPenpotPath';
export * from './createPenpotRectangle';
export * from './createPenpotText';
3 changes: 2 additions & 1 deletion ui-src/lib/penpot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CircleShape } from '@ui/lib/types/circle/circleShape';
import { FrameShape } from '@ui/lib/types/frame/frameShape';
import { GroupShape } from '@ui/lib/types/group/groupShape';
import { ImageShape } from '@ui/lib/types/image/imageShape';
import { PathShape } from '@ui/lib/types/path/pathShape';
import { RectShape } from '@ui/lib/types/rect/rectShape';
import { TextShape } from '@ui/lib/types/text/textShape';

Expand All @@ -18,7 +19,7 @@ export interface PenpotFile {
closeBool(): void;
createRect(rect: RectShape): void;
createCircle(circle: CircleShape): void;
// createPath(path: any): void;
createPath(path: PathShape): void;
createText(options: TextShape): void;
createImage(image: ImageShape): void;
// createSVG(svg: any): void;
Expand Down
48 changes: 48 additions & 0 deletions ui-src/lib/types/path/PathContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export const VECTOR_LINE_TO: unique symbol = Symbol.for('line-to');
export const VECTOR_CLOSE_PATH: unique symbol = Symbol.for('close-path');
export const VECTOR_MOVE_TO: unique symbol = Symbol.for('move-to');
export const VECTOR_CURVE_TO: unique symbol = Symbol.for('curve-to');

export type PathContent = Segment[];
export type Segment = LineTo | ClosePath | MoveTo | CurveTo;
export type Command =
| 'line-to'
| 'close-path'
| 'move-to'
| 'curve-to'
| typeof VECTOR_LINE_TO
| typeof VECTOR_CLOSE_PATH
| typeof VECTOR_MOVE_TO
| typeof VECTOR_CURVE_TO;

type LineTo = {
command: 'line-to' | typeof VECTOR_LINE_TO;
params: {
x: number;
y: number;
};
};

type ClosePath = {
command: 'close-path' | typeof VECTOR_CLOSE_PATH;
};

type MoveTo = {
command: 'move-to' | typeof VECTOR_MOVE_TO;
params: {
x: number;
y: number;
};
};

type CurveTo = {
command: 'curve-to' | typeof VECTOR_CURVE_TO;
params: {
x: number;
y: number;
c1x: number;
c1y: number;
c2x: number;
c2y: number;
};
};
Loading
Loading