diff --git a/src/display/editor/tools.js b/src/display/editor/tools.js index 75c2ca16b334d..4b4cdcf61bcc8 100644 --- a/src/display/editor/tools.js +++ b/src/display/editor/tools.js @@ -963,6 +963,10 @@ class AnnotationEditorUIManager { }, { capture: true, signal } ); + window.addEventListener("beforeunload", this.#beforeUnload.bind(this), { + capture: true, + signal, + }); this.#addSelectionListener(); this.#addDragAndDropListeners(); this.#addKeyboardManager(); @@ -1398,6 +1402,11 @@ class AnnotationEditorUIManager { this.highlightSelection(methodOfCreation, /* comment */ true); } + #beforeUnload(e) { + this.commitOrRemove(); + this.currentLayer?.endDrawingSession(/* isAborted = */ false); + } + #displayFloatingToolbar() { const selection = document.getSelection(); if (!selection || selection.isCollapsed) { diff --git a/test/integration/ink_editor_spec.mjs b/test/integration/ink_editor_spec.mjs index f9b8a0a2c54a0..bdd4a372d5b29 100644 --- a/test/integration/ink_editor_spec.mjs +++ b/test/integration/ink_editor_spec.mjs @@ -17,6 +17,7 @@ import { awaitPromise, clearEditors, closePages, + countStorageEntries, dragAndDrop, getAnnotationSelector, getEditors, @@ -1236,3 +1237,43 @@ describe("Ink must update its color", () => { ); }); }); + +describe("Ink must committed when leaving the tab", () => { + let pages; + + beforeEach(async () => { + pages = await loadAndWait("empty.pdf", ".annotationEditorLayer"); + }); + + afterEach(async () => { + await closePages(pages); + }); + + it("must check that the annotation storage is updated when leaving the tab", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await switchToInk(page); + + const rect = await getRect(page, ".annotationEditorLayer"); + + const x = rect.x + 20; + const y = rect.y + 20; + await drawLine(page, x, y, x + 50, y + 50); + + const count = await countStorageEntries(page); + expect(count).withContext(`In ${browserName}`).toEqual(0); + + // Trigger the beforeunload event to force auto-commit + await page.evaluate(() => { + window.dispatchEvent(new Event("beforeunload")); + }); + + // Wait for the annotation to be committed to storage + await waitForStorageEntries(page, 1); + + const countAfter = await countStorageEntries(page); + expect(countAfter).withContext(`In ${browserName}`).toEqual(1); + }) + ); + }); +});