mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-23 00:28:06 +02:00
[Editor] Make editors movable in using the keyboard (bug 1845088)
Selected editors can be moved in using the arrows: - up/down/left/right will move the editors of 1 in page unit; - ctrl (or meta)+up/down/left/right will move them of 10 in page unit.
This commit is contained in:
parent
48cc67f17e
commit
bb6936c931
8 changed files with 311 additions and 50 deletions
|
@ -18,8 +18,13 @@
|
|||
// eslint-disable-next-line max-len
|
||||
/** @typedef {import("./tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */
|
||||
|
||||
import {
|
||||
AnnotationEditorParamsType,
|
||||
FeatureTest,
|
||||
shadow,
|
||||
unreachable,
|
||||
} from "../../shared/util.js";
|
||||
import { bindEvents, ColorManager } from "./tools.js";
|
||||
import { FeatureTest, shadow, unreachable } from "../../shared/util.js";
|
||||
|
||||
/**
|
||||
* @typedef {Object} AnnotationEditorParameters
|
||||
|
@ -261,13 +266,7 @@ class AnnotationEditor {
|
|||
this.fixAndSetPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate the editor position within its parent.
|
||||
* @param {number} x - x-translation in screen coordinates.
|
||||
* @param {number} y - y-translation in screen coordinates.
|
||||
*/
|
||||
translate(x, y) {
|
||||
const [width, height] = this.parentDimensions;
|
||||
#translate([width, height], x, y) {
|
||||
[x, y] = this.screenToPageTranslation(x, y);
|
||||
|
||||
this.x += x / width;
|
||||
|
@ -276,6 +275,26 @@ class AnnotationEditor {
|
|||
this.fixAndSetPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate the editor position within its parent.
|
||||
* @param {number} x - x-translation in screen coordinates.
|
||||
* @param {number} y - y-translation in screen coordinates.
|
||||
*/
|
||||
translate(x, y) {
|
||||
this.#translate(this.parentDimensions, x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate the editor position within its page and adjust the scroll
|
||||
* in order to have the editor in the view.
|
||||
* @param {number} x - x-translation in page coordinates.
|
||||
* @param {number} y - y-translation in page coordinates.
|
||||
*/
|
||||
translateInPage(x, y) {
|
||||
this.#translate(this.pageDimensions, x, y);
|
||||
this.div.scrollIntoView({ block: "nearest" });
|
||||
}
|
||||
|
||||
fixAndSetPosition() {
|
||||
const [pageWidth, pageHeight] = this.pageDimensions;
|
||||
let { x, y, width, height } = this;
|
||||
|
@ -663,7 +682,7 @@ class AnnotationEditor {
|
|||
cmd,
|
||||
undo,
|
||||
mustExec: true,
|
||||
type: this.resizeType,
|
||||
type: AnnotationEditorParamsType.RESIZE,
|
||||
overwriteIfSameType: true,
|
||||
keepUndo: true,
|
||||
});
|
||||
|
@ -922,13 +941,6 @@ class AnnotationEditor {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {number} the type to use in the undo/redo stack when resizing.
|
||||
*/
|
||||
get resizeType() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {boolean} true if this editor can be resized.
|
||||
*/
|
||||
|
|
|
@ -71,7 +71,7 @@ class FreeTextEditor extends AnnotationEditor {
|
|||
// See bug 1831574.
|
||||
["ctrl+s", "mac+meta+s", "ctrl+p", "mac+meta+p"],
|
||||
FreeTextEditor.prototype.commitOrRemove,
|
||||
/* bubbles = */ true,
|
||||
{ bubbles: true },
|
||||
],
|
||||
[
|
||||
["ctrl+Enter", "mac+meta+Enter", "Escape", "mac+Escape"],
|
||||
|
|
|
@ -157,11 +157,6 @@ class InkEditor extends AnnotationEditor {
|
|||
];
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
get resizeType() {
|
||||
return AnnotationEditorParamsType.INK_DIMS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the thickness and make this action undoable.
|
||||
* @param {number} thickness
|
||||
|
|
|
@ -13,11 +13,8 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
AnnotationEditorParamsType,
|
||||
AnnotationEditorType,
|
||||
} from "../../shared/util.js";
|
||||
import { AnnotationEditor } from "./editor.js";
|
||||
import { AnnotationEditorType } from "../../shared/util.js";
|
||||
import { PixelsPerInch } from "../display_utils.js";
|
||||
import { StampAnnotationElement } from "../annotation_layer.js";
|
||||
|
||||
|
@ -126,11 +123,6 @@ class StampEditor extends AnnotationEditor {
|
|||
}
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
get resizeType() {
|
||||
return AnnotationEditorParamsType.STAMP_DIMS;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
remove() {
|
||||
if (this.#bitmapId) {
|
||||
|
|
|
@ -355,14 +355,14 @@ class KeyboardManager {
|
|||
this.allKeys = new Set();
|
||||
|
||||
const { isMac } = FeatureTest.platform;
|
||||
for (const [keys, callback, bubbles = false] of callbacks) {
|
||||
for (const [keys, callback, options = {}] of callbacks) {
|
||||
for (const key of keys) {
|
||||
const isMacKey = key.startsWith("mac+");
|
||||
if (isMac && isMacKey) {
|
||||
this.callbacks.set(key.slice(4), { callback, bubbles });
|
||||
this.callbacks.set(key.slice(4), { callback, options });
|
||||
this.allKeys.add(key.split("+").at(-1));
|
||||
} else if (!isMac && !isMacKey) {
|
||||
this.callbacks.set(key, { callback, bubbles });
|
||||
this.callbacks.set(key, { callback, options });
|
||||
this.allKeys.add(key.split("+").at(-1));
|
||||
}
|
||||
}
|
||||
|
@ -410,8 +410,15 @@ class KeyboardManager {
|
|||
if (!info) {
|
||||
return;
|
||||
}
|
||||
const { callback, bubbles } = info;
|
||||
callback.bind(self)();
|
||||
const {
|
||||
callback,
|
||||
options: { bubbles = false, args = [], checker = null },
|
||||
} = info;
|
||||
|
||||
if (checker && !checker(self, event)) {
|
||||
return;
|
||||
}
|
||||
callback.bind(self, ...args)();
|
||||
|
||||
// For example, ctrl+s in a FreeText must be handled by the viewer, hence
|
||||
// the event must bubble.
|
||||
|
@ -548,9 +555,29 @@ class AnnotationEditorUIManager {
|
|||
hasSelectedEditor: false,
|
||||
};
|
||||
|
||||
#translation = [0, 0];
|
||||
|
||||
#translationTimeoutId = null;
|
||||
|
||||
#container = null;
|
||||
|
||||
static #TRANSLATE_SMALL = 1; // page units.
|
||||
|
||||
static #TRANSLATE_BIG = 10; // page units.
|
||||
|
||||
static get _keyboardManager() {
|
||||
const arrowChecker = self => {
|
||||
// If the focused element is an input, we don't want to handle the arrow.
|
||||
// For example, sliders can be controlled with the arrow keys.
|
||||
const { activeElement } = document;
|
||||
return (
|
||||
activeElement &&
|
||||
self.#container.contains(activeElement) &&
|
||||
self.hasSomethingToControl()
|
||||
);
|
||||
};
|
||||
const small = this.#TRANSLATE_SMALL;
|
||||
const big = this.#TRANSLATE_BIG;
|
||||
return shadow(
|
||||
this,
|
||||
"_keyboardManager",
|
||||
|
@ -592,6 +619,46 @@ class AnnotationEditorUIManager {
|
|||
["Escape", "mac+Escape"],
|
||||
AnnotationEditorUIManager.prototype.unselectAll,
|
||||
],
|
||||
[
|
||||
["ArrowLeft", "mac+ArrowLeft"],
|
||||
AnnotationEditorUIManager.prototype.translateSelectedEditors,
|
||||
{ args: [-small, 0], checker: arrowChecker },
|
||||
],
|
||||
[
|
||||
["ctrl+ArrowLeft", "mac+meta+ArrowLeft"],
|
||||
AnnotationEditorUIManager.prototype.translateSelectedEditors,
|
||||
{ args: [-big, 0], checker: arrowChecker },
|
||||
],
|
||||
[
|
||||
["ArrowRight", "mac+ArrowRight"],
|
||||
AnnotationEditorUIManager.prototype.translateSelectedEditors,
|
||||
{ args: [small, 0], checker: arrowChecker },
|
||||
],
|
||||
[
|
||||
["ctrl+ArrowRight", "mac+meta+ArrowRight"],
|
||||
AnnotationEditorUIManager.prototype.translateSelectedEditors,
|
||||
{ args: [big, 0], checker: arrowChecker },
|
||||
],
|
||||
[
|
||||
["ArrowUp", "mac+ArrowUp"],
|
||||
AnnotationEditorUIManager.prototype.translateSelectedEditors,
|
||||
{ args: [0, -small], checker: arrowChecker },
|
||||
],
|
||||
[
|
||||
["ctrl+ArrowUp", "mac+meta+ArrowUp"],
|
||||
AnnotationEditorUIManager.prototype.translateSelectedEditors,
|
||||
{ args: [0, -big], checker: arrowChecker },
|
||||
],
|
||||
[
|
||||
["ArrowDown", "mac+ArrowDown"],
|
||||
AnnotationEditorUIManager.prototype.translateSelectedEditors,
|
||||
{ args: [0, small], checker: arrowChecker },
|
||||
],
|
||||
[
|
||||
["ctrl+ArrowDown", "mac+meta+ArrowDown"],
|
||||
AnnotationEditorUIManager.prototype.translateSelectedEditors,
|
||||
{ args: [0, big], checker: arrowChecker },
|
||||
],
|
||||
])
|
||||
);
|
||||
}
|
||||
|
@ -1260,6 +1327,10 @@ class AnnotationEditorUIManager {
|
|||
this.#activeEditor?.commitOrRemove();
|
||||
}
|
||||
|
||||
hasSomethingToControl() {
|
||||
return this.#activeEditor || this.hasSelection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select the editors.
|
||||
* @param {Array<AnnotationEditor>} editors
|
||||
|
@ -1296,7 +1367,7 @@ class AnnotationEditorUIManager {
|
|||
return;
|
||||
}
|
||||
|
||||
if (this.#selectedEditors.size === 0) {
|
||||
if (!this.hasSelection) {
|
||||
return;
|
||||
}
|
||||
for (const editor of this.#selectedEditors) {
|
||||
|
@ -1308,6 +1379,53 @@ class AnnotationEditorUIManager {
|
|||
});
|
||||
}
|
||||
|
||||
translateSelectedEditors(x, y) {
|
||||
this.commitOrRemove();
|
||||
if (!this.hasSelection) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.#translation[0] += x;
|
||||
this.#translation[1] += y;
|
||||
const [totalX, totalY] = this.#translation;
|
||||
const editors = [...this.#selectedEditors];
|
||||
|
||||
// We don't want to have an undo/redo for each translation so we wait a bit
|
||||
// before adding the command to the command manager.
|
||||
const TIME_TO_WAIT = 1000;
|
||||
|
||||
if (this.#translationTimeoutId) {
|
||||
clearTimeout(this.#translationTimeoutId);
|
||||
}
|
||||
|
||||
this.#translationTimeoutId = setTimeout(() => {
|
||||
this.#translationTimeoutId = null;
|
||||
this.#translation[0] = this.#translation[1] = 0;
|
||||
|
||||
this.addCommands({
|
||||
cmd: () => {
|
||||
for (const editor of editors) {
|
||||
if (this.#allEditors.has(editor.id)) {
|
||||
editor.translateInPage(totalX, totalY);
|
||||
}
|
||||
}
|
||||
},
|
||||
undo: () => {
|
||||
for (const editor of editors) {
|
||||
if (this.#allEditors.has(editor.id)) {
|
||||
editor.translateInPage(-totalX, -totalY);
|
||||
}
|
||||
}
|
||||
},
|
||||
mustExec: false,
|
||||
});
|
||||
}, TIME_TO_WAIT);
|
||||
|
||||
for (const editor of editors) {
|
||||
editor.translateInPage(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the current editor the one passed as argument?
|
||||
* @param {AnnotationEditor} editor
|
||||
|
|
|
@ -77,14 +77,13 @@ const AnnotationEditorType = {
|
|||
};
|
||||
|
||||
const AnnotationEditorParamsType = {
|
||||
FREETEXT_SIZE: 1,
|
||||
FREETEXT_COLOR: 2,
|
||||
FREETEXT_OPACITY: 3,
|
||||
INK_COLOR: 11,
|
||||
INK_THICKNESS: 12,
|
||||
INK_OPACITY: 13,
|
||||
INK_DIMS: 14,
|
||||
STAMP_DIMS: 21,
|
||||
RESIZE: 1,
|
||||
FREETEXT_SIZE: 11,
|
||||
FREETEXT_COLOR: 12,
|
||||
FREETEXT_OPACITY: 13,
|
||||
INK_COLOR: 21,
|
||||
INK_THICKNESS: 22,
|
||||
INK_OPACITY: 23,
|
||||
};
|
||||
|
||||
// Permission flags from Table 22, Section 7.6.3.2 of the PDF specification.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue