-
Notifications
You must be signed in to change notification settings - Fork 10k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Editor] Add a color picker with predefined colors for hightlighting …
…text (bug 1866434)
- Loading branch information
1 parent
9ac1ac6
commit f52697d
Showing
19 changed files
with
581 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
/* Copyright 2023 Mozilla Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { AnnotationEditorParamsType, shadow } from "../../shared/util.js"; | ||
import { KeyboardManager } from "./tools.js"; | ||
import { noContextMenu } from "../display_utils.js"; | ||
|
||
class ColorPicker { | ||
#boundKeyDown = this.#keyDown.bind(this); | ||
|
||
#button = null; | ||
|
||
#buttonSwatch = null; | ||
|
||
#defaultColor; | ||
|
||
#dropdown = null; | ||
|
||
#dropdownWasFromKeyboard = false; | ||
|
||
#isMainColorPicker = false; | ||
|
||
#eventBus; | ||
|
||
#uiManager = null; | ||
|
||
static get _keyboardManager() { | ||
return shadow( | ||
this, | ||
"_keyboardManager", | ||
new KeyboardManager([ | ||
[ | ||
["Escape", "mac+Escape"], | ||
ColorPicker.prototype._hideDropdownFromKeyboard, | ||
], | ||
[[" ", "mac+ "], ColorPicker.prototype._colorSelectFromKeyboard], | ||
[ | ||
["ArrowDown", "ArrowRight", "mac+ArrowDown", "mac+ArrowRight"], | ||
ColorPicker.prototype._moveToNext, | ||
], | ||
[ | ||
["ArrowUp", "ArrowLeft", "mac+ArrowUp", "mac+ArrowLeft"], | ||
ColorPicker.prototype._moveToPrevious, | ||
], | ||
[["Home", "mac+Home"], ColorPicker.prototype._moveToBeginning], | ||
[["End", "mac+End"], ColorPicker.prototype._moveToEnd], | ||
]) | ||
); | ||
} | ||
|
||
constructor({ editor = null, uiManager = null }) { | ||
this.#isMainColorPicker = !editor; | ||
this.#uiManager = editor?._uiManager || uiManager; | ||
this.#eventBus = this.#uiManager._eventBus; | ||
this.#defaultColor = | ||
editor?.color || this.#uiManager?.highlightColors.values().next().value; | ||
} | ||
|
||
renderButton() { | ||
const button = (this.#button = document.createElement("button")); | ||
button.className = "colorPicker"; | ||
button.tabIndex = "0"; | ||
button.setAttribute("data-l10n-id", "pdfjs-editor-colorpicker-button"); | ||
button.setAttribute("aria-haspopup", true); | ||
button.addEventListener("click", this.#openDropdown.bind(this)); | ||
const swatch = (this.#buttonSwatch = document.createElement("span")); | ||
swatch.className = "swatch"; | ||
swatch.style.backgroundColor = this.#defaultColor; | ||
button.append(swatch); | ||
return button; | ||
} | ||
|
||
renderMainDropdown() { | ||
const div = document.createElement("div"); | ||
div.className = "colorPicker"; | ||
const span = document.createElement("span"); | ||
div.append(span); | ||
span.id = "highlightColorPickerLabel"; | ||
span.setAttribute( | ||
"data-l10n-id", | ||
"pdfjs-editor-highlight-colorpicker-label" | ||
); | ||
|
||
const dropdown = (this.#dropdown = this.#getDropdownRoot( | ||
AnnotationEditorParamsType.DEFAULT_HIGHLIGHT_COLOR | ||
)); | ||
dropdown.setAttribute("aria-orientation", "horizontal"); | ||
dropdown.setAttribute("aria-labelledby", "highlightColorPickerLabel"); | ||
div.append(dropdown); | ||
|
||
return div; | ||
} | ||
|
||
#getDropdownRoot(paramType) { | ||
const div = document.createElement("div"); | ||
div.addEventListener("contextmenu", noContextMenu); | ||
div.className = "dropdown"; | ||
div.role = "listbox"; | ||
div.setAttribute("aria-multiselectable", false); | ||
div.setAttribute("aria-orientation", "vertical"); | ||
div.setAttribute("data-l10n-id", "pdfjs-editor-colorpicker-dropdown"); | ||
for (const [name, color] of this.#uiManager.highlightColors) { | ||
const button = document.createElement("button"); | ||
button.tabIndex = "0"; | ||
button.role = "option"; | ||
button.setAttribute("data-color", color); | ||
button.setAttribute("data-l10n-id", `pdfjs-editor-colorpicker-${name}`); | ||
const swatch = document.createElement("span"); | ||
button.append(swatch); | ||
swatch.className = "swatch"; | ||
swatch.style.backgroundColor = color; | ||
if (color === this.#defaultColor) { | ||
button.classList.add("selected"); | ||
button.setAttribute("aria-selected", true); | ||
} else { | ||
button.setAttribute("aria-selected", false); | ||
} | ||
button.addEventListener( | ||
"click", | ||
this.#colorSelect.bind(this, paramType, color) | ||
); | ||
div.append(button); | ||
} | ||
|
||
div.addEventListener("keydown", this.#boundKeyDown); | ||
|
||
return div; | ||
} | ||
|
||
#colorSelect(type, color, event) { | ||
event.stopPropagation(); | ||
this.#eventBus.dispatch("switchannotationeditorparams", { | ||
source: this, | ||
type, | ||
value: color, | ||
}); | ||
} | ||
|
||
_colorSelectFromKeyboard(event) { | ||
const color = event.target.getAttribute("data-color"); | ||
if (!color) { | ||
return; | ||
} | ||
this.#colorSelect(color, event); | ||
} | ||
|
||
_moveToNext(event) { | ||
if (event.target === this.#button) { | ||
this.#dropdown.firstChild?.focus(); | ||
return; | ||
} | ||
event.target.nextSibling?.focus(); | ||
} | ||
|
||
_moveToPrevious(event) { | ||
event.target.previousSibling?.focus(); | ||
} | ||
|
||
_moveToBeginning() { | ||
this.#dropdown.firstChild?.focus(); | ||
} | ||
|
||
_moveToEnd() { | ||
this.#dropdown.lastChild?.focus(); | ||
} | ||
|
||
#keyDown(event) { | ||
ColorPicker._keyboardManager.exec(this, event); | ||
} | ||
|
||
#openDropdown(event) { | ||
if (this.#dropdown && !this.#dropdown.classList.contains("hidden")) { | ||
this.hideDropdown(); | ||
return; | ||
} | ||
this.#button.addEventListener("keydown", this.#boundKeyDown); | ||
this.#dropdownWasFromKeyboard = event.detail === 0; | ||
if (this.#dropdown) { | ||
this.#dropdown.classList.toggle("hidden", false); | ||
return; | ||
} | ||
const root = (this.#dropdown = this.#getDropdownRoot( | ||
AnnotationEditorParamsType.HIGHLIGHT_COLOR | ||
)); | ||
this.#button.append(root); | ||
} | ||
|
||
hideDropdown() { | ||
this.#dropdown?.classList.add("hidden"); | ||
} | ||
|
||
_hideDropdownFromKeyboard() { | ||
if ( | ||
this.#isMainColorPicker || | ||
!this.#dropdown || | ||
this.#dropdown.classList.contains("hidden") | ||
) { | ||
return; | ||
} | ||
this.hideDropdown(); | ||
this.#button.removeEventListener("keydown", this.#boundKeyDown); | ||
this.#button.focus({ | ||
preventScroll: true, | ||
focusVisible: this.#dropdownWasFromKeyboard, | ||
}); | ||
} | ||
|
||
updateColor(color) { | ||
if (this.#buttonSwatch) { | ||
this.#buttonSwatch.style.backgroundColor = color; | ||
} | ||
if (!this.#dropdown) { | ||
return; | ||
} | ||
|
||
const i = this.#uiManager.highlightColors.values(); | ||
for (const child of this.#dropdown.children) { | ||
const isSelected = i.next().value === color; | ||
child.setAttribute("aria-selected", isSelected); | ||
child.classList.toggle("selected", isSelected); | ||
} | ||
} | ||
|
||
destroy() { | ||
this.#button?.remove(); | ||
this.#button = null; | ||
this.#buttonSwatch = null; | ||
this.#dropdown?.remove(); | ||
this.#dropdown = null; | ||
} | ||
} | ||
|
||
export { ColorPicker }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.