2024-05-03 06:23:07 -05:00
|
|
|
import { translateFills } from '@plugin/translators';
|
2024-05-07 05:18:15 -05:00
|
|
|
import { translateFontId } from '@plugin/translators/text/font';
|
|
|
|
import { StyleTextSegment, translateParagraphProperties } from '@plugin/translators/text/paragraph';
|
2024-05-03 06:23:07 -05:00
|
|
|
import {
|
|
|
|
translateFontStyle,
|
|
|
|
translateHorizontalAlign,
|
|
|
|
translateLetterSpacing,
|
|
|
|
translateLineHeight,
|
|
|
|
translateTextDecoration,
|
|
|
|
translateTextTransform
|
2024-05-07 05:18:15 -05:00
|
|
|
} from '@plugin/translators/text/properties';
|
2024-05-03 06:23:07 -05:00
|
|
|
|
2024-05-06 01:06:14 -05:00
|
|
|
import { TextNode as PenpotTextNode, TextStyle } from '@ui/lib/types/shapes/textShape';
|
2024-05-03 06:23:07 -05:00
|
|
|
|
2024-05-09 09:59:27 -05:00
|
|
|
export const translateStyleTextSegments = async (
|
2024-05-03 06:23:07 -05:00
|
|
|
node: TextNode,
|
|
|
|
segments: StyleTextSegment[]
|
2024-05-09 09:59:27 -05:00
|
|
|
): Promise<PenpotTextNode[]> => {
|
|
|
|
const partials = await Promise.all(
|
|
|
|
segments.map(async segment => ({
|
|
|
|
textNode: await translateStyleTextSegment(node, segment),
|
|
|
|
segment
|
|
|
|
}))
|
|
|
|
);
|
2024-05-03 06:23:07 -05:00
|
|
|
|
2024-05-07 05:18:15 -05:00
|
|
|
return translateParagraphProperties(node, partials);
|
2024-05-03 06:23:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-05-09 09:59:27 -05:00
|
|
|
const translateStyleTextSegment = async (
|
|
|
|
node: TextNode,
|
|
|
|
segment: StyleTextSegment
|
|
|
|
): Promise<PenpotTextNode> => {
|
2024-05-03 06:23:07 -05:00
|
|
|
return {
|
2024-05-09 09:59:27 -05:00
|
|
|
fills: await translateFills(segment.fills, node.width, node.height),
|
2024-05-03 06:23:07 -05:00
|
|
|
text: segment.characters,
|
|
|
|
...transformTextStyle(node, segment)
|
|
|
|
};
|
|
|
|
};
|