2024-06-06 02:37:35 -05:00
|
|
|
import { remoteComponentLibrary } from '@plugin/RemoteComponentLibrary';
|
2024-05-27 04:50:59 -05:00
|
|
|
import { transformGroupNodeLike, transformSceneNode } from '@plugin/transformers';
|
2024-06-14 01:15:49 -05:00
|
|
|
import { transformLayoutItemZIndex, transformMaskFigmaIds } from '@plugin/transformers/partials';
|
2024-05-31 04:25:32 -05:00
|
|
|
import { sleep } from '@plugin/utils';
|
2024-05-27 04:50:59 -05:00
|
|
|
|
2024-05-29 10:33:29 -05:00
|
|
|
import { PenpotNode } from '@ui/types';
|
2024-05-27 04:50:59 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates the children of a node that acts as a mask.
|
|
|
|
* We need to split the children into two groups: the ones that are masked and the ones that are not.
|
|
|
|
*
|
|
|
|
* The masked children will be grouped together in a mask group.
|
|
|
|
* The unmasked children will be returned as they are.
|
|
|
|
*
|
|
|
|
* @maskIndex The index of the mask node in the children array
|
|
|
|
*/
|
|
|
|
export const translateMaskChildren = async (
|
|
|
|
children: readonly SceneNode[],
|
|
|
|
maskIndex: number,
|
|
|
|
baseX: number,
|
2024-06-14 01:15:49 -05:00
|
|
|
baseY: number,
|
|
|
|
zIndex: number,
|
|
|
|
itemReverseZIndex: boolean = false
|
2024-05-27 04:50:59 -05:00
|
|
|
): Promise<PenpotNode[]> => {
|
|
|
|
const maskChild = children[maskIndex];
|
2024-06-14 01:15:49 -05:00
|
|
|
const unmaskedChildren = await translateChildren(
|
|
|
|
children.slice(0, maskIndex),
|
|
|
|
baseX,
|
|
|
|
baseY,
|
|
|
|
zIndex,
|
|
|
|
itemReverseZIndex
|
|
|
|
);
|
2024-05-27 04:50:59 -05:00
|
|
|
const maskedChildren = await translateChildren(children.slice(maskIndex), baseX, baseY);
|
|
|
|
|
2024-06-14 01:15:49 -05:00
|
|
|
const lastZIndex = itemReverseZIndex ? zIndex - unmaskedChildren.length - 1 : zIndex;
|
|
|
|
|
2024-05-27 04:50:59 -05:00
|
|
|
const maskGroup = {
|
2024-06-03 02:38:44 -05:00
|
|
|
...transformMaskFigmaIds(maskChild),
|
2024-06-14 01:15:49 -05:00
|
|
|
...transformGroupNodeLike(maskChild, baseX, baseY, zIndex),
|
|
|
|
...transformLayoutItemZIndex(lastZIndex),
|
2024-05-27 04:50:59 -05:00
|
|
|
children: maskedChildren,
|
|
|
|
maskedGroup: true
|
|
|
|
};
|
|
|
|
|
|
|
|
return [...unmaskedChildren, maskGroup];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const translateChildren = async (
|
|
|
|
children: readonly SceneNode[],
|
2024-05-31 04:25:32 -05:00
|
|
|
baseX: number = 0,
|
2024-06-14 01:15:49 -05:00
|
|
|
baseY: number = 0,
|
|
|
|
zIndex: number = 0,
|
|
|
|
itemReverseZIndex: boolean = false
|
2024-05-27 04:50:59 -05:00
|
|
|
): Promise<PenpotNode[]> => {
|
2024-05-31 04:25:32 -05:00
|
|
|
const transformedChildren: PenpotNode[] = [];
|
|
|
|
|
|
|
|
for (const child of children) {
|
2024-06-14 01:15:49 -05:00
|
|
|
const penpotNode = await transformSceneNode(
|
|
|
|
child,
|
|
|
|
baseX,
|
|
|
|
baseY,
|
|
|
|
itemReverseZIndex ? zIndex-- : zIndex
|
|
|
|
);
|
2024-05-31 04:25:32 -05:00
|
|
|
|
|
|
|
if (penpotNode) transformedChildren.push(penpotNode);
|
|
|
|
|
|
|
|
await sleep(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return transformedChildren;
|
2024-05-27 04:50:59 -05:00
|
|
|
};
|
2024-06-06 02:37:35 -05:00
|
|
|
|
|
|
|
export const translateRemoteChildren = async (): Promise<PenpotNode[]> => {
|
|
|
|
const transformedChildren: PenpotNode[] = [];
|
2024-06-06 10:24:59 -05:00
|
|
|
let currentRemote = 1;
|
|
|
|
|
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_STEP',
|
|
|
|
data: 'remote'
|
|
|
|
});
|
2024-06-06 02:37:35 -05:00
|
|
|
|
|
|
|
while (remoteComponentLibrary.remaining() > 0) {
|
2024-06-06 10:24:59 -05:00
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_TOTAL_ITEMS',
|
|
|
|
data: remoteComponentLibrary.total()
|
|
|
|
});
|
|
|
|
|
2024-06-06 02:37:35 -05:00
|
|
|
const child = remoteComponentLibrary.next();
|
|
|
|
|
|
|
|
const penpotNode = await transformSceneNode(child);
|
|
|
|
|
|
|
|
if (penpotNode) transformedChildren.push(penpotNode);
|
|
|
|
|
2024-06-06 10:24:59 -05:00
|
|
|
figma.ui.postMessage({
|
|
|
|
type: 'PROGRESS_PROCESSED_ITEMS',
|
|
|
|
data: currentRemote++
|
|
|
|
});
|
|
|
|
|
2024-06-06 02:37:35 -05:00
|
|
|
await sleep(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return transformedChildren;
|
|
|
|
};
|