|
1 | 1 | import type { PlatformConfig } from '@/entrypoints/subtitles.content/platforms' |
2 | 2 | import type { SubtitlesFetcher } from '@/utils/subtitles/fetchers/types' |
3 | | -import type { SubtitlesFragment } from '@/utils/subtitles/types' |
| 3 | +import type { SubtitlesFragment, SubtitlesTranslationBlock } from '@/utils/subtitles/types' |
4 | 4 | import { i18n } from '#imports' |
5 | 5 | import { toast } from 'sonner' |
6 | 6 | import { HIDE_NATIVE_CAPTIONS_STYLE_ID, NAVIGATION_HANDLER_DELAY, TRANSLATE_BUTTON_CONTAINER_ID } from '@/utils/constants/subtitles' |
7 | 7 | import { waitForElement } from '@/utils/dom/wait-for-element' |
8 | 8 | import { ToastSubtitlesError } from '@/utils/subtitles/errors' |
| 9 | +import { createSubtitlesBlocks, findNextBlockToTranslate, updateBlockState } from '@/utils/subtitles/processor/block-strategy' |
9 | 10 | import { translateSubtitles } from '@/utils/subtitles/processor/translator' |
| 11 | +import { currentSubtitleAtom, subtitlesStore, subtitlesTranslationBlocksAtom } from './atoms' |
10 | 12 | import { renderSubtitlesTranslateButton } from './renderer/render-translate-button' |
11 | 13 | import { SubtitlesScheduler } from './subtitles-scheduler' |
12 | 14 |
|
@@ -39,12 +41,14 @@ export class UniversalVideoAdapter { |
39 | 41 |
|
40 | 42 | private resetSubtitlesData() { |
41 | 43 | this.subtitlesScheduler?.reset() |
| 44 | + this.stopBlockMonitoring() |
42 | 45 | this.originalSubtitles = [] |
43 | 46 | this.subtitlesFetcher.cleanup() |
44 | 47 | } |
45 | 48 |
|
46 | 49 | private resetForNavigation() { |
47 | 50 | this.destroyScheduler() |
| 51 | + this.stopBlockMonitoring() |
48 | 52 | this.originalSubtitles = [] |
49 | 53 | this.cachedVideoId = null |
50 | 54 | this.subtitlesFetcher.cleanup() |
@@ -194,17 +198,96 @@ export class UniversalVideoAdapter { |
194 | 198 | try { |
195 | 199 | this.subtitlesScheduler?.setState('processing') |
196 | 200 |
|
197 | | - const translated = await translateSubtitles(this.originalSubtitles) |
| 201 | + const subtitlesBlocks = createSubtitlesBlocks(this.originalSubtitles) |
| 202 | + subtitlesStore.set(subtitlesTranslationBlocksAtom, subtitlesBlocks) |
198 | 203 |
|
199 | | - if (this.subtitlesScheduler) { |
200 | | - this.subtitlesScheduler.supplementSubtitles(translated) |
| 204 | + const video = this.subtitlesScheduler?.getVideoElement() |
| 205 | + const currentTimeMs = (video?.currentTime ?? 0) * 1000 |
| 206 | + const firstBlockToTranslate = findNextBlockToTranslate(subtitlesBlocks, currentTimeMs) |
| 207 | + |
| 208 | + if (firstBlockToTranslate) { |
| 209 | + await this.translateSubtitlesBlock(firstBlockToTranslate) |
201 | 210 | } |
202 | 211 |
|
203 | | - this.subtitlesScheduler?.setState('completed') |
| 212 | + this.startBlockMonitoring() |
| 213 | + this.subtitlesScheduler?.setState('idle') |
| 214 | + } |
| 215 | + catch (error) { |
| 216 | + const errorMessage = error instanceof Error ? error.message : String(error) |
| 217 | + this.subtitlesScheduler?.setState('error', { message: errorMessage }) |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + private async translateSubtitlesBlock(batch: SubtitlesTranslationBlock) { |
| 222 | + const subtitlesBlocks = subtitlesStore.get(subtitlesTranslationBlocksAtom) |
| 223 | + subtitlesStore.set(subtitlesTranslationBlocksAtom, updateBlockState(subtitlesBlocks, batch.id, 'processing')) |
| 224 | + |
| 225 | + const currentSubtitle = subtitlesStore.get(currentSubtitleAtom) |
| 226 | + if (!currentSubtitle) { |
| 227 | + this.subtitlesScheduler?.setState('processing') |
| 228 | + } |
| 229 | + |
| 230 | + try { |
| 231 | + const translated = await translateSubtitles(batch.fragments) |
| 232 | + |
| 233 | + const updatedBatches = subtitlesStore.get(subtitlesTranslationBlocksAtom) |
| 234 | + subtitlesStore.set( |
| 235 | + subtitlesTranslationBlocksAtom, |
| 236 | + updateBlockState(updatedBatches, batch.id, 'completed'), |
| 237 | + ) |
| 238 | + |
| 239 | + this.subtitlesScheduler?.supplementSubtitles(translated) |
| 240 | + this.subtitlesScheduler?.setState('idle') |
204 | 241 | } |
205 | 242 | catch (error) { |
| 243 | + const updatedBatches = subtitlesStore.get(subtitlesTranslationBlocksAtom) |
| 244 | + subtitlesStore.set( |
| 245 | + subtitlesTranslationBlocksAtom, |
| 246 | + updateBlockState(updatedBatches, batch.id, 'error'), |
| 247 | + ) |
| 248 | + |
206 | 249 | const errorMessage = error instanceof Error ? error.message : String(error) |
207 | 250 | this.subtitlesScheduler?.setState('error', { message: errorMessage }) |
208 | 251 | } |
209 | 252 | } |
| 253 | + |
| 254 | + private handleBlockCheck = () => { |
| 255 | + if (!this.subtitlesScheduler) |
| 256 | + return |
| 257 | + |
| 258 | + const video = this.subtitlesScheduler.getVideoElement() |
| 259 | + |
| 260 | + const currentTimeMs = video.currentTime * 1_000 |
| 261 | + const blocks = subtitlesStore.get(subtitlesTranslationBlocksAtom) |
| 262 | + |
| 263 | + if (blocks.some(b => b.state === 'processing')) |
| 264 | + return |
| 265 | + |
| 266 | + const nextBlock = findNextBlockToTranslate(blocks, currentTimeMs) |
| 267 | + if (nextBlock) { |
| 268 | + void this.translateSubtitlesBlock(nextBlock) |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + private startBlockMonitoring() { |
| 273 | + if (!this.subtitlesScheduler) |
| 274 | + return |
| 275 | + |
| 276 | + const video = this.subtitlesScheduler.getVideoElement() |
| 277 | + |
| 278 | + video.addEventListener('seeked', this.handleBlockCheck) |
| 279 | + video.addEventListener('timeupdate', this.handleBlockCheck) |
| 280 | + } |
| 281 | + |
| 282 | + private stopBlockMonitoring() { |
| 283 | + subtitlesStore.set(subtitlesTranslationBlocksAtom, []) |
| 284 | + |
| 285 | + if (!this.subtitlesScheduler) |
| 286 | + return |
| 287 | + |
| 288 | + const video = this.subtitlesScheduler.getVideoElement() |
| 289 | + |
| 290 | + video.removeEventListener('seeked', this.handleBlockCheck) |
| 291 | + video.removeEventListener('timeupdate', this.handleBlockCheck) |
| 292 | + } |
210 | 293 | } |
0 commit comments