@@ -302,19 +302,22 @@ export const alignLyrics = (
302302/**
303303 * 对齐歌词的翻译和音译
304304 * 根据开始时间将同一时间的多行歌词分为一组,第一行作为主句,第二行作为翻译,第三行作为音译
305- * @param lyrics 未设置翻译和音译的歌词数据
305+ * @param lyrics 未设置翻译和音译的歌词数据 (Readonly)
306306 * @param endTime 对齐时如何处理附加行的结束时间(忽略、匹配、设为最大值)
307307 * @param maxTimeDiff 允许匹配的最大时间差(单位:毫秒),超过该时间差的行将不会被视为同一行
308- * @returns 对齐后的歌词数据
308+ * @param skipSort 跳过排序步骤以只对齐相邻行
309+ * @returns 对齐后的歌词数据 (新副本)
309310 */
310311export const alignLyricLines = (
311- lyrics : LyricLine [ ] ,
312+ lyrics : Readonly < LyricLine [ ] > ,
312313 {
313314 endTime = "set" ,
314315 maxTimeDiff = 0 , // 默认严格匹配
316+ skipSort = false ,
315317 } : Partial < {
316318 endTime : "ignore" | "match" | "set" ;
317319 maxTimeDiff : number ;
320+ skipSort : boolean ;
318321 } > = { } ,
319322) : LyricLine [ ] => {
320323 if ( ! lyrics . length ) return [ ] ;
@@ -330,15 +333,19 @@ export const alignLyricLines = (
330333 const isTimeMatch = ( baseLine : LyricLine | undefined , addLine : LyricLine | undefined ) => {
331334 if ( ! baseLine || ! addLine ) return false ;
332335 const timeDiff = Math . abs ( toStartTime ( baseLine ) - toStartTime ( addLine ) ) ;
336+ if ( ! Number . isFinite ( timeDiff ) ) return false ;
333337 if ( timeDiff > maxTimeDiff ) return false ;
334338 if ( endTime === "match" ) {
335339 const endTimeDiff = Math . abs ( toEndTime ( baseLine ) - toEndTime ( addLine ) ) ;
340+ if ( ! Number . isFinite ( endTimeDiff ) ) return false ;
336341 if ( endTimeDiff > maxTimeDiff ) return false ;
337342 }
338343 return true ;
339344 } ;
340345 // 按开始时间分组
341- const sorted = [ ...lyrics ] . sort ( ( a , b ) => toStartTime ( a ) - toStartTime ( b ) ) ;
346+ const sorted = skipSort
347+ ? lyrics
348+ : [ ...lyrics ] . sort ( ( a , b ) => toStartTime ( a ) - toStartTime ( b ) ) ;
342349 const groups : LyricLine [ ] [ ] = [ ] ;
343350 for ( const line of sorted ) {
344351 const last = groups [ groups . length - 1 ] ?. [ 0 ] ;
@@ -372,7 +379,8 @@ export const alignLyricLines = (
372379 } ;
373380 // 组装:第 1 行主句;第 2 行翻译;第 3 行音译;其余行舍去
374381 const aligned = groups . map ( ( group ) => {
375- const base = { ...group [ 0 ] } as LyricLine ;
382+ // 使用 cloneDeep 保证数据不可变性
383+ const base = cloneDeep ( group [ 0 ] ) as LyricLine ;
376384 const tran = group [ 1 ] ;
377385 const roma = group [ 2 ] ;
378386 mergeAddLine ( base , tran , "translatedLyric" ) ;
0 commit comments