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; 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 maskIndex = node.children.findIndex(nodeActsAsMask);
const containsMask = maskIndex !== -1; const containsMask = maskIndex !== -1;
return { return {
children: containsMask children: containsMask
? await translateMaskChildren(node.children, maskIndex) ? translateMaskChildren(node.children, maskIndex)
: await translateChildren(node.children) : translateChildren(node.children)
}; };
}; };

View file

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

View file

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

View file

@ -78,7 +78,7 @@ export const transformDocumentNode = async (node: DocumentNode): Promise<PenpotD
if (remoteComponentLibrary.remaining() > 0) { if (remoteComponentLibrary.remaining() > 0) {
children.push({ children.push({
name: 'External Components', 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'; return node.type === 'SECTION';
}; };
export const transformFrameNode = async ( export const transformFrameNode = (
node: FrameNode | SectionNode | ComponentSetNode node: FrameNode | SectionNode | ComponentSetNode
): Promise<FrameShape> => { ): FrameShape => {
let frameSpecificAttributes: Partial<FrameShape> = {}; let frameSpecificAttributes: Partial<FrameShape> = {};
let referencePoint: Point = { x: node.absoluteTransform[0][2], y: node.absoluteTransform[1][2] }; let referencePoint: Point = { x: node.absoluteTransform[0][2], y: node.absoluteTransform[1][2] };
@ -60,7 +60,7 @@ export const transformFrameNode = async (
...referencePoint, ...referencePoint,
...frameSpecificAttributes, ...frameSpecificAttributes,
...transformDimension(node), ...transformDimension(node),
...(await transformChildren(node)), ...transformChildren(node),
...transformSceneNode(node), ...transformSceneNode(node),
...transformOverrides(node) ...transformOverrides(node)
}; };

View file

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

View file

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

View file

@ -9,6 +9,6 @@ export const transformPageNode = async (node: PageNode): Promise<PenpotPage> =>
options: { options: {
background: node.backgrounds.length ? translatePageFill(node.backgrounds[0]) : undefined 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); penpotNode = await transformBooleanNode(node);
break; break;
case 'COMPONENT': case 'COMPONENT':
penpotNode = await transformComponentNode(node); penpotNode = transformComponentNode(node);
break; break;
case 'INSTANCE': case 'INSTANCE':
penpotNode = await transformInstanceNode(node); penpotNode = await transformInstanceNode(node);

View file

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

View file

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