1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

Merge pull request #17650 from calixteman/editor_highlight_keyboard

[Editor] Add a way to highlight text in using the keyboard (bug 1877426)
This commit is contained in:
calixteman 2024-02-11 18:52:37 +01:00 committed by GitHub
commit f15b4b34fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 107 additions and 7 deletions

View file

@ -679,14 +679,35 @@ class AnnotationEditorLayer {
/**
* SelectionChange callback.
* @param {Event} _event
* @param {Event} event
*/
selectionStart(_event) {
this.#textLayer?.div.addEventListener(
"pointerup",
this.#boundPointerUpAfterSelection,
{ once: true }
);
selectionStart(event) {
if (
!this.#textLayer ||
event.target.parentElement.closest(".textLayer") !== this.#textLayer.div
) {
return;
}
if (this.#uiManager.isShiftKeyDown) {
const keyup = () => {
if (this.#uiManager.isShiftKeyDown) {
return;
}
window.removeEventListener("keyup", keyup);
window.removeEventListener("blur", keyup);
this.pointerUpAfterSelection({});
};
window.addEventListener("keyup", keyup);
window.addEventListener("blur", keyup);
} else {
this.#textLayer.div.addEventListener(
"pointerup",
this.#boundPointerUpAfterSelection,
{ once: true }
);
}
}
/**

View file

@ -581,6 +581,8 @@ class AnnotationEditorUIManager {
#boundKeydown = this.keydown.bind(this);
#boundKeyup = this.keyup.bind(this);
#boundOnEditingAction = this.onEditingAction.bind(this);
#boundOnPageChanging = this.onPageChanging.bind(this);
@ -765,6 +767,7 @@ class AnnotationEditorUIManager {
realScale: PixelsPerInch.PDF_TO_CSS_UNITS,
rotation: 0,
};
this.isShiftKeyDown = false;
}
destroy() {
@ -915,6 +918,7 @@ class AnnotationEditorUIManager {
}
blur() {
this.isShiftKeyDown = false;
if (!this.hasSelection) {
return;
}
@ -952,10 +956,12 @@ class AnnotationEditorUIManager {
// 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);
window.addEventListener("keyup", this.#boundKeyup);
}
#removeKeyboardManager() {
window.removeEventListener("keydown", this.#boundKeydown);
window.removeEventListener("keyup", this.#boundKeyup);
}
#addCopyPasteListeners() {
@ -1084,11 +1090,24 @@ class AnnotationEditorUIManager {
* @param {KeyboardEvent} event
*/
keydown(event) {
if (!this.isShiftKeyDown && event.key === "Shift") {
this.isShiftKeyDown = true;
}
if (!this.isEditorHandlingKeyboard) {
AnnotationEditorUIManager._keyboardManager.exec(this, event);
}
}
/**
* Keyup callback.
* @param {KeyboardEvent} event
*/
keyup(event) {
if (this.isShiftKeyDown && event.key === "Shift") {
this.isShiftKeyDown = false;
}
}
/**
* Execute an action for a given name.
* For example, the user can click on the "Undo" entry in the context menu