Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions backend/internal/service/lyric_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"encoding/xml"
"fmt"
"io"
"strconv"
Expand All @@ -28,6 +29,7 @@ type LyricViewLine struct {
RomanLyric string `json:"romanLyric,omitempty"`
IsBG bool `json:"isBg"`
IsDuet bool `json:"isDuet"`
Key string `json:"key,omitempty"`
Words []LyricViewWord `json:"words,omitempty"`
}

Expand All @@ -38,15 +40,24 @@ type LyricViewResponse struct {
}

// toViewResponse 将 amll-ttml 解析结果转为前端友好的响应结构
func toViewResponse(parsed ttml.TTMLLyric) *LyricViewResponse {
func toViewResponse(parsed ttml.TTMLLyric, lineKeys []string) *LyricViewResponse {
resp := &LyricViewResponse{
Metadata: make(map[string][]string),
Lines: make([]LyricViewLine, 0, len(parsed.LyricLines)),
}
for _, m := range parsed.Metadata {
resp.Metadata[m.Key] = append(resp.Metadata[m.Key], m.Value...)
}
keyIdx := 0
currentKey := ""
for _, line := range parsed.LyricLines {
if !line.IsBG {
currentKey = ""
if keyIdx < len(lineKeys) {
currentKey = lineKeys[keyIdx]
}
keyIdx++
}
var text strings.Builder
// 逐字透传
words := make([]LyricViewWord, 0, len(line.Words))
Expand All @@ -68,12 +79,57 @@ func toViewResponse(parsed ttml.TTMLLyric) *LyricViewResponse {
RomanLyric: line.RomanLyric,
IsBG: line.IsBG,
IsDuet: line.IsDuet,
Key: currentKey,
Words: words,
})
}
return resp
}

// extractTTMLLineKeys 按顺序提取 body 内每个 <p> 的 itunes:key
func extractTTMLLineKeys(ttmlText string) []string {
decoder := xml.NewDecoder(strings.NewReader(ttmlText))
keys := make([]string, 0)
inBody := false
for {
tok, err := decoder.Token()
if err != nil {
break
}
switch t := tok.(type) {
case xml.StartElement:
switch t.Name.Local {
case "body":
inBody = true
case "p":
if !inBody {
continue
}
var key string
hasBegin, hasEnd := false, false
for _, attr := range t.Attr {
switch attr.Name.Local {
case "begin":
hasBegin = true
case "end":
hasEnd = true
case "key":
key = strings.TrimSpace(attr.Value)
}
}
if hasBegin && hasEnd {
keys = append(keys, key)
}
}
case xml.EndElement:
if t.Name.Local == "body" {
inBody = false
}
}
}
return keys
}

// parseTTML 解析 TTML
func parseTTML(ttmlText string) (ttml.TTMLLyric, error) {
return ttml.ParseLyric(dedupeTransliterations(ttmlText))
Expand Down Expand Up @@ -120,7 +176,7 @@ func (s *LyricsService) ViewLyric(ctx context.Context, filename string) (*LyricV
if err != nil {
return nil, fmt.Errorf("parse ttml: %w", err)
}
return toViewResponse(parsed), nil
return toViewResponse(parsed, extractTTMLLineKeys(content.String())), nil
}

// ParseLyric 解析任意 TTML 文本为结构化歌词数据,
Expand All @@ -129,5 +185,5 @@ func (s *LyricsService) ParseLyric(ctx context.Context, ttmlText string) (*Lyric
if err != nil {
return nil, fmt.Errorf("parse ttml: %w", err)
}
return toViewResponse(parsed), nil
return toViewResponse(parsed, extractTTMLLineKeys(ttmlText)), nil
}
9 changes: 5 additions & 4 deletions frontend/src/boot/PlayerBoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
* 播放器动作的模块级载体:Boot 渲染期间同步赋值。
* usePlayer() 展开后消费方拿到的永远是最新引用,无需 Context。
*/
export const playerActions = {} as PlayerActions;

Check warning on line 224 in frontend/src/boot/PlayerBoot.tsx

View workflow job for this annotation

GitHub Actions / check

Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components

/**
* 播放器引擎 Boot(替代原 PlayerProvider):
Expand Down Expand Up @@ -1018,6 +1018,7 @@
cover: track.cover,
platform: 'qq',
level: q,
customTtml: track.customTtml,
},
});
return;
Expand Down Expand Up @@ -1396,6 +1397,10 @@
(async () => {
try {
const { parseLrc, parseTTML } = await import('@applemusic-like-lyrics/lyric');
if (track.customTtml) {
setLyricData(parseTTML(track.customTtml).lines);
return;
}
// QQ 曲目优先用库内已校对过的 qq-lyrics TTML
if (track.platform === 'qq' && track.ncmSongId) {
const ttml = await api.getQqLyricTtml(track.ncmSongId);
Expand All @@ -1410,10 +1415,6 @@
setLyricData(track.customLyricLines);
return;
}
if (track.customTtml) {
setLyricData(parseTTML(track.customTtml).lines);
return;
}
const rawLyricFile = track.hit?.rawLyricFile;
if (rawLyricFile && !track.skipTtml) {
const ttml = await api.getRawLyricTtml(rawLyricFile);
Expand Down
118 changes: 69 additions & 49 deletions frontend/src/components/LyricViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
showHeader?: boolean;
showActions?: boolean;
rawLyricFile?: string;
/** 是否展示歌词行号 */
showLineNumbers?: boolean;
}

/** 平台歌词模式返回:视图数据 + 原始歌词文本与下载文件名 */
Expand All @@ -38,7 +40,7 @@
}

/** 按平台决定下载扩展名 */
export const ONLINE_LYRIC_EXT: Record<OnlinePlatform, string> = {

Check warning on line 43 in frontend/src/components/LyricViewer.tsx

View workflow job for this annotation

GitHub Actions / check

Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components
qq: 'qrc',
kugou: 'krc',
ncm: 'lrc',
Expand Down Expand Up @@ -244,6 +246,7 @@
showHeader = true,
showActions = true,
rawLyricFile,
showLineNumbers = false,
}: LyricViewerProps) {
const [dlError, setDlError] = useState('');
const onlinePlatform = online?.platform;
Expand Down Expand Up @@ -365,63 +368,80 @@

{/* 歌词内容 */}
<motion.div variants={listItem} className="space-y-1">
{groups.map((group, gi) => (
<motion.div key={gi} variants={listItem} className="space-y-0.5">
{group.map((line, li) => (
<div
key={li}
className={`flex gap-3 rounded-lg px-3 py-1.5 transition-colors hover:bg-primary-tint/50 sm:gap-4 sm:px-4 ${
line.isDuet ? 'flex-row-reverse text-right' : ''
}`}
>
{/* 时间轴 */}
{groups.map((group, gi) => {
const lineNo = group.find((line) => !line.isBg)?.key || String(gi + 1);
return (
<motion.div
key={gi}
variants={listItem}
className="flex gap-3 rounded-lg px-3 py-1.5 transition-colors hover:bg-primary-tint/50 sm:gap-4 sm:px-4"
>
{/* 行号 */}
{showLineNumbers && (
<span
className={`shrink-0 pt-0.5 font-mono text-xs font-semibold text-primary sm:text-sm ${
line.isDuet ? 'text-right' : ''
}`}
style={{ minWidth: '72px' }}
className="shrink-0 select-none pt-0.5 text-right font-mono text-xs tabular-nums text-ink-3 sm:text-sm"
style={{ minWidth: '24px' }}
>
{formatLyricTime(line.startTime)}
{lineNo}
</span>
)}

{/* 歌词文本 */}
<div className="min-w-0 flex-1">
{line.isBg ? (
<BgLine line={line} />
) : (
<>
<WordCells line={line} />
<div className="min-w-0 flex-1 space-y-0.5">
{group.map((line, li) => (
<div
key={li}
className={`flex gap-3 ${line.isDuet ? 'flex-row-reverse text-right' : ''}`}
>
{/* 时间轴 */}
<span
className={`shrink-0 pt-0.5 font-mono text-xs font-semibold text-primary sm:text-sm ${
line.isDuet ? 'text-right' : ''
}`}
style={{ minWidth: '72px' }}
>
{formatLyricTime(line.startTime)}
</span>

{/* 翻译 */}
{line.translatedLyric && (
<div
className={`mt-1 flex items-start gap-1.5 text-sm text-primary ${
line.isDuet ? 'flex-row-reverse justify-start text-right' : 'pl-1'
}`}
>
<Languages className="mt-0.5 h-3.5 w-3.5 shrink-0" />
<span>{line.translatedLyric}</span>
</div>
)}
{/* 歌词文本 */}
<div className="min-w-0 flex-1">
{line.isBg ? (
<BgLine line={line} />
) : (
<>
<WordCells line={line} />

{/* 翻译 */}
{line.translatedLyric && (
<div
className={`mt-1 flex items-start gap-1.5 text-sm text-primary ${
line.isDuet ? 'flex-row-reverse justify-start text-right' : 'pl-1'
}`}
>
<Languages className="mt-0.5 h-3.5 w-3.5 shrink-0" />
<span>{line.translatedLyric}</span>
</div>
)}

{/* 整行音译 */}
{line.romanLyric && (
<div
className={`mt-1 flex items-start gap-1.5 text-xs text-purple-500 ${
line.isDuet ? 'flex-row-reverse justify-start text-right' : 'pl-1'
}`}
>
<TypeIcon className="mt-0.5 h-3.5 w-3.5 shrink-0" />
<span className="font-mono break-words">{line.romanLyric}</span>
</div>
{/* 整行音译 */}
{line.romanLyric && (
<div
className={`mt-1 flex items-start gap-1.5 text-xs text-purple-500 ${
line.isDuet ? 'flex-row-reverse justify-start text-right' : 'pl-1'
}`}
>
<TypeIcon className="mt-0.5 h-3.5 w-3.5 shrink-0" />
<span className="font-mono break-words">{line.romanLyric}</span>
</div>
)}
</>
)}
</>
)}
</div>
</div>
</div>
))}
</div>
))}
</motion.div>
))}
</motion.div>
);
})}
</motion.div>

{/* 操作按钮 */}
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/detail/DetailTopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Link, useNavigate } from 'react-router-dom';
import { ArrowLeft, Users, X } from 'lucide-react';
import { useAuth } from '@/hooks/useAuth';
import { NotificationBellResponsive } from '@/components/notification/NotificationBellResponsive';
import { UserMenu } from '@/components/UserMenu';
import { Separator } from '@/components/ui/separator';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { ScrollArea } from '@/components/ui/scroll-area';
import type { Viewer } from '@/hooks/useViewers';

/** 在线观看者指示器 */
function ViewersIndicator({
export function ViewersIndicator({
count,
viewers,
showModal,
Expand Down Expand Up @@ -130,6 +131,7 @@ export function DetailTopBar({
showModal={showModal}
setShowModal={setShowModal}
/>
<UserMenu />
</div>
</div>
</header>
Expand Down
43 changes: 42 additions & 1 deletion frontend/src/components/review/LyricEditorHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ import { ToolMode, toolModeAtom } from '@/editor/states/main.ts';
import { metadataEditorDialogAtom } from '@/editor/states/dialogs.ts';
import { EditorMenuBar } from '@/components/review/EditorTopMenu';
import { UserMenu } from '@/components/UserMenu';
import { ViewersIndicator } from '@/components/detail/DetailTopBar';
import { statusMeta } from '@/components/submission/status';
import type { Viewer } from '@/hooks/useViewers';
import { cn } from '@/lib/utils';

/** 在线观看者数据 */
export interface EditorViewersState {
count: number;
viewers: Viewer[];
showModal: boolean;
setShowModal: (open: boolean) => void;
}

export interface EditorHeaderProps {
title: string;
subtitle: string;
Expand All @@ -26,6 +37,10 @@ export interface EditorHeaderProps {
mode?: 'review' | 'creator';
/** 无参考音频:隐藏打轴与音频相关控件 */
textOnly?: boolean;
/** 当前投稿状态(随 WS 广播实时同步) */
status?: string;
/** 在线观看者(WS) */
viewers?: EditorViewersState;
}

/** 参考音频选择器 */
Expand Down Expand Up @@ -132,9 +147,14 @@ export function LyricEditorHeader({
saving,
mode = 'review',
textOnly = false,
status,
viewers,
}: EditorHeaderProps) {
const [, setMetadataEditorDialog] = useAtom(metadataEditorDialogAtom);
const isCreator = mode === 'creator';
const statusBadge = status
? (statusMeta[status] ?? { label: status, className: 'bg-surface-2 text-ink-2' })
: null;

return (
<div className="z-50 flex shrink-0 items-center justify-between gap-3 border-b border-border bg-background px-3 py-2">
Expand All @@ -150,7 +170,20 @@ export function LyricEditorHeader({
</Button>
<div className="min-w-0">
<div className="truncate text-sm font-medium">{title}</div>
<div className="truncate text-[11px] text-ink-3">{subtitle}</div>
<div className="flex min-w-0 items-center gap-1.5">
{statusBadge && (
<span
className={cn(
'shrink-0 rounded px-1.5 py-0.5 text-[10px] font-medium',
statusBadge.className
)}
title="投稿状态"
>
{statusBadge.label}
</span>
)}
<div className="truncate text-[11px] text-ink-3">{subtitle}</div>
</div>
</div>
</div>

Expand Down Expand Up @@ -188,6 +221,14 @@ export function LyricEditorHeader({
{saving ? <Loader2 className="size-4 animate-spin" /> : <Save className="size-4" />}
{isCreator ? '提交修改' : '提交'}
</Button>
{viewers && (
<ViewersIndicator
count={viewers.count}
viewers={viewers.viewers}
showModal={viewers.showModal}
setShowModal={viewers.setShowModal}
/>
)}
<UserMenu />
</div>
</div>
Expand Down
Loading
Loading