Skip to content

Commit

Permalink
[Editor] Don't steal the keyboard events when editing mode is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Oct 29, 2023
1 parent 6115a32 commit cf0db08
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -906,13 +906,11 @@ class AnnotationEditorUIManager {
#addKeyboardManager() {
// The keyboard events are caught at the container level in order to be able
// to execute some callbacks even if the current page doesn't have focus.
window.addEventListener("keydown", this.#boundKeydown, { capture: true });
window.addEventListener("keydown", this.#boundKeydown);
}

#removeKeyboardManager() {
window.removeEventListener("keydown", this.#boundKeydown, {
capture: true,
});
window.removeEventListener("keydown", this.#boundKeydown);
}

#addCopyPasteListeners() {
Expand Down
62 changes: 62 additions & 0 deletions test/integration/freetext_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2981,4 +2981,66 @@ describe("FreeText Editor", () => {
);
});
});

describe("Avoid to steal keyboard events", () => {
let pages;

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

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

it("must check the keyboard event is limited to the input", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await switchToFreeText(page);

const rect = await page.$eval(".annotationEditorLayer", el => {
// With Chrome something is wrong when serializing a DomRect,
// hence we extract the values and just return them.
const { x, y } = el.getBoundingClientRect();
return { x, y };
});

const data = "Hello PDF.js World !!";
await page.mouse.click(rect.x + 100, rect.y + 100);
await page.waitForSelector(getEditorSelector(0), {
visible: true,
});
await page.type(`${getEditorSelector(0)} .internal`, data);

// Commit.
await page.keyboard.press("Escape");
await page.waitForSelector(
`${getEditorSelector(0)} .overlay.enabled`
);

await page.focus("#pageNumber");
const promise = page.evaluate(
() =>
new Promise(resolve => {
document
.getElementById("pageNumber")
.addEventListener("keyup", resolve, { once: true });
})
);
await page.keyboard.press("Backspace");
await promise;

let content = await page.$eval("#pageNumber", el =>
el.innerText.trimEnd()
);
expect(content).withContext(`In ${browserName}`).toEqual("");

content = await page.$eval(getEditorSelector(0), el =>
el.innerText.trimEnd()
);
expect(content).withContext(`In ${browserName}`).toEqual(data);
})
);
});
});
});
5 changes: 5 additions & 0 deletions web/pdf_find_bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,21 @@ class PDFFindBar {
this.findField.addEventListener("input", () => {
this.dispatchEvent("");
});
this.findField.addEventListener("keydown", e => {
e.stopPropagation();
});

this.bar.addEventListener("keydown", e => {
switch (e.keyCode) {
case 13: // Enter
if (e.target === this.findField) {
this.dispatchEvent("again", e.shiftKey);
e.stopPropagation();
}
break;
case 27: // Escape
this.close();
e.stopPropagation();
break;
}
});
Expand Down
3 changes: 3 additions & 0 deletions web/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ class Toolbar {
pageNumber.addEventListener("click", function () {
this.select();
});
pageNumber.addEventListener("keydown", event => {
event.stopPropagation();
});
pageNumber.addEventListener("change", function () {
self.eventBus.dispatch("pagenumberchanged", {
source: self,
Expand Down

0 comments on commit cf0db08

Please sign in to comment.