-
Notifications
You must be signed in to change notification settings - Fork 10
/
import.html
263 lines (235 loc) · 6.2 KB
/
import.html
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
<style>
:root {
--spacing: 0.8rem;
}
* {
box-sizing: border-box;
}
body {
background-color: var(--figma-color-bg);
color: var(--figma-color-text);
margin: 0;
padding: var(--spacing);
}
html,
body {
height: 100%;
}
form {
display: flex;
flex-direction: column;
gap: var(--spacing);
height: 100%;
}
button {
appearance: none;
border-radius: 4px;
padding: var(--spacing);
}
select,
input,
textarea {
font-family: Andale Mono, monospace;
font-size: 0.9rem;
padding: var(--spacing);
}
textarea {
flex: 1;
white-space: pre;
}
form>* {
display: block;
width: 100%;
}
button {
background-color: var(--figma-color-bg-brand);
border: none;
color: var(--figma-color-text-onbrand);
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue",
sans-serif;
font-weight: bold;
}
select,
input,
textarea {
background-color: var(--figma-color-bg-secondary);
color: var(--figma-color-text-secondary);
border: 2px solid var(--figma-color-border);
}
input:focus,
textarea:focus {
border-color: var(--figma-color-border-selected);
outline: none;
}
select,
input {
flex: 1;
}
form .row {
display: flex;
gap: 0.5rem;
}
</style>
<form>
<div class="row">
<select id="collectionSelect"></select>
<input placeholder="Collection Name" required type="text" id="collectionInput" />
</div>
<div class="row">
<select id="modeSelect"></select>
<input placeholder="Mode Name" required type="text" id="modeInput" />
</div>
<textarea required placeholder="Tokens JSON">
{
"color": {
"grouptyped": {
"$type": "color",
"brown": { "$value": "#a2845e" },
"danger-deep": { "$value": "{color.valuetyped.danger}" }
},
"valuetyped": {
"red": {
"$value": "{color.deep.deep.deep.deep.deep}"
},
"danger": { "$value": "{color.valuetyped.red}" }
},
"deep": {"deep": {"deep": {"deep": {"deep": {"$type": "color", "$value": "#FF0000" }}}}}
},
"spacing": {
"$type": "number",
"some numbers": {
"spacer0": {"$value": 0},
"spacerXs": {"$value": 4},
"spacerS": {"$value": 8},
"spacerM": {"$value": 16},
"spacerX": {"$value": 24},
"spacerXl": {"$value": 32},
"spacerXxl": {"$value": 40},
"spacex": {
"funniness": {"$value": 0},
"cleverness": {"$value": 1}
}
}
}
}
</textarea>
<button type="submit">Import Variables</button>
</form>
<script>
const NEW_VALUE = "new";
let collections = {};
const collectionSelect = document.getElementById("collectionSelect");
const collectionInput = document.getElementById("collectionInput");
const modeSelect = document.getElementById("modeSelect");
const modeInput = document.getElementById("modeInput");
function renderOptions(select, newName, options = []) {
options = [{ name: `+ New ${newName}`, value: NEW_VALUE }, ...options];
select.innerHTML = options
.map(
({ name, value = name }) => `<option value="${value}">${name}</option>`
)
.join("");
select.value = NEW_VALUE;
}
window.onmessage = (event) => {
let message = event.data.pluginMessage;
let collectionOptions = [];
if (message.type === "LOAD_COLLECTIONS") {
collections = {};
const options = [];
Object.entries(message.collections).forEach(([name, collection]) => {
collections[collection.id] = collection;
options.push({
name,
value: collection.id,
});
});
renderOptions(collectionSelect, "Collection", options);
handleCollectionChange();
}
collectionSelect.append(...collectionOptions);
};
function hideInputElement(inputElement, value) {
inputElement.style.display = "none";
// handle the case if it's an existing mode
inputElement.value = value;
// disable input
inputElement.disabled = true;
}
function showInputElement(inputElement) {
inputElement.style.display = "block";
inputElement.disabled = false;
inputElement.value = "";
}
function handleCollectionChange() {
// handle the case if it's a new Collection
const currentCollection = collections[collectionSelect.value];
// enable input
if (collectionSelect.value === NEW_VALUE) {
showInputElement(collectionInput);
renderOptions(modeSelect, "Mode");
handleModeChange();
collectionInput.focus();
} else {
hideInputElement(collectionInput, collectionSelect.value);
renderOptions(
modeSelect,
"Mode",
currentCollection.modes.map(({ modeId, name }) => ({
name,
value: modeId,
}))
);
// set to default mode on collection change
modeSelect.value = currentCollection.defaultModeId || "new";
handleModeChange();
}
}
function handleModeChange() {
// enable input
if (modeSelect.value === NEW_VALUE) {
showInputElement(modeInput);
modeInput.focus();
} else {
hideInputElement(modeInput, modeSelect.value);
}
}
collectionSelect.addEventListener("change", handleCollectionChange);
modeSelect.addEventListener("change", handleModeChange);
document.querySelector("form").addEventListener("submit", (e) => {
const selectedCollection = {
name: collectionInput.value.trim(),
id: collectionSelect.value === NEW_VALUE ? null : collectionSelect.value,
};
const selectedMode = {
name: modeInput.value.trim(),
id: modeSelect.value === NEW_VALUE ? null : modeSelect.value,
};
const body = document.querySelector("textarea").value.trim();
e.preventDefault();
if (isValidJSON(body) && selectedCollection.name) {
parent.postMessage(
{
pluginMessage: {
selectedCollection,
selectedMode,
body,
type: "IMPORT",
},
},
"*"
);
} else {
alert("Invalid filename or JSON");
}
});
function isValidJSON(body) {
try {
JSON.parse(body);
return true;
} catch (e) {
return false;
}
}
</script>