UNPKG

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