forked from Momo707577045/iconfont-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
484 lines (457 loc) · 12.8 KB
/
index.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="keywords" content="iconfont preview for web">
<meta name="description" content="iconfont preview for web, Momo's Blog, LuckyMomo">
<title>Iconfont Preview</title>
<style>
/*全局设置*/
body::-webkit-scrollbar { display: none}
p {
margin: 0;
}
[v-cloak] {
display: none;
}
#app {
text-align: center;
padding: 10px 50px 80px;
}
.p-action {
margin: 20px auto;
max-width: 1100px;
width: 100%;
font-size: 35px;
text-align: center;
font-weight: bold;
}
.p-reset, .p-copy-type, .p-change-background, .p-show-all {
position: absolute;
top: 38px;
right: 100px;
padding: 8px 20px;
font-size: 20px;
color: white;
cursor: pointer;
border-radius: 4px;
border: 1px solid #aaaaaa;
background-color: #3D8AC7;
opacity: 1;
transition: 0.3s all;
}
.p-reset:hover, .p-copy-type:hover, .p-change-background:hover, .p-show-all:hover {
opacity: 0.9;
}
.p-copy-type {
left: 100px;
right: auto;
}
.p-show-all {
right: 245px;
}
.p-change-background {
left: 260px;
right: auto;
}
.p-github, .p-other {
position: fixed;
right: 50px;
bottom: 70px;
background-color: #eff3f6;
background-image: linear-gradient(-180deg, #fafbfc, #eff3f6 90%);
color: #24292e;
border: 1px solid rgba(27, 31, 35, .2);
border-radius: 3px;
cursor: pointer;
display: inline-block;
font-size: 14px;
font-weight: 600;
line-height: 20px;
padding: 6px 12px;
}
.p-help {
position: fixed;
right: 50px;
top: 50px;
width: 30px;
height: 30px;
color: #666666;
z-index: 2;
line-height: 30px;
font-weight: bolder;
border-radius: 50%;
border: 1px solid rgba(27, 31, 35, .2);
cursor: pointer;
background-color: #eff3f6;
background-image: linear-gradient(-180deg, #fafbfc, #eff3f6 90%);
}
.p-github:hover, .p-other:hover, .p-help {
opacity: 0.9;
}
.p-other {
bottom: 30px;
}
/*字体文件载入*/
.p-input-container {
display: flex;
padding-top: 50px;
}
.p-input-container .line {
vertical-align: top;
width: 5px;
height: 400px;
transform-origin: top;
background-repeat: repeat;
background-image: url(http://www.luckly-mjw.cn/baseSource/icon-dotted-y.png);
}
.p-input-container .url-box, .file-box {
height: 300px;
flex: 1;
position: relative;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.p-input-container .url-box input {
margin-bottom: 30px;
display: block;
width: 280px;
padding: 10px;
font-size: 24px;
border-radius: 4px;
box-shadow: none;
color: #444444;
border: 1px solid #cccccc;
}
.p-input-container .input-box, .url-box div {
position: relative;
display: inline-block;
height: 50px;
line-height: 50px;
padding: 10px 30px;
font-size: 30px;
color: white;
cursor: pointer;
border-radius: 4px;
border: 1px solid #aaaaaa;
background-color: #3D8AC7;
opacity: 1;
transition: 0.3s all;
}
.p-input-container .input-box:hover, .url-box div:hover {
opacity: 0.9;
}
.p-input-container .input-box input {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
}
.p-input-container .url-box div {
width: 240px;
}
/*icon 列表样式*/
.p-icon-item {
margin: 6px;
padding: 10px 6px;
display: inline-block;
text-align: center;
border-radius: 4px;
cursor: pointer;
border: 1px solid #dddddd;
overflow: hidden;
white-space: pre-line;
box-sizing: border-box;
transition-duration: .4s;
transition-property: background, box-shadow;
}
.p-icon-item-background-0 {
background-image: url(http://upyun.luckly-mjw.cn/Assets/base-source/png-background.png);
}
.p-icon-item-background-1 {
background-color: #FCFCFC;
}
.p-icon-item-background-2 {
background-color: #EBEBEB;
}
.p-icon-item:hover {
background: #ffffff;
border-color: transparent;
box-shadow: 0 5px 18px 0 rgba(0, 0, 0, 0.3);
}
.p-icon-item:active {
color: white;
background: #7BD784;
}
.p-icon-item .iconfont {
margin: 0 20px;
padding: 6px;
border-radius: 4px;
font-size: 28px;
}
.p-icon-item .name {
font-size: 12px;
font-weight: bold;
}
.p-icon-item .value {
font-size: 12px;
font-weight: bold;
}
.p-icon-item svg {
width: 36px;
height: 36px;
}
</style>
</head>
<body>
<section id="app" v-cloak>
<!--顶部操作提示-->
<section class="p-action g-box">{{tips}}</section>
<div v-if="iconList.length > 0" class="p-copy-type" @click="copyType = copyType === 'value' ? 'name' : 'value'">复制 {{ copyType }}</div>
<div v-if="iconList.length > 0" class="p-change-background" @click="backgroundIndex = (backgroundIndex + 1) % 3">切换背景</div>
<div v-if="iconList.length > 0 && bufferStr" class="p-show-all" @click="showAll">显示副本 unicode</div>
<div v-if="iconList.length > 0" class="p-reset" @click="reload">重新载入</div>
<a class="p-help" target="_blank" href="https://segmentfault.com/a/1190000020121850">?</a>
<a class="p-github" target="_blank" href="https://github.com/Momo707577045/iconfont-preview">github</a>
<a class="p-other" target="_blank" href="http://blog.luckly-mjw.cn/tool-show/index.html">其他实用工具</a>
<!--文件载入-->
<section class="p-input-container" v-if="iconList.length === 0">
<div class="url-box">
<input type="text" v-model="url" placeholder="在线 ttf、css 或 js 链接">
<div @click="getURLFile">线上链接预览</div>
</div>
<div class="line"></div>
<div class="file-box">
<div class="input-box" v-if="iconList.length === 0">
<label>解析本地 ttf、woff、otf 文件</label>
<input type="file" @change="getLocalTTF" accept=".ttf,.woff,.otf">
</div>
</div>
</section>
<!--icon 展示-->
<section v-else>
<div v-for="item in iconList" class="p-icon-item" :class="[`p-icon-item-background-${backgroundIndex}`]" @click="copyToClipboard(item[copyType])">
<svg v-if="isSymbol" class="icon" aria-hidden="true" v-html="`<use xlink:href='${item.value}'></use>`"></svg>
<p v-else class="iconfont" v-html="isCSS ? '' : item.show" :class="isCSS ? item.show : '' "></p>
<p class="name">{{item.name}}</p>
<p class="value">{{item.value}}</p>
</div>
</section>
</section>
</body>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?1f12b0865d866ae1b93514870d93ce89";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<!--ttf文件解析库-->
<script src="https://cdn.jsdelivr.net/npm/opentype.js@latest/dist/opentype.min.js"></script>
<!--vue 前端框架-->
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>
<script>
new Vue({
el: '#app',
data() {
return {
backgroundIndex: 0, // 卡牌背景颜色
url: '//at.alicdn.com/t/font_1349299_kjq7u4s9t8m.css', // 在线链接
tips: '请选择字体文件源', // 顶部提示
timeouter: 0, // 操作提示计时器
copyType: 'value', // icon 复制类型
bufferStr: null, // 字体编码内容
isSymbol: false, // 是否 symbol 模式
isCSS: false, // 是否 css 文件链接
iconList: [], // icon 列表
}
},
methods: {
// ajax 请求
ajax(options) {
options = options || {};
let xhr = new XMLHttpRequest();
if (options.type === 'buffer') {
xhr.responseType = 'arraybuffer';
}
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
let status = xhr.status;
if (status >= 200 && status < 300) {
console.log(xhr)
options.success && options.success(xhr.response);
} else {
options.fail && options.fail(status);
}
}
};
xhr.open("GET", options.url, true);
xhr.send(null);
},
// 获取在线文件
getURLFile() {
if (!this.url) {
alert('请输入链接')
return
}
if (this.url.toLowerCase().indexOf('.ttf') !== -1) {
this.getOnlineTTF()
} else if (this.url.toLowerCase().indexOf('.css') !== -1) {
this.getOnlineCSS()
} else if (this.url.toLowerCase().indexOf('.js') !== -1) {
this.getOnlineJS()
}
},
// 解析本地 ttf 文件
getLocalTTF(event) {
// 解析文件内容
let file = event.target.files[0]
let reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = (evt) => { this.parseIcon(evt.target.result); }
// base64 编码,动态加入 @font-face
let readerBase64 = new FileReader();
readerBase64.readAsDataURL(file)
readerBase64.onload = (evt) => { this.setStyle(evt.target.result) }
},
// 解析在线 ttf 文件
getOnlineTTF() {
// 远程获取文件
this.ajax({
url: this.url,
type: 'buffer',
success: (params) => { this.parseIcon(params) }
})
this.setStyle(this.url)
},
// 解析 CSS 文件
getOnlineCSS() {
// 远程获取文件
this.isCSS = true;
this.ajax({
url: this.url,
success: (params) => {
this.setStyle('', params)
params.replace(/\.([^:^ ]+):[\s\S]+?content: "\\([^"]+)";/gi, (...item) => {
this.iconList.push({
show: item[1],
name: item[1],
value: `&#${item[2]};`,
})
})
}
})
},
// 解析 JS 文件
getOnlineJS() {
// 远程获取文件
this.isSymbol = true;
this.ajax({
url: this.url,
success: (params) => {
let $script = document.createElement('script')
$script.src = this.url
document.body.append($script);
console.log(params)
//id="iconexchange"
params.replace(/id="([^"]+)"/gi, (...item) => {
this.iconList.push({
show: item[1].replace(/icon/, ''),
name: item[1].replace(/icon/, ''),
value: `#${item[1]}`,
})
})
}
})
},
// 打开所有副本
showAll() {
this.parseIcon(this.bufferStr, true)
},
// 解析icon
parseIcon(bufferStr, showAll) {
this.bufferStr = bufferStr
this.iconList = []
let result = window.opentype.parse(this.bufferStr);
for (let key in result.glyphs.glyphs) {
let item = result.glyphs.glyphs[key]
if (!item.unicode) {
} else if (showAll) { // 是否显示所有 unicodes
let valueStr = ''
item.unicodes.forEach(unicode => valueStr += `&#${unicode};\n`)
this.iconList.push({
name: item.name,
value: valueStr,
show: `&#${item.unicode};`
})
} else {
this.iconList.push({
name: item.name,
show: `&#${item.unicode};`,
value: `&#${item.unicode};`
})
}
}
this.iconList.forEach(item => {
item.value = item.value.replace(/&#([^;]+);/ig, (target, value) => {
return `&#x${parseInt(value).toString(16)};`
})
})
this.tips = '点击复制icon代码'
},
// 添加style
setStyle(url, cssFile) {
let $style = document.createElement('style')
if (cssFile) {
$style.innerHTML = cssFile
} else {
$style.innerHTML = `
@font-face {
font-family: 'iconfont';
src: url('${url}') format('truetype');
}
.iconfont {
font-family: "iconfont" !important;
font-size: 24px;font-style: normal;
-webkit-font-smoothing: antialiased;
-webkit-text-stroke-width: 0.2px;
-moz-osx-font-smoothing: grayscale;
}`;
}
document.body.append($style);
},
// 拷贝剪切板
copyToClipboard(content) {
clearTimeout(this.timeouter)
this.tips = `复制成功: ${content}`
this.timeouter = setTimeout(() => {this.tips = `点击复制icon代码`}, 1000)
if (!document.queryCommandSupported('copy')) {
return false
}
let $input = document.createElement('input')
$input.style.opacity = '0'
$input.value = content
document.body.appendChild($input)
$input.select()
const result = document.execCommand('copy')
document.body.removeChild($input)
$input = null
return result
},
// 页面重新加载
reload() {
location.reload();
}
}
})
</script>
</html>