UNPKG

3.36 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * JavaScript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.PDFCursorTools = exports.CursorTool = void 0;
28
29var _grab_to_pan = require("./grab_to_pan.js");
30
31var _ui_utils = require("./ui_utils.js");
32
33const CursorTool = {
34 SELECT: 0,
35 HAND: 1,
36 ZOOM: 2
37};
38exports.CursorTool = CursorTool;
39
40class PDFCursorTools {
41 constructor({
42 container,
43 eventBus,
44 cursorToolOnLoad = CursorTool.SELECT
45 }) {
46 this.container = container;
47 this.eventBus = eventBus;
48 this.active = CursorTool.SELECT;
49 this.activeBeforePresentationMode = null;
50 this.handTool = new _grab_to_pan.GrabToPan({
51 element: this.container
52 });
53 this.#addEventListeners();
54 Promise.resolve().then(() => {
55 this.switchTool(cursorToolOnLoad);
56 });
57 }
58
59 get activeTool() {
60 return this.active;
61 }
62
63 switchTool(tool) {
64 if (this.activeBeforePresentationMode !== null) {
65 return;
66 }
67
68 if (tool === this.active) {
69 return;
70 }
71
72 const disableActiveTool = () => {
73 switch (this.active) {
74 case CursorTool.SELECT:
75 break;
76
77 case CursorTool.HAND:
78 this.handTool.deactivate();
79 break;
80
81 case CursorTool.ZOOM:
82 }
83 };
84
85 switch (tool) {
86 case CursorTool.SELECT:
87 disableActiveTool();
88 break;
89
90 case CursorTool.HAND:
91 disableActiveTool();
92 this.handTool.activate();
93 break;
94
95 case CursorTool.ZOOM:
96 default:
97 console.error(`switchTool: "${tool}" is an unsupported value.`);
98 return;
99 }
100
101 this.active = tool;
102 this.#dispatchEvent();
103 }
104
105 #dispatchEvent() {
106 this.eventBus.dispatch("cursortoolchanged", {
107 source: this,
108 tool: this.active
109 });
110 }
111
112 #addEventListeners() {
113 this.eventBus._on("switchcursortool", evt => {
114 this.switchTool(evt.tool);
115 });
116
117 this.eventBus._on("presentationmodechanged", evt => {
118 switch (evt.state) {
119 case _ui_utils.PresentationModeState.FULLSCREEN:
120 {
121 const previouslyActive = this.active;
122 this.switchTool(CursorTool.SELECT);
123 this.activeBeforePresentationMode = previouslyActive;
124 break;
125 }
126
127 case _ui_utils.PresentationModeState.NORMAL:
128 {
129 const previouslyActive = this.activeBeforePresentationMode;
130 this.activeBeforePresentationMode = null;
131 this.switchTool(previouslyActive);
132 break;
133 }
134 }
135 });
136 }
137
138}
139
140exports.PDFCursorTools = PDFCursorTools;
\No newline at end of file