Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Editor] Don't add the keyboard listener on the color-picker each time the dropdown menu is shown #17539

Merged
merged 1 commit into from
Jan 20, 2024
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
47 changes: 37 additions & 10 deletions src/display/editor/color_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class ColorPicker {
button.setAttribute("data-l10n-id", "pdfjs-editor-colorpicker-button");
button.setAttribute("aria-haspopup", true);
button.addEventListener("click", this.#openDropdown.bind(this));
button.addEventListener("keydown", this.#boundKeyDown);
const swatch = (this.#buttonSwatch = document.createElement("span"));
swatch.className = "swatch";
swatch.style.backgroundColor = this.#defaultColor;
Expand Down Expand Up @@ -141,6 +142,10 @@ class ColorPicker {
}

_colorSelectFromKeyboard(event) {
if (event.target === this.#button) {
this.#openDropdown(event);
return;
}
const color = event.target.getAttribute("data-color");
if (!color) {
return;
Expand All @@ -149,6 +154,10 @@ class ColorPicker {
}

_moveToNext(event) {
if (!this.#isDropdownVisible) {
this.#openDropdown(event);
return;
}
if (event.target === this.#button) {
this.#dropdown.firstChild?.focus();
return;
Expand All @@ -157,14 +166,34 @@ class ColorPicker {
}

_moveToPrevious(event) {
if (
event.target === this.#dropdown?.firstChild ||
event.target === this.#button
) {
if (this.#isDropdownVisible) {
this._hideDropdownFromKeyboard();
}
return;
}
if (!this.#isDropdownVisible) {
this.#openDropdown(event);
}
event.target.previousSibling?.focus();
}

_moveToBeginning() {
_moveToBeginning(event) {
if (!this.#isDropdownVisible) {
this.#openDropdown(event);
return;
}
this.#dropdown.firstChild?.focus();
}

_moveToEnd() {
_moveToEnd(event) {
if (!this.#isDropdownVisible) {
this.#openDropdown(event);
return;
}
this.#dropdown.lastChild?.focus();
}

Expand All @@ -173,11 +202,10 @@ class ColorPicker {
}

#openDropdown(event) {
if (this.#dropdown && !this.#dropdown.classList.contains("hidden")) {
if (this.#isDropdownVisible) {
this.hideDropdown();
return;
}
this.#button.addEventListener("keydown", this.#boundKeyDown);
this.#dropdownWasFromKeyboard = event.detail === 0;
window.addEventListener("pointerdown", this.#boundPointerDown);
if (this.#dropdown) {
Expand All @@ -200,16 +228,15 @@ class ColorPicker {
window.removeEventListener("pointerdown", this.#boundPointerDown);
}

get #isDropdownVisible() {
return this.#dropdown && !this.#dropdown.classList.contains("hidden");
}

_hideDropdownFromKeyboard() {
if (
this.#isMainColorPicker ||
!this.#dropdown ||
this.#dropdown.classList.contains("hidden")
) {
if (this.#isMainColorPicker || !this.#isDropdownVisible) {
return;
}
this.hideDropdown();
this.#button.removeEventListener("keydown", this.#boundKeyDown);
this.#button.focus({
preventScroll: true,
focusVisible: this.#dropdownWasFromKeyboard,
Expand Down
19 changes: 19 additions & 0 deletions test/integration/highlight_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,25 @@ describe("Highlight Editor", () => {
await page.waitForSelector(
`.page[data-page-number = "1"] svg.highlight[fill = "#FF00FF"]`
);

for (let i = 0; i < 4; i++) {
await page.keyboard.press("ArrowUp");
}
await page.waitForSelector(
`${sel} .editToolbar button.colorPicker .dropdown.hidden`
);
await page.keyboard.press("ArrowDown");
await page.waitForSelector(
`${sel} .editToolbar button.colorPicker .dropdown:not(.hidden)`
);
await page.keyboard.press("ArrowUp");
await page.waitForSelector(
`${sel} .editToolbar button.colorPicker .dropdown.hidden`
);
await page.keyboard.press(" ");
await page.waitForSelector(
`${sel} .editToolbar button.colorPicker .dropdown:not(.hidden)`
);
})
);
});
Expand Down