-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
113 lines (97 loc) · 3.44 KB
/
main.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
import { categorys } from './categorys.ts'
import hljs from 'highlight.js/lib/core'
import javascript from 'highlight.js/lib/languages/javascript'
import 'lu2/theme/edge/css/common/ui.css'
import 'lu2/theme/edge/js/common/all.js'
import 'highlight.js/styles/default.css'
const list = document.getElementById('list')!
const selCategory = document.getElementById('sel-category')! as HTMLSelectElement
const selType = document.getElementById('sel-type')! as HTMLSelectElement
const selColor = document.getElementById('sel-color')! as HTMLInputElement
const selCopy = document.getElementById('sel-copy')! as HTMLSelectElement
const lightTip = document.getElementById('lightTip')!
const btnHelp = document.getElementById('btnHelp')! as HTMLButtonElement
const dlgHelp = document.getElementById('dlgHelp')! as HTMLDialogElement
const helpCode = document.getElementById('helpCode')! as HTMLDialogElement
const styleSheet = new CSSStyleSheet()
document.adoptedStyleSheets.push(styleSheet)
// 渲染所有图标
function render() {
const cat = selCategory.value as 'base' | 'svc' | 'ext'
const type = selType.value
const color = selColor.value
const icons = categorys[cat]
let html = ''
Object.keys(icons)
.sort((groupName1, groupName2) => {
const num1 = parseInt(groupName1.split('-')[0])
const num2 = parseInt(groupName2.split('-')[0])
return num1 > num2 ? 1 : -1
}) // 按键排序
.forEach((groupName) => {
let groupIcons = icons[groupName] as any[]
groupIcons.sort((g1, g2) => (g1.order > g2.order ? 1 : -1))
html += `<h3 class='icons-group'>${groupName}<sup class='icon-counts' title='图标数'>${groupIcons.length}</sup></h3>`
html += groupIcons
.map(
(item) => `
<div class="icon-item">
<i class="${cat} ${type}-${item.name}" data-name="${item.name}"></i>
<div class='icon-title' title="${item.nameCn}">${item.nameCn}</div>
<div class='icon-name' title="${item.name}">${item.name}</div>
</div>
`
)
.join('')
})
list.innerHTML = html
styleSheet.replaceSync(`i {color: ${color}!important;`)
}
selCategory.addEventListener('change', render)
selType.addEventListener('change', render)
selColor.addEventListener('input', render)
render()
// 点击复制
list.addEventListener('click', function (ev: MouseEvent) {
const target = ev.target as HTMLElement
const name = target.dataset.name
if (name) {
const cls = 'ci-' + name
const tag = `<i class="${cls}></i>`
const copyText = selCopy.value === 'all' ? tag : cls
navigator.clipboard.writeText(copyText)
lightTip.textContent = copyText + ' 复制成功'
lightTip.show()
}
})
// 弹出帮助
btnHelp.addEventListener('click', function () {
dlgHelp.open = true
})
// 高亮代码
hljs.registerLanguage('javascript', javascript)
const highlightedCode = hljs.highlight(
`import { defineConfig } from "vite";
import UnoCSS from "unocss/vite";
import presetIcons from "@unocss/preset-icons";
export default defineConfig({
plugins: [
UnoCSS({
presets: [
presetIcons({
prefix: "",
extraProperties: {
display: "inline-block",
"vertical-align": "middle"
},
collections: {
ci: () => import("@opentiny/cloud-icons/json/icons.json").then((i) => i.default)
}
})
],
})
],
});`,
{ language: 'javascript' }
).value
helpCode.innerHTML = highlightedCode