0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2025-03-05 20:31:26 -05:00
penpot-exporter-figma-plugin/plugin-src/translators/text/paragraph/translateParagraphProperties.ts
Jordi Sala Morales 342ab90e69
Translate line endings done with shift+Enter in Figma ()
* Translate line endings done with shift+Enter in Figma

* add changelog
2024-06-13 09:10:11 +02:00

66 lines
1.5 KiB
TypeScript

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.replace(/\u2028/g, '\n')
}));
};
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;
};