UNPKG

11.8 kBJavaScriptView Raw
1var $f7e14e656343df57$exports = require("./textSelection.main.js");
2var $cvDGj$react = require("react");
3var $cvDGj$reactariautils = require("@react-aria/utils");
4
5
6function $parcel$export(e, n, v, s) {
7 Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
8}
9
10$parcel$export(module.exports, "useMove", () => $c09386fc48fa427d$export$36da96379f79f245);
11/*
12 * Copyright 2020 Adobe. All rights reserved.
13 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License. You may obtain a copy
15 * of the License at http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software distributed under
18 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
19 * OF ANY KIND, either express or implied. See the License for the specific language
20 * governing permissions and limitations under the License.
21 */
22
23
24function $c09386fc48fa427d$export$36da96379f79f245(props) {
25 let { onMoveStart: onMoveStart, onMove: onMove, onMoveEnd: onMoveEnd } = props;
26 let state = (0, $cvDGj$react.useRef)({
27 didMove: false,
28 lastPosition: null,
29 id: null
30 });
31 let { addGlobalListener: addGlobalListener, removeGlobalListener: removeGlobalListener } = (0, $cvDGj$reactariautils.useGlobalListeners)();
32 let move = (0, $cvDGj$reactariautils.useEffectEvent)((originalEvent, pointerType, deltaX, deltaY)=>{
33 if (deltaX === 0 && deltaY === 0) return;
34 if (!state.current.didMove) {
35 state.current.didMove = true;
36 onMoveStart === null || onMoveStart === void 0 ? void 0 : onMoveStart({
37 type: 'movestart',
38 pointerType: pointerType,
39 shiftKey: originalEvent.shiftKey,
40 metaKey: originalEvent.metaKey,
41 ctrlKey: originalEvent.ctrlKey,
42 altKey: originalEvent.altKey
43 });
44 }
45 onMove === null || onMove === void 0 ? void 0 : onMove({
46 type: 'move',
47 pointerType: pointerType,
48 deltaX: deltaX,
49 deltaY: deltaY,
50 shiftKey: originalEvent.shiftKey,
51 metaKey: originalEvent.metaKey,
52 ctrlKey: originalEvent.ctrlKey,
53 altKey: originalEvent.altKey
54 });
55 });
56 let end = (0, $cvDGj$reactariautils.useEffectEvent)((originalEvent, pointerType)=>{
57 (0, $f7e14e656343df57$exports.restoreTextSelection)();
58 if (state.current.didMove) onMoveEnd === null || onMoveEnd === void 0 ? void 0 : onMoveEnd({
59 type: 'moveend',
60 pointerType: pointerType,
61 shiftKey: originalEvent.shiftKey,
62 metaKey: originalEvent.metaKey,
63 ctrlKey: originalEvent.ctrlKey,
64 altKey: originalEvent.altKey
65 });
66 });
67 let moveProps = (0, $cvDGj$react.useMemo)(()=>{
68 let moveProps = {};
69 let start = ()=>{
70 (0, $f7e14e656343df57$exports.disableTextSelection)();
71 state.current.didMove = false;
72 };
73 if (typeof PointerEvent === 'undefined') {
74 let onMouseMove = (e)=>{
75 if (e.button === 0) {
76 var _state_current_lastPosition, _state_current_lastPosition1;
77 var _state_current_lastPosition_pageX, _state_current_lastPosition_pageY;
78 move(e, 'mouse', e.pageX - ((_state_current_lastPosition_pageX = (_state_current_lastPosition = state.current.lastPosition) === null || _state_current_lastPosition === void 0 ? void 0 : _state_current_lastPosition.pageX) !== null && _state_current_lastPosition_pageX !== void 0 ? _state_current_lastPosition_pageX : 0), e.pageY - ((_state_current_lastPosition_pageY = (_state_current_lastPosition1 = state.current.lastPosition) === null || _state_current_lastPosition1 === void 0 ? void 0 : _state_current_lastPosition1.pageY) !== null && _state_current_lastPosition_pageY !== void 0 ? _state_current_lastPosition_pageY : 0));
79 state.current.lastPosition = {
80 pageX: e.pageX,
81 pageY: e.pageY
82 };
83 }
84 };
85 let onMouseUp = (e)=>{
86 if (e.button === 0) {
87 end(e, 'mouse');
88 removeGlobalListener(window, 'mousemove', onMouseMove, false);
89 removeGlobalListener(window, 'mouseup', onMouseUp, false);
90 }
91 };
92 moveProps.onMouseDown = (e)=>{
93 if (e.button === 0) {
94 start();
95 e.stopPropagation();
96 e.preventDefault();
97 state.current.lastPosition = {
98 pageX: e.pageX,
99 pageY: e.pageY
100 };
101 addGlobalListener(window, 'mousemove', onMouseMove, false);
102 addGlobalListener(window, 'mouseup', onMouseUp, false);
103 }
104 };
105 let onTouchMove = (e)=>{
106 let touch = [
107 ...e.changedTouches
108 ].findIndex(({ identifier: identifier })=>identifier === state.current.id);
109 if (touch >= 0) {
110 var _state_current_lastPosition, _state_current_lastPosition1;
111 let { pageX: pageX, pageY: pageY } = e.changedTouches[touch];
112 var _state_current_lastPosition_pageX, _state_current_lastPosition_pageY;
113 move(e, 'touch', pageX - ((_state_current_lastPosition_pageX = (_state_current_lastPosition = state.current.lastPosition) === null || _state_current_lastPosition === void 0 ? void 0 : _state_current_lastPosition.pageX) !== null && _state_current_lastPosition_pageX !== void 0 ? _state_current_lastPosition_pageX : 0), pageY - ((_state_current_lastPosition_pageY = (_state_current_lastPosition1 = state.current.lastPosition) === null || _state_current_lastPosition1 === void 0 ? void 0 : _state_current_lastPosition1.pageY) !== null && _state_current_lastPosition_pageY !== void 0 ? _state_current_lastPosition_pageY : 0));
114 state.current.lastPosition = {
115 pageX: pageX,
116 pageY: pageY
117 };
118 }
119 };
120 let onTouchEnd = (e)=>{
121 let touch = [
122 ...e.changedTouches
123 ].findIndex(({ identifier: identifier })=>identifier === state.current.id);
124 if (touch >= 0) {
125 end(e, 'touch');
126 state.current.id = null;
127 removeGlobalListener(window, 'touchmove', onTouchMove);
128 removeGlobalListener(window, 'touchend', onTouchEnd);
129 removeGlobalListener(window, 'touchcancel', onTouchEnd);
130 }
131 };
132 moveProps.onTouchStart = (e)=>{
133 if (e.changedTouches.length === 0 || state.current.id != null) return;
134 let { pageX: pageX, pageY: pageY, identifier: identifier } = e.changedTouches[0];
135 start();
136 e.stopPropagation();
137 e.preventDefault();
138 state.current.lastPosition = {
139 pageX: pageX,
140 pageY: pageY
141 };
142 state.current.id = identifier;
143 addGlobalListener(window, 'touchmove', onTouchMove, false);
144 addGlobalListener(window, 'touchend', onTouchEnd, false);
145 addGlobalListener(window, 'touchcancel', onTouchEnd, false);
146 };
147 } else {
148 let onPointerMove = (e)=>{
149 if (e.pointerId === state.current.id) {
150 var _state_current_lastPosition, _state_current_lastPosition1;
151 let pointerType = e.pointerType || 'mouse';
152 var _state_current_lastPosition_pageX, _state_current_lastPosition_pageY;
153 // Problems with PointerEvent#movementX/movementY:
154 // 1. it is always 0 on macOS Safari.
155 // 2. On Chrome Android, it's scaled by devicePixelRatio, but not on Chrome macOS
156 move(e, pointerType, e.pageX - ((_state_current_lastPosition_pageX = (_state_current_lastPosition = state.current.lastPosition) === null || _state_current_lastPosition === void 0 ? void 0 : _state_current_lastPosition.pageX) !== null && _state_current_lastPosition_pageX !== void 0 ? _state_current_lastPosition_pageX : 0), e.pageY - ((_state_current_lastPosition_pageY = (_state_current_lastPosition1 = state.current.lastPosition) === null || _state_current_lastPosition1 === void 0 ? void 0 : _state_current_lastPosition1.pageY) !== null && _state_current_lastPosition_pageY !== void 0 ? _state_current_lastPosition_pageY : 0));
157 state.current.lastPosition = {
158 pageX: e.pageX,
159 pageY: e.pageY
160 };
161 }
162 };
163 let onPointerUp = (e)=>{
164 if (e.pointerId === state.current.id) {
165 let pointerType = e.pointerType || 'mouse';
166 end(e, pointerType);
167 state.current.id = null;
168 removeGlobalListener(window, 'pointermove', onPointerMove, false);
169 removeGlobalListener(window, 'pointerup', onPointerUp, false);
170 removeGlobalListener(window, 'pointercancel', onPointerUp, false);
171 }
172 };
173 moveProps.onPointerDown = (e)=>{
174 if (e.button === 0 && state.current.id == null) {
175 start();
176 e.stopPropagation();
177 e.preventDefault();
178 state.current.lastPosition = {
179 pageX: e.pageX,
180 pageY: e.pageY
181 };
182 state.current.id = e.pointerId;
183 addGlobalListener(window, 'pointermove', onPointerMove, false);
184 addGlobalListener(window, 'pointerup', onPointerUp, false);
185 addGlobalListener(window, 'pointercancel', onPointerUp, false);
186 }
187 };
188 }
189 let triggerKeyboardMove = (e, deltaX, deltaY)=>{
190 start();
191 move(e, 'keyboard', deltaX, deltaY);
192 end(e, 'keyboard');
193 };
194 moveProps.onKeyDown = (e)=>{
195 switch(e.key){
196 case 'Left':
197 case 'ArrowLeft':
198 e.preventDefault();
199 e.stopPropagation();
200 triggerKeyboardMove(e, -1, 0);
201 break;
202 case 'Right':
203 case 'ArrowRight':
204 e.preventDefault();
205 e.stopPropagation();
206 triggerKeyboardMove(e, 1, 0);
207 break;
208 case 'Up':
209 case 'ArrowUp':
210 e.preventDefault();
211 e.stopPropagation();
212 triggerKeyboardMove(e, 0, -1);
213 break;
214 case 'Down':
215 case 'ArrowDown':
216 e.preventDefault();
217 e.stopPropagation();
218 triggerKeyboardMove(e, 0, 1);
219 break;
220 }
221 };
222 return moveProps;
223 }, [
224 state,
225 addGlobalListener,
226 removeGlobalListener,
227 move,
228 end
229 ]);
230 return {
231 moveProps: moveProps
232 };
233}
234
235
236//# sourceMappingURL=useMove.main.js.map