Skip to content

Commit 94d2d60

Browse files
committed
隐藏行时间戳功能
1 parent 2054eda commit 94d2d60

File tree

6 files changed

+60
-9
lines changed

6 files changed

+60
-9
lines changed

src/core/convert/formats/lrc.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { LyricLine, Persist } from '@core/types'
33
import { coreCreate } from '@states/stores/core'
44

55
import { str2ms } from '@utils/formatTime'
6+
import { pairwise } from '@utils/pairwise'
67

78
import MANIFEST from '../manifest.json'
89
import type { Convert as CV } from '../types'
@@ -67,11 +68,8 @@ export function parseLRC(lrc: string): Persist {
6768
})
6869
})
6970
lyricLines.sort((a, b) => a.startTime - b.startTime)
70-
for (let i = lyricLines.length - 1; i > 0; i--) {
71-
const line = lyricLines[i]!
72-
const prevLine = lyricLines[i - 1]!
73-
prevLine.endTime = line.startTime
74-
prevLine.syllables[0]!.endTime = line.startTime
71+
for (const [prev, curr] of pairwise(lyricLines)) {
72+
prev.endTime = prev.syllables[0]!.endTime = curr.startTime
7573
}
7674
if (lyricLines.length && metadata.length && metadata.length.length) {
7775
const length = str2ms(metadata.length[0]!)

src/core/pref/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface PreferenceSchema {
1818
alwaysIgnoreBackground: boolean
1919
hideLineTiming: boolean
2020
autoConnectLineTimes: boolean
21+
autoConnectThresholdMs: number
2122
// Roman
2223
sylRomanEnabled: boolean
2324
swapTranslateRoman: boolean
@@ -39,6 +40,7 @@ export const getDefaultPref = (): PreferenceSchema => ({
3940
alwaysIgnoreBackground: false,
4041
hideLineTiming: false,
4142
autoConnectLineTimes: false,
43+
autoConnectThresholdMs: 100,
4244
sylRomanEnabled: false,
4345
swapTranslateRoman: false,
4446
sidebarWidth: 360,

src/states/services/port.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { 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

710
import { editHistory } from './history'
811

@@ -23,12 +26,36 @@ export function applyPersist(data: Persist) {
2326

2427
export 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+
}

src/ui/editor/content/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export function useContentCtxItems({ lineIndex, sylIndex }: ContentCtxStates) {
8888
runtimeStore.selectLine(...newLines)
8989
nextTick(() =>
9090
staticStore.scrollToHook?.(
91-
newLines.reduce((acc, line) => Math.max(acc, coreStore.lyricLines.indexOf(line)), 0),
91+
Math.max(0, ...newLines.map((l) => coreStore.lyricLines.indexOf(l))),
9292
{ align: 'nearest' },
9393
),
9494
)

src/ui/sidebar/tabs/preference/PreferenceTab.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ const displayName = __APP_DISPLAY_NAME__
121121
:disabled="!prefStore.hideLineTiming"
122122
experimental
123123
/>
124+
<PrefNumberItem
125+
pref-key="autoConnectThresholdMs"
126+
label="连接阈值"
127+
desc="连接相邻行时允许的最大间隔 (毫秒)"
128+
:min="0"
129+
:max="5000"
130+
:disabled="!prefStore.hideLineTiming || !prefStore.autoConnectLineTimes"
131+
experimental
132+
/>
124133
</div>
125134
<div class="pref-group">
126135
<div class="pref-group-title">音译</div>

src/utils/pairwise.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Returns consecutive pairs from the given iterable.
3+
*
4+
* Example: `0, 1, 2, 3` -> `[0, 1], [1, 2], [2, 3]`
5+
*/
6+
export function* pairwise<T>(iterable: Iterable<T>): Generator<[T, T], void, unknown> {
7+
let prev: T | undefined
8+
let hasPrev = false
9+
10+
for (const curr of iterable) {
11+
if (hasPrev) yield [prev!, curr]
12+
prev = curr
13+
hasPrev = true
14+
}
15+
}

0 commit comments

Comments
 (0)