2
0

pdf_cursor_tools.js 3.1 KB

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