mirror of
https://github.com/atdunbg/Nekosonic-Music.git
synced 2026-06-22 00:58:51 +08:00
播放列表可以定位正在播放的歌曲位置 添加歌词翻译 新增快捷键 播放/暂停 重构主题设置,支持多种主题 修复评论playerbar查看点击后一直默认评论打开抽屉页面问题
61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import { ref, watch } from 'vue';
|
|
import { invoke } from '@tauri-apps/api/core';
|
|
import { parseLrc, mergeTranslation, getCurrentLyricIndex, LyricLine } from '../utils/lyric';
|
|
import { usePlayerStore } from '../stores/player';
|
|
|
|
export function useLyric() {
|
|
const player = usePlayerStore();
|
|
|
|
const lyrics = ref<LyricLine[]>([]);
|
|
const currentLyricIdx = ref(-1);
|
|
const showTranslation = ref(true);
|
|
const hasTranslation = ref(false);
|
|
|
|
watch(() => player.currentSong, async (song) => {
|
|
if (!song) {
|
|
lyrics.value = [];
|
|
currentLyricIdx.value = -1;
|
|
hasTranslation.value = false;
|
|
return;
|
|
}
|
|
try {
|
|
const jsonStr: string = await invoke('get_lyric', { id: song.id });
|
|
const data = JSON.parse(jsonStr);
|
|
const lrc = data?.lrc?.lyric || '';
|
|
const tLrc = data?.tlyric?.lyric || '';
|
|
let parsed = lrc ? parseLrc(lrc) : [];
|
|
if (tLrc && parsed.length > 0) {
|
|
parsed = mergeTranslation(parsed, tLrc);
|
|
hasTranslation.value = parsed.some(l => l.translation);
|
|
} else {
|
|
hasTranslation.value = false;
|
|
}
|
|
lyrics.value = parsed;
|
|
currentLyricIdx.value = -1;
|
|
} catch {
|
|
lyrics.value = [];
|
|
hasTranslation.value = false;
|
|
}
|
|
}, { immediate: true });
|
|
|
|
watch(() => player.currentTime, (t) => {
|
|
if (lyrics.value.length === 0) return;
|
|
const idx = getCurrentLyricIndex(lyrics.value, t);
|
|
if (idx !== currentLyricIdx.value) {
|
|
currentLyricIdx.value = idx;
|
|
}
|
|
});
|
|
|
|
function toggleTranslation() {
|
|
showTranslation.value = !showTranslation.value;
|
|
}
|
|
|
|
return {
|
|
lyrics,
|
|
currentLyricIdx,
|
|
hasTranslation,
|
|
showTranslation,
|
|
toggleTranslation,
|
|
};
|
|
}
|