@sublane/supplier-fluent-read 0.1.2 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sublane/supplier-fluent-read",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"exports": {
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"license": "GPL-3.0",
|
|
48
48
|
"description": "FluentRead 供应商(Unofficial):视频字幕翻译垂直切片——vendor 树经 patches 维护,adapter 实现框架契约",
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@sublane/core": "^0.
|
|
50
|
+
"@sublane/core": "^0.3.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@sublane/core": "workspace:*"
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
diff --git a/src/features/video-subtitle/content/translationCache.ts b/src/features/video-subtitle/content/translationCache.ts
|
|
2
|
+
index d07d213..7d4f774 100644
|
|
3
|
+
--- a/src/features/video-subtitle/content/translationCache.ts
|
|
4
|
+
+++ b/src/features/video-subtitle/content/translationCache.ts
|
|
5
|
+
@@ -7,14 +7,22 @@
|
|
6
|
+
import {VideoTranslationScheduler} from './translationScheduler';
|
|
7
|
+
import {selectVideoSubtitlePretranslationCues} from './subtitleLogic';
|
|
8
|
+
import type {VideoSubtitleCue} from './youtubeSubtitleData';
|
|
9
|
+
+/** [wep patch 0015] 活动译文缓存注册表:adapter 侧据此发现 live 管线的活动实例(F-26 cue 流数据源)。 */
|
|
10
|
+
+export const videoTranslationCaches = new Set<VideoTranslationCache>();
|
|
11
|
+
+/** [wep patch 0015] 译文事件监听:缓存写入(译文产出)时通知,source 为规范化原文,translation 为已译文本。 */
|
|
12
|
+
+export type VideoTranslationListener = (source: string, translation: string) => void;
|
|
13
|
+
export class VideoTranslationCache {
|
|
14
|
+
private readonly translatedVideoCache = new Map<string, string>();
|
|
15
|
+
private readonly inFlightVideoTranslations = new Map<string, Promise<string>>();
|
|
16
|
+
private readonly videoTranslationFailures = new Map<string, {attempts: number; retryAt: number}>();
|
|
17
|
+
private readonly translationScheduler: VideoTranslationScheduler;
|
|
18
|
+
private pretranslationCacheVersion = 0;
|
|
19
|
+
+ private readonly translationListeners = new Set<VideoTranslationListener>();
|
|
20
|
+
+ /** [wep patch 0015] 实例创建时刻(adapter 排序取最新实例)。 */
|
|
21
|
+
+ readonly createdAt = Date.now();
|
|
22
|
+
constructor(translate: (text: string, signal: AbortSignal) => Promise<string>) {
|
|
23
|
+
this.translationScheduler = new VideoTranslationScheduler(translate);
|
|
24
|
+
+ videoTranslationCaches.add(this);
|
|
25
|
+
}
|
|
26
|
+
clear(): void {
|
|
27
|
+
this.translatedVideoCache.clear();
|
|
28
|
+
@@ -37,6 +45,11 @@ export class VideoTranslationCache {
|
|
29
|
+
peek(source: string): string | undefined {
|
|
30
|
+
return this.translatedVideoCache.get(source.replace(/[\s\u3000]+/g, ' ').trim());
|
|
31
|
+
}
|
|
32
|
+
+ /** [wep patch 0015] 订阅译文产出事件(F-27/P1 数据面:adapter 经此读取 vendor 翻译节奏的产出)。 */
|
|
33
|
+
+ subscribeTranslations(listener: VideoTranslationListener): () => void {
|
|
34
|
+
+ this.translationListeners.add(listener);
|
|
35
|
+
+ return () => this.translationListeners.delete(listener);
|
|
36
|
+
+ }
|
|
37
|
+
retainPrefetches(sources: readonly string[]): void {
|
|
38
|
+
const keys = new Set(sources.map(source => source.replace(/[\s\u3000]+/g, ' ').trim()));
|
|
39
|
+
for (const source of this.translationScheduler.retainPrefetches(keys)) {
|
|
40
|
+
@@ -79,6 +92,7 @@ export class VideoTranslationCache {
|
|
41
|
+
if (requestVersion === this.pretranslationCacheVersion) {
|
|
42
|
+
this.videoTranslationFailures.delete(key);
|
|
43
|
+
this.translatedVideoCache.set(key, result);
|
|
44
|
+
+ for (const listener of this.translationListeners) listener(key, result);
|
|
45
|
+
if (this.translatedVideoCache.size > 160) {
|
|
46
|
+
const oldestKey = this.translatedVideoCache.keys().next().value;
|
|
47
|
+
if (oldestKey) this.translatedVideoCache.delete(oldestKey);
|
package/src/adapter.ts
CHANGED
|
@@ -6,13 +6,15 @@
|
|
|
6
6
|
* 模块边界:只认识框架契约与 vendor 公共面(public.ts / config store);不翻译、不解析字幕、不渲染。
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
9
|
-
PluginMountContext, SupplierRuntimeConfig, VideoSubtitlePluginInstance,
|
|
9
|
+
CueStreamEvent, PluginMountContext, SupplierRuntimeConfig, VideoSubtitlePluginInstance,
|
|
10
10
|
} from '@sublane/core'
|
|
11
11
|
import browser from 'webextension-polyfill'
|
|
12
12
|
import { config, configReady, requestConfigPatch } from '../vendor/src/services/config/store'
|
|
13
13
|
import { mountVideoSubtitleTranslation, isSupportedVideoPage } from '../vendor/src/features/video-subtitle/public'
|
|
14
|
+
import { videoTranslationCaches } from '../vendor/src/features/video-subtitle/content/translationCache'
|
|
14
15
|
import { fluentReadHostRequirements } from './manifest'
|
|
15
16
|
import { laneToAppearancePosition } from './lane'
|
|
17
|
+
import { createDetachedInstance as createFluentDetachedInstance } from './detached'
|
|
16
18
|
import { attachPanelDrag } from './panel-drag'
|
|
17
19
|
import { fluentReadSettingsSchema } from './settings-schema'
|
|
18
20
|
|
|
@@ -192,6 +194,26 @@ export function createFluentReadInstance(ctx: PluginMountContext): VideoSubtitle
|
|
|
192
194
|
void requestConfigPatch({ ...(laneChanged ? lanePatch(next.lane) : {}), ...diffSettingsPatch(next.settings) }, browser.runtime.sendMessage.bind(browser.runtime))
|
|
193
195
|
.catch((error) => console.warn('[fluent-read] 配置同步失败', error))
|
|
194
196
|
},
|
|
197
|
+
// 缺口2(F-26/0.3.1):fluent 译文数据面——vendor 翻译缓存产出事件流(patch 0015 注册表)。
|
|
198
|
+
// 语义声明:YouTube 默认 roll-up DOM 模式无 cue 区间概念,SubtitleCue.tsMs = 译文首见时刻
|
|
199
|
+
// (宿主 video currentTime);reset 为空快照(句子译文无时间轴可回放),宿主以 upsert 累积。
|
|
200
|
+
subscribeCues(listener: (event: CueStreamEvent) => void): () => void {
|
|
201
|
+
const cache = [...videoTranslationCaches].sort((a, b) => a.createdAt - b.createdAt).at(-1)
|
|
202
|
+
listener({ type: 'reset', cues: [], engine: 'fluent-read' })
|
|
203
|
+
if (!cache) return () => {}
|
|
204
|
+
return cache.subscribeTranslations((source: string, translation: string) => {
|
|
205
|
+
let tsMs = 0
|
|
206
|
+
try {
|
|
207
|
+
const video = document.querySelector<HTMLVideoElement>('video.html5-main-video, video')
|
|
208
|
+
if (video && Number.isFinite(video.currentTime)) tsMs = Math.round(video.currentTime * 1000)
|
|
209
|
+
} catch { /* 主世界不可达时时间轴缺省 0(时间轴语义见上注) */ }
|
|
210
|
+
listener({
|
|
211
|
+
type: 'upsert',
|
|
212
|
+
cues: [{ tsMs, original: source, translations: { 'fluent-read': translation } }],
|
|
213
|
+
engine: 'fluent-read',
|
|
214
|
+
})
|
|
215
|
+
})
|
|
216
|
+
},
|
|
195
217
|
}
|
|
196
218
|
return instance
|
|
197
219
|
}
|
|
@@ -208,6 +230,8 @@ export const fluentReadPlugin = {
|
|
|
208
230
|
bilingualDownload: true,
|
|
209
231
|
sidebar: false,
|
|
210
232
|
manualTimingOffset: true,
|
|
233
|
+
// F-27/P1:支持 detached 渲染(宿主 feature-detect 首选旗标)
|
|
234
|
+
detachedRender: true,
|
|
211
235
|
},
|
|
212
236
|
settingsSchema: fluentReadSettingsSchema,
|
|
213
237
|
contentScriptRequirements: [
|
|
@@ -223,4 +247,6 @@ export const fluentReadPlugin = {
|
|
|
223
247
|
},
|
|
224
248
|
],
|
|
225
249
|
createInstance: createFluentReadInstance,
|
|
250
|
+
// F-27/P1:detached 渲染实例(实现见 ./detached.ts;live 零变化,与 live 并存 R1)
|
|
251
|
+
createDetachedInstance: createFluentDetachedInstance,
|
|
226
252
|
}
|
package/src/detached.ts
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file suppliers/fluent-read/src/detached.ts
|
|
3
|
+
* 文件职责:fluent-read 的 F-27/P1 detached 渲染实例——在宿主提供的任意 video 元素上,以 vendor 原生
|
|
4
|
+
* UI 规则渲染宿主注入的字幕轨(数据注入 + 时钟外驱 + injected-only)。
|
|
5
|
+
* 主要内容:R2 达成机制 = **Shadow DOM 同构**:宿主父级挂 shadow host,shadow root 注入 vendor
|
|
6
|
+
* 全局样式表(installVideoSubtitleStyle 的同一份 CSS 全文),shadow 内重建同构 DOM
|
|
7
|
+
* (layer > panel > original/translation overlay,同款 ID/class/属性)——vendor 全部 ID/class
|
|
8
|
+
* 规则原样命中 shadow 内元素(by construction),且 shadow 边界使同款 ID 与 live 零冲突
|
|
9
|
+
* (R1/G3:live 的 document 级 getElementById 永远看不到 shadow 内部;vendor 卸载路径的
|
|
10
|
+
* querySelectorAll 也扫不进 shadow)。appearance vars 经 vendor 导出纯函数
|
|
11
|
+
* getVideoSubtitleAppearanceCssVars(同函数实例,R2);字号/布局公式复刻 ui.ts:570-619
|
|
12
|
+
* (锚定注释)但以宿主元素为根(R2 通用路径:无 X 探测/原生字幕探测/菜单预留——注入模式
|
|
13
|
+
* 无原生字幕,normalizedCaption 语义即面板内双行);显示模式同源(vendor config,
|
|
14
|
+
* layer 类切换同 applyVideoDisplayState);lane 经 laneToAppearancePosition(G4);
|
|
15
|
+
* karaoke:Q4 裁决终态渲染——textContent 直写(不走逐字装配)。
|
|
16
|
+
* 模块边界:只经 vendor 公开面(导出函数/常量/config 单例只读)与框架契约;live 行为零变化(N2);
|
|
17
|
+
* 不写 vendor config、不触碰 live DOM(R1)。
|
|
18
|
+
*/
|
|
19
|
+
import type {
|
|
20
|
+
CueStreamEvent, DetachedHandle, DetachedInstanceContext, SupplierRuntimeConfig, TrackSource,
|
|
21
|
+
VideoSubtitlePluginInstance,
|
|
22
|
+
} from '@sublane/core'
|
|
23
|
+
import {
|
|
24
|
+
installVideoSubtitleStyle, markVideoUi,
|
|
25
|
+
VIDEO_NORMALIZED_CAPTION_OVERLAY_ID, VIDEO_SUBTITLE_PANEL_ACTIVE_CLASS,
|
|
26
|
+
VIDEO_SUBTITLE_PANEL_ID, VIDEO_TRANSLATION_LAYER_ID, VIDEO_TRANSLATION_OVERLAY_ID,
|
|
27
|
+
} from '../vendor/src/features/video-subtitle/content/ui'
|
|
28
|
+
import { getVideoSubtitleAppearanceCssVars, normalizeVideoSubtitleAppearance } from '../vendor/src/core/config/videoSubtitleAppearance'
|
|
29
|
+
import { fitVideoSubtitleFontSize } from '../vendor/src/features/video-subtitle/content/subtitleLayout'
|
|
30
|
+
import { config } from '../vendor/src/services/config/store'
|
|
31
|
+
import { laneToAppearancePosition } from './lane'
|
|
32
|
+
import { cuesFromVideoSubtitleCues, currentCueAt, toVideoSubtitleCues } from './fluent-track'
|
|
33
|
+
|
|
34
|
+
const PANEL_ACTIVE_CLASS = VIDEO_SUBTITLE_PANEL_ACTIVE_CLASS
|
|
35
|
+
|
|
36
|
+
/** detached 面板在同构 DOM 上的定位/样式应用(宿主元素为根;公式锚定 ui.ts:570-619,
|
|
37
|
+
* 差异:无 X viewport/无菜单预留/无原生字幕探测——注入模式语义即 normalizedCaption 双行)。 */
|
|
38
|
+
function layoutPanel(panel: HTMLElement, video: HTMLVideoElement, bottomOffsetPercent: number): number {
|
|
39
|
+
const hostRect = video.getBoundingClientRect()
|
|
40
|
+
const hostWidth = hostRect.width || 960
|
|
41
|
+
const hostHeight = hostRect.height || 540
|
|
42
|
+
const appearance = normalizeVideoSubtitleAppearance(config.videoSubtitleAppearance)
|
|
43
|
+
Object.entries(getVideoSubtitleAppearanceCssVars(appearance)).forEach(([name, value]) => panel.style.setProperty(name, value))
|
|
44
|
+
panel.style.width = 'max-content'
|
|
45
|
+
panel.style.setProperty('max-width', `${Math.max(0, hostWidth * appearance.maxWidth / 100)}px`, 'important')
|
|
46
|
+
// 缺口4-居中:宿主根下水平居中(CSS 方式,resize 免 JS;live 用实测宽度居中 ui.ts:609-615——
|
|
47
|
+
// detached 的 layer 内联了 inset 0,此三行即等价居中;声明为 detached 居中实现)
|
|
48
|
+
panel.style.left = '0'
|
|
49
|
+
panel.style.right = '0'
|
|
50
|
+
panel.style.margin = '0 auto'
|
|
51
|
+
const baseFontSize = Math.min(Math.max(hostWidth * 0.022, 16), 30)
|
|
52
|
+
const fontScale = appearance.fontScale / 100
|
|
53
|
+
panel.style.setProperty('--fluent-read-video-subtitle-font-size', `${baseFontSize * fontScale}px`)
|
|
54
|
+
// 车道覆盖(G4):detached lane → bottomOffset%;appearance.bottomOffset===10 的 clamp 特例同款(ui.ts:589)
|
|
55
|
+
const requestedOffset = bottomOffsetPercent === 10
|
|
56
|
+
? Math.min(Math.max(hostHeight * 0.1, 52), 96)
|
|
57
|
+
: Math.max(12, hostHeight * bottomOffsetPercent / 100)
|
|
58
|
+
const offset = Math.min(requestedOffset, Math.max(0, hostHeight - 24))
|
|
59
|
+
panel.style.setProperty('--fluent-read-video-subtitle-bottom', `${offset}px`)
|
|
60
|
+
panel.style.setProperty('top', appearance.position === 'top' ? `${offset}px` : appearance.position === 'center' ? `${hostHeight / 2}px` : 'auto', 'important')
|
|
61
|
+
panel.style.setProperty('bottom', appearance.position === 'bottom' ? 'var(--fluent-read-video-subtitle-bottom)' : 'auto', 'important')
|
|
62
|
+
const panelMaxHeight = Math.min(
|
|
63
|
+
Math.max(0, hostHeight - (appearance.position === 'center' ? 24 : offset + 12)),
|
|
64
|
+
Math.round(hostHeight * 0.42),
|
|
65
|
+
)
|
|
66
|
+
panel.style.setProperty('max-height', `${panelMaxHeight}px`, 'important')
|
|
67
|
+
panel.style.transform = appearance.position === 'center' ? 'translateY(-50%)' : 'none'
|
|
68
|
+
return baseFontSize * fontScale
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** F-27/P1:fluent-read detached 渲染实例(F-27 契约的可选实现)。 */
|
|
72
|
+
export async function createDetachedInstance(
|
|
73
|
+
ctx: DetachedInstanceContext,
|
|
74
|
+
video: HTMLVideoElement,
|
|
75
|
+
source: TrackSource,
|
|
76
|
+
options?: DetachedMountOptions,
|
|
77
|
+
): Promise<VideoSubtitlePluginInstance & { detach(): DetachedHandle }> {
|
|
78
|
+
// ①vendor 全局样式表就位(幂等;live 同款依赖)并取全文注入 shadow(R2 by construction)。
|
|
79
|
+
// 缺口4-style 所有权:仅当 style 由本 detached 创建时,dispose 才在"无 live DOM 依赖"前提下移除;
|
|
80
|
+
// live 先于 detached 挂载时 style 已存在(非本实例创建),dispose 不动它(live 仍需要)。
|
|
81
|
+
let styleOwnedByDetached = false
|
|
82
|
+
if (!document.getElementById('fluent-read-video-subtitle-style')) {
|
|
83
|
+
installVideoSubtitleStyle()
|
|
84
|
+
styleOwnedByDetached = true
|
|
85
|
+
}
|
|
86
|
+
const vendorCss = document.getElementById('fluent-read-video-subtitle-style')?.textContent ?? ''
|
|
87
|
+
|
|
88
|
+
// ②数据面:注入轨 → vendor cue(时间轴数据;R5 缺译→无 translation 字段)
|
|
89
|
+
let currentSource: TrackSource = source
|
|
90
|
+
let vendorCues = toVideoSubtitleCues((await currentSource.getTrack().catch(() => null))?.cues ?? [])
|
|
91
|
+
const unsubscribers: Array<() => void> = []
|
|
92
|
+
// 缺口3:有效轨回显流的订阅登记(数据变更点 notify——见 ingest/watchSource)
|
|
93
|
+
const cueListeners = new Set<(event: CueStreamEvent) => void>()
|
|
94
|
+
const notifyCueUpsert = () => {
|
|
95
|
+
const cues = cuesFromVideoSubtitleCues(vendorCues)
|
|
96
|
+
for (const listener of cueListeners) listener({ type: 'upsert', cues })
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ③shadow host(宿主 video 父级,Q5 默认;host 不带 vendor ID——避免 document 级误命中)
|
|
100
|
+
const parentEl = video.parentElement ?? document.body
|
|
101
|
+
const computedStyle = window.getComputedStyle(parentEl)
|
|
102
|
+
if (computedStyle.position === 'static') parentEl.style.position = 'relative'
|
|
103
|
+
const shadowHost = document.createElement('div')
|
|
104
|
+
shadowHost.style.cssText = 'position:absolute;inset:0;pointer-events:none;overflow:hidden;z-index:2147483645;'
|
|
105
|
+
const shadowRoot = shadowHost.attachShadow({ mode: 'open' })
|
|
106
|
+
const styleEl = document.createElement('style')
|
|
107
|
+
styleEl.textContent = vendorCss
|
|
108
|
+
shadowRoot.appendChild(styleEl)
|
|
109
|
+
isolateEvents(shadowHost)
|
|
110
|
+
parentEl.appendChild(shadowHost)
|
|
111
|
+
|
|
112
|
+
// ④shadow 内同构 DOM(layer > panel > overlays;同款 ID/class/属性 → vendor 全部规则命中)
|
|
113
|
+
const layer = document.createElement('div')
|
|
114
|
+
layer.id = VIDEO_TRANSLATION_LAYER_ID
|
|
115
|
+
layer.className = 'fluent-read-video-subtitle-layer fluent-read-video-ui notranslate'
|
|
116
|
+
layer.setAttribute('data-fluent-read-ui', 'video-subtitle')
|
|
117
|
+
layer.setAttribute('translate', 'no')
|
|
118
|
+
const panel = document.createElement('div')
|
|
119
|
+
panel.id = VIDEO_SUBTITLE_PANEL_ID
|
|
120
|
+
panel.className = 'fluent-read-video-subtitle-panel fluent-read-video-ui notranslate'
|
|
121
|
+
panel.setAttribute('data-fluent-read-ui', 'video-subtitle')
|
|
122
|
+
panel.setAttribute('translate', 'no')
|
|
123
|
+
markVideoUi(panel)
|
|
124
|
+
const originalOverlay = document.createElement('div')
|
|
125
|
+
originalOverlay.id = VIDEO_NORMALIZED_CAPTION_OVERLAY_ID
|
|
126
|
+
originalOverlay.className = 'fluent-read-video-subtitle-original notranslate'
|
|
127
|
+
originalOverlay.setAttribute('translate', 'no')
|
|
128
|
+
originalOverlay.setAttribute('aria-live', 'polite')
|
|
129
|
+
const translationOverlay = document.createElement('div')
|
|
130
|
+
translationOverlay.id = VIDEO_TRANSLATION_OVERLAY_ID
|
|
131
|
+
translationOverlay.className = 'fluent-read-video-subtitle notranslate'
|
|
132
|
+
translationOverlay.setAttribute('translate', 'no')
|
|
133
|
+
translationOverlay.setAttribute('aria-live', 'polite')
|
|
134
|
+
panel.appendChild(originalOverlay)
|
|
135
|
+
panel.appendChild(translationOverlay)
|
|
136
|
+
layer.appendChild(panel)
|
|
137
|
+
shadowRoot.appendChild(layer)
|
|
138
|
+
|
|
139
|
+
// ⑤显示模式同源(vendor config;layer 类切换同 applyVideoDisplayState 语义)
|
|
140
|
+
const applyDisplayMode = () => {
|
|
141
|
+
const mode = config.videoSubtitleDisplayMode
|
|
142
|
+
layer.classList.toggle('fluent-read-video-display-translation-only', mode === 'translation-only')
|
|
143
|
+
layer.classList.toggle('fluent-read-video-display-original-only', mode === 'original-only')
|
|
144
|
+
layer.classList.toggle('fluent-read-video-display-hidden', config.videoSubtitleVisible === false)
|
|
145
|
+
}
|
|
146
|
+
applyDisplayMode()
|
|
147
|
+
|
|
148
|
+
// ⑥定位(lane 覆盖 + 宿主根公式)与自适应字号(同款 fit 函数)
|
|
149
|
+
const applyLayout = () => {
|
|
150
|
+
const lanePosition = laneToAppearancePosition(ctx.config.lane)
|
|
151
|
+
const appearance = normalizeVideoSubtitleAppearance(config.videoSubtitleAppearance)
|
|
152
|
+
const bottomOffsetPercent = lanePosition?.bottomOffset ?? appearance.bottomOffset
|
|
153
|
+
const sizedFontPx = layoutPanel(panel, video, bottomOffsetPercent)
|
|
154
|
+
panel.classList.add(PANEL_ACTIVE_CLASS)
|
|
155
|
+
fitVideoSubtitleFontSize(sizedFontPx, parseFloat(panel.style.maxHeight || '0') || sizedFontPx * 4,
|
|
156
|
+
(size) => panel.style.setProperty('--fluent-read-video-subtitle-font-size', `${size}px`),
|
|
157
|
+
() => panel.scrollHeight)
|
|
158
|
+
}
|
|
159
|
+
applyLayout()
|
|
160
|
+
|
|
161
|
+
// ⑦渲染(Q4 终态:textContent 直写;:empty 规则隐藏空行;R5 缺译=无译文行)
|
|
162
|
+
const renderAt = (timeMs: number) => {
|
|
163
|
+
const cue = currentCueAt(vendorCues, timeMs)
|
|
164
|
+
originalOverlay.textContent = cue?.text ?? ''
|
|
165
|
+
translationOverlay.textContent = cue?.translation ?? ''
|
|
166
|
+
const active = Boolean(translationOverlay.textContent || originalOverlay.textContent)
|
|
167
|
+
panel.classList.toggle(PANEL_ACTIVE_CLASS, active)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ⑧时钟(R3):宿主 video 唯一权威;禁自建节拍
|
|
171
|
+
const onTime = () => renderAt(video.currentTime * 1000)
|
|
172
|
+
video.addEventListener('timeupdate', onTime)
|
|
173
|
+
video.addEventListener('seeked', onTime)
|
|
174
|
+
onTime()
|
|
175
|
+
|
|
176
|
+
// ⑧b 缺口4:宿主尺寸变化跟踪——字号/可用宽/定位全部依赖宿主 rect,resize 后重排
|
|
177
|
+
// (ResizeObserver,disconnect 归 R6 清理)
|
|
178
|
+
const resizeObserver = new ResizeObserver(() => applyLayout())
|
|
179
|
+
resizeObserver.observe(video)
|
|
180
|
+
|
|
181
|
+
// ⑨流式补译/换源(onUpdate + setSource)
|
|
182
|
+
const ingest = async () => {
|
|
183
|
+
vendorCues = toVideoSubtitleCues((await currentSource.getTrack().catch(() => null))?.cues ?? [])
|
|
184
|
+
renderAt(video.currentTime * 1000)
|
|
185
|
+
notifyCueUpsert()
|
|
186
|
+
}
|
|
187
|
+
const watchSource = () => {
|
|
188
|
+
if (!currentSource.onUpdate) return
|
|
189
|
+
unsubscribers.push(currentSource.onUpdate((track) => {
|
|
190
|
+
vendorCues = toVideoSubtitleCues(track.cues)
|
|
191
|
+
onTime()
|
|
192
|
+
notifyCueUpsert()
|
|
193
|
+
}))
|
|
194
|
+
}
|
|
195
|
+
watchSource()
|
|
196
|
+
|
|
197
|
+
// ⑩R6/A9 清理:节点/监听/订阅全归零;style 仅在"本 detached 创建且无 live DOM 依赖"时移除(缺口4)
|
|
198
|
+
let cleaned = false
|
|
199
|
+
const cleanup = () => {
|
|
200
|
+
if (cleaned) return
|
|
201
|
+
cleaned = true
|
|
202
|
+
resizeObserver.disconnect()
|
|
203
|
+
video.removeEventListener('timeupdate', onTime)
|
|
204
|
+
video.removeEventListener('seeked', onTime)
|
|
205
|
+
for (const unsubscribe of unsubscribers.splice(0)) unsubscribe()
|
|
206
|
+
shadowHost.remove()
|
|
207
|
+
if (styleOwnedByDetached
|
|
208
|
+
&& !document.getElementById(VIDEO_SUBTITLE_PANEL_ID)
|
|
209
|
+
&& !document.getElementById(VIDEO_TRANSLATION_LAYER_ID)) {
|
|
210
|
+
document.getElementById('fluent-read-video-subtitle-style')?.remove()
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const instance = {
|
|
215
|
+
mount() { /* 挂载已在 createDetachedInstance 完成(幂等空实现) */ },
|
|
216
|
+
unmount() { cleanup() },
|
|
217
|
+
setConfig(next: SupplierRuntimeConfig) {
|
|
218
|
+
// lane 变更即时生效(M2);settings 的样式权威在 vendor config(与 live 同源)——P1 语义注记
|
|
219
|
+
void next
|
|
220
|
+
applyLayout()
|
|
221
|
+
applyDisplayMode()
|
|
222
|
+
onTime()
|
|
223
|
+
},
|
|
224
|
+
detach(): DetachedHandle {
|
|
225
|
+
return {
|
|
226
|
+
unmount() { cleanup() },
|
|
227
|
+
setSource(next: TrackSource) {
|
|
228
|
+
currentSource = next
|
|
229
|
+
void ingest()
|
|
230
|
+
watchSource()
|
|
231
|
+
},
|
|
232
|
+
// 缺口3(0.3.1):有效轨回显流——订阅即 reset(vendor cue 规范化视图),ingest/onUpdate → upsert
|
|
233
|
+
subscribeCues(listener: (event: CueStreamEvent) => void): () => void {
|
|
234
|
+
cueListeners.add(listener)
|
|
235
|
+
listener({ type: 'reset', cues: cuesFromVideoSubtitleCues(vendorCues) })
|
|
236
|
+
return () => { cueListeners.delete(listener) }
|
|
237
|
+
},
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
} satisfies VideoSubtitlePluginInstance & { detach(): DetachedHandle }
|
|
241
|
+
return instance
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** 事件隔离:detached 面板不劫持宿主交互(轻量版:面板层 pointer-events 已由 CSS 关) */
|
|
245
|
+
function isolateEvents(host: HTMLElement): void {
|
|
246
|
+
host.style.pointerEvents = 'none'
|
|
247
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file suppliers/fluent-read/src/fluent-track.ts
|
|
3
|
+
* 文件职责:SubtitleCue(框架注入轨,F-27 契约)↔ VideoSubtitleCue(vendor 原生形状)的双向映射,
|
|
4
|
+
* 与注入轨的时间轴选择(currentCueAt,R3 时钟的纯函数核)。
|
|
5
|
+
* 主要内容:toVideoSubtitleCues(正向,含 durationMs 缺省推断与末条语义)、
|
|
6
|
+
* cuesFromVideoSubtitleCues(逆向,契约夹具)、currentCueAt(区间 [start, start+duration) 命中)。
|
|
7
|
+
* 模块边界:纯函数,零浏览器依赖;时间语义按提案 §4.0-3;R5:translations 缺项与 null 等价(降级原文行)。
|
|
8
|
+
*/
|
|
9
|
+
import type { SubtitleCue } from '@sublane/core'
|
|
10
|
+
import type { VideoSubtitleCue } from '../vendor/src/features/video-subtitle/content/youtubeSubtitleData'
|
|
11
|
+
|
|
12
|
+
export interface ToVendorOptions {
|
|
13
|
+
/** 末条 cue 的轨终点(ms);缺省 Infinity(播到轨终点语义) */
|
|
14
|
+
trackEndMs?: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** 注入 cue → vendor 原生 cue(F-27 数据面正向映射);译文列 = translations['fluent-read'](R5 缺项≡null) */
|
|
18
|
+
export function toVideoSubtitleCues(cues: SubtitleCue[], opts: ToVendorOptions = {}): Array<VideoSubtitleCue & { translation?: string }> {
|
|
19
|
+
return cues.map((cue, index) => {
|
|
20
|
+
const startMs = cue.tsMs
|
|
21
|
+
let durationMs: number
|
|
22
|
+
if (cue.durationMs != null) {
|
|
23
|
+
durationMs = cue.durationMs
|
|
24
|
+
} else if (index + 1 < cues.length) {
|
|
25
|
+
durationMs = Math.max(0, cues[index + 1]!.tsMs - startMs)
|
|
26
|
+
} else {
|
|
27
|
+
durationMs = opts.trackEndMs != null ? Math.max(0, opts.trackEndMs - startMs) : Number.POSITIVE_INFINITY
|
|
28
|
+
}
|
|
29
|
+
const fluentTranslation = cue.translations?.['fluent-read']
|
|
30
|
+
const vendorCue: VideoSubtitleCue & { translation?: string } = { startMs, durationMs, text: cue.original }
|
|
31
|
+
if (fluentTranslation != null) vendorCue.translation = fluentTranslation
|
|
32
|
+
return vendorCue
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** vendor cue → 注入 cue(逆向:契约测试夹具/往返证明;Infinity duration → 无 durationMs) */
|
|
37
|
+
export function cuesFromVideoSubtitleCues(vendorCues: Array<VideoSubtitleCue & { translation?: string }>): SubtitleCue[] {
|
|
38
|
+
return vendorCues.map((vendorCue) => {
|
|
39
|
+
const cue: SubtitleCue = { tsMs: vendorCue.startMs, original: vendorCue.text }
|
|
40
|
+
if (Number.isFinite(vendorCue.durationMs) && vendorCue.durationMs > 0) cue.durationMs = vendorCue.durationMs
|
|
41
|
+
if (vendorCue.translation != null) cue.translations = { 'fluent-read': vendorCue.translation }
|
|
42
|
+
return cue
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 时间轴选择:区间 [startMs, startMs+durationMs) 命中;Infinity duration 播到轨终点;间隙/越界 → null */
|
|
47
|
+
export function currentCueAt(cues: Array<VideoSubtitleCue & { translation?: string }>, timeMs: number): (VideoSubtitleCue & { translation?: string }) | null {
|
|
48
|
+
if (!Number.isFinite(timeMs) || timeMs < 0) return null
|
|
49
|
+
return cues.find((cue) => timeMs >= cue.startMs && timeMs < cue.startMs + cue.durationMs) ?? null
|
|
50
|
+
}
|
|
@@ -7,14 +7,22 @@
|
|
|
7
7
|
import {VideoTranslationScheduler} from './translationScheduler';
|
|
8
8
|
import {selectVideoSubtitlePretranslationCues} from './subtitleLogic';
|
|
9
9
|
import type {VideoSubtitleCue} from './youtubeSubtitleData';
|
|
10
|
+
/** [wep patch 0015] 活动译文缓存注册表:adapter 侧据此发现 live 管线的活动实例(F-26 cue 流数据源)。 */
|
|
11
|
+
export const videoTranslationCaches = new Set<VideoTranslationCache>();
|
|
12
|
+
/** [wep patch 0015] 译文事件监听:缓存写入(译文产出)时通知,source 为规范化原文,translation 为已译文本。 */
|
|
13
|
+
export type VideoTranslationListener = (source: string, translation: string) => void;
|
|
10
14
|
export class VideoTranslationCache {
|
|
11
15
|
private readonly translatedVideoCache = new Map<string, string>();
|
|
12
16
|
private readonly inFlightVideoTranslations = new Map<string, Promise<string>>();
|
|
13
17
|
private readonly videoTranslationFailures = new Map<string, {attempts: number; retryAt: number}>();
|
|
14
18
|
private readonly translationScheduler: VideoTranslationScheduler;
|
|
15
19
|
private pretranslationCacheVersion = 0;
|
|
20
|
+
private readonly translationListeners = new Set<VideoTranslationListener>();
|
|
21
|
+
/** [wep patch 0015] 实例创建时刻(adapter 排序取最新实例)。 */
|
|
22
|
+
readonly createdAt = Date.now();
|
|
16
23
|
constructor(translate: (text: string, signal: AbortSignal) => Promise<string>) {
|
|
17
24
|
this.translationScheduler = new VideoTranslationScheduler(translate);
|
|
25
|
+
videoTranslationCaches.add(this);
|
|
18
26
|
}
|
|
19
27
|
clear(): void {
|
|
20
28
|
this.translatedVideoCache.clear();
|
|
@@ -37,6 +45,11 @@ export class VideoTranslationCache {
|
|
|
37
45
|
peek(source: string): string | undefined {
|
|
38
46
|
return this.translatedVideoCache.get(source.replace(/[\s\u3000]+/g, ' ').trim());
|
|
39
47
|
}
|
|
48
|
+
/** [wep patch 0015] 订阅译文产出事件(F-27/P1 数据面:adapter 经此读取 vendor 翻译节奏的产出)。 */
|
|
49
|
+
subscribeTranslations(listener: VideoTranslationListener): () => void {
|
|
50
|
+
this.translationListeners.add(listener);
|
|
51
|
+
return () => this.translationListeners.delete(listener);
|
|
52
|
+
}
|
|
40
53
|
retainPrefetches(sources: readonly string[]): void {
|
|
41
54
|
const keys = new Set(sources.map(source => source.replace(/[\s\u3000]+/g, ' ').trim()));
|
|
42
55
|
for (const source of this.translationScheduler.retainPrefetches(keys)) {
|
|
@@ -79,6 +92,7 @@ export class VideoTranslationCache {
|
|
|
79
92
|
if (requestVersion === this.pretranslationCacheVersion) {
|
|
80
93
|
this.videoTranslationFailures.delete(key);
|
|
81
94
|
this.translatedVideoCache.set(key, result);
|
|
95
|
+
for (const listener of this.translationListeners) listener(key, result);
|
|
82
96
|
if (this.translatedVideoCache.size > 160) {
|
|
83
97
|
const oldestKey = this.translatedVideoCache.keys().next().value;
|
|
84
98
|
if (oldestKey) this.translatedVideoCache.delete(oldestKey);
|