-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* paragraph space and paragraph indent fixes fixes fine tuning fine tuning fine tuning fine tuning fixes * wip * fix * paragraph space and paragraph indent * changeset * Delete .changeset/new-walls-sort.md --------- Co-authored-by: Jordi Sala Morales <jordism91@gmail.com>
- Loading branch information
1 parent
58f7b0a
commit 4ded73e
Showing
5 changed files
with
150 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"penpot-exporter": minor | ||
--- | ||
|
||
Paragraph spacing and indent support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
plugin-src/translators/text/translateParagraphProperties.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { TextNode as PenpotTextNode } from '@ui/lib/types/text/textContent'; | ||
|
||
export const translateParagraphProperties = ( | ||
node: TextNode, | ||
segments: PenpotTextNode[] | ||
): PenpotTextNode[] => { | ||
if (node.paragraphSpacing === 0 && node.paragraphIndent === 0) return segments; | ||
|
||
const splitSegments: PenpotTextNode[] = [segmentIndent(node.paragraphIndent)]; | ||
|
||
segments.forEach(segment => { | ||
splitSegments.push(...splitTextNodeByEOL(segment)); | ||
}); | ||
|
||
return addParagraphProperties(splitSegments, node.paragraphIndent, node.paragraphSpacing); | ||
}; | ||
|
||
const splitTextNodeByEOL = (node: PenpotTextNode): PenpotTextNode[] => { | ||
const split = node.text.split(/(\n)/).filter(text => text !== ''); | ||
|
||
return split.map(text => ({ | ||
...node, | ||
text: text | ||
})); | ||
}; | ||
|
||
const addParagraphProperties = ( | ||
nodes: PenpotTextNode[], | ||
indent: number, | ||
paragraphSpacing: number | ||
): PenpotTextNode[] => { | ||
const indentedTextNodes: PenpotTextNode[] = []; | ||
|
||
nodes.forEach(node => { | ||
indentedTextNodes.push(node); | ||
|
||
if (node.text !== '\n') return; | ||
|
||
if (paragraphSpacing !== 0) { | ||
indentedTextNodes.push(segmentParagraphSpacing(paragraphSpacing)); | ||
} | ||
|
||
if (indent !== 0) { | ||
indentedTextNodes.push(segmentIndent(indent)); | ||
} | ||
}); | ||
|
||
return indentedTextNodes; | ||
}; | ||
|
||
const segmentIndent = (indent: number): PenpotTextNode => { | ||
return { | ||
text: ' '.repeat(indent), | ||
fontId: 'sourcesanspro', | ||
fontVariantId: 'regular', | ||
fontSize: '5', | ||
fontStyle: 'normal', | ||
fontWeight: '400', | ||
lineHeight: 1, | ||
letterSpacing: 0 | ||
}; | ||
}; | ||
|
||
const segmentParagraphSpacing = (paragraphSpacing: number): PenpotTextNode => { | ||
return { | ||
text: '\n', | ||
fontId: 'sourcesanspro', | ||
fontVariantId: 'regular', | ||
fontSize: paragraphSpacing.toString(), | ||
fontStyle: 'normal', | ||
fontWeight: '400', | ||
lineHeight: 1, | ||
letterSpacing: 0 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { translateFills } from '@plugin/translators'; | ||
import { | ||
translateFontId, | ||
translateFontStyle, | ||
translateHorizontalAlign, | ||
translateLetterSpacing, | ||
translateLineHeight, | ||
translateParagraphProperties, | ||
translateTextDecoration, | ||
translateTextTransform | ||
} from '@plugin/translators/text'; | ||
|
||
import { TextNode as PenpotTextNode, TextStyle } from '@ui/lib/types/text/textContent'; | ||
|
||
type StyleTextSegment = Pick< | ||
StyledTextSegment, | ||
| 'characters' | ||
| 'start' | ||
| 'end' | ||
| 'fontName' | ||
| 'fontSize' | ||
| 'fontWeight' | ||
| 'lineHeight' | ||
| 'letterSpacing' | ||
| 'textCase' | ||
| 'textDecoration' | ||
| 'fills' | ||
>; | ||
|
||
export const translateStyleTextSegments = ( | ||
node: TextNode, | ||
segments: StyleTextSegment[] | ||
): PenpotTextNode[] => { | ||
const textNodes = segments.map(segment => { | ||
return translateStyleTextSegment(node, segment); | ||
}); | ||
|
||
return translateParagraphProperties(node, textNodes); | ||
}; | ||
|
||
export const transformTextStyle = ( | ||
node: TextNode, | ||
segment: StyleTextSegment | ||
): Partial<TextStyle> => { | ||
return { | ||
...translateFontId(segment.fontName, segment.fontWeight), | ||
fontFamily: segment.fontName.family, | ||
fontSize: segment.fontSize.toString(), | ||
fontStyle: translateFontStyle(segment.fontName.style), | ||
fontWeight: segment.fontWeight.toString(), | ||
textAlign: translateHorizontalAlign(node.textAlignHorizontal), | ||
textDecoration: translateTextDecoration(segment), | ||
textTransform: translateTextTransform(segment), | ||
letterSpacing: translateLetterSpacing(segment), | ||
lineHeight: translateLineHeight(segment) | ||
}; | ||
}; | ||
|
||
const translateStyleTextSegment = (node: TextNode, segment: StyleTextSegment): PenpotTextNode => { | ||
return { | ||
fills: translateFills(segment.fills, node.width, node.height), | ||
text: segment.characters, | ||
...transformTextStyle(node, segment) | ||
}; | ||
}; |