-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(player): 添加播放时间格式切换功能 #680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,9 +80,9 @@ | |
| </div> | ||
| <!-- 进度条 --> | ||
| <div class="slider"> | ||
| <span>{{ msToTime(statusStore.currentTime) }}</span> | ||
| <span @click="toggleTimeFormat">{{ timeDisplay0 }}</span> | ||
| <PlayerSlider :show-tooltip="false" /> | ||
| <span>{{ msToTime(statusStore.duration) }}</span> | ||
| <span @click="toggleTimeFormat">{{ timeDisplay1 }}</span> | ||
| </div> | ||
| </div> | ||
| <n-flex class="right" align="center" justify="end"> | ||
|
|
@@ -95,19 +95,29 @@ | |
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import { useMusicStore, useStatusStore, useDataStore } from "@/stores"; | ||
| import { msToTime } from "@/utils/time"; | ||
| import { useMusicStore, useStatusStore, useDataStore, useSettingStore } from "@/stores"; | ||
| import { openDownloadSong, openPlaylistAdd } from "@/utils/modal"; | ||
| import { toLikeSong } from "@/utils/auth"; | ||
| import { useSongManager } from "@/core/player/SongManager"; | ||
| import { usePlayerController } from "@/core/player/PlayerController"; | ||
| import { getTimeDisplay, TIME_FORMATS } from "@/utils/format"; | ||
|
|
||
| const dataStore = useDataStore(); | ||
| const musicStore = useMusicStore(); | ||
| const statusStore = useStatusStore(); | ||
| const settingStore = useSettingStore(); | ||
|
|
||
| const songManager = useSongManager(); | ||
| const player = usePlayerController(); | ||
|
|
||
| const timeDisplay = getTimeDisplay(() => settingStore.timeFormatFullPlayer, statusStore); | ||
| const timeDisplay0 = timeDisplay(0); | ||
| const timeDisplay1 = timeDisplay(1); | ||
|
|
||
| const toggleTimeFormat = () => { | ||
| const currentIndex = TIME_FORMATS.indexOf(settingStore.timeFormatFullPlayer); | ||
| settingStore.timeFormatFullPlayer = TIME_FORMATS[(currentIndex + 1) % TIME_FORMATS.length]; | ||
| }; | ||
|
Comment on lines
+117
to
+120
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| </script> | ||
|
|
||
| <style lang="scss" scoped> | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -353,3 +353,29 @@ export const getPlayerInfo = (song?: SongType, sep: string = "/"): string | null | |||||||||||||||||||||||||||||
| if (!info) return null; | ||||||||||||||||||||||||||||||
| return `${info.name} - ${info.artist}`; | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // 歌曲播放时间显示类型 | ||||||||||||||||||||||||||||||
| export type TimeDisplayType = "current" | "total" | "remaining"; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // 歌曲播放时间显示格式 | ||||||||||||||||||||||||||||||
| export const TIME_FORMATS = ["current-total", "remaining-total", "current-remaining"] as const; | ||||||||||||||||||||||||||||||
| export type TimeFormat = typeof TIME_FORMATS[number]; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export const displayTimeFormat = (format: TimeFormat): [TimeDisplayType, TimeDisplayType] => { | ||||||||||||||||||||||||||||||
| switch (format) { | ||||||||||||||||||||||||||||||
| case "current-total": return ["current", "total"]; | ||||||||||||||||||||||||||||||
| case "remaining-total": return ["remaining", "total"]; | ||||||||||||||||||||||||||||||
| case "current-remaining": return ["current", "remaining"]; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+364
to
+369
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个
Suggested change
|
||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export const getTimeDisplay = ( | ||||||||||||||||||||||||||||||
| format: () => TimeFormat, statusStore: { currentTime: number, duration: number } | ||||||||||||||||||||||||||||||
| ) => (index: number) => computed(() => { | ||||||||||||||||||||||||||||||
| const display = displayTimeFormat(format())[index]; | ||||||||||||||||||||||||||||||
| switch (display) { | ||||||||||||||||||||||||||||||
| case "current": return msToTime(statusStore.currentTime); | ||||||||||||||||||||||||||||||
| case "total": return msToTime(statusStore.duration); | ||||||||||||||||||||||||||||||
| case "remaining": return "-" + msToTime(statusStore.duration - statusStore.currentTime); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
Comment on lines
+372
to
+381
This comment was marked as outdated.
Sorry, something went wrong. |
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toggleTimeFormat函数中的逻辑与PlayerControl.vue中的几乎完全相同,只是操作的settingStore属性不同。为了遵循 DRY (Don't Repeat Yourself) 原则并提高代码的可维护性,建议将这个切换逻辑提取到一个公共的工具函数中,例如在src/utils/format.ts中创建一个getNextTimeFormat函数。这样两个组件都可以复用这个逻辑。