-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ts
112 lines (88 loc) · 3.82 KB
/
code.ts
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
figma.showUI(__html__, { themeColors: true, height: 300, width: 300 });
async function runStyles() {
const colorCollection = figma.variables.createVariableCollection("colors");
const localColorStyles = await figma.getLocalPaintStylesAsync();
for (let style of localColorStyles) {
if (style.paints[0].type === "SOLID") {
let variable = figma.variables.createVariable(
style.name.toLowerCase(),
colorCollection.id,
"COLOR"
);
variable.setValueForMode(
colorCollection.defaultModeId,
style.paints[0].color
);
}
}
figma.notify("Styles have been converted successfully! Collection name: 'colors' ");
}
async function runTextStyles() {
const localTextStyles = await figma.getLocalTextStylesAsync();
const textCollection = figma.variables.createVariableCollection("textStyles");
const modeId = textCollection.modes[0].modeId;
localTextStyles.forEach(style => {
if (style.fontName && style.fontName.family && style.fontName.family.trim() !== '') {
createVariable(`${style.name}/fontFamily`, textCollection, modeId, style.fontName.family, 'STRING');
}
if (style.fontName && style.fontName.style && style.fontName.style.trim() !== '') {
createVariable(`${style.name}/fontStyle`, textCollection, modeId, style.fontName.style, 'STRING');
}
const fontWeight = parseFontWeight(style.fontName.style);
if (fontWeight !== undefined) {
createVariable(`${style.name}/fontWeight`, textCollection, modeId, fontWeight, 'FLOAT');
}
if (typeof style.fontSize === 'number') {
createVariable(`${style.name}/fontSize`, textCollection, modeId, style.fontSize, 'FLOAT');
}
if (typeof style.paragraphSpacing === 'number') {
createVariable(`${style.name}/paragraphSpacing`, textCollection, modeId, style.paragraphSpacing, 'FLOAT');
}
if (style.letterSpacing && !isNaN(style.letterSpacing.value)) {
createVariable(`${style.name}/letterSpacing`, textCollection, modeId, style.letterSpacing.value, 'FLOAT');
}
if (style.lineHeight && typeof style.lineHeight === 'object') {
let lineHeightValue: number;
if ('value' in style.lineHeight && style.lineHeight.unit !== 'AUTO') {
if (style.lineHeight.unit === 'PERCENT') {
lineHeightValue = style.fontSize * (style.lineHeight.value / 100);
} else {
lineHeightValue = style.lineHeight.value;
}
} else {
lineHeightValue = style.fontSize;
}
createVariable(`${style.name}/lineHeight`, textCollection, modeId, lineHeightValue, 'FLOAT');
}
});
console.log("Variables created for each text style property in the 'textStyles' collection.");
figma.notify("Text styles have been converted successfully! Collection name: 'textStyles' ");
}
function createVariable(name: any, collection: any, modeId: any, value: any, type: any) {
const resolvedType = type;
const variable = figma.variables.createVariable(name, collection, resolvedType);
variable.setValueForMode(modeId, value);
return variable;
}
type FontWeightStyle = "Thin" | "ExtraLight" | "Light" | "Regular" | "Medium" | "SemiBold" | "Bold" | "ExtraBold" | "Black";
function parseFontWeight(fontStyle: FontWeightStyle): number | undefined {
const weightMap: { [key in FontWeightStyle]: number } = {
"Thin": 100,
"ExtraLight": 200,
"Light": 300,
"Regular": 400,
"Medium": 500,
"SemiBold": 600,
"Bold": 700,
"ExtraBold": 800,
"Black": 900
};
return weightMap[fontStyle] || undefined;
}
figma.ui.onmessage = (message) => {
if (message.type === "run-styles") {
runStyles();
} else if (message.type === 'run-text-styles') {
runTextStyles();
}
};