0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2025-01-08 16:10:07 -05:00
penpot-exporter-figma-plugin/plugin-src/translators/text/paragraph/Paragraph.ts
Jordi Sala Morales b00370410c
Improve paragraph indent (#191)
* Additional layout properties (#190)

* additional layout properties

* changeset

* add changelog

---------

Co-authored-by: Alex Sánchez <sion333@gmail.com>
2024-06-26 10:07:56 +02:00

91 lines
2.5 KiB
TypeScript

import { TextNode as PenpotTextNode } from '@ui/lib/types/shapes/textShape';
import { List } from './List';
import { TextSegment } from './translateParagraphProperties';
export class Paragraph {
private isParagraphStarting = false;
private isPreviousNodeAList = false;
private firstTextNode: PenpotTextNode | null = null;
private list = new List();
public format(node: TextNode, textNode: PenpotTextNode, segment: TextSegment): PenpotTextNode[] {
const textNodes: PenpotTextNode[] = [];
const spacing = this.applySpacing(segment, node);
if (spacing) textNodes.push(spacing);
const indentation = this.applyIndentation(textNode, segment, node);
if (indentation) textNodes.push(indentation);
textNodes.push(textNode);
this.isPreviousNodeAList = segment.listOptions.type !== 'NONE';
this.isParagraphStarting = textNode.text === '\n';
return textNodes;
}
private applyIndentation(
textNode: PenpotTextNode,
segment: TextSegment,
node: TextNode
): PenpotTextNode | undefined {
if (this.isParagraphStarting || this.isFirstTextNode(textNode)) {
this.list.update(textNode, segment);
return segment.listOptions.type !== 'NONE'
? this.list.getCurrentList(textNode, segment)
: this.segmentIndent(node.paragraphIndent);
}
}
private applySpacing(segment: TextSegment, node: TextNode): PenpotTextNode | undefined {
if (this.isParagraphStarting) {
const isList = segment.listOptions.type !== 'NONE';
return this.segmentParagraphSpacing(
this.isPreviousNodeAList && isList ? node.listSpacing : node.paragraphSpacing
);
}
}
private isFirstTextNode(textNode: PenpotTextNode) {
if (this.firstTextNode === null) {
this.firstTextNode = textNode;
return true;
}
return false;
}
private segmentIndent(indent: number): PenpotTextNode | undefined {
if (indent === 0) return;
return {
text: ' ',
fontId: 'sourcesanspro',
fontVariantId: 'regular',
fontSize: '1',
fontStyle: 'normal',
fontWeight: '400',
lineHeight: '1',
letterSpacing: indent.toString()
};
}
private segmentParagraphSpacing(paragraphSpacing: number): PenpotTextNode | undefined {
if (paragraphSpacing === 0) return;
return {
text: '\n',
fontId: 'sourcesanspro',
fontVariantId: 'regular',
fontSize: paragraphSpacing.toString(),
fontStyle: 'normal',
fontWeight: '400',
lineHeight: '1',
letterSpacing: '0'
};
}
}