crInput.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.RawTouchscreenImpl = exports.RawMouseImpl = exports.RawKeyboardImpl = void 0;
  6. var input = _interopRequireWildcard(require("../input"));
  7. var _macEditingCommands = require("../macEditingCommands");
  8. var _utils = require("../../utils");
  9. var _crProtocolHelper = require("./crProtocolHelper");
  10. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  11. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  12. /**
  13. * Copyright 2017 Google Inc. All rights reserved.
  14. * Modifications copyright (c) Microsoft Corporation.
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the 'License');
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an 'AS IS' BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. class RawKeyboardImpl {
  29. constructor(_client, _isMac, _dragManger) {
  30. this._client = _client;
  31. this._isMac = _isMac;
  32. this._dragManger = _dragManger;
  33. }
  34. _commandsForCode(code, modifiers) {
  35. if (!this._isMac) return [];
  36. const parts = [];
  37. for (const modifier of ['Shift', 'Control', 'Alt', 'Meta']) {
  38. if (modifiers.has(modifier)) parts.push(modifier);
  39. }
  40. parts.push(code);
  41. const shortcut = parts.join('+');
  42. let commands = _macEditingCommands.macEditingCommands[shortcut] || [];
  43. if ((0, _utils.isString)(commands)) commands = [commands];
  44. // Commands that insert text are not supported
  45. commands = commands.filter(x => !x.startsWith('insert'));
  46. // remove the trailing : to match the Chromium command names.
  47. return commands.map(c => c.substring(0, c.length - 1));
  48. }
  49. async keydown(modifiers, code, keyCode, keyCodeWithoutLocation, key, location, autoRepeat, text) {
  50. if (code === 'Escape' && (await this._dragManger.cancelDrag())) return;
  51. const commands = this._commandsForCode(code, modifiers);
  52. await this._client.send('Input.dispatchKeyEvent', {
  53. type: text ? 'keyDown' : 'rawKeyDown',
  54. modifiers: (0, _crProtocolHelper.toModifiersMask)(modifiers),
  55. windowsVirtualKeyCode: keyCodeWithoutLocation,
  56. code,
  57. commands,
  58. key,
  59. text,
  60. unmodifiedText: text,
  61. autoRepeat,
  62. location,
  63. isKeypad: location === input.keypadLocation
  64. });
  65. }
  66. async keyup(modifiers, code, keyCode, keyCodeWithoutLocation, key, location) {
  67. await this._client.send('Input.dispatchKeyEvent', {
  68. type: 'keyUp',
  69. modifiers: (0, _crProtocolHelper.toModifiersMask)(modifiers),
  70. key,
  71. windowsVirtualKeyCode: keyCodeWithoutLocation,
  72. code,
  73. location
  74. });
  75. }
  76. async sendText(text) {
  77. await this._client.send('Input.insertText', {
  78. text
  79. });
  80. }
  81. }
  82. exports.RawKeyboardImpl = RawKeyboardImpl;
  83. class RawMouseImpl {
  84. constructor(page, client, dragManager) {
  85. this._client = void 0;
  86. this._page = void 0;
  87. this._dragManager = void 0;
  88. this._page = page;
  89. this._client = client;
  90. this._dragManager = dragManager;
  91. }
  92. async move(x, y, button, buttons, modifiers, forClick) {
  93. const actualMove = async () => {
  94. await this._client.send('Input.dispatchMouseEvent', {
  95. type: 'mouseMoved',
  96. button,
  97. buttons: (0, _crProtocolHelper.toButtonsMask)(buttons),
  98. x,
  99. y,
  100. modifiers: (0, _crProtocolHelper.toModifiersMask)(modifiers)
  101. });
  102. };
  103. if (forClick) {
  104. // Avoid extra protocol calls related to drag and drop, because click relies on
  105. // move-down-up protocol commands being sent synchronously.
  106. return actualMove();
  107. }
  108. await this._dragManager.interceptDragCausedByMove(x, y, button, buttons, modifiers, actualMove);
  109. }
  110. async down(x, y, button, buttons, modifiers, clickCount) {
  111. if (this._dragManager.isDragging()) return;
  112. await this._client.send('Input.dispatchMouseEvent', {
  113. type: 'mousePressed',
  114. button,
  115. buttons: (0, _crProtocolHelper.toButtonsMask)(buttons),
  116. x,
  117. y,
  118. modifiers: (0, _crProtocolHelper.toModifiersMask)(modifiers),
  119. clickCount
  120. });
  121. }
  122. async up(x, y, button, buttons, modifiers, clickCount) {
  123. if (this._dragManager.isDragging()) {
  124. await this._dragManager.drop(x, y, modifiers);
  125. return;
  126. }
  127. await this._client.send('Input.dispatchMouseEvent', {
  128. type: 'mouseReleased',
  129. button,
  130. buttons: (0, _crProtocolHelper.toButtonsMask)(buttons),
  131. x,
  132. y,
  133. modifiers: (0, _crProtocolHelper.toModifiersMask)(modifiers),
  134. clickCount
  135. });
  136. }
  137. async wheel(x, y, buttons, modifiers, deltaX, deltaY) {
  138. await this._client.send('Input.dispatchMouseEvent', {
  139. type: 'mouseWheel',
  140. x,
  141. y,
  142. modifiers: (0, _crProtocolHelper.toModifiersMask)(modifiers),
  143. deltaX,
  144. deltaY
  145. });
  146. }
  147. }
  148. exports.RawMouseImpl = RawMouseImpl;
  149. class RawTouchscreenImpl {
  150. constructor(client) {
  151. this._client = void 0;
  152. this._client = client;
  153. }
  154. async tap(x, y, modifiers) {
  155. await Promise.all([this._client.send('Input.dispatchTouchEvent', {
  156. type: 'touchStart',
  157. modifiers: (0, _crProtocolHelper.toModifiersMask)(modifiers),
  158. touchPoints: [{
  159. x,
  160. y
  161. }]
  162. }), this._client.send('Input.dispatchTouchEvent', {
  163. type: 'touchEnd',
  164. modifiers: (0, _crProtocolHelper.toModifiersMask)(modifiers),
  165. touchPoints: []
  166. })]);
  167. }
  168. }
  169. exports.RawTouchscreenImpl = RawTouchscreenImpl;