crDragDrop.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.DragManager = void 0;
  6. var _utils = require("../../utils");
  7. var _crProtocolHelper = require("./crProtocolHelper");
  8. /**
  9. * Copyright (c) Microsoft Corporation.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. class DragManager {
  24. constructor(page) {
  25. this._crPage = void 0;
  26. this._dragState = null;
  27. this._lastPosition = {
  28. x: 0,
  29. y: 0
  30. };
  31. this._crPage = page;
  32. }
  33. async cancelDrag() {
  34. if (!this._dragState) return false;
  35. await this._crPage._mainFrameSession._client.send('Input.dispatchDragEvent', {
  36. type: 'dragCancel',
  37. x: this._lastPosition.x,
  38. y: this._lastPosition.y,
  39. data: {
  40. items: [],
  41. dragOperationsMask: 0xFFFF
  42. }
  43. });
  44. this._dragState = null;
  45. return true;
  46. }
  47. async interceptDragCausedByMove(x, y, button, buttons, modifiers, moveCallback) {
  48. this._lastPosition = {
  49. x,
  50. y
  51. };
  52. if (this._dragState) {
  53. await this._crPage._mainFrameSession._client.send('Input.dispatchDragEvent', {
  54. type: 'dragOver',
  55. x,
  56. y,
  57. data: this._dragState,
  58. modifiers: (0, _crProtocolHelper.toModifiersMask)(modifiers)
  59. });
  60. return;
  61. }
  62. if (button !== 'left') return moveCallback();
  63. const client = this._crPage._mainFrameSession._client;
  64. let onDragIntercepted;
  65. const dragInterceptedPromise = new Promise(x => onDragIntercepted = x);
  66. await Promise.all(this._crPage._page.frames().map(async frame => {
  67. await frame.nonStallingEvaluateInExistingContext(function () {
  68. let didStartDrag = Promise.resolve(false);
  69. let dragEvent = null;
  70. const dragListener = event => dragEvent = event;
  71. const mouseListener = () => {
  72. didStartDrag = new Promise(callback => {
  73. window.addEventListener('dragstart', dragListener, {
  74. once: true,
  75. capture: true
  76. });
  77. setTimeout(() => callback(dragEvent ? !dragEvent.defaultPrevented : false), 0);
  78. });
  79. };
  80. window.addEventListener('mousemove', mouseListener, {
  81. once: true,
  82. capture: true
  83. });
  84. window.__cleanupDrag = async () => {
  85. const val = await didStartDrag;
  86. window.removeEventListener('mousemove', mouseListener, {
  87. capture: true
  88. });
  89. window.removeEventListener('dragstart', dragListener, {
  90. capture: true
  91. });
  92. delete window.__cleanupDrag;
  93. return val;
  94. };
  95. }.toString(), true, 'utility').catch(() => {});
  96. }));
  97. client.on('Input.dragIntercepted', onDragIntercepted);
  98. try {
  99. await client.send('Input.setInterceptDrags', {
  100. enabled: true
  101. });
  102. } catch {
  103. // If Input.setInterceptDrags is not supported, just do a regular move.
  104. // This can be removed once we stop supporting old Electron.
  105. client.off('Input.dragIntercepted', onDragIntercepted);
  106. return moveCallback();
  107. }
  108. await moveCallback();
  109. const expectingDrag = (await Promise.all(this._crPage._page.frames().map(async frame => {
  110. return frame.nonStallingEvaluateInExistingContext('window.__cleanupDrag && window.__cleanupDrag()', false, 'utility').catch(() => false);
  111. }))).some(x => x);
  112. this._dragState = expectingDrag ? (await dragInterceptedPromise).data : null;
  113. client.off('Input.dragIntercepted', onDragIntercepted);
  114. await client.send('Input.setInterceptDrags', {
  115. enabled: false
  116. });
  117. if (this._dragState) {
  118. await this._crPage._mainFrameSession._client.send('Input.dispatchDragEvent', {
  119. type: 'dragEnter',
  120. x,
  121. y,
  122. data: this._dragState,
  123. modifiers: (0, _crProtocolHelper.toModifiersMask)(modifiers)
  124. });
  125. }
  126. }
  127. isDragging() {
  128. return !!this._dragState;
  129. }
  130. async drop(x, y, modifiers) {
  131. (0, _utils.assert)(this._dragState, 'missing drag state');
  132. await this._crPage._mainFrameSession._client.send('Input.dispatchDragEvent', {
  133. type: 'drop',
  134. x,
  135. y,
  136. data: this._dragState,
  137. modifiers: (0, _crProtocolHelper.toModifiersMask)(modifiers)
  138. });
  139. this._dragState = null;
  140. }
  141. }
  142. exports.DragManager = DragManager;