@sublane/supplier-fluent-read 0.3.1 → 0.3.2
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.3.
|
|
3
|
+
"version": "0.3.2",
|
|
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.3.
|
|
50
|
+
"@sublane/core": "^0.3.1"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@sublane/core": "workspace:*"
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
diff --git a/src/features/video-subtitle/content/translationCache.ts b/src/features/video-subtitle/content/translationCache.ts
|
|
2
|
-
index d07d213..
|
|
2
|
+
index d07d213..bc9cbc9 100644
|
|
3
3
|
--- a/src/features/video-subtitle/content/translationCache.ts
|
|
4
4
|
+++ b/src/features/video-subtitle/content/translationCache.ts
|
|
5
|
-
@@ -7,14 +7,
|
|
5
|
+
@@ -7,14 +7,29 @@
|
|
6
6
|
import {VideoTranslationScheduler} from './translationScheduler';
|
|
7
7
|
import {selectVideoSubtitlePretranslationCues} from './subtitleLogic';
|
|
8
8
|
import type {VideoSubtitleCue} from './youtubeSubtitleData';
|
|
9
9
|
+/** [wep patch 0015] 活动译文缓存注册表:adapter 侧据此发现 live 管线的活动实例(F-26 cue 流数据源)。 */
|
|
10
10
|
+export const videoTranslationCaches = new Set<VideoTranslationCache>();
|
|
11
|
+
+/** [wep patch 0015] 注册表新增事件:实例创建即通知——adapter 侧据此自动(重)绑定最新缓存(F-28:宿主零时序义务)。 */
|
|
12
|
+
+const videoTranslationCacheAddedListeners = new Set<(cache: VideoTranslationCache) => void>();
|
|
13
|
+
+export function onVideoTranslationCacheAdded(cb: (cache: VideoTranslationCache) => void): () => void {
|
|
14
|
+
+ videoTranslationCacheAddedListeners.add(cb);
|
|
15
|
+
+ return () => videoTranslationCacheAddedListeners.delete(cb);
|
|
16
|
+
+}
|
|
11
17
|
+/** [wep patch 0015] 译文事件监听:缓存写入(译文产出)时通知,source 为规范化原文,translation 为已译文本。 */
|
|
12
18
|
+export type VideoTranslationListener = (source: string, translation: string) => void;
|
|
13
19
|
export class VideoTranslationCache {
|
|
@@ -22,10 +28,11 @@ index d07d213..7d4f774 100644
|
|
|
22
28
|
constructor(translate: (text: string, signal: AbortSignal) => Promise<string>) {
|
|
23
29
|
this.translationScheduler = new VideoTranslationScheduler(translate);
|
|
24
30
|
+ videoTranslationCaches.add(this);
|
|
31
|
+
+ for (const listener of videoTranslationCacheAddedListeners) listener(this);
|
|
25
32
|
}
|
|
26
33
|
clear(): void {
|
|
27
34
|
this.translatedVideoCache.clear();
|
|
28
|
-
@@ -37,6 +
|
|
35
|
+
@@ -37,6 +52,11 @@ export class VideoTranslationCache {
|
|
29
36
|
peek(source: string): string | undefined {
|
|
30
37
|
return this.translatedVideoCache.get(source.replace(/[\s\u3000]+/g, ' ').trim());
|
|
31
38
|
}
|
|
@@ -37,7 +44,7 @@ index d07d213..7d4f774 100644
|
|
|
37
44
|
retainPrefetches(sources: readonly string[]): void {
|
|
38
45
|
const keys = new Set(sources.map(source => source.replace(/[\s\u3000]+/g, ' ').trim()));
|
|
39
46
|
for (const source of this.translationScheduler.retainPrefetches(keys)) {
|
|
40
|
-
@@ -79,6 +
|
|
47
|
+
@@ -79,6 +99,7 @@ export class VideoTranslationCache {
|
|
41
48
|
if (requestVersion === this.pretranslationCacheVersion) {
|
|
42
49
|
this.videoTranslationFailures.delete(key);
|
|
43
50
|
this.translatedVideoCache.set(key, result);
|
package/src/adapter.ts
CHANGED
|
@@ -11,7 +11,8 @@ import type {
|
|
|
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
|
+
import { videoTranslationCaches, onVideoTranslationCacheAdded } from '../vendor/src/features/video-subtitle/content/translationCache'
|
|
15
|
+
import { bindLatestCache } from './cache-watch'
|
|
15
16
|
import { fluentReadHostRequirements } from './manifest'
|
|
16
17
|
import { laneToAppearancePosition } from './lane'
|
|
17
18
|
import { createDetachedInstance as createFluentDetachedInstance } from './detached'
|
|
@@ -194,25 +195,32 @@ export function createFluentReadInstance(ctx: PluginMountContext): VideoSubtitle
|
|
|
194
195
|
void requestConfigPatch({ ...(laneChanged ? lanePatch(next.lane) : {}), ...diffSettingsPatch(next.settings) }, browser.runtime.sendMessage.bind(browser.runtime))
|
|
195
196
|
.catch((error) => console.warn('[fluent-read] 配置同步失败', error))
|
|
196
197
|
},
|
|
197
|
-
// 缺口2(F-26/0.3.1):fluent 译文数据面——vendor
|
|
198
|
+
// 缺口2(F-26/0.3.1)+F-28(0.3.2):fluent 译文数据面——vendor 翻译缓存产出事件流。
|
|
199
|
+
// F-28 订阅时序:bindLatestCache 挂起/自动重绑——早订阅(vendor 未挂载,缓存未建)不 noop,
|
|
200
|
+
// 缓存创建即送达;实例重挂载/SPA 导航产生新缓存时自动切绑最新(宿主零时序义务)。
|
|
198
201
|
// 语义声明:YouTube 默认 roll-up DOM 模式无 cue 区间概念,SubtitleCue.tsMs = 译文首见时刻
|
|
199
202
|
// (宿主 video currentTime);reset 为空快照(句子译文无时间轴可回放),宿主以 upsert 累积。
|
|
200
203
|
subscribeCues(listener: (event: CueStreamEvent) => void): () => void {
|
|
201
|
-
const cache = [...videoTranslationCaches].sort((a, b) => a.createdAt - b.createdAt).at(-1)
|
|
202
204
|
listener({ type: 'reset', cues: [], engine: 'fluent-read' })
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
205
|
+
return bindLatestCache(
|
|
206
|
+
{
|
|
207
|
+
getAll: () => [...videoTranslationCaches],
|
|
208
|
+
onAdded: (cb) => onVideoTranslationCacheAdded(cb),
|
|
209
|
+
},
|
|
210
|
+
(cache, emit) => cache.subscribeTranslations((source, translation) => emit({ source, translation })),
|
|
211
|
+
({ source, translation }) => {
|
|
212
|
+
let tsMs = 0
|
|
213
|
+
try {
|
|
214
|
+
const video = document.querySelector<HTMLVideoElement>('video.html5-main-video, video')
|
|
215
|
+
if (video && Number.isFinite(video.currentTime)) tsMs = Math.round(video.currentTime * 1000)
|
|
216
|
+
} catch { /* 主世界不可达时时间轴缺省 0(时间轴语义见上注) */ }
|
|
217
|
+
listener({
|
|
218
|
+
type: 'upsert',
|
|
219
|
+
cues: [{ tsMs, original: source, translations: { 'fluent-read': translation } }],
|
|
220
|
+
engine: 'fluent-read',
|
|
221
|
+
})
|
|
222
|
+
},
|
|
223
|
+
)
|
|
216
224
|
},
|
|
217
225
|
}
|
|
218
226
|
return instance
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file suppliers/fluent-read/src/cache-watch.ts
|
|
3
|
+
* 文件职责:F-28(0.3.2)——fluent 译文缓存绑定器:订阅时刻无缓存则挂起,缓存创建/更替时自动
|
|
4
|
+
* (重)绑定最新实例——宿主零时序义务(早订阅不 noop;SPA 导航/实例重挂载后无需重订阅)。
|
|
5
|
+
* 主要内容:bindLatestCache 参数化纯逻辑(getAll/onAdded/subscribeOne 依赖注入,node 直测)。
|
|
6
|
+
* 模块边界:不认识浏览器/DOM;事件映射(缓存事件→CueStreamEvent)归 adapter 接线层。
|
|
7
|
+
*/
|
|
8
|
+
export interface CacheRegistry<TCache> {
|
|
9
|
+
getAll(): TCache[]
|
|
10
|
+
onAdded(cb: (cache: TCache) => void): () => void
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** 绑定最新缓存实例并跟随更替;返回总退订。
|
|
14
|
+
* 语义:①订阅时已有缓存 → 绑 createdAt 最新者;②无缓存 → 挂起(不发事件不报错),
|
|
15
|
+
* onAdded 触发即绑新缓存(新实例 createdAt 恒更晚,直接切换);③旧绑定先退订再绑新
|
|
16
|
+
* (旧实例迟到事件不送达);④总退订停一切。 */
|
|
17
|
+
export function bindLatestCache<TCache, TEvent>(
|
|
18
|
+
registry: CacheRegistry<TCache> & { getAll(): Array<TCache & { createdAt: number }> },
|
|
19
|
+
subscribeOne: (cache: TCache, emit: (payload: TEvent) => void) => () => void,
|
|
20
|
+
emit: (payload: TEvent) => void,
|
|
21
|
+
): () => void {
|
|
22
|
+
let offCurrent: (() => void) | null = null
|
|
23
|
+
const bind = (cache: TCache) => {
|
|
24
|
+
offCurrent?.()
|
|
25
|
+
offCurrent = subscribeOne(cache, emit)
|
|
26
|
+
}
|
|
27
|
+
const latest = [...registry.getAll()].sort((a, b) => a.createdAt - b.createdAt).at(-1)
|
|
28
|
+
if (latest) bind(latest)
|
|
29
|
+
const offAdded = registry.onAdded((cache) => bind(cache))
|
|
30
|
+
return () => {
|
|
31
|
+
offCurrent?.()
|
|
32
|
+
offCurrent = null
|
|
33
|
+
offAdded()
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/detached.ts
CHANGED
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
} from '../vendor/src/features/video-subtitle/content/ui'
|
|
28
28
|
import { getVideoSubtitleAppearanceCssVars, normalizeVideoSubtitleAppearance } from '../vendor/src/core/config/videoSubtitleAppearance'
|
|
29
29
|
import { fitVideoSubtitleFontSize } from '../vendor/src/features/video-subtitle/content/subtitleLayout'
|
|
30
|
-
import { config } from '../vendor/src/services/config/store'
|
|
30
|
+
import { config, subscribeConfig } from '../vendor/src/services/config/store'
|
|
31
31
|
import { laneToAppearancePosition } from './lane'
|
|
32
32
|
import { cuesFromVideoSubtitleCues, currentCueAt, toVideoSubtitleCues } from './fluent-track'
|
|
33
33
|
|
|
@@ -178,6 +178,15 @@ export async function createDetachedInstance(
|
|
|
178
178
|
const resizeObserver = new ResizeObserver(() => applyLayout())
|
|
179
179
|
resizeObserver.observe(video)
|
|
180
180
|
|
|
181
|
+
// ⑧c F-30(0.3.2):config 跟随——vendor 配置 store 的 subscribeConfig(vendor runtime 自用同款)
|
|
182
|
+
// 接通:live 侧样式/显示模式变更 → detached 重排+重染。0.3.1 曾以"vendor 无订阅 API"为由
|
|
183
|
+
// 文档化此限制,系取证疏漏(store.ts:833 一直导出 subscribeConfig)——本版接通并勘误。
|
|
184
|
+
const offConfig = subscribeConfig(() => {
|
|
185
|
+
applyLayout()
|
|
186
|
+
applyDisplayMode()
|
|
187
|
+
onTime()
|
|
188
|
+
})
|
|
189
|
+
|
|
181
190
|
// ⑨流式补译/换源(onUpdate + setSource)
|
|
182
191
|
const ingest = async () => {
|
|
183
192
|
vendorCues = toVideoSubtitleCues((await currentSource.getTrack().catch(() => null))?.cues ?? [])
|
|
@@ -200,6 +209,7 @@ export async function createDetachedInstance(
|
|
|
200
209
|
if (cleaned) return
|
|
201
210
|
cleaned = true
|
|
202
211
|
resizeObserver.disconnect()
|
|
212
|
+
offConfig()
|
|
203
213
|
video.removeEventListener('timeupdate', onTime)
|
|
204
214
|
video.removeEventListener('seeked', onTime)
|
|
205
215
|
for (const unsubscribe of unsubscribers.splice(0)) unsubscribe()
|
|
@@ -215,7 +225,7 @@ export async function createDetachedInstance(
|
|
|
215
225
|
mount() { /* 挂载已在 createDetachedInstance 完成(幂等空实现) */ },
|
|
216
226
|
unmount() { cleanup() },
|
|
217
227
|
setConfig(next: SupplierRuntimeConfig) {
|
|
218
|
-
// lane 变更即时生效(M2)
|
|
228
|
+
// lane 变更即时生效(M2);样式/显示模式经 subscribeConfig 跟随 vendor config(F-30/0.3.2 接通)
|
|
219
229
|
void next
|
|
220
230
|
applyLayout()
|
|
221
231
|
applyDisplayMode()
|
|
@@ -9,6 +9,12 @@ import {selectVideoSubtitlePretranslationCues} from './subtitleLogic';
|
|
|
9
9
|
import type {VideoSubtitleCue} from './youtubeSubtitleData';
|
|
10
10
|
/** [wep patch 0015] 活动译文缓存注册表:adapter 侧据此发现 live 管线的活动实例(F-26 cue 流数据源)。 */
|
|
11
11
|
export const videoTranslationCaches = new Set<VideoTranslationCache>();
|
|
12
|
+
/** [wep patch 0015] 注册表新增事件:实例创建即通知——adapter 侧据此自动(重)绑定最新缓存(F-28:宿主零时序义务)。 */
|
|
13
|
+
const videoTranslationCacheAddedListeners = new Set<(cache: VideoTranslationCache) => void>();
|
|
14
|
+
export function onVideoTranslationCacheAdded(cb: (cache: VideoTranslationCache) => void): () => void {
|
|
15
|
+
videoTranslationCacheAddedListeners.add(cb);
|
|
16
|
+
return () => videoTranslationCacheAddedListeners.delete(cb);
|
|
17
|
+
}
|
|
12
18
|
/** [wep patch 0015] 译文事件监听:缓存写入(译文产出)时通知,source 为规范化原文,translation 为已译文本。 */
|
|
13
19
|
export type VideoTranslationListener = (source: string, translation: string) => void;
|
|
14
20
|
export class VideoTranslationCache {
|
|
@@ -23,6 +29,7 @@ export class VideoTranslationCache {
|
|
|
23
29
|
constructor(translate: (text: string, signal: AbortSignal) => Promise<string>) {
|
|
24
30
|
this.translationScheduler = new VideoTranslationScheduler(translate);
|
|
25
31
|
videoTranslationCaches.add(this);
|
|
32
|
+
for (const listener of videoTranslationCacheAddedListeners) listener(this);
|
|
26
33
|
}
|
|
27
34
|
clear(): void {
|
|
28
35
|
this.translatedVideoCache.clear();
|