0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2025-01-03 05:10:13 -05:00

make children return an array of promises

This commit is contained in:
Jordi Sala Morales 2024-06-19 07:05:10 +00:00
parent 4e5d01adb3
commit c692b8834d
No known key found for this signature in database
GPG key ID: C5127140107F55FD
11 changed files with 32 additions and 38 deletions

View file

@ -6,13 +6,13 @@ const nodeActsAsMask = (node: SceneNode): boolean => {
return 'isMask' in node && node.isMask;
};
export const transformChildren = async (node: ChildrenMixin): Promise<Children> => {
export const transformChildren = (node: ChildrenMixin): Children => {
const maskIndex = node.children.findIndex(nodeActsAsMask);
const containsMask = maskIndex !== -1;
return {
children: containsMask
? await translateMaskChildren(node.children, maskIndex)
: await translateChildren(node.children)
? translateMaskChildren(node.children, maskIndex)
: translateChildren(node.children)
};
};

View file

@ -22,7 +22,7 @@ export const transformBooleanNode = async (node: BooleanOperationNode): Promise<
name: node.name,
boolType: translateBoolType(node.booleanOperation),
...transformFigmaIds(node),
...(await transformChildren(node)),
...transformChildren(node),
...transformFills(node),
...transformEffects(node),
...transformStrokes(node),

View file

@ -18,7 +18,7 @@ import {
import { ComponentRoot } from '@ui/types';
export const transformComponentNode = async (node: ComponentNode): Promise<ComponentRoot> => {
export const transformComponentNode = (node: ComponentNode): ComponentRoot => {
componentsLibrary.register(node.id, {
type: 'component',
name: node.name,
@ -33,7 +33,7 @@ export const transformComponentNode = async (node: ComponentNode): Promise<Compo
...transformProportion(node),
...transformLayoutAttributes(node, true),
...transformCornerRadius(node),
...(await transformChildren(node)),
...transformChildren(node),
...transformDimension(node),
...transformRotationAndPosition(node),
...transformConstraints(node),

View file

@ -78,7 +78,7 @@ export const transformDocumentNode = async (node: DocumentNode): Promise<PenpotD
if (remoteComponentLibrary.remaining() > 0) {
children.push({
name: 'External Components',
children: await translateRemoteChildren()
children: translateRemoteChildren()
});
}

View file

@ -23,9 +23,9 @@ const isSectionNode = (node: FrameNode | SectionNode | ComponentSetNode): node i
return node.type === 'SECTION';
};
export const transformFrameNode = async (
export const transformFrameNode = (
node: FrameNode | SectionNode | ComponentSetNode
): Promise<FrameShape> => {
): FrameShape => {
let frameSpecificAttributes: Partial<FrameShape> = {};
let referencePoint: Point = { x: node.absoluteTransform[0][2], y: node.absoluteTransform[1][2] };
@ -60,7 +60,7 @@ export const transformFrameNode = async (
...referencePoint,
...frameSpecificAttributes,
...transformDimension(node),
...(await transformChildren(node)),
...transformChildren(node),
...transformSceneNode(node),
...transformOverrides(node)
};

View file

@ -11,20 +11,20 @@ import { transformChildren } from '@plugin/transformers/partials';
import { GroupShape } from '@ui/lib/types/shapes/groupShape';
export const transformGroupNode = async (node: GroupNode): Promise<GroupShape> => {
export const transformGroupNode = (node: GroupNode): GroupShape => {
return {
...transformFigmaIds(node),
...transformGroupNodeLike(node),
...transformEffects(node),
...transformBlend(node),
...(await transformChildren(node)),
...transformChildren(node),
...transformOverrides(node)
};
};
export const transformGroupNodeLike = (
node: BaseNodeMixin & LayoutMixin & SceneNodeMixin
): GroupShape => {
): Omit<GroupShape, 'children'> => {
return {
type: 'group',
name: node.name,

View file

@ -64,7 +64,7 @@ export const transformInstanceNode = async (
...transformRotationAndPosition(node),
...transformConstraints(node),
...transformAutoLayout(node),
...(await transformChildren(node)),
...transformChildren(node),
...transformOverrides(node)
};
};

View file

@ -9,6 +9,6 @@ export const transformPageNode = async (node: PageNode): Promise<PenpotPage> =>
options: {
background: node.backgrounds.length ? translatePageFill(node.backgrounds[0]) : undefined
},
children: await translateChildren(node.children)
children: translateChildren(node.children)
};
};

View file

@ -54,7 +54,7 @@ export const transformSceneNode = async (node: SceneNode): Promise<PenpotNode |
penpotNode = await transformBooleanNode(node);
break;
case 'COMPONENT':
penpotNode = await transformComponentNode(node);
penpotNode = transformComponentNode(node);
break;
case 'INSTANCE':
penpotNode = await transformInstanceNode(node);

View file

@ -1,8 +1,8 @@
import { remoteComponentLibrary } from '@plugin/RemoteComponentLibrary';
import { transformGroupNodeLike, transformSceneNode } from '@plugin/transformers';
import { transformMaskFigmaIds } from '@plugin/transformers/partials';
import { sleep } from '@plugin/utils';
import { GroupShape } from '@ui/lib/types/shapes/groupShape';
import { PenpotNode } from '@ui/types';
/**
@ -14,14 +14,14 @@ import { PenpotNode } from '@ui/types';
*
* @maskIndex The index of the mask node in the children array
*/
export const translateMaskChildren = async (
export const translateMaskChildren = (
children: readonly SceneNode[],
maskIndex: number
): Promise<PenpotNode[]> => {
): Promise<PenpotNode | undefined>[] => {
const maskChild = children[maskIndex];
const unmaskedChildren = await translateChildren(children.slice(0, maskIndex));
const maskedChildren = await translateChildren(children.slice(maskIndex));
const unmaskedChildren = translateChildren(children.slice(0, maskIndex));
const maskedChildren = translateChildren(children.slice(maskIndex));
if (
maskChild.type === 'STICKY' ||
@ -38,32 +38,30 @@ export const translateMaskChildren = async (
return [...unmaskedChildren, ...maskedChildren];
}
const maskGroup = {
const maskGroup = Promise.resolve<GroupShape>({
...transformMaskFigmaIds(maskChild),
...transformGroupNodeLike(maskChild),
children: maskedChildren,
maskedGroup: true
};
});
return [...unmaskedChildren, maskGroup];
};
export const translateChildren = async (children: readonly SceneNode[]): Promise<PenpotNode[]> => {
const transformedChildren: PenpotNode[] = [];
export const translateChildren = (
children: readonly SceneNode[]
): Promise<PenpotNode | undefined>[] => {
const transformedChildren: Promise<PenpotNode | undefined>[] = [];
for (const child of children) {
const penpotNode = await transformSceneNode(child);
if (penpotNode) transformedChildren.push(penpotNode);
await sleep(0);
transformedChildren.push(transformSceneNode(child));
}
return transformedChildren;
};
export const translateRemoteChildren = async (): Promise<PenpotNode[]> => {
const transformedChildren: PenpotNode[] = [];
export const translateRemoteChildren = (): Promise<PenpotNode | undefined>[] => {
const transformedChildren: Promise<PenpotNode | undefined>[] = [];
let currentRemote = 1;
figma.ui.postMessage({
@ -79,16 +77,12 @@ export const translateRemoteChildren = async (): Promise<PenpotNode[]> => {
const child = remoteComponentLibrary.next();
const penpotNode = await transformSceneNode(child);
if (penpotNode) transformedChildren.push(penpotNode);
transformedChildren.push(transformSceneNode(child));
figma.ui.postMessage({
type: 'PROGRESS_PROCESSED_ITEMS',
data: currentRemote++
});
await sleep(0);
}
return transformedChildren;

View file

@ -1,3 +1,3 @@
import { PenpotNode } from '@ui/types';
export type Children = { children?: PenpotNode[] };
export type Children = { children: Promise<PenpotNode | undefined>[] | (PenpotNode | undefined)[] };