Skip to content

Commit

Permalink
reworked remote components
Browse files Browse the repository at this point in the history
  • Loading branch information
Cenadros committed Jun 28, 2024
1 parent 6a5ec1b commit c2eb728
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 133 deletions.
39 changes: 0 additions & 39 deletions plugin-src/RemoteComponents.ts

This file was deleted.

3 changes: 0 additions & 3 deletions plugin-src/libraries.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { ComponentShape } from '@ui/lib/types/shapes/componentShape';

import { RemoteComponentsLibrary } from './RemoteComponents';

export const textStyles: Map<string, TextStyle | undefined> = new Map();
export const paintStyles: Map<string, PaintStyle | undefined> = new Map();
export const overrides: Map<string, NodeChangeProperty[]> = new Map();
export const images: Map<string, Image | null> = new Map();
export const components: Map<string, ComponentShape> = new Map();
export const remoteComponents = new RemoteComponentsLibrary();
21 changes: 0 additions & 21 deletions plugin-src/processors/processPages.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
import { sleep } from '@common/sleep';

import { remoteComponents } from '@plugin/libraries';
import { transformPageNode } from '@plugin/transformers';
import { translateRemoteChildren } from '@plugin/translators';

import { PenpotPage } from '@ui/lib/types/penpotPage';

export const processPages = async (node: DocumentNode): Promise<PenpotPage[]> => {
const children = await processLocalDocument(node);
const remoteComponents = await processRemoteComponents();
if (remoteComponents) {
children.push(remoteComponents);
}

return children;
};

const processRemoteComponents = async (): Promise<PenpotPage | undefined> => {
if (remoteComponents.remaining() > 0) {
return {
name: 'External Components',
children: await translateRemoteChildren()
};
}
};

const processLocalDocument = async (node: DocumentNode): Promise<PenpotPage[]> => {
const children = [];
let currentPage = 1;

Expand Down
43 changes: 13 additions & 30 deletions plugin-src/transformers/transformInstanceNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { overrides, remoteComponents } from '@plugin/libraries';
import { overrides } from '@plugin/libraries';
import {
transformAutoLayout,
transformBlend,
Expand Down Expand Up @@ -28,18 +28,14 @@ export const transformInstanceNode = async (
}

const primaryComponent = getPrimaryComponent(mainComponent);
if (isUnprocessableComponent(primaryComponent)) {
return;
}

if (primaryComponent.remote) {
registerExternalComponents(primaryComponent);
}

registerTextVariableOverrides(node, primaryComponent);

if (node.overrides.length > 0) {
node.overrides.forEach(override => overrides.set(override.id, override.overriddenFields));
const isOrphan = isOrphanInstance(primaryComponent);
let nodeOverrides = {};
if (!isOrphan) {
registerTextVariableOverrides(node, primaryComponent);
if (node.overrides.length > 0) {
node.overrides.forEach(override => overrides.set(override.id, override.overriddenFields));
}
nodeOverrides = transformOverrides(node);
}

return {
Expand All @@ -48,6 +44,7 @@ export const transformInstanceNode = async (
mainComponentFigmaId: mainComponent.id,
isComponentRoot: isComponentRoot(node),
showContent: !node.clipsContent,
isOrphan,
...transformFigmaIds(node),
...transformFills(node),
...transformEffects(node),
Expand All @@ -62,7 +59,7 @@ export const transformInstanceNode = async (
...transformConstraints(node),
...transformAutoLayout(node),
...(await transformChildren(node)),
...transformOverrides(node)
...nodeOverrides
};
};

Expand All @@ -74,14 +71,6 @@ const getPrimaryComponent = (mainComponent: ComponentNode): ComponentNode | Comp
return mainComponent;
};

const registerExternalComponents = (primaryComponent: ComponentNode | ComponentSetNode): void => {
if (remoteComponents.has(primaryComponent.id)) {
return;
}

remoteComponents.register(primaryComponent.id, primaryComponent);
};

const getComponentTextPropertyOverrides = (
node: InstanceNode,
primaryComponent: ComponentNode | ComponentSetNode
Expand Down Expand Up @@ -133,14 +122,8 @@ const registerTextVariableOverrides = (
}
};

/**
* We do not want to process component instances in the following scenarios:
*
* 1. If the component does not have a parent. (it's been removed)
* 2. Main component can be in a ComponentSet (the same logic applies to the parent).
*/
const isUnprocessableComponent = (primaryComponent: ComponentNode | ComponentSetNode): boolean => {
return primaryComponent.parent === null && !primaryComponent.remote;
const isOrphanInstance = (primaryComponent: ComponentNode | ComponentSetNode): boolean => {
return primaryComponent.parent === null || primaryComponent.remote;
};

const isComponentRoot = (node: InstanceNode): boolean => {
Expand Down
33 changes: 0 additions & 33 deletions plugin-src/translators/translateChildren.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { sleep } from '@common/sleep';

import { remoteComponents } from '@plugin/libraries';
import { transformGroupNodeLike, transformSceneNode } from '@plugin/transformers';
import { transformMaskFigmaIds } from '@plugin/transformers/partials';

Expand Down Expand Up @@ -62,35 +61,3 @@ export const translateChildren = async (children: readonly SceneNode[]): Promise

return transformedChildren;
};

export const translateRemoteChildren = async (): Promise<PenpotNode[]> => {
const transformedChildren: PenpotNode[] = [];
let currentRemote = 1;

figma.ui.postMessage({
type: 'PROGRESS_STEP',
data: 'remote'
});

while (remoteComponents.remaining() > 0) {
figma.ui.postMessage({
type: 'PROGRESS_TOTAL_ITEMS',
data: remoteComponents.total()
});

const child = remoteComponents.next();

const penpotNode = await transformSceneNode(child);

if (penpotNode) transformedChildren.push(penpotNode);

figma.ui.postMessage({
type: 'PROGRESS_PROCESSED_ITEMS',
data: currentRemote++
});

await sleep(0);
}

return transformedChildren;
};
5 changes: 0 additions & 5 deletions ui-src/components/ExporterProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ const stepMessages: Record<Steps, Messages> = {
total: 'pages processed 💪',
current: 'Currently processing layer'
},
remote: {
total: 'remote components processed 📦',
current: 'Currently processing layer'
},
images: {
total: 'images downloaded 📸'
},
Expand Down Expand Up @@ -74,7 +70,6 @@ const StepProgress = (): JSX.Element | null => {

switch (step) {
case 'processing':
case 'remote':
case 'images':
case 'optimization':
case 'building':
Expand Down
1 change: 0 additions & 1 deletion ui-src/context/useFigma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export type UseFigmaHook = {

export type Steps =
| 'processing'
| 'remote'
| 'images'
| 'optimization'
| 'building'
Expand Down
13 changes: 12 additions & 1 deletion ui-src/parser/creators/createComponentInstance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { PenpotFile } from '@ui/lib/types/penpotFile';
import { Uuid } from '@ui/lib/types/utils/uuid';
import { components, parseFigmaId } from '@ui/parser';
import { ComponentInstance } from '@ui/types';

import { createArtboard } from '.';

let remoteFileId: Uuid | undefined = undefined;

export const createComponentInstance = (
file: PenpotFile,
{
Expand All @@ -23,7 +26,7 @@ export const createComponentInstance = (
}

shape.shapeRef = uiComponent.mainInstanceId;
shape.componentFile = file.getId();
shape.componentFile = shape.isOrphan ? getRemoteFileId(file) : file.getId();
shape.componentRoot = isComponentRoot;
shape.componentId = uiComponent.componentId;

Expand All @@ -46,3 +49,11 @@ const createUiComponent = (file: PenpotFile, mainComponentFigmaId: string) => {

return uiComponent;
};

const getRemoteFileId = (file: PenpotFile): Uuid => {
if (!remoteFileId) {
remoteFileId = file.newId();
}

return remoteFileId;
};
1 change: 1 addition & 0 deletions ui-src/types/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type ComponentInstance = ShapeGeomAttributes &
figmaRelatedId?: string;
isComponentRoot: boolean;
showContent?: boolean;
isOrphan: boolean;
type: 'instance';
};

Expand Down

0 comments on commit c2eb728

Please sign in to comment.