Skip to content

Commit

Permalink
Merge pull request #19144 from calixteman/no_focus_if_invisible
Browse files Browse the repository at this point in the history
[Editor] Don't focus a newly added drawing if it isn't visible on screen
  • Loading branch information
calixteman authored Dec 2, 2024
2 parents 36b1ba8 + 673c93e commit 97c7a8e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/display/editor/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ class DrawingEditor extends AnnotationEditor {
this.#mustBeCommitted = false;
this.commit();
this.parent.setSelected(this);
this.div.focus();
if (this.isOnScreen) {
this.div.focus();
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/display/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,12 @@ class AnnotationEditor {
return this.div && !this.isAttachedToDOM;
}

get isOnScreen() {
const { top, left, bottom, right } = this.getClientDimensions();
const { innerHeight, innerWidth } = window;
return left < innerWidth && right > 0 && top < innerHeight && bottom > 0;
}

#addFocusListeners() {
if (this.#focusAC || !this.div) {
return;
Expand Down
57 changes: 57 additions & 0 deletions test/integration/ink_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -612,4 +612,61 @@ describe("Ink Editor", () => {
);
});
});

describe("Annotation mustn't take focus if it isn't visible", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait("tracemonkey.pdf", ".annotationEditorLayer");
});

afterAll(async () => {
await closePages(pages);
});

it("must check that the focus isn't taken", 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;
const clickHandle = await waitForPointerUp(page);
await page.mouse.move(x, y);
await page.mouse.down();
await page.mouse.move(x + 50, y + 50);
await page.mouse.up();
await awaitPromise(clickHandle);

await page.evaluate(() => {
window.focusedIds = [];
window.focusCallback = e => {
window.focusedIds.push(e.target.id);
};
window.addEventListener("focusin", window.focusCallback);
});

const oneToFourteen = Array.from(new Array(13).keys(), n => n + 2);
for (const pageNumber of oneToFourteen) {
await scrollIntoView(
page,
`.page[data-page-number = "${pageNumber}"]`
);
}

const ids = await page.evaluate(() => {
const { focusedIds, focusCallback } = window;
window.removeEventListener("focusin", focusCallback);
delete window.focusCallback;
delete window.focusedIds;
return focusedIds;
});

expect(ids).withContext(`In ${browserName}`).toEqual([]);
})
);
});
});
});

0 comments on commit 97c7a8e

Please sign in to comment.