0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 21:53:27 -05:00
penpot-exporter-figma-plugin/plugin-src/translators/text/paragraph/translateParagraphProperties.ts
Jordi Sala Morales 2920ac297b
Implement unordered and ordered lists (#88)
* wip

* implement bullet points

* needs more ifs

* refactor

* revert

* refactor

* fix and refactor

* refactor

* refactor

* refactor

* add ordered class

* wip

* wip

* fixes

* package.json

* little refactors

* base list

* fixes

* abstract baselist

* refactors

* refactor

* changeset

* fix eslint issue

* fix

* fixes

* fixes

---------

Co-authored-by: Alex Sánchez <sion333@gmail.com>
2024-05-07 12:18:15 +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
}));
};
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;
};