wkInput.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. 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); }
  10. 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; }
  11. /**
  12. * Copyright 2017 Google Inc. All rights reserved.
  13. * Modifications copyright (c) Microsoft Corporation.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the 'License');
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an 'AS IS' BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. function toModifiersMask(modifiers) {
  28. // From Source/WebKit/Shared/WebEvent.h
  29. let mask = 0;
  30. if (modifiers.has('Shift')) mask |= 1;
  31. if (modifiers.has('Control')) mask |= 2;
  32. if (modifiers.has('Alt')) mask |= 4;
  33. if (modifiers.has('Meta')) mask |= 8;
  34. return mask;
  35. }
  36. function toButtonsMask(buttons) {
  37. let mask = 0;
  38. if (buttons.has('left')) mask |= 1;
  39. if (buttons.has('right')) mask |= 2;
  40. if (buttons.has('middle')) mask |= 4;
  41. return mask;
  42. }
  43. class RawKeyboardImpl {
  44. constructor(session) {
  45. this._pageProxySession = void 0;
  46. this._session = void 0;
  47. this._pageProxySession = session;
  48. }
  49. setSession(session) {
  50. this._session = session;
  51. }
  52. async keydown(modifiers, code, keyCode, keyCodeWithoutLocation, key, location, autoRepeat, text) {
  53. const parts = [];
  54. for (const modifier of ['Shift', 'Control', 'Alt', 'Meta']) {
  55. if (modifiers.has(modifier)) parts.push(modifier);
  56. }
  57. parts.push(code);
  58. const shortcut = parts.join('+');
  59. let commands = _macEditingCommands.macEditingCommands[shortcut];
  60. if ((0, _utils.isString)(commands)) commands = [commands];
  61. await this._pageProxySession.send('Input.dispatchKeyEvent', {
  62. type: 'keyDown',
  63. modifiers: toModifiersMask(modifiers),
  64. windowsVirtualKeyCode: keyCode,
  65. code,
  66. key,
  67. text,
  68. unmodifiedText: text,
  69. autoRepeat,
  70. macCommands: commands,
  71. isKeypad: location === input.keypadLocation
  72. });
  73. }
  74. async keyup(modifiers, code, keyCode, keyCodeWithoutLocation, key, location) {
  75. await this._pageProxySession.send('Input.dispatchKeyEvent', {
  76. type: 'keyUp',
  77. modifiers: toModifiersMask(modifiers),
  78. key,
  79. windowsVirtualKeyCode: keyCode,
  80. code,
  81. isKeypad: location === input.keypadLocation
  82. });
  83. }
  84. async sendText(text) {
  85. await this._session.send('Page.insertText', {
  86. text
  87. });
  88. }
  89. }
  90. exports.RawKeyboardImpl = RawKeyboardImpl;
  91. class RawMouseImpl {
  92. constructor(session) {
  93. this._pageProxySession = void 0;
  94. this._session = void 0;
  95. this._page = void 0;
  96. this._pageProxySession = session;
  97. }
  98. setSession(session) {
  99. this._session = session;
  100. }
  101. async move(x, y, button, buttons, modifiers, forClick) {
  102. await this._pageProxySession.send('Input.dispatchMouseEvent', {
  103. type: 'move',
  104. button,
  105. buttons: toButtonsMask(buttons),
  106. x,
  107. y,
  108. modifiers: toModifiersMask(modifiers)
  109. });
  110. }
  111. async down(x, y, button, buttons, modifiers, clickCount) {
  112. await this._pageProxySession.send('Input.dispatchMouseEvent', {
  113. type: 'down',
  114. button,
  115. buttons: toButtonsMask(buttons),
  116. x,
  117. y,
  118. modifiers: toModifiersMask(modifiers),
  119. clickCount
  120. });
  121. }
  122. async up(x, y, button, buttons, modifiers, clickCount) {
  123. await this._pageProxySession.send('Input.dispatchMouseEvent', {
  124. type: 'up',
  125. button,
  126. buttons: toButtonsMask(buttons),
  127. x,
  128. y,
  129. modifiers: toModifiersMask(modifiers),
  130. clickCount
  131. });
  132. }
  133. async wheel(x, y, buttons, modifiers, deltaX, deltaY) {
  134. var _this$_page;
  135. if ((_this$_page = this._page) !== null && _this$_page !== void 0 && _this$_page._browserContext._options.isMobile) throw new Error('Mouse wheel is not supported in mobile WebKit');
  136. await this._session.send('Page.updateScrollingState');
  137. // Wheel events hit the compositor first, so wait one frame for it to be synced.
  138. await this._page.mainFrame().evaluateExpression(`new Promise(requestAnimationFrame)`, {
  139. world: 'utility'
  140. });
  141. await this._pageProxySession.send('Input.dispatchWheelEvent', {
  142. x,
  143. y,
  144. deltaX,
  145. deltaY,
  146. modifiers: toModifiersMask(modifiers)
  147. });
  148. }
  149. setPage(page) {
  150. this._page = page;
  151. }
  152. }
  153. exports.RawMouseImpl = RawMouseImpl;
  154. class RawTouchscreenImpl {
  155. constructor(session) {
  156. this._pageProxySession = void 0;
  157. this._pageProxySession = session;
  158. }
  159. async tap(x, y, modifiers) {
  160. await this._pageProxySession.send('Input.dispatchTapEvent', {
  161. x,
  162. y,
  163. modifiers: toModifiersMask(modifiers)
  164. });
  165. }
  166. }
  167. exports.RawTouchscreenImpl = RawTouchscreenImpl;