-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyleguide.js
207 lines (178 loc) · 5.43 KB
/
styleguide.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
/* global chrome */
// NECESSARY VARIABLES
let received = false;
let googleFonts;
const defaultText = 'The quick brown fox jumps over the lazy dog';
const defaultStyleText = 'Aa';
// COMMUNICATION
chrome.runtime.sendMessage({response: true});
chrome.runtime.onMessage.addListener(insertStyles);
// HELPERS
function setSource(title, address) {
const titleLink = makeLink(title, address, 'source ')
const heading = document.getElementById('source');
heading.appendChild(titleLink);
}
function makeLink(title, address, linkinfo) {
const link = document.createElement('a');
link.textContent = title;
link.title = title;
link.href = address;
link.setAttribute('aria-label', linkinfo + title);
link.target = '_blank';
return link;
}
// GENERATE THE STYLE GUIDE
function insertStyles(message) {
if (message && !received) {
received = true;
googleFonts = message.googleFonts;
setSource(message.title, message.address);
for (let i in message.data)
addCard(message.data[i], i);
}
}
function addCard(row, type) {
for (let i of row) {
if (type === 'fonts')
checkFont(i, type);
else if (type === 'style' || type === 'weight')
addFontStyle(i, 'styles', type);
else
addColor(i, type);
}
}
// COLORS ACTIONS
function addColor(item, type) {
const card = document.createElement('li');
card.classList.add('color');
const swatch = document.createElement('div');
swatch.classList.add('circleSwatch');
swatch.style.backgroundColor = item;
const {hex, opacity} = convertColors(item);
const desc = makeLink('#' + hex, 'http://www.colorhexa.com/' + hex, 'color ');
desc.classList.add('colorDesc');
const section = document.getElementById(type);
card.appendChild(swatch);
card.appendChild(desc);
if (opacity < 1) {
const opacityDesc = document.createElement('span');
opacityDesc.classList.add('opacityDesc');
opacityDesc.textContent = 'Opacity: ' + opacity;
card.appendChild(opacityDesc);
}
section.appendChild(card);
}
function convertColors(color) {
const rgba = new RegExp('rgba');
const replacer = rgba.test(color) ? 'rgba(' : 'rgb(';
const parsed = color.replace(replacer, '')
.replace(')', '')
.split(', ');
const opacity = parsed.length === 4 ? parsed.pop() : 1;
const hex = parsed.map(el => {
const hexodec = parseInt(el).toString(16);
return hexodec.length === 1 ? '0' + hexodec : hexodec;
}).join('');
return {hex, opacity};
}
// FONTS ACTIONS
const safeFonts = [
'arial',
'helvetica',
'times new roman',
'times',
'courier new',
'courier',
'palatino',
'verdana',
'georgia',
'vomic sans ms',
'trebuchet ms',
'arial black',
'impact',
'monospace',
'sans-serif',
'serif',
'tahoma',
'andale mono',
'-apple-system',
];
function checkFont(item, type) {
const goalFont = item.split(',')[0].replace(/"/g, '');
if (safeFonts.includes(goalFont.toLowerCase()))
addFreeFont(item, type, goalFont, false);
else if (googleFonts.includes(goalFont))
addFreeFont(item, type, goalFont, true);
else
addProprietaryFont(item, goalFont);
}
function addFreeFont(item, type, goalFont, link) {
// const card = document.createElement('li');
// card.classList.add('font');
const swatch = document.createElement('span');
swatch.classList.add('fontSwatch');
swatch.textContent = defaultText;
swatch.style.fontFamily = item;
let desc;
if (link) {
desc = makeLink(
goalFont,
'https://fonts.google.com/specimen/' + goalFont,
'font famliy '
);
desc.classList.add('fontDesc');
loadFonts(goalFont, swatch);
} else {
desc = document.createElement('span');
desc.textContent = goalFont;
}
desc.classList.add('fontDesc');
const swatchSection = document.getElementById('fontSwatches');
const descSection = document.getElementById('fontDescs');
descSection.appendChild(desc);
swatchSection.appendChild(swatch);
}
function addProprietaryFont(item, goalFont) {
if (document.getElementById('prop').textContent === '') {
document.getElementById('prop').textContent = 'Proprietary Typefaces';
document.getElementById('prop').style.height = 'auto';
}
const card = document.createElement('li');
card.classList.add('pFont');
const desc = document.createElement('span');
desc.classList.add('pFontDesc');
desc.textContent = goalFont;
const section = document.getElementById('pFonts');
card.appendChild(desc);
section.appendChild(card);
}
function addFontStyle(item, type, style) {
const card = document.createElement('li');
card.classList.add('style');
const swatch = document.createElement('div');
swatch.classList.add('styleSwatch');
const text = document.createElement('span');
text.classList.add('styleText');
const atr = 'font-' + style + ':' + item;
text.setAttribute('style', atr);
text.textContent = defaultStyleText;
swatch.appendChild(text);
const desc = document.createElement('span');
desc.classList.add('styleDesc');
desc.textContent = style + ': ' + item;
const section = document.getElementById(type);
card.appendChild(swatch);
card.appendChild(desc);
section.appendChild(card);
}
function loadFonts(fontName, element) {
const head = document.getElementsByTagName('head')[0];
const link = document.createElement('link');
link.id = fontName;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'http://fonts.googleapis.com/css?family=' + fontName;
head.appendChild(link);
element.style.fontFamily = fontName;
}