11import { cloneDeep } from 'lodash-es'
22
3- import type { MetadataKey , Persist } from '@core/types'
3+ import type { LyricLine , MetadataKey , Persist } from '@core/types'
44
5- import { useCoreStore , useRuntimeStore } from '@states/stores'
5+ import { useCoreStore , usePrefStore , useRuntimeStore } from '@states/stores'
6+
7+ import { alignLineTime } from '@utils/alignLineSylTime'
8+ import { pairwise } from '@utils/pairwise'
69
710import { editHistory } from './history'
811
@@ -23,12 +26,36 @@ export function applyPersist(data: Persist) {
2326
2427export function collectPersist ( ) : Persist {
2528 const coreStore = useCoreStore ( )
29+ const prefStore = usePrefStore ( )
30+ const lines = cloneDeep ( coreStore . lyricLines )
31+ if ( prefStore . hideLineTiming ) lines . forEach ( ( line ) => alignLineTime ( line ) )
32+ if ( prefStore . autoConnectLineTimes ) connectLineTimes ( lines , prefStore . autoConnectThresholdMs )
2633 const outputData : Persist = {
2734 metadata : [ ...coreStore . metadata ] . reduce (
2835 ( obj , { key, values } ) => ( ( obj [ key ] = [ ...values ] ) , obj ) ,
2936 { } as Record < MetadataKey , string [ ] > ,
3037 ) ,
31- lines : cloneDeep ( coreStore . lyricLines ) ,
38+ lines,
3239 }
3340 return outputData
3441}
42+
43+ function connectLineTimes ( lines : LyricLine [ ] , thresMs : number ) {
44+ function classifyLines ( lines : LyricLine [ ] ) {
45+ type Groups = [ normal : LyricLine [ ] , duet : LyricLine [ ] , bg : LyricLine [ ] , duetBg : LyricLine [ ] ]
46+ const groups : Groups = [ [ ] , [ ] , [ ] , [ ] ]
47+ const classifier = ( line : LyricLine ) : 0 | 1 | 2 | 3 => {
48+ if ( line . duet && line . background ) return 3
49+ if ( line . duet ) return 1
50+ if ( line . background ) return 2
51+ return 0
52+ }
53+ lines . forEach ( ( l ) => groups [ classifier ( l ) ] . push ( l ) )
54+ return groups
55+ }
56+ function connectLines ( lines : LyricLine [ ] ) {
57+ for ( const [ prev , curr ] of pairwise ( lines ) )
58+ if ( curr . startTime - prev . endTime <= thresMs ) prev . endTime = curr . startTime
59+ }
60+ classifyLines ( lines ) . forEach ( ( group ) => connectLines ( group ) )
61+ }
0 commit comments