pdf_cursor_tools.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFCursorTools = exports.CursorTool = void 0;
  27. var _grab_to_pan = require("./grab_to_pan.js");
  28. var _ui_utils = require("./ui_utils.js");
  29. const CursorTool = {
  30. SELECT: 0,
  31. HAND: 1,
  32. ZOOM: 2
  33. };
  34. exports.CursorTool = CursorTool;
  35. class PDFCursorTools {
  36. constructor({
  37. container,
  38. eventBus,
  39. cursorToolOnLoad = CursorTool.SELECT
  40. }) {
  41. this.container = container;
  42. this.eventBus = eventBus;
  43. this.active = CursorTool.SELECT;
  44. this.activeBeforePresentationMode = null;
  45. this.handTool = new _grab_to_pan.GrabToPan({
  46. element: this.container
  47. });
  48. this._addEventListeners();
  49. Promise.resolve().then(() => {
  50. this.switchTool(cursorToolOnLoad);
  51. });
  52. }
  53. get activeTool() {
  54. return this.active;
  55. }
  56. switchTool(tool) {
  57. if (this.activeBeforePresentationMode !== null) {
  58. return;
  59. }
  60. if (tool === this.active) {
  61. return;
  62. }
  63. const disableActiveTool = () => {
  64. switch (this.active) {
  65. case CursorTool.SELECT:
  66. break;
  67. case CursorTool.HAND:
  68. this.handTool.deactivate();
  69. break;
  70. case CursorTool.ZOOM:
  71. }
  72. };
  73. switch (tool) {
  74. case CursorTool.SELECT:
  75. disableActiveTool();
  76. break;
  77. case CursorTool.HAND:
  78. disableActiveTool();
  79. this.handTool.activate();
  80. break;
  81. case CursorTool.ZOOM:
  82. default:
  83. console.error(`switchTool: "${tool}" is an unsupported value.`);
  84. return;
  85. }
  86. this.active = tool;
  87. this._dispatchEvent();
  88. }
  89. _dispatchEvent() {
  90. this.eventBus.dispatch("cursortoolchanged", {
  91. source: this,
  92. tool: this.active
  93. });
  94. }
  95. _addEventListeners() {
  96. this.eventBus._on("switchcursortool", evt => {
  97. this.switchTool(evt.tool);
  98. });
  99. this.eventBus._on("presentationmodechanged", evt => {
  100. switch (evt.state) {
  101. case _ui_utils.PresentationModeState.FULLSCREEN:
  102. {
  103. const previouslyActive = this.active;
  104. this.switchTool(CursorTool.SELECT);
  105. this.activeBeforePresentationMode = previouslyActive;
  106. break;
  107. }
  108. case _ui_utils.PresentationModeState.NORMAL:
  109. {
  110. const previouslyActive = this.activeBeforePresentationMode;
  111. this.activeBeforePresentationMode = null;
  112. this.switchTool(previouslyActive);
  113. break;
  114. }
  115. }
  116. });
  117. }
  118. }
  119. exports.PDFCursorTools = PDFCursorTools;