fix(desktop): 关闭浏览器专有兜底(SW 注册 / Cache 虚拟路径 / 5MB 告警 / 自动快照)+ 隐藏与失焦时强制刷盘

This commit is contained in:
qiqi committed 2026-11-17 06:38:27 +08:00
1 parent f0ee2c1c61
commit 2b8bcbb74c
4 files changed
+29 -5

No files matched your search

+13 -3
View File
@@ -125,6 +125,10 @@
// ========== 浏览器本地模式(Cloudflare Pages 免费版)==========
// 数据层:localStorage + IndexedDB;网络层:直连兼容 API(CORS 已验证允许)
// 桌面端(资料目录)是否生效:一堆"浏览器专有"的兜底要按它关掉
function wFileStoreOn() {
try { const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null; return !!(F && F.enabled && F.enabled()) } catch (e) { return false }
}
function storageRuntime() {
if (typeof window === 'undefined') return false
// 桌面端:资料目录就绪后一律走文件底座(内存表 + 异步落盘);否则用原来的 localStorage 实现
@@ -243,7 +247,7 @@
if (typeof state !== 'object' || state === null) return state
const bytes = Number(state.bytes) || 0
// localStorage ~5MB;接近 80% 即预警,避免「画布未保存、空间不足」忽然出现
if (bytes > 4 * 1024 * 1024) {
if (!wFileStoreOn() && bytes > 4 * 1024 * 1024) {
const issues = Array.isArray(state.issues) ? state.issues.slice() : []
issues.push(iwError('storage', '本地存储空间将满(localStorage 已用 ' + fmtStorage(bytes) + ' / 约5MB),建议导出备份后清理图库/不用的画布'))
return Object.assign({}, state, { issues })
@@ -262,7 +266,7 @@
if (typeof indexedDB !== 'undefined') out.indexedDB = true
else out.issues.push(iwError('storage', 'IndexedDB 不可用'))
out.ok = out.localStorage && out.indexedDB
if (out.bytes > 4 * 1024 * 1024) out.issues.push(iwError('storage', '本地存储空间将满(localStorage 已用 ' + fmtStorage(out.bytes) + ' / 约5MB),建议导出备份后清理图库/不用的画布'))
if (!wFileStoreOn() && out.bytes > 4 * 1024 * 1024) out.issues.push(iwError('storage', '本地存储空间将满(localStorage 已用 ' + fmtStorage(out.bytes) + ' / 约5MB),建议导出备份后清理图库/不用的画布'))
return out
}
// localStorage 当前占用字节(UTF-16 计,用于设置页展示;不含 IndexedDB)
@@ -985,6 +989,9 @@
}
const cacheDoneUrls = new Set()
async function iwbCacheWrite(base64) {
// 桌面端:Cache Storage 的虚拟路径 /__iwb_cache__/* 靠 Service Worker 直出,而桌面不注册 SW
// ⇒ 这条兜底只会产生"裂图"。直接判失败,让调用方保留原值/走文件通道。
if (wFileStoreOn()) return null
try {
if (typeof caches === 'undefined' || typeof caches.open !== 'function' || !base64 || String(base64).indexOf('data:') !== 0) return null
const url = wCacheKeyOf(base64)
@@ -5472,7 +5479,8 @@ const tbCenterSeed = (type, refs) => {
// 持久化存储状态(2026-11-07):非持久 = 浏览器空间紧张时可能被直接清掉。逻辑在 src hook 内
const storagePersist = IWWorkbenchHooks.useStoragePersist(3000)
// 自动备份(2026-11-07):30s 后若距上次 ≥24h 就快照 localStorage → IDB,保留 7 份
const autoBackupKit = IWWorkbenchHooks.useAutoBackup(30000)
// 桌面端不做自动快照(数据已经落盘在资料目录);网页版保持原行为
const autoBackupKit = IWWorkbenchHooks.useAutoBackup(wFileStoreOn() ? 0 : 30000)
const [settingOpen, setSettingOpen] = useState(false)
const [settingTabReq, setSettingTabReq] = useState('gen') // v3.87.26:外部点击"内存"时直接打开诊断页
// v3.87.26:顶部栏实时信息(内存/规模)。注意**不能在 useState 惰性初始值里读 refs**
@@ -5602,6 +5610,8 @@ const tbCenterSeed = (type, refs) => {
const swConfirmedRef = useRef(false)
// 注册 Service Worker 以检测「新版本」→ 顶部提示 → 一键刷新更新(学 opentu 的 PWA 机制)
useEffect(() => {
// 桌面端不注册 SW:本地端没有 HTTP 服务、也不需要离线缓存,注册只会 404 并拖慢启动
if (wFileStoreOn()) return
if (typeof navigator === 'undefined' || !navigator.serviceWorker) return
try {
navigator.serviceWorker.register('/sw.js').then((registration) => {
+12 -1
View File
@@ -377,5 +377,16 @@
__internals: { snapshot, loadInto, sha16, extOf, dataUrlToBytes, bytesToDataUrl },
}
// 关窗/收托盘前强制刷盘(beforeunload 在桌面端不保证,但能救一次是一次)
try { global.addEventListener('beforeunload', () => { try { flush() } catch (e) { /* ignore */ } }) } catch (e) { /* ignore */ }
// 落盘兜底:桌面端点关闭是「收进托盘」,beforeunload 不一定触发;
// 窗口被隐藏/失焦时也刷一次,避免最后 800ms 的改动只存在内存里。
try {
global.addEventListener('beforeunload', () => { try { flush() } catch (e) { /* ignore */ } })
global.addEventListener('pagehide', () => { try { flush() } catch (e) { /* ignore */ } })
if (global.document && global.document.addEventListener) {
global.document.addEventListener('visibilitychange', () => {
try { if (global.document.visibilityState === 'hidden') flush() } catch (e) { /* ignore */ }
})
}
if (global.addEventListener) global.addEventListener('blur', () => { try { flush() } catch (e) { /* ignore */ } })
} catch (e) { /* ignore */ }
})(typeof window !== 'undefined' ? window : globalThis)
+1 -1
View File
@@ -438,7 +438,7 @@ function SettingsModal(props) {
? ' 自动保存生成的图片到「默认下载文件夹」(设置 → 通用 → 下载;粘贴/插入的图片不保存)'
: ' 自动保存生成的图片到本地(下载到浏览器下载目录,长久保存;粘贴/插入的图片不下载)')),
el('div', { className: 'iwb-hint' }, isDesk()
? '本地存储:' + fmtStorage(localStorageBytes()) + '(应用私有目录)。画布与图片分层存储——文本/引用存 localStorage,图片存 IndexedDB;建议定期用「备份与还原」导出 zip 到磁盘。'
? '数据体积:' + fmtStorage(localStorageBytes()) + '(全部存于「资料目录」:data.json + images/)。图片是真实文件,画布里只留引用;备份用「备份与还原」导出 zip。'
: '本地存储:' + fmtStorage(localStorageBytes()) + ' / 约 5MB。画布与图片分层存储——文本/引用存 localStorage,图片自动存 IndexedDB(容量大得多),写满会提示,不会静默丢失。'),
el('div', { className: 'iwb-section-sub' }, '原图备份'),
el('div', { className: 'iwb-row' },
+3
View File
@@ -150,6 +150,9 @@
return r
}, [])
useEffect(() => {
// 桌面端(资料目录已生效)不做自动快照:数据本身就落盘了,再存一份 IndexedDB 快照是纯冗余
// 调用方传 0 表示关闭;仍保留手动「立即备份」与列表读取。
if (!(Number(delayMs) > 0)) return undefined
let alive = true
const t = setTimeout(async () => {
try {