[Editing] Disable the HandTool during editing (bug 1792422)

This extends the approach used in PresentationMode to also cover the AnnotationEditor, and tries to handle the combination of both cases correctly.
In order to simplify the overall implementation we simply track the *first* seen "previous" cursorTool, and don't allow it to be reset as long as either PresentationMode or an AnnotationEditor is being used.
This commit is contained in:
Jonas Jenwald 2022-09-28 23:25:51 +02:00
parent 5675a6ee64
commit 205ab95819

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
import { AnnotationEditorType } from "pdfjs-lib";
import { GrabToPan } from "./grab_to_pan.js";
import { PresentationModeState } from "./ui_utils.js";
@ -40,7 +41,7 @@ class PDFCursorTools {
this.eventBus = eventBus;
this.active = CursorTool.SELECT;
this.activeBeforePresentationMode = null;
this.previouslyActive = null;
this.handTool = new GrabToPan({
element: this.container,
@ -63,13 +64,13 @@ class PDFCursorTools {
}
/**
* NOTE: This method is ignored while Presentation Mode is active.
* @param {number} tool - The cursor mode that should be switched to,
* must be one of the values in {CursorTool}.
*/
switchTool(tool) {
if (this.activeBeforePresentationMode !== null) {
return; // Cursor tools cannot be used in Presentation Mode.
if (this.previouslyActive !== null) {
// Cursor tools cannot be used in PresentationMode/AnnotationEditor.
return;
}
if (tool === this.active) {
return; // The requested tool is already active.
@ -121,24 +122,54 @@ class PDFCursorTools {
this.switchTool(evt.tool);
});
this.eventBus._on("presentationmodechanged", evt => {
switch (evt.state) {
case PresentationModeState.FULLSCREEN: {
const previouslyActive = this.active;
let annotationEditorMode = AnnotationEditorType.NONE,
presentationModeState = PresentationModeState.NORMAL;
this.switchTool(CursorTool.SELECT);
this.activeBeforePresentationMode = previouslyActive;
break;
}
case PresentationModeState.NORMAL: {
const previouslyActive = this.activeBeforePresentationMode;
const disableActive = () => {
const previouslyActive = this.active;
if (previouslyActive !== null) {
this.activeBeforePresentationMode = null;
this.switchTool(previouslyActive);
}
break;
}
this.switchTool(CursorTool.SELECT);
this.previouslyActive ??= previouslyActive; // Keep track of the first one.
};
const enableActive = () => {
const previouslyActive = this.previouslyActive;
if (
previouslyActive !== null &&
annotationEditorMode === AnnotationEditorType.NONE &&
presentationModeState === PresentationModeState.NORMAL
) {
this.previouslyActive = null;
this.switchTool(previouslyActive);
}
};
this.eventBus._on("secondarytoolbarreset", evt => {
if (this.previouslyActive !== null) {
annotationEditorMode = AnnotationEditorType.NONE;
presentationModeState = PresentationModeState.NORMAL;
enableActive();
}
});
this.eventBus._on("annotationeditormodechanged", ({ mode }) => {
annotationEditorMode = mode;
if (mode === AnnotationEditorType.NONE) {
enableActive();
} else {
disableActive();
}
});
this.eventBus._on("presentationmodechanged", ({ state }) => {
presentationModeState = state;
if (state === PresentationModeState.NORMAL) {
enableActive();
} else if (state === PresentationModeState.FULLSCREEN) {
disableActive();
}
});
}