Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
41 changes: 41 additions & 0 deletions test/integration/ink_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
awaitPromise,
clearEditors,
closePages,
countStorageEntries,
dragAndDrop,
getAnnotationSelector,
getEditors,
Expand Down Expand Up @@ -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);
})
);
});
});