pdf_cursor_tools.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFCursorTools = exports.CursorTool = void 0;
  27. var _pdf = require("../pdf");
  28. var _grab_to_pan = require("./grab_to_pan.js");
  29. var _ui_utils = require("./ui_utils.js");
  30. const CursorTool = {
  31. SELECT: 0,
  32. HAND: 1,
  33. ZOOM: 2
  34. };
  35. exports.CursorTool = CursorTool;
  36. class PDFCursorTools {
  37. constructor({
  38. container,
  39. eventBus,
  40. cursorToolOnLoad = CursorTool.SELECT
  41. }) {
  42. this.container = container;
  43. this.eventBus = eventBus;
  44. this.active = CursorTool.SELECT;
  45. this.previouslyActive = null;
  46. this.handTool = new _grab_to_pan.GrabToPan({
  47. element: this.container
  48. });
  49. this.#addEventListeners();
  50. Promise.resolve().then(() => {
  51. this.switchTool(cursorToolOnLoad);
  52. });
  53. }
  54. get activeTool() {
  55. return this.active;
  56. }
  57. switchTool(tool) {
  58. if (this.previouslyActive !== null) {
  59. return;
  60. }
  61. if (tool === this.active) {
  62. return;
  63. }
  64. const disableActiveTool = () => {
  65. switch (this.active) {
  66. case CursorTool.SELECT:
  67. break;
  68. case CursorTool.HAND:
  69. this.handTool.deactivate();
  70. break;
  71. case CursorTool.ZOOM:
  72. }
  73. };
  74. switch (tool) {
  75. case CursorTool.SELECT:
  76. disableActiveTool();
  77. break;
  78. case CursorTool.HAND:
  79. disableActiveTool();
  80. this.handTool.activate();
  81. break;
  82. case CursorTool.ZOOM:
  83. default:
  84. console.error(`switchTool: "${tool}" is an unsupported value.`);
  85. return;
  86. }
  87. this.active = tool;
  88. this.#dispatchEvent();
  89. }
  90. #dispatchEvent() {
  91. this.eventBus.dispatch("cursortoolchanged", {
  92. source: this,
  93. tool: this.active
  94. });
  95. }
  96. #addEventListeners() {
  97. this.eventBus._on("switchcursortool", evt => {
  98. this.switchTool(evt.tool);
  99. });
  100. let annotationEditorMode = _pdf.AnnotationEditorType.NONE,
  101. presentationModeState = _ui_utils.PresentationModeState.NORMAL;
  102. const disableActive = () => {
  103. const previouslyActive = this.active;
  104. this.switchTool(CursorTool.SELECT);
  105. this.previouslyActive ??= previouslyActive;
  106. };
  107. const enableActive = () => {
  108. const previouslyActive = this.previouslyActive;
  109. if (previouslyActive !== null && annotationEditorMode === _pdf.AnnotationEditorType.NONE && presentationModeState === _ui_utils.PresentationModeState.NORMAL) {
  110. this.previouslyActive = null;
  111. this.switchTool(previouslyActive);
  112. }
  113. };
  114. this.eventBus._on("secondarytoolbarreset", evt => {
  115. if (this.previouslyActive !== null) {
  116. annotationEditorMode = _pdf.AnnotationEditorType.NONE;
  117. presentationModeState = _ui_utils.PresentationModeState.NORMAL;
  118. enableActive();
  119. }
  120. });
  121. this.eventBus._on("annotationeditormodechanged", ({
  122. mode
  123. }) => {
  124. annotationEditorMode = mode;
  125. if (mode === _pdf.AnnotationEditorType.NONE) {
  126. enableActive();
  127. } else {
  128. disableActive();
  129. }
  130. });
  131. this.eventBus._on("presentationmodechanged", ({
  132. state
  133. }) => {
  134. presentationModeState = state;
  135. if (state === _ui_utils.PresentationModeState.NORMAL) {
  136. enableActive();
  137. } else if (state === _ui_utils.PresentationModeState.FULLSCREEN) {
  138. disableActive();
  139. }
  140. });
  141. }
  142. }
  143. exports.PDFCursorTools = PDFCursorTools;