Skip to content

Commit 1efa948

Browse files
committed
refactor: Controller
1 parent 71cb6f1 commit 1efa948

File tree

2 files changed

+4
-110
lines changed

2 files changed

+4
-110
lines changed

components/hana/VolumeController.vue renamed to components/hana/Controller/Volume.vue

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script lang="ts" setup>
2-
const isIdle = ref(true)
32
const volume = ref(0)
43
const previousVolume = ref(0)
54
@@ -10,16 +9,11 @@ const volumeIcon = computed(() =>
109
onMounted(() => {
1110
const { $audioPlayer } = useNuxtApp()
1211
13-
const unsubscribeIdle = $audioPlayer.subscribe((state) => {
14-
isIdle.value = state.playbackState === 'idle'
15-
})
16-
1712
const unsubscribeVolume = $audioPlayer.subscribe((state) => {
1813
volume.value = state.volume
1914
})
2015
2116
onUnmounted(() => {
22-
unsubscribeIdle()
2317
unsubscribeVolume()
2418
})
2519
})
@@ -51,8 +45,7 @@ function handleInput(e: Event) {
5145

5246
<template>
5347
<div
54-
v-if="!isIdle"
55-
class="relative w-14 overflow-hidden hana-card transition-all duration-300 hidden xl:block"
48+
class="relative w-14 overflow-hidden hana-card transition-all duration-300"
5649
:style="{ height: controllerHeight }"
5750
@mouseenter="toggleShowVolumePanel"
5851
@mouseleave="toggleShowVolumePanel"
@@ -63,7 +56,7 @@ function handleInput(e: Event) {
6356
</p>
6457
<input
6558
id="volume-progress"
66-
class="h-2 appearance-none rounded outline-none transition-[box-shadow] duration-200 accent-hana-blue dark:bg-hana-black-700 focus-visible:ring-2 focus-visible:ring-hana-blue-300 dark:accent-hana-blue-300"
59+
class="h-24 w-2 rounded outline-none transition-[box-shadow] duration-200 accent-hana-blue dark:bg-hana-black-700 focus-visible:ring-2 focus-visible:ring-hana-blue-300"
6760
type="range"
6861
min="0"
6962
max="1"

components/hana/Controller.vue renamed to components/hana/Controller/index.vue

Lines changed: 2 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const { dialogCount } = toRefs(dialogStore)
1313
const route = useRoute()
1414
const { path } = toRefs(route)
1515
16-
// #region 滚动相关逻辑
1716
const { progress } = useLoadingIndicator()
1817
const canScroll = ref(false)
1918
@@ -30,9 +29,7 @@ const showPercent = ref(true)
3029
function toggleShowPercent() {
3130
showPercent.value = !showPercent.value
3231
}
33-
// #endregion
3432
35-
// #region 评论相关逻辑
3633
const commentWhiteList = ['/recently'] // 在白名单中的路径不显示评论
3734
3835
const hasComments = ref(false)
@@ -75,96 +72,18 @@ function scrollToTop() {
7572
})
7673
}
7774
}
78-
// #endregion
7975
80-
// #region 音量控制相关逻辑
8176
const isIdle = ref(true)
82-
const volume = ref(0)
83-
const previousVolume = ref(0)
84-
85-
const volumeIcon = computed(() =>
86-
volume.value > 0.5 ? 'lucide:volume-2' : volume.value > 0 ? 'lucide:volume-1' : 'lucide:volume-off',
87-
)
8877
8978
onMounted(() => {
9079
const { $audioPlayer } = useNuxtApp()
91-
92-
const unsubscribeIdle = $audioPlayer.subscribe((state) => {
93-
isIdle.value = state.playbackState === 'idle'
94-
})
95-
96-
const unsubscribeVolume = $audioPlayer.subscribe((state) => {
97-
volume.value = state.volume
98-
})
99-
100-
onUnmounted(() => {
101-
unsubscribeIdle()
102-
unsubscribeVolume()
103-
})
80+
onUnmounted($audioPlayer.subscribe(state => isIdle.value = state.playbackState === 'idle'))
10481
})
105-
106-
function toggleMuted() {
107-
const { $audioPlayer } = useNuxtApp()
108-
if ($audioPlayer.getState().isMuted) {
109-
$audioPlayer.setVolume(previousVolume.value)
110-
}
111-
else {
112-
previousVolume.value = volume.value
113-
$audioPlayer.setVolume(0)
114-
}
115-
}
116-
117-
const showVolumePanel = ref(false)
118-
function toggleShowVolumePanel() {
119-
showVolumePanel.value = !showVolumePanel.value
120-
}
121-
122-
const controllerHeight = computed(() => showVolumePanel.value ? '208px' : '56px')
123-
124-
function handleInput(e: Event) {
125-
const { $audioPlayer } = useNuxtApp()
126-
const target = e.target as HTMLInputElement
127-
$audioPlayer.setVolume(target.valueAsNumber)
128-
}
129-
// #endregion
13082
</script>
13183

13284
<template>
13385
<transition-group name="controller" tag="div" class="fixed bottom-10 right-10 flex flex-col gap-4">
134-
<div
135-
v-if="dialogCount === 0 && !isIdle"
136-
class="relative w-14 overflow-hidden hana-card transition-all duration-300"
137-
:style="{ height: controllerHeight }"
138-
@mouseenter="toggleShowVolumePanel"
139-
@mouseleave="toggleShowVolumePanel"
140-
>
141-
<div class="absolute bottom-2 flex flex-col items-center gap-4">
142-
<p class="text-xs text-text dark:text-hana-white-700">
143-
{{ Math.round(volume * 100) }}%
144-
</p>
145-
<input
146-
id="volume-progress"
147-
class="h-24 w-2 rounded outline-none transition-[box-shadow] duration-200 accent-hana-blue dark:bg-hana-black-700 focus-visible:ring-2 focus-visible:ring-hana-blue-300"
148-
type="range"
149-
min="0"
150-
max="1"
151-
step="0.01"
152-
:value="volume"
153-
:aria-valuemin="0"
154-
:aria-valuemax="1"
155-
:aria-valuenow="volume"
156-
aria-label="音量"
157-
:style="`--progress: ${volume}`"
158-
@input="handleInput"
159-
>
160-
<div
161-
class="mt-auto size-10 hana-button items-end items-center justify-center font-bold"
162-
@click="toggleMuted"
163-
>
164-
<icon :name="volumeIcon" size="20" />
165-
</div>
166-
</div>
167-
</div>
86+
<HanaControllerVolume v-if="dialogCount === 0 && !isIdle" />
16887

16988
<div v-if="dialogCount === 0 && hasComments" class="relative hana-card">
17089
<HanaTooltip content="滚到评论" position="left" animation="slide">
@@ -213,22 +132,4 @@ function handleInput(e: Event) {
213132
.controller-leave-active {
214133
position: absolute;
215134
}
216-
217-
#volume-progress {
218-
background: linear-gradient(to right,
219-
oklch(0.5 0.1102 250.04) calc(var(--progress) * 100%),
220-
oklch(0.93 0.0358 205.23) calc(var(--progress) * 100%)
221-
);
222-
writing-mode: vertical-lr;
223-
direction: rtl;
224-
}
225-
226-
.dark {
227-
#volume-progress {
228-
background: linear-gradient(to right,
229-
oklch(0.75 0.0883 226.04) calc(var(--progress) * 100%),
230-
oklch(0.93 0.0358 205.23) calc(var(--progress) * 100%)
231-
);
232-
}
233-
}
234135
</style>

0 commit comments

Comments
 (0)