-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathindex.ts
More file actions
71 lines (58 loc) · 1.44 KB
/
Copy pathindex.ts
File metadata and controls
71 lines (58 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { reflectColorToFigmaColor } from "@design-sdk/figma-node-conversion";
import { Color } from "@reflect-ui/core";
// FIXME - this should be repplaced by reflect's `TextManifest` when fully constructed
interface FigmaRenderTextManifest {
name?: string;
text: string;
fontName?: FontName;
fontSize: number;
color: Color;
}
export async function renderText(
textManifest: FigmaRenderTextManifest
): Promise<TextNode> {
const text = figma.createText();
if (textManifest.fontName) {
text.fontName = textManifest.fontName;
} else {
// resolve fonts (required for changing text characters)
await resolvefonts(text);
}
text.characters = textManifest.text;
text.name = textManifest.name ?? "text";
// randomize font size
text.fontSize = textManifest.fontSize;
const textColor = reflectColorToFigmaColor(textManifest.color);
text.fills = [
{
type: "SOLID",
color: textColor,
},
];
return text;
}
async function resolvefonts(text: TextNode) {
await Promise.all(
text
.getRangeAllFontNames(0, text.characters.length)
.map(figma.loadFontAsync)
);
}
export async function replaceTextCharacters(
text: TextNode,
{
characters,
name,
}: {
name?: string;
characters: string;
}
) {
// resolve fonts (required for changing text characters)
await resolvefonts(text);
text.characters = characters;
if (name) {
text.name = name;
}
return text;
}