Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Masked Groups #114

Merged
merged 16 commits into from
May 27, 2024
5 changes: 5 additions & 0 deletions .changeset/dull-donkeys-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"penpot-exporter": minor
---

Added support for masked groups
16 changes: 11 additions & 5 deletions plugin-src/transformers/partials/transformChildren.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { transformSceneNode } from '@plugin/transformers';
import { translateMaskChildren, translateNonMaskChildren } from '@plugin/translators';

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

const nodeActsAsMask = (node: SceneNode): boolean => {
return 'isMask' in node && node.isMask;
};

export const transformChildren = async (
node: ChildrenMixin,
baseX: number = 0,
baseY: number = 0
): Promise<Children> => {
const maskIndex = node.children.findIndex(nodeActsAsMask);
const containsMask = maskIndex !== -1;

return {
children: (
await Promise.all(node.children.map(child => transformSceneNode(child, baseX, baseY)))
).filter((child): child is PenpotNode => !!child)
children: containsMask
? await translateMaskChildren(node.children, maskIndex, baseX, baseY)
: await translateNonMaskChildren(node.children, baseX, baseY)
};
};
1 change: 1 addition & 0 deletions plugin-src/translators/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './translateBlendMode';
export * from './translateBlurEffects';
export * from './translateBoolType';
export * from './translateChildren';
export * from './translateShadowEffects';
export * from './translateStrokes';
45 changes: 45 additions & 0 deletions plugin-src/translators/translateChildren.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { transformGroupNodeLike, transformSceneNode } from '@plugin/transformers';

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

/**
* 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.
*
* @maskedIndex The index of the mask node in the children array
*/
export const translateMaskChildren = async (
children: readonly SceneNode[],
maskedIndex: number,
baseX: number,
baseY: number
): Promise<PenpotNode[]> => {
const maskChild = children[maskedIndex];
const unmaskedChildren = await translateNonMaskChildren(
children.slice(0, maskedIndex),
baseX,
baseY
);
const maskedChildren = await translateNonMaskChildren(children.slice(maskedIndex), baseX, baseY);

const maskGroup = {
...transformGroupNodeLike(maskChild, baseX, baseY),
children: maskedChildren,
maskedGroup: true
};

return [...unmaskedChildren, maskGroup];
};

export const translateNonMaskChildren = async (
children: readonly SceneNode[],
baseX: number,
baseY: number
): Promise<PenpotNode[]> => {
return (await Promise.all(children.map(child => transformSceneNode(child, baseX, baseY)))).filter(
(child): child is PenpotNode => !!child
);
};