0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2025-04-18 01:34:17 -05:00

optimize code

This commit is contained in:
Jordi Sala Morales 2024-06-18 09:13:00 +00:00
parent 860c7eee11
commit 31327d6d2f
No known key found for this signature in database
GPG key ID: C5127140107F55FD
10 changed files with 103 additions and 100 deletions

View file

@ -8,17 +8,16 @@ import { createItems } from '.';
export const createArtboard = (
file: PenpotFile,
{ type, fills, strokes, children = [], figmaId, figmaRelatedId, shapeRef, ...rest }: FrameShape
{ type, children = [], figmaId, figmaRelatedId, ...shape }: FrameShape
): Uuid | undefined => {
const id = parseFigmaId(file, figmaId);
file.addArtboard({
id,
shapeRef: shapeRef ?? parseFigmaId(file, figmaRelatedId, true),
fills: symbolFills(fills),
strokes: symbolStrokes(strokes),
...rest
});
shape.id = id;
shape.shapeRef ??= parseFigmaId(file, figmaRelatedId, true);
shape.fills = symbolFills(shape.fills);
shape.strokes = symbolStrokes(shape.strokes);
file.addArtboard(shape);
createItems(file, children);

View file

@ -7,16 +7,15 @@ import { createItems } from '.';
export const createBool = (
file: PenpotFile,
{ type, fills, strokes, boolType, figmaId, figmaRelatedId, children = [], ...rest }: BoolShape
{ type, figmaId, figmaRelatedId, children = [], ...shape }: BoolShape
) => {
file.addBool({
id: parseFigmaId(file, figmaId),
shapeRef: parseFigmaId(file, figmaRelatedId, true),
fills: symbolFills(fills),
strokes: symbolStrokes(strokes),
boolType: symbolBoolType(boolType),
...rest
});
shape.id = parseFigmaId(file, figmaId);
shape.shapeRef = parseFigmaId(file, figmaRelatedId, true);
shape.fills = symbolFills(shape.fills);
shape.strokes = symbolStrokes(shape.strokes);
shape.boolType = symbolBoolType(shape.boolType);
file.addBool(shape);
createItems(file, children);

View file

@ -5,13 +5,12 @@ import { symbolFills, symbolStrokes } from '@ui/parser/creators/symbols';
export const createCircle = (
file: PenpotFile,
{ type, fills, strokes, figmaId, figmaRelatedId, ...rest }: CircleShape
{ type, figmaId, figmaRelatedId, ...shape }: CircleShape
) => {
file.createCircle({
id: parseFigmaId(file, figmaId),
shapeRef: parseFigmaId(file, figmaRelatedId, true),
fills: symbolFills(fills),
strokes: symbolStrokes(strokes),
...rest
});
shape.id = parseFigmaId(file, figmaId);
shape.shapeRef = parseFigmaId(file, figmaRelatedId, true);
shape.fills = symbolFills(shape.fills);
shape.strokes = symbolStrokes(shape.strokes);
file.createCircle(shape);
};

View file

@ -8,21 +8,20 @@ import { createArtboard } from '.';
export const createComponent = (file: PenpotFile, { figmaId }: ComponentRoot) => {
const component = componentsLibrary.get(figmaId);
if (!component) {
return;
}
const uiComponent = uiComponents.get(figmaId);
const componentId = uiComponent?.componentId ?? file.newId();
const componentId = getComponentId(file, figmaId);
const { type, ...shape } = component;
const frameId = createArtboard(file, {
...component,
componentFile: file.getId(),
componentId,
componentRoot: true,
mainInstance: true,
type: 'frame'
});
shape.componentFile = file.getId();
shape.componentId = componentId;
shape.componentRoot = true;
shape.mainInstance = true;
const frameId = createArtboard(file, shape);
if (!frameId) {
return;
@ -35,3 +34,9 @@ export const createComponent = (file: PenpotFile, { figmaId }: ComponentRoot) =>
mainInstanceId: frameId
});
};
const getComponentId = (file: PenpotFile, figmaId: string) => {
const uiComponent = uiComponents.get(figmaId);
return uiComponent?.componentId ?? file.newId();
};

View file

@ -13,31 +13,37 @@ export const createComponentInstance = (
figmaId,
figmaRelatedId,
isComponentRoot,
...rest
...shape
}: ComponentInstance
) => {
let uiComponent = uiComponents.get(mainComponentFigmaId);
const uiComponent =
uiComponents.get(mainComponentFigmaId) ?? createUiComponent(file, mainComponentFigmaId);
if (!uiComponent) {
const mainInstanceId = parseFigmaId(file, mainComponentFigmaId);
if (!mainInstanceId) {
return;
}
uiComponent = {
componentId: file.newId(),
componentFigmaId: mainComponentFigmaId,
mainInstanceId
};
uiComponents.register(mainComponentFigmaId, uiComponent);
return;
}
createArtboard(file, {
...rest,
shapeRef: uiComponent.mainInstanceId,
componentFile: file.getId(),
componentRoot: isComponentRoot,
componentId: uiComponent.componentId,
type: 'frame'
});
shape.shapeRef = uiComponent.mainInstanceId;
shape.componentFile = file.getId();
shape.componentRoot = isComponentRoot;
shape.componentId = uiComponent.componentId;
createArtboard(file, shape);
};
const createUiComponent = (file: PenpotFile, mainComponentFigmaId: string) => {
const mainInstanceId = parseFigmaId(file, mainComponentFigmaId);
if (!mainInstanceId) {
return;
}
const uiComponent = {
componentId: file.newId(),
componentFigmaId: mainComponentFigmaId,
mainInstanceId
};
uiComponents.register(mainComponentFigmaId, uiComponent);
return uiComponent;
};

View file

@ -41,20 +41,19 @@ const createComponentLibrary = async (file: PenpotFile, uiComponent: UiComponent
return;
}
const { children = [], fills, strokes, ...rest } = component;
const { children = [], ...shape } = component;
file.startComponent({
...rest,
fills: symbolFills(fills),
strokes: symbolStrokes(strokes),
id: uiComponent.componentId,
componentId: uiComponent.componentId,
mainInstancePage: uiComponent.mainInstancePage,
mainInstanceId: uiComponent.mainInstanceId,
componentRoot: true,
mainInstance: true,
componentFile: file.getId()
});
shape.fills = symbolFills(shape.fills);
shape.strokes = symbolStrokes(shape.strokes);
shape.id = uiComponent.componentId;
shape.componentId = uiComponent.componentId;
shape.mainInstancePage = uiComponent.mainInstancePage;
shape.mainInstanceId = uiComponent.mainInstanceId;
shape.componentRoot = true;
shape.mainInstance = true;
shape.componentFile = file.getId();
file.startComponent(shape);
createItems(file, children);

View file

@ -6,13 +6,12 @@ import { createItems } from '.';
export const createGroup = (
file: PenpotFile,
{ type, children = [], figmaId, figmaRelatedId, ...rest }: GroupShape
{ type, children = [], figmaId, figmaRelatedId, ...shape }: GroupShape
) => {
file.addGroup({
id: parseFigmaId(file, figmaId),
shapeRef: parseFigmaId(file, figmaRelatedId, true),
...rest
});
shape.id = parseFigmaId(file, figmaId);
shape.shapeRef = parseFigmaId(file, figmaRelatedId, true);
file.addGroup(shape);
createItems(file, children);

View file

@ -5,14 +5,13 @@ import { symbolFills, symbolPathContent, symbolStrokes } from '@ui/parser/creato
export const createPath = (
file: PenpotFile,
{ type, fills, strokes, content, figmaId, figmaRelatedId, ...rest }: PathShape
{ type, figmaId, figmaRelatedId, ...shape }: PathShape
) => {
file.createPath({
id: parseFigmaId(file, figmaId),
shapeRef: parseFigmaId(file, figmaRelatedId, true),
fills: symbolFills(fills),
strokes: symbolStrokes(strokes),
content: symbolPathContent(content),
...rest
});
shape.id = parseFigmaId(file, figmaId);
shape.shapeRef = parseFigmaId(file, figmaRelatedId, true);
shape.fills = symbolFills(shape.fills);
shape.strokes = symbolStrokes(shape.strokes);
shape.content = symbolPathContent(shape.content);
file.createPath(shape);
};

View file

@ -5,13 +5,12 @@ import { symbolFills, symbolStrokes } from '@ui/parser/creators/symbols';
export const createRectangle = (
file: PenpotFile,
{ type, fills, strokes, figmaId, figmaRelatedId, ...rest }: RectShape
{ type, figmaId, figmaRelatedId, ...shape }: RectShape
) => {
file.createRect({
id: parseFigmaId(file, figmaId),
shapeRef: parseFigmaId(file, figmaRelatedId, true),
fills: symbolFills(fills),
strokes: symbolStrokes(strokes),
...rest
});
shape.id = parseFigmaId(file, figmaId);
shape.shapeRef = parseFigmaId(file, figmaRelatedId, true);
shape.fills = symbolFills(shape.fills);
shape.strokes = symbolStrokes(shape.strokes);
file.createRect(shape);
};

View file

@ -5,15 +5,14 @@ import { symbolFills, symbolStrokes } from '@ui/parser/creators/symbols';
export const createText = (
file: PenpotFile,
{ type, strokes, figmaId, content, figmaRelatedId, ...rest }: TextShape
{ type, figmaId, figmaRelatedId, ...shape }: TextShape
) => {
file.createText({
id: parseFigmaId(file, figmaId),
shapeRef: parseFigmaId(file, figmaRelatedId, true),
content: parseContent(content),
strokes: symbolStrokes(strokes),
...rest
});
shape.id = parseFigmaId(file, figmaId);
shape.shapeRef = parseFigmaId(file, figmaRelatedId, true);
shape.content = parseContent(shape.content);
shape.strokes = symbolStrokes(shape.strokes);
file.createText(shape);
};
const parseContent = (content: TextContent | undefined): TextContent | undefined => {