Skip to content

Commit

Permalink
structure for fills in text and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Cenadros committed Apr 17, 2024
1 parent 8cd5618 commit ed921d4
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 34 deletions.
1 change: 1 addition & 0 deletions plugin-src/transformers/partials/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from './transformDimensionAndPosition';
export * from './transformFills';
export * from './transformSceneNode';
export * from './transformStrokes';
export * from './transformTextStyle';
export * from './transformVectorPaths';
16 changes: 16 additions & 0 deletions plugin-src/transformers/partials/transformTextStyle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { translateTextDecoration, translateTextTransform } from '@plugin/translators';

import { Paragraph, TextNode } from '@ui/lib/types/text/textContent';

export const transformTextStyle = (
node: StyledTextSegment
): Partial<Paragraph> | Partial<TextNode> => {
return {
fontFamily: node.fontName.family,
fontSize: node.fontSize.toString(),
fontStyle: node.fontName.style,
fontWeight: node.fontWeight.toString(),
textDecoration: translateTextDecoration(node),
textTransform: translateTextTransform(node)
};
};
40 changes: 8 additions & 32 deletions plugin-src/transformers/transformTextNode.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import {
transformBlend,
transformDimensionAndPosition,
transformSceneNode
transformFills,
transformSceneNode,
transformTextStyle
} from '@plugin/transformers/partials';
import {
translateFills,
translateTextDecoration,
translateTextTransform
} from '@plugin/translators';
import { translateStyledTextSegments } from '@plugin/translators';

import { TextNode as PenpotTextNode } from '@ui/lib/types/text/textContent';
import { TextShape } from '@ui/lib/types/text/textShape';

export const transformTextNode = (node: TextNode, baseX: number, baseY: number): TextShape => {
Expand All @@ -22,23 +19,7 @@ export const transformTextNode = (node: TextNode, baseX: number, baseY: number):
'textCase',
'textDecoration',
'fills'
]);

const children: PenpotTextNode[] = styledTextSegments.map(segment => {
figma.ui.postMessage({ type: 'FONT_NAME', data: segment.fontName.family });

return {
text: segment.characters,
fills: translateFills(segment.fills, node.width, node.height),
fontFamily: segment.fontName.family,
fontSize: segment.fontSize.toString(),
fontStyle: segment.fontName.style,
fontWeight: segment.fontWeight.toString(),
textDecoration: translateTextDecoration(segment),
textTransform: translateTextTransform(segment)
};
});

]) as StyledTextSegment[];
return {
type: 'text',
name: node.name,
Expand All @@ -50,14 +31,9 @@ export const transformTextNode = (node: TextNode, baseX: number, baseY: number):
children: [
{
type: 'paragraph',
fills: translateFills(node.fills, node.width, node.height),
fontFamily: children[0].fontFamily,
fontSize: children[0].fontSize,
fontStyle: children[0].fontStyle,
fontWeight: children[0].fontWeight,
textDecoration: children[0].textDecoration,
textTransform: children[0].textTransform,
children: children
children: translateStyledTextSegments(styledTextSegments, node.width, node.height),
...(styledTextSegments.length ? transformTextStyle(styledTextSegments[0]) : {}),
...transformFills(node)
}
]
}
Expand Down
1 change: 1 addition & 0 deletions plugin-src/translators/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './translateBlendMode';
export * from './translateFills';
export * from './translateStrokes';
export * from './translateStyledTextSegments';
export * from './translateTextDecoration';
export * from './translateTextTransform';
export * from './translateVectorPaths';
5 changes: 3 additions & 2 deletions plugin-src/translators/translateFills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ export const translateFills = (
height: number
): Fill[] => {
const figmaFills = fills === figma.mixed ? [] : fills;
let penpotFills: Fill[] = [];
const penpotFills: Fill[] = [];

for (const fill of figmaFills) {
const penpotFill = translateFill(fill, width, height);
if (penpotFill) {
penpotFills = [penpotFill, ...penpotFills];
// colors are applied in reverse order in Figma, that's why we unshift
penpotFills.unshift(penpotFill);
}
}

Expand Down
20 changes: 20 additions & 0 deletions plugin-src/translators/translateStyledTextSegments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { transformTextStyle } from '@plugin/transformers/partials';
import { translateFills } from '@plugin/translators/translateFills';

import { TextNode } from '@ui/lib/types/text/textContent';

export const translateStyledTextSegments = (
segments: StyledTextSegment[],
width: number,
height: number
): TextNode[] => {
return segments.map(segment => {
figma.ui.postMessage({ type: 'FONT_NAME', data: segment.fontName.family });

return {
fills: translateFills(segment.fills, width, height),
text: segment.characters,
...transformTextStyle(segment)
};
});
};

0 comments on commit ed921d4

Please sign in to comment.