pdf_cursor_tools.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PDFCursorTools = exports.CursorTool = undefined;
  20. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  21. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  22. var _grab_to_pan = require('./grab_to_pan');
  23. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  24. var CursorTool = {
  25. SELECT: 0,
  26. HAND: 1,
  27. ZOOM: 2
  28. };
  29. var PDFCursorTools = function () {
  30. function PDFCursorTools(_ref) {
  31. var _this = this;
  32. var container = _ref.container,
  33. eventBus = _ref.eventBus,
  34. preferences = _ref.preferences;
  35. _classCallCheck(this, PDFCursorTools);
  36. this.container = container;
  37. this.eventBus = eventBus;
  38. this.active = CursorTool.SELECT;
  39. this.activeBeforePresentationMode = null;
  40. this.handTool = new _grab_to_pan.GrabToPan({ element: this.container });
  41. this._addEventListeners();
  42. Promise.all([preferences.get('cursorToolOnLoad'), preferences.get('enableHandToolOnLoad')]).then(function (_ref2) {
  43. var _ref3 = _slicedToArray(_ref2, 2),
  44. cursorToolPref = _ref3[0],
  45. handToolPref = _ref3[1];
  46. if (handToolPref === true) {
  47. preferences.set('enableHandToolOnLoad', false);
  48. if (cursorToolPref === CursorTool.SELECT) {
  49. cursorToolPref = CursorTool.HAND;
  50. preferences.set('cursorToolOnLoad', cursorToolPref).catch(function () {});
  51. }
  52. }
  53. _this.switchTool(cursorToolPref);
  54. }).catch(function () {});
  55. }
  56. _createClass(PDFCursorTools, [{
  57. key: 'switchTool',
  58. value: function switchTool(tool) {
  59. var _this2 = this;
  60. if (this.activeBeforePresentationMode !== null) {
  61. return;
  62. }
  63. if (tool === this.active) {
  64. return;
  65. }
  66. var disableActiveTool = function disableActiveTool() {
  67. switch (_this2.active) {
  68. case CursorTool.SELECT:
  69. break;
  70. case CursorTool.HAND:
  71. _this2.handTool.deactivate();
  72. break;
  73. case CursorTool.ZOOM:
  74. }
  75. };
  76. switch (tool) {
  77. case CursorTool.SELECT:
  78. disableActiveTool();
  79. break;
  80. case CursorTool.HAND:
  81. disableActiveTool();
  82. this.handTool.activate();
  83. break;
  84. case CursorTool.ZOOM:
  85. default:
  86. console.error('switchTool: "' + tool + '" is an unsupported value.');
  87. return;
  88. }
  89. this.active = tool;
  90. this._dispatchEvent();
  91. }
  92. }, {
  93. key: '_dispatchEvent',
  94. value: function _dispatchEvent() {
  95. this.eventBus.dispatch('cursortoolchanged', {
  96. source: this,
  97. tool: this.active
  98. });
  99. }
  100. }, {
  101. key: '_addEventListeners',
  102. value: function _addEventListeners() {
  103. var _this3 = this;
  104. this.eventBus.on('switchcursortool', function (evt) {
  105. _this3.switchTool(evt.tool);
  106. });
  107. this.eventBus.on('presentationmodechanged', function (evt) {
  108. if (evt.switchInProgress) {
  109. return;
  110. }
  111. var previouslyActive = void 0;
  112. if (evt.active) {
  113. previouslyActive = _this3.active;
  114. _this3.switchTool(CursorTool.SELECT);
  115. _this3.activeBeforePresentationMode = previouslyActive;
  116. } else {
  117. previouslyActive = _this3.activeBeforePresentationMode;
  118. _this3.activeBeforePresentationMode = null;
  119. _this3.switchTool(previouslyActive);
  120. }
  121. });
  122. }
  123. }, {
  124. key: 'activeTool',
  125. get: function get() {
  126. return this.active;
  127. }
  128. }]);
  129. return PDFCursorTools;
  130. }();
  131. exports.CursorTool = CursorTool;
  132. exports.PDFCursorTools = PDFCursorTools;