合并页面关闭时的自动保存触发 #57

Merged
WuYongJie merged 1 commits from codex/canvas-leave-save-once into main 2026-11-06 16:11:48 +08:00
2 changed files with 15 additions and 1 deletions

View File

@ -5413,7 +5413,6 @@ export default function CanvasEditorLegacyPage({
const onVisibilityChange = () => {
if (!canvasLeaveApprovedRef.current && document.visibilityState === "hidden" && hasUnsavedCanvasChanges()) {
writeCanvasDraft(id, { base: savedCanvasSnapshotRef.current!.snapshot, snapshot: JSON.stringify({ canvasData: buildPersistedCanvasData() }) });
retrySync();
}
};
const retryTimer = window.setInterval(() => {

View File

@ -143,6 +143,21 @@ test("only unsaved content blocks closing and beforeunload does not issue duplic
expect(errors).toEqual([]);
});
test("pagehide and visibilitychange issue only one closing save", async ({ page }) => {
const { state, errors } = await setup(page);
state.failSave = true;
await moveImage(page);
const writes = state.writes.length;
await page.evaluate(() => {
window.dispatchEvent(new Event("pagehide"));
Object.defineProperty(document, "visibilityState", { value: "hidden", configurable: true });
document.dispatchEvent(new Event("visibilitychange"));
});
await expect.poll(() => state.writes.length).toBe(writes + 1);
expect(state.submissions).toHaveLength(0);
expect(errors).toEqual([]);
});
test("leaving with pending changes can continue editing or save before navigating", async ({ page }) => {
const { state, errors } = await setup(page);
await page.route(url => url.pathname === "/", route => route.fulfill({ contentType: "text/html; charset=utf-8", body: "<p>已返回首页</p>" }));