Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feature/component-i…
Browse files Browse the repository at this point in the history
…nstance
  • Loading branch information
jordisala1991 committed May 29, 2024
2 parents 1050f9e + 5d4ace3 commit 0942b9a
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 69 deletions.
23 changes: 23 additions & 0 deletions plugin-src/ComponentLibrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentShape } from '@ui/lib/types/shapes/componentShape';

class ComponentLibrary {
private components: Record<string, ComponentShape> = {};

public register(id: string, component: ComponentShape) {
this.components[id] = component;
}

public get(id: string): ComponentShape | undefined {
return this.components[id];
}

public all(): Record<string, ComponentShape> {
return this.components;
}

public init(components: Record<string, ComponentShape>): void {
this.components = components;
}
}

export const componentsLibrary = new ComponentLibrary();
14 changes: 10 additions & 4 deletions plugin-src/transformers/transformComponentNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { componentsLibrary } from '@plugin/ComponentLibrary';
import {
transformBlend,
transformChildren,
transformCornerRadius,
transformDimensionAndPosition,
transformEffects,
Expand All @@ -8,16 +10,15 @@ import {
transformSceneNode,
transformStrokes
} from '@plugin/transformers/partials';
import { transformChildren } from '@plugin/transformers/partials';

import { ComponentShape } from '@ui/lib/types/shapes/componentShape';
import { ComponentRoot } from '@ui/types';

export const transformComponentNode = async (
node: ComponentNode,
baseX: number,
baseY: number
): Promise<ComponentShape> => {
return {
): Promise<ComponentRoot> => {
componentsLibrary.register(node.id, {
type: 'component',
name: node.name,
path: '',
Expand All @@ -30,5 +31,10 @@ export const transformComponentNode = async (
...transformCornerRadius(node),
...(await transformChildren(node, baseX + node.x, baseY + node.y)),
...transformDimensionAndPosition(node, baseX, baseY)
});

return {
figmaId: node.id,
type: 'component'
};
};
7 changes: 5 additions & 2 deletions plugin-src/transformers/transformDocumentNode.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { PenpotDocument } from '@ui/lib/types/penpotDocument';
import { componentsLibrary } from '@plugin/ComponentLibrary';

import { PenpotDocument } from '@ui/types';

import { transformPageNode } from '.';

export const transformDocumentNode = async (node: DocumentNode): Promise<PenpotDocument> => {
return {
name: node.name,
children: await Promise.all(node.children.map(child => transformPageNode(child)))
children: await Promise.all(node.children.map(child => transformPageNode(child))),
components: componentsLibrary.all()
};
};
2 changes: 1 addition & 1 deletion plugin-src/transformers/transformSceneNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PenpotNode } from '@ui/lib/types/penpotNode';
import { PenpotNode } from '@ui/types';

import {
transformBooleanNode,
Expand Down
2 changes: 1 addition & 1 deletion plugin-src/translators/translateChildren.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { transformGroupNodeLike, transformSceneNode } from '@plugin/transformers';

import { PenpotNode } from '@ui/lib/types/penpotNode';
import { PenpotNode } from '@ui/types';

/**
* Translates the children of a node that acts as a mask.
Expand Down
2 changes: 1 addition & 1 deletion ui-src/components/PenpotExporter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { useEffect, useState } from 'react';
import { FormProvider, useForm } from 'react-hook-form';

import { Stack } from '@ui/components/Stack';
import { PenpotDocument } from '@ui/lib/types/penpotDocument';
import { parse } from '@ui/parser';
import { PenpotDocument } from '@ui/types';

import { MissingFontsSection } from './MissingFontsSection';

Expand Down
6 changes: 0 additions & 6 deletions ui-src/lib/types/penpotDocument.ts

This file was deleted.

2 changes: 1 addition & 1 deletion ui-src/lib/types/utils/children.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { PenpotNode } from '@ui/lib/types/penpotNode';
import { PenpotNode } from '@ui/types';

export type Children = { children?: PenpotNode[] };
38 changes: 17 additions & 21 deletions ui-src/parser/creators/createComponent.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
import { componentsLibrary } from '@plugin/ComponentLibrary';

import { PenpotFile } from '@ui/lib/types/penpotFile';
import { ComponentShape } from '@ui/lib/types/shapes/componentShape';
import { components } from '@ui/parser/libraries';
import { uiComponents } from '@ui/parser/libraries';
import { ComponentRoot } from '@ui/types';

import { createArtboard } from '.';

export const createComponent = (
file: PenpotFile,
{ type, path, children = [], ...rest }: ComponentShape
) => {
export const createComponent = (file: PenpotFile, { figmaId }: ComponentRoot) => {
const frameId = file.newId();
const componentId = file.newId();

const commonStructure = {
...rest,
children,
const component = componentsLibrary.get(figmaId);
if (!component) {
return;
}

createArtboard(file, {
...component,
componentFile: file.getId(),
componentId: componentId,
componentRoot: true,
mainInstance: true
};

createArtboard(file, {
...commonStructure,
id: frameId,
mainInstance: true,
id: frameId,
type: 'frame'
});

components.add({
...commonStructure,
id: componentId,
mainInstanceId: frameId,
uiComponents.register(figmaId, {
componentId,
mainInstancePage: file.getCurrentPageId(),
path,
type
componentFigmaId: figmaId,
mainInstanceId: frameId
});
};
25 changes: 21 additions & 4 deletions ui-src/parser/creators/createComponentLibrary.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { componentsLibrary } from '@plugin/ComponentLibrary';

import { PenpotFile } from '@ui/lib/types/penpotFile';
import { ComponentShape } from '@ui/lib/types/shapes/componentShape';
import { components } from '@ui/parser/libraries';
import { uiComponents } from '@ui/parser/libraries';

import { createItems } from '.';

export const createComponentLibrary = (file: PenpotFile) => {
components.get().forEach(({ children = [], ...rest }: ComponentShape) => {
file.startComponent(rest);
uiComponents.all().forEach(uiComponent => {
const component = componentsLibrary.get(uiComponent.componentFigmaId);
if (!component) {
return;
}

const { children = [], ...rest } = component;

file.startComponent({
...rest,
id: uiComponent.componentId,
componentId: uiComponent.componentId,
mainInstancePage: uiComponent.mainInstancePage,
mainInstanceId: uiComponent.mainInstanceId,
componentRoot: true,
mainInstance: true,
componentFile: file.getId()
});

createItems(file, children);

Expand Down
2 changes: 1 addition & 1 deletion ui-src/parser/creators/createItems.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PenpotFile } from '@ui/lib/types/penpotFile';
import { PenpotNode } from '@ui/lib/types/penpotNode';
import { PenpotNode } from '@ui/types';

import {
createArtboard,
Expand Down
19 changes: 0 additions & 19 deletions ui-src/parser/libraries/Components.ts

This file was deleted.

30 changes: 30 additions & 0 deletions ui-src/parser/libraries/UiComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Uuid } from '@ui/lib/types/utils/uuid';

type UiComponent = {
componentId: Uuid;
mainInstancePage: Uuid;
mainInstanceId: Uuid;
componentFigmaId: string;
};

class UiComponents {
private components: Record<string, UiComponent> = {};

public register(id: string, component: UiComponent) {
this.components[id] = component;
}

public get(id: string): UiComponent | undefined {
return this.components[id];
}

public all(): UiComponent[] {
return Object.values(this.components);
}

public init() {
this.components = {};
}
}

export const uiComponents = new UiComponents();
2 changes: 1 addition & 1 deletion ui-src/parser/libraries/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './Components';
export * from './UiComponents';
11 changes: 7 additions & 4 deletions ui-src/parser/parse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { componentsLibrary } from '@plugin/ComponentLibrary';

import { createFile } from '@ui/lib/penpot';
import { PenpotDocument } from '@ui/lib/types/penpotDocument';
import { createComponentLibrary, createPage } from '@ui/parser/creators';
import { components } from '@ui/parser/libraries';
import { uiComponents } from '@ui/parser/libraries/UiComponents';
import { PenpotDocument } from '@ui/types';

export const parse = ({ name, children = [] }: PenpotDocument) => {
components.clear();
export const parse = ({ name, children = [], components }: PenpotDocument) => {
componentsLibrary.init(components);
uiComponents.init();

const file = createFile(name);

Expand Down
4 changes: 4 additions & 0 deletions ui-src/types/componentRoot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type ComponentRoot = {
figmaId: string;
type: 'component';
};
3 changes: 3 additions & 0 deletions ui-src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './componentRoot';
export * from './penpotDocument';
export * from './penpotNode';
8 changes: 8 additions & 0 deletions ui-src/types/penpotDocument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PenpotPage } from '@ui/lib/types/penpotPage';
import { ComponentShape } from '@ui/lib/types/shapes/componentShape';

export type PenpotDocument = {
name: string;
children?: PenpotPage[];
components: Record<string, ComponentShape>;
};
6 changes: 3 additions & 3 deletions ui-src/lib/types/penpotNode.ts → ui-src/types/penpotNode.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BoolShape } from '@ui/lib/types/shapes/boolShape';
import { CircleShape } from '@ui/lib/types/shapes/circleShape';
import { ComponentShape } from '@ui/lib/types/shapes/componentShape';
import { FrameShape } from '@ui/lib/types/shapes/frameShape';
import { GroupShape } from '@ui/lib/types/shapes/groupShape';
import { InstanceShape } from '@ui/lib/types/shapes/instanceShape';
import { PathShape } from '@ui/lib/types/shapes/pathShape';
import { RectShape } from '@ui/lib/types/shapes/rectShape';
import { TextShape } from '@ui/lib/types/shapes/textShape';
import { ComponentRoot } from '@ui/types';

export type PenpotNode =
| FrameShape
Expand All @@ -16,5 +16,5 @@ export type PenpotNode =
| CircleShape
| TextShape
| BoolShape
| ComponentShape
| InstanceShape;
| InstanceShape
| ComponentRoot;

0 comments on commit 0942b9a

Please sign in to comment.