-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ts
264 lines (250 loc) · 7.39 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Figma Plugin APIの初期化
figma.showUI(__html__, {
width: 320,
height: 480,
themeColors: true,
title: "Local Variables Starter",
});
// デザイントークンの定義
const tokens: DesignTokens = {
colors: {
primitive: {
blue: {
1: { r: 0.94, g: 0.97, b: 1.0 }, // #f0f7ff
2: { r: 0.88, g: 0.94, b: 1.0 }, // #e1f0ff
3: { r: 0.82, g: 0.91, b: 1.0 }, // #d1e8ff
4: { r: 0.71, g: 0.85, b: 1.0 }, // #b5d9ff
5: { r: 0.55, g: 0.77, b: 0.99 }, // #8dc4fc
6: { r: 0.39, g: 0.69, b: 0.99 }, // #63b0fc
7: { r: 0.24, g: 0.59, b: 0.99 }, // #3d96fc
8: { r: 0.08, g: 0.49, b: 0.98 }, // #147dfa
9: { r: 0.04, g: 0.41, b: 0.87 }, // #0a68dd
10: { r: 0.03, g: 0.33, b: 0.73 }, // #0854ba
11: { r: 0.02, g: 0.25, b: 0.58 }, // #054094
12: { r: 0.01, g: 0.17, b: 0.43 }, // #032b6e
},
green: {
1: { r: 0.94, g: 0.99, b: 0.95 }, // #f0fcf2
2: { r: 0.87, g: 0.97, b: 0.89 }, // #def8e3
3: { r: 0.8, g: 0.95, b: 0.83 }, // #ccf2d4
4: { r: 0.69, g: 0.92, b: 0.74 }, // #b0ebbe
5: { r: 0.53, g: 0.87, b: 0.6 }, // #87dd99
6: { r: 0.37, g: 0.82, b: 0.46 }, // #5ed175
7: { r: 0.22, g: 0.75, b: 0.33 }, // #38bf54
8: { r: 0.06, g: 0.67, b: 0.2 }, // #0fab33
9: { r: 0.05, g: 0.58, b: 0.17 }, // #0d942b
10: { r: 0.04, g: 0.48, b: 0.14 }, // #0a7a24
11: { r: 0.03, g: 0.39, b: 0.11 }, // #08631c
12: { r: 0.02, g: 0.29, b: 0.08 }, // #044a15
},
red: {
1: { r: 1.0, g: 0.95, b: 0.95 }, // #fff2f2
2: { r: 1.0, g: 0.89, b: 0.89 }, // #ffe3e3
3: { r: 1.0, g: 0.82, b: 0.82 }, // #ffd1d1
4: { r: 1.0, g: 0.71, b: 0.71 }, // #ffb5b5
5: { r: 0.99, g: 0.55, b: 0.55 }, // #fc8d8d
6: { r: 0.99, g: 0.39, b: 0.39 }, // #fc6363
7: { r: 0.99, g: 0.24, b: 0.24 }, // #fc3d3d
8: { r: 0.98, g: 0.08, b: 0.08 }, // #fa1414
9: { r: 0.87, g: 0.04, b: 0.04 }, // #dd0a0a
10: { r: 0.73, g: 0.03, b: 0.03 }, // #ba0808
11: { r: 0.58, g: 0.02, b: 0.02 }, // #940505
12: { r: 0.43, g: 0.01, b: 0.01 }, // #6e0303
},
yellow: {
1: { r: 1.0, g: 0.99, b: 0.94 }, // #fffcf0
2: { r: 1.0, g: 0.97, b: 0.87 }, // #fff8de
3: { r: 1.0, g: 0.95, b: 0.8 }, // #fff3cc
4: { r: 1.0, g: 0.92, b: 0.69 }, // #ffebb0
5: { r: 0.99, g: 0.87, b: 0.53 }, // #fcdd87
6: { r: 0.99, g: 0.82, b: 0.37 }, // #fcd15e
7: { r: 0.99, g: 0.75, b: 0.22 }, // #fcbf38
8: { r: 0.98, g: 0.67, b: 0.06 }, // #faab0f
9: { r: 0.87, g: 0.58, b: 0.04 }, // #dd940a
10: { r: 0.73, g: 0.48, b: 0.03 }, // #ba7a08
11: { r: 0.58, g: 0.39, b: 0.02 }, // #946305
12: { r: 0.43, g: 0.29, b: 0.01 }, // #6e4a03
},
orange: {
1: { r: 1.0, g: 0.97, b: 0.94 }, // #fff7f0
2: { r: 1.0, g: 0.94, b: 0.87 }, // #ffede0
3: { r: 1.0, g: 0.91, b: 0.8 }, // #ffe8cc
4: { r: 1.0, g: 0.85, b: 0.69 }, // #ffd9b0
5: { r: 0.99, g: 0.77, b: 0.53 }, // #fcc487
6: { r: 0.99, g: 0.69, b: 0.37 }, // #fcaf5e
7: { r: 0.99, g: 0.59, b: 0.22 }, // #fc9638
8: { r: 0.98, g: 0.49, b: 0.06 }, // #fa7d0f
9: { r: 0.87, g: 0.41, b: 0.04 }, // #dd690a
10: { r: 0.73, g: 0.33, b: 0.03 }, // #ba5508
11: { r: 0.58, g: 0.25, b: 0.02 }, // #944105
12: { r: 0.43, g: 0.17, b: 0.01 }, // #6e2f03
},
},
},
spacing: {
none: 0,
px: 1,
"4xs": 2,
"3xs": 4,
"2xs": 6,
xs: 8,
sm: 10,
md: 12,
base: 16,
lg: 20,
xl: 24,
"2xl": 32,
"3xl": 40,
"4xl": 48,
"5xl": 64,
"6xl": 80,
negative: {
px: -1,
"4xs": -2,
"3xs": -4,
"2xs": -6,
xs: -8,
sm: -10,
md: -12,
base: -16,
},
},
typography: {
size: {
xs: 12,
sm: 14,
base: 16,
lg: 18,
xl: 20,
"2xl": 24,
"3xl": 28,
"4xl": 32,
"5xl": 40,
"6xl": 48,
},
weight: {
normal: 400,
bold: 700,
},
lineHeight: {
none: "100%",
tight: "125%",
normal: "150%",
relaxed: "175%",
loose: "200%",
},
},
radius: {
none: 0,
sm: 2,
base: 4,
md: 6,
lg: 8,
xl: 12,
full: 9999,
},
};
// Local Variablesの作成関数
async function createVariableCollection(name: string) {
const collections = await figma.variables.getLocalVariableCollectionsAsync();
const existingCollection = collections.find((c) => c.name === name);
if (existingCollection) {
return existingCollection;
}
return await figma.variables.createVariableCollection(name);
}
// 変数の作成関数
async function createVariable(
collection: VariableCollection,
name: string,
type: VariableResolvedDataType,
value: any
) {
const variable = await figma.variables.createVariable(name, collection, type);
await variable.setValueForMode(collection.defaultModeId, value);
return variable;
}
interface InitMessage {
type: "init-variables";
categories: {
colors: boolean;
spacing: boolean;
typography: boolean;
radius: boolean;
};
modeName: string;
collectionName: string;
}
async function initializeVariables(
categories: InitMessage["categories"],
modeName: string,
collectionName: string
) {
const collection = await createVariableCollection(collectionName);
collection.renameMode(collection.defaultModeId, modeName);
// Colors
if (categories.colors) {
for (const [colorName, colorGroup] of Object.entries(
tokens.colors.primitive
)) {
for (const [shade, value] of Object.entries(
colorGroup as Record<string, { r: number; g: number; b: number }>
)) {
const variableName = `color/${colorName}/${shade}`;
await createVariable(collection, variableName, "COLOR", {
r: value.r,
g: value.g,
b: value.b,
});
}
}
}
// Spacing
if (categories.spacing) {
for (const [key, value] of Object.entries(tokens.spacing)) {
if (key !== "negative" && typeof value === "number") {
await createVariable(collection, `spacing/${key}`, "FLOAT", value);
}
}
// Negative spacing
for (const [key, value] of Object.entries(tokens.spacing.negative)) {
await createVariable(
collection,
`spacing/negative/${key}`,
"FLOAT",
value
);
}
}
// Typography
if (categories.typography) {
for (const [category, values] of Object.entries(tokens.typography)) {
for (const [key, value] of Object.entries(values)) {
await createVariable(
collection,
`typography/${category}/${key}`,
"FLOAT",
typeof value === "string" ? parseFloat(value) : value
);
}
}
}
// Radius
if (categories.radius) {
for (const [key, value] of Object.entries(tokens.radius)) {
await createVariable(collection, `radius/${key}`, "FLOAT", value);
}
}
}
// UIからのメッセージ処理
figma.ui.onmessage = async (msg: InitMessage) => {
if (msg.type === "init-variables") {
try {
await initializeVariables(msg.categories, msg.modeName, msg.collectionName);
figma.notify("Local Variables created successfully! 🎉");
} catch (error) {
figma.notify("Error creating variables", { error: true });
console.error(error);
}
}
};