From a9bffe39132fe313525dcd29b4ac8ae057132bf9 Mon Sep 17 00:00:00 2001 From: Gabe Date: Thu, 30 Oct 2025 00:55:17 +0800 Subject: [PATCH] feat: can set rootMargin for IntersectionObserver. --- src/config/i18n.js | 5 +++ src/config/setting.js | 1 + src/libs/translator.js | 6 ++-- src/views/Options/Setting.js | 70 +++++++++++++++++++++--------------- 4 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/config/i18n.js b/src/config/i18n.js index 45c761b..17f2ad0 100644 --- a/src/config/i18n.js +++ b/src/config/i18n.js @@ -1784,6 +1784,11 @@ export const I18N = { en: `Highlight after translation`, zh_TW: `翻譯後高亮`, }, + pagescroll_root_margin: { + zh: `滚动加载提前触发 (px)`, + en: `Early triggering of scroll loading (px)`, + zh_TW: `滾動載入提前觸發 (px)`, + } }; export const newI18n = (lang) => (key) => I18N[key]?.[lang] || ""; diff --git a/src/config/setting.js b/src/config/setting.js index 038a461..47dad1b 100644 --- a/src/config/setting.js +++ b/src/config/setting.js @@ -182,4 +182,5 @@ export const DEFAULT_SETTING = { transAllnow: false, // 是否立即全部翻译 subtitleSetting: DEFAULT_SUBTITLE_SETTING, // 字幕设置 logLevel: LogLevel.INFO.value, // 日志级别 + rootMargin: 200, // 提前触发翻译 }; diff --git a/src/libs/translator.js b/src/libs/translator.js index d3891ce..489ed0c 100644 --- a/src/libs/translator.js +++ b/src/libs/translator.js @@ -544,11 +544,13 @@ export class Translator { // 监控翻译单元的可见性 #createIntersectionObserver() { + const { transInterval, rootMargin = 200 } = this.#setting; + const pending = new Set(); const flush = debounce(() => { pending.forEach((node) => this.#performSyncNode(node)); pending.clear(); - }, this.#setting.transInterval); + }, transInterval); return new IntersectionObserver( (entries) => { @@ -562,7 +564,7 @@ export class Translator { } }); }, - { threshold: 0.01 } + { threshold: 0.01, rootMargin: `${rootMargin}px 0px ${rootMargin}px 0px` } ); } diff --git a/src/views/Options/Setting.js b/src/views/Options/Setting.js index 61553a9..be72d61 100644 --- a/src/views/Options/Setting.js +++ b/src/views/Options/Setting.js @@ -105,6 +105,7 @@ export default function Settings() { skipLangs = [], // detectRemote = true, transAllnow = false, + rootMargin = 200, } = setting; const { isHide = false, fabClickAction = 0 } = fab || {}; @@ -298,34 +299,6 @@ export default function Settings() { {i18n("secondary_context_menus")} - - - {i18n("mk_pagescroll")} - {i18n("mk_pageopen")} - - - {/* - - {i18n("enable")} - {i18n("disable")} - - */} + + + {i18n("mk_pagescroll")} + {i18n("mk_pageopen")} + + + + + + {/* + + {i18n("enable")} + {i18n("disable")} + + */}