-
Notifications
You must be signed in to change notification settings - Fork 44
/
app.js
274 lines (224 loc) · 8.3 KB
/
app.js
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
265
266
267
268
269
270
271
272
273
274
(function() {
'use strict';
var clipboard = new Clipboard('.clipboardButton');
getAllGoogleFonts();
simulateFout.addEventListener('change', fout);
downloadFont.addEventListener('change', download);
useColours.addEventListener('change', colour);
uploadFont.addEventListener('change', upload);
uploadFontBtn.addEventListener('click', uploadBtn);
document.addEventListener('dragover', function(event) { event.preventDefault(); });
document.addEventListener('dragenter', dragEnter);
document.addEventListener('dragleave', dragLeave);
document.addEventListener('drop', drop);
download();
fallback.style.fontFamily = fallbackOutput.style.fontFamily = fallbackName.value;
webfont.style.fontFamily = webfontOutput.style.fontFamily = webfontName.value;
fallback.style.fontSize = fallbackOutput.style.fontSize = '16px';
webfont.style.fontSize = webfontOutput.style.fontSize = '16px';
fallback.style.lineHeight = fallbackOutput.style.lineHeight = '1.6';
webfont.style.lineHeight = webfontOutput.style.lineHeight = '1.6';
updateClipboardButtons();
fallbackName.addEventListener('input', updateFontFamily);
webfontName.addEventListener('input', updateFontFamily);
fallbackSize.addEventListener('input', updateFontSize);
webfontSize.addEventListener('input', updateFontSize);
fallbackLineHeight.addEventListener('input', updateLineHeight);
webfontLineHeight.addEventListener('input', updateLineHeight);
fallbackSpacing.addEventListener('input', updateFontSpacing);
webfontSpacing.addEventListener('input', updateFontSpacing);
fallbackWordSpacing.addEventListener('input', updateFontWordSpacing);
webfontWordSpacing.addEventListener('input', updateFontWordSpacing);
fallbackWeight.addEventListener('input', updateFontWeight);
webfontWeight.addEventListener('input', updateFontWeight);
webfontOutput.addEventListener('blur', changeText);
webfontOutput.addEventListener('focus', clearText);
clipboard.on('success', function(e) {
var span = e.trigger.querySelector('span')
span.textContent = 'Copied!';
setTimeout(function() {
span.textContent = 'Copy CSS to clipboard';
}, 1000);
});
clipboard.on('error', function(e) {
var span = e.trigger.querySelector('span')
span.textContent = 'Error copying :(';
setTimeout(function() {
span.textContent = 'Copy CSS to clipboard';
}, 1000);
});
function clearText() {
fallbackOutput.style.height = this.offsetHeight + 'px';
fallbackOutput.innerHTML = "";
}
function changeText() {
fallbackOutput.style.height = 'auto';
fallbackOutput.innerHTML = this.innerHTML;
}
function updateFontSize(event) {
var value = event.target.value + 'px';
var which = event.target.dataset.target;
updateStyle('font-size', which, value);
updateStyle('font-size', which + 'Output', value);
document.getElementById(which + 'SizeDisplay').textContent = value;
}
function updateLineHeight(event) {
var value = event.target.value;
var which = event.target.dataset.target;
updateStyle('line-height', which, value);
updateStyle('line-height', which + 'Output', value);
document.getElementById(which + 'LineHeightDisplay').textContent = value;
}
function updateFontSpacing(event) {
var value = event.target.value + 'px';
var which = event.target.dataset.target;
updateStyle('letter-spacing', which, value);
updateStyle('letter-spacing', which + 'Output', value);
document.getElementById(which + 'SpacingDisplay').textContent = value;
}
function updateFontWordSpacing(event) {
var value = event.target.value + 'px';
var which = event.target.dataset.target;
updateStyle('word-spacing', which, value);
updateStyle('word-spacing', which + 'Output', value);
document.getElementById(which + 'WordSpacingDisplay').textContent = value;
}
function updateFontFamily(event) {
var value = event.target.value;
var which = event.target.dataset.target;
updateStyle('font-family', which, value);
updateStyle('font-family', which + 'Output', value);
if (which === 'webfont') {
download();
}
}
function updateFontWeight(event) {
var value = event.target.value;
var which = event.target.dataset.target;
updateStyle('font-weight', which, value);
updateStyle('font-weight', which + 'Output', value);
document.getElementById(which + 'WeightDisplay').textContent = value;
}
function updateStyle(name, element, value) {
document.getElementById(element).style[name] = value;
updateClipboardButtons();
}
function updateClipboardButtons() {
var fallbackCss = fallbackOutput.style.cssText.split('; ').join('\n');
var webfontCss = webfontOutput.style.cssText.split('; ').join('\n');
document
.getElementById('fallbackClipboardButton')
.setAttribute('data-clipboard-text', fallbackCss);
document
.getElementById('webfontClipboardButton')
.setAttribute('data-clipboard-text', webfontCss);
}
function fout(event) {
if (!event.target.checked) {
clearTimeout(window.__timeout1);
clearTimeout(window.__timeout2);
clearTimeout(window.__timeout3);
fallbackOutput.style.visibility = 'visible';
webfontOutput.style.visibility = 'visible';
} else {
startFout();
}
}
function startFout() {
fallbackOutput.style.visibility = 'hidden';
webfontOutput.style.visibility = 'hidden';
window.__timeout1 = setTimeout(function() {
fallbackOutput.style.visibility = 'visible';
window.__timeout2 = setTimeout(function() {
fallbackOutput.style.visibility = 'hidden';
webfontOutput.style.visibility = 'visible';
window.__timeout3 = setTimeout(startFout, 1000);
}, 500);
}, 100)
}
function download() {
var shouldDownload = downloadFont.checked;
if (!shouldDownload)
return;
var url = 'https://fonts.googleapis.com/css?family=' + webfontName.value.trim() +
':300,300i,400,400i,700,700i,900,900i';
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
document.head.appendChild(link);
}
function colour() {
var shouldColour = useColours.checked;
fallbackOutput.style.color = shouldColour ? 'red' : 'black';
}
function getAllGoogleFonts() {
var request = new XMLHttpRequest();
var url = 'https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyAkX01E9DhABr4cn4tKD26JuHQstaT5-Ss';
request.open('GET', url, true);
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
var data = JSON.parse(request.responseText);
var names = data.items;
var options = '';
for (var i = 0; i < names.length; i++) {
options += '<option value="'+ names[i].family +'"/>';
}
document.getElementById('families').innerHTML = options;
}
};
request.send();
}
var currentObjectURL = '';
function upload() {
var file = uploadFont.files[0];
if (!file) return;
processFile(file);
}
function processFile(file) {
if (currentObjectURL) {
URL.revokeObjectURL(currentObjectURL);
}
var cssName = JSON.stringify(file.name);
var style = document.createElement('style');
webfontName.value = file.name;
currentObjectURL = URL.createObjectURL(file);
style.textContent = `
@font-face {
font-family: ${cssName};
src: url(${JSON.stringify(currentObjectURL)});
}
`;
document.head.appendChild(style);
updateStyle('font-family', 'webfont', cssName);
updateStyle('font-family', 'webfont' + 'Output', cssName);
}
function uploadBtn(event) {
event.preventDefault();
uploadFont.click();
}
var supportedExtensions = uploadFont.accept.split(',');
// lol drag & drop is still horrible
var enterCount = 0;
function dragEnter() {
enterCount++;
dropAlert.style.opacity = 1;
}
function dragLeave() {
enterCount--;
if (!enterCount) {
dropAlert.style.opacity = 0;
}
}
function drop(event) {
event.preventDefault();
enterCount = 0;
dropAlert.style.opacity = 0;
var file = event.dataTransfer.files[0];
if (!file) return;
var fileSupported = supportedExtensions.some(function(ext) {
return file.name.endsWith(ext);
});
if (!fileSupported) return;
processFile(file);
}
})();