0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2025-01-06 23:00:55 -05:00
penpot-exporter-figma-plugin/plugin-src/translators/text/paragraph/translateParagraphProperties.ts

67 lines
1.5 KiB
TypeScript
Raw Normal View History

import { TextNode as PenpotTextNode } from '@ui/lib/types/shapes/textShape';
import { Paragraph } from './Paragraph';
export type StyleTextSegment = Pick<
StyledTextSegment,
| 'characters'
| 'start'
| 'end'
| 'fontName'
| 'fontSize'
| 'fontWeight'
| 'lineHeight'
| 'letterSpacing'
| 'textCase'
| 'textDecoration'
| 'indentation'
| 'listOptions'
| 'fills'
>;
type PartialTranslation = {
textNodes: PenpotTextNode[];
segment: StyleTextSegment;
};
export const translateParagraphProperties = (
node: TextNode,
partials: { textNode: PenpotTextNode; segment: StyleTextSegment }[]
): PenpotTextNode[] => {
const splitSegments: PartialTranslation[] = [];
partials.forEach(({ textNode, segment }) => {
splitSegments.push({
textNodes: splitTextNodeByEOL(textNode),
segment
});
});
return addParagraphProperties(node, splitSegments);
};
const splitTextNodeByEOL = (node: PenpotTextNode): PenpotTextNode[] => {
const split = node.text.split(/(\n)/).filter(text => text !== '');
return split.map(text => ({
...node,
text: text
}));
};
const addParagraphProperties = (
node: TextNode,
partials: PartialTranslation[]
): PenpotTextNode[] => {
const formattedParagraphs: PenpotTextNode[] = [];
const paragraph = new Paragraph();
partials.forEach(({ textNodes, segment }) =>
textNodes.forEach(textNode => {
formattedParagraphs.push(...paragraph.format(node, textNode, segment));
})
);
return formattedParagraphs;
};