forked from ShouChenICU/FastSend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NavBar.vue
250 lines (226 loc) · 6.63 KB
/
NavBar.vue
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
<script setup lang="ts">
const localePath = useLocalePath()
const { locale, setLocale } = useI18n()
const selectedLocale = ref('')
const colorMode = useColorMode()
const isBgBlur = ref(false)
const userInfo = useUserInfo()
const tmpNickname = ref('')
const userInfoPopover = ref()
const i18nPopover = ref()
const isConfirmDefault = useConfirmDefault()
const availableLocales = ref([
{ name: 'English', code: 'en', icon: 'icon-park-outline:english' },
{ name: '中文', code: 'zh', icon: 'icon-park-outline:chinese' }
])
// 暗色模式切换
function switchColorMode() {
if (colorMode.preference === 'light') {
colorMode.preference = 'dark'
} else {
colorMode.preference = 'light'
}
}
// 中英语言切换
function switchI18n() {
if (locale.value === 'en') {
setLocale('zh')
} else {
setLocale('en')
}
}
// 是否开启发送方自动确认
function switchConfirmDefault() {
localStorage.setItem('isConfirmDefault', JSON.stringify(isConfirmDefault.value))
}
// 展示昵称编辑弹框
function showNicknameEditor(event: Event) {
tmpNickname.value = userInfo.value.nickname
userInfoPopover.value.toggle(event)
}
// 编辑昵称
function editNickname() {
userInfo.value.nickname = tmpNickname.value.trim().substring(0, 16)
if (!userInfo.value.nickname) {
userInfo.value.nickname = 'User_' + genRandomString(6)
}
localStorage.setItem('nickname', userInfo.value.nickname)
userInfoPopover.value.hide()
}
// 编辑头像
function editAvatar() {
selectAvatar((url) => {
if (url) {
userInfo.value.avatarURL = url
localStorage.setItem('avatarURL', url)
}
})
}
// 重置用户信息
function clearUserInfo() {
initAvatar()
userInfo.value.nickname = 'User_' + genRandomString(6)
tmpNickname.value = userInfo.value.nickname
localStorage.setItem('nickname', userInfo.value.nickname)
}
function initAvatar() {
const fr = new FileReader()
fr.onload = () => {
userInfo.value.avatarURL = fr.result + ''
localStorage.setItem('avatarURL', userInfo.value.avatarURL)
}
fetch('/akari.webp')
.then((res) => res.blob())
.then((blob) => fr.readAsDataURL(blob))
}
onMounted(() => {
window.addEventListener('scroll', () => {
isBgBlur.value = getScrollTop() > 64
})
// 初始化昵称
let nickname = localStorage.getItem('nickname')
if (!nickname) {
nickname = 'User_' + genRandomString(6)
localStorage.setItem('nickname', nickname)
}
userInfo.value.nickname = nickname
// 初始化头像
const avatarURL = localStorage.getItem('avatarURL')
if (!avatarURL) {
initAvatar()
} else {
userInfo.value.avatarURL = avatarURL
}
// 初始化配置
if (getValFromLocalStorage('isConfirmDefault', false)) {
isConfirmDefault.value = true
}
})
</script>
<template>
<nav
class="flex flex-row items-center py-3 px-4 md:py-4 md:px-[10vw] sticky left-0 right-0 top-0 z-50 nav-bar"
:class="{ 'backdrop-blur': isBgBlur }"
>
<NuxtLink :to="localePath('/')">
<div class="tracking-wider">
<img src="/favicon.webp" class="inline-block size-[32px] mr-1" />FastSend
</div>
</NuxtLink>
<div class="flex-1"></div>
<div class="contents text-sm">
<Avatar
:image="userInfo.avatarURL"
shape="circle"
class="shadow cursor-pointer"
@click="showNicknameEditor"
/>
<p class="ml-2 truncate shrink-[1000] hidden md:block">
{{ userInfo.nickname }}
</p>
</div>
<!-- 用户信息弹出框 -->
<Popover ref="userInfoPopover">
<div class="relative p-3 m-2">
<div class="absolute top-0 right-0 z-10">
<Button
severity="secondary"
text
@click="clearUserInfo"
size="small"
class="py-3"
aria-label="Language"
>
<Icon name="material-symbols:sync-rounded" class="text-rose-500 dark:text-rose-600" />
</Button>
</div>
<div class="relative flex flex-col items-center gap-4">
<Avatar
:image="userInfo.avatarURL"
shape="circle"
class="shadow-md cursor-pointer"
size="xlarge"
@click="editAvatar"
/>
<InputGroup>
<InputText
severity="contrast"
size="small"
placeholder="昵称"
v-model:model-value="tmpNickname"
@keydown.enter="editNickname"
/>
<Button severity="contrast" size="small" class="m-0" @click="editNickname"
><Icon name="material-symbols:check-rounded"
/></Button>
</InputGroup>
</div>
<Divider />
<div class="flex flex-row items-center justify-between">
<p class="text-sm">{{ $t('label.autoConfirmBySender') }}</p>
<ToggleSwitch v-model="isConfirmDefault" @change="switchConfirmDefault" />
</div>
</div>
</Popover>
<div class="contents">
<NuxtLink to="https://www.buymeacoffee.com/shouchen" target="_blank" class="ml-2 md:ml-4">
<Button severity="warning" text size="small" class="py-3" aria-label="Buy Me A Coffee">
<IconCoffee class="size-5 text-black dark:text-white/90" />
</Button>
</NuxtLink>
<Button
severity="secondary"
text
@click="switchI18n"
size="small"
class="py-3"
aria-label="Language"
>
<Icon
name="icon-park-outline:chinese"
class="text-black/90 dark:text-white/90"
v-if="locale === 'zh'"
/>
<Icon
name="icon-park-outline:english"
class="text-black/90 dark:text-white/90"
v-else-if="locale === 'en'"
/>
</Button>
<!-- <Popover ref="i18nPopover">
<Listbox
v-model="selectedLocale"
:options="availableLocales"
optionLabel="name"
class="border-0"
><template #option="slotProps">
<div class="flex items-center gap-2 text-sm">
<Icon :name="slotProps.option.icon" />
<div>{{ slotProps.option.name }}</div>
</div>
</template>
</Listbox>
</Popover> -->
<Button
severity="secondary"
text
@click="switchColorMode"
size="small"
class="py-3"
aria-label="Dark"
>
<Icon
name="solar:moon-linear"
class="text-yellow-500/90"
v-if="colorMode.preference === 'dark'"
/>
<Icon name="solar:sun-broken" class="text-black" v-else />
</Button>
</div>
</nav>
</template>
<style scoped>
.nav-bar {
transition: backdrop-filter 0.5s ease;
}
</style>