fix(canvas): 初始默认视口不再落盘(否则打开画布会跳过 doFit)

根因(iwb-picker 8 条超时的真因,实测确认):
useViewportPersistence 在首帧就把 useState 的初始视口 {x:40,y:30,scale:1} 写进
iwb_viewport / iwb_viewport_<ws>。之后打开画布时 loadWsInto 读到这份「已保存视口」,
就跳过了 doFit ⇒ 节点停在默认位置(实测 g1 在屏幕 x=120,被 318px 左停靠栏盖住),
点击落到浏览器插件的空状态层 .iwb-cwv-empty 上,Playwright 一直重试到 30s 超时。

修法:
- 首帧 effect 只标记 mounted,不落盘;只有发生真实视口变化(fit / 用户平移缩放)后才写
- flushViewport(beforeunload 兜底)同样要求「发生过真实变化」

验证:
- iwb-picker 9/9 全绿(此前 8 红)
- verify:ci 全绿(facts 数字已 sync)
- 已知红基线同步:33 → 25 条(picker 移出)
This commit is contained in:
qiqi authored and qiqi@local committed 2026-11-27 06:28:20 +08:00
1 parent 50accf6e4d
commit 42c9df52da
3 files changed
+10 -11

No files matched your search

+7
View File
@@ -23,8 +23,13 @@
const viewportRef = useRef(viewport); viewportRef.current = viewport
const activeWsRef = useRef(activeWsId); activeWsRef.current = activeWsId
const saveTimer = useRef(null)
// 2026-11-27:首帧的「初始默认视口」不能落盘。
// 否则下次打开画布会把它当成「用户保存的视口」,跳过 doFit ⇒ 节点留在默认位置(被停靠栏盖住)。
const mountedRef = useRef(false) // 首次 effect 运行 = 还没发生任何视口变化
const userRef = useRef(false) // 发生过「适应视图 / 用户平移缩放」才算真实视口
let flushCache = null
const flushViewport = () => {
if (!userRef.current) return
const v = viewportRef.current, ws = activeWsRef.current
try {
const st = (typeof global.IWBStorageLocal !== 'undefined' && global.IWBStorageLocal.getStore) ? global.IWBStorageLocal.getStore() : null
@@ -35,6 +40,8 @@
} catch (e) { /* ignore */ }
}
useEffect(() => {
if (!mountedRef.current) { mountedRef.current = true; return } // 跳过初始值
userRef.current = true
clearTimeout(saveTimer.current)
saveTimer.current = setTimeout(flushViewport, 300)
return () => clearTimeout(saveTimer.current)