Skip to content

Commit 1b20ba5

Browse files
committed
Ends the current drawing session when closing the tab (bug 2015385)
1 parent 0b5f402 commit 1b20ba5

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/display/editor/tools.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,10 @@ class AnnotationEditorUIManager {
963963
},
964964
{ capture: true, signal }
965965
);
966+
window.addEventListener("beforeunload", this.#beforeUnload.bind(this), {
967+
capture: true,
968+
signal,
969+
});
966970
this.#addSelectionListener();
967971
this.#addDragAndDropListeners();
968972
this.#addKeyboardManager();
@@ -1398,6 +1402,11 @@ class AnnotationEditorUIManager {
13981402
this.highlightSelection(methodOfCreation, /* comment */ true);
13991403
}
14001404

1405+
#beforeUnload(e) {
1406+
this.commitOrRemove();
1407+
this.currentLayer?.endDrawingSession(/* isAborted = */ false);
1408+
}
1409+
14011410
#displayFloatingToolbar() {
14021411
const selection = document.getSelection();
14031412
if (!selection || selection.isCollapsed) {

test/integration/ink_editor_spec.mjs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
awaitPromise,
1818
clearEditors,
1919
closePages,
20+
countStorageEntries,
2021
dragAndDrop,
2122
getAnnotationSelector,
2223
getEditors,
@@ -1236,3 +1237,43 @@ describe("Ink must update its color", () => {
12361237
);
12371238
});
12381239
});
1240+
1241+
describe("Ink must committed when leaving the tab", () => {
1242+
let pages;
1243+
1244+
beforeEach(async () => {
1245+
pages = await loadAndWait("empty.pdf", ".annotationEditorLayer");
1246+
});
1247+
1248+
afterEach(async () => {
1249+
await closePages(pages);
1250+
});
1251+
1252+
it("must check that the annotation storage is updated when leaving the tab", async () => {
1253+
await Promise.all(
1254+
pages.map(async ([browserName, page]) => {
1255+
await switchToInk(page);
1256+
1257+
const rect = await getRect(page, ".annotationEditorLayer");
1258+
1259+
const x = rect.x + 20;
1260+
const y = rect.y + 20;
1261+
await drawLine(page, x, y, x + 50, y + 50);
1262+
1263+
const count = await countStorageEntries(page);
1264+
expect(count).withContext(`In ${browserName}`).toEqual(0);
1265+
1266+
// Trigger the beforeunload event to force auto-commit
1267+
await page.evaluate(() => {
1268+
window.dispatchEvent(new Event("beforeunload"));
1269+
});
1270+
1271+
// Wait for the annotation to be committed to storage
1272+
await waitForStorageEntries(page, 1);
1273+
1274+
const countAfter = await countStorageEntries(page);
1275+
expect(countAfter).withContext(`In ${browserName}`).toEqual(1);
1276+
})
1277+
);
1278+
});
1279+
});

0 commit comments

Comments
 (0)