Skip to content

Commit

Permalink
Merge pull request #17204 from calixteman/issue17167
Browse files Browse the repository at this point in the history
[Editor] Don't steal the keyboard events when editing mode is enabled
  • Loading branch information
calixteman authored Oct 30, 2023
2 parents 1ab0f8a + 77475ac commit 50c0fcc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,15 +611,27 @@ class AnnotationEditorUIManager {
);
};

const textInputChecker = (_self, { target: el }) => {
if (el instanceof HTMLInputElement) {
const { type } = el;
return type !== "text" && type !== "number";
}
return true;
};

const small = this.TRANSLATE_SMALL;
const big = this.TRANSLATE_BIG;

return shadow(
this,
"_keyboardManager",
new KeyboardManager([
[["ctrl+a", "mac+meta+a"], proto.selectAll],
[["ctrl+z", "mac+meta+z"], proto.undo],
[
["ctrl+a", "mac+meta+a"],
proto.selectAll,
{ checker: textInputChecker },
],
[["ctrl+z", "mac+meta+z"], proto.undo, { checker: textInputChecker }],
[
// On mac, depending of the OS version, the event.key is either "z" or
// "Z" when the user presses "meta+shift+z".
Expand All @@ -631,6 +643,7 @@ class AnnotationEditorUIManager {
"mac+meta+shift+Z",
],
proto.redo,
{ checker: textInputChecker },
],
[
[
Expand All @@ -647,6 +660,7 @@ class AnnotationEditorUIManager {
"mac+Delete",
],
proto.delete,
{ checker: textInputChecker },
],
[
["Enter", "mac+Enter"],
Expand Down Expand Up @@ -906,13 +920,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
72 changes: 72 additions & 0 deletions test/integration/freetext_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2981,4 +2981,76 @@ 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`
);

let promise = page.evaluate(
() =>
new Promise(resolve => {
document.addEventListener("selectionchange", resolve, {
once: true,
});
})
);
await page.click("#pageNumber");
await promise;

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);
})
);
});
});
});

0 comments on commit 50c0fcc

Please sign in to comment.