Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 20x 3x 3x 20x 3x 10x 3x 2x 2x 2x 2x 3x 3x 26x 26x 26x 26x 26x 26x 26x 26x 52x 26x 26x 26x 35x 26x 35x 26x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 1x 1x 3x 3x 2x 26x 3x 26x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 3x 3x 3x 10x 2x 2x 10x 10x 10x 2x 8x 2x 10x 3x 3x 3x 3x 3x 1x 1x 10x 3x 3x 3x 3x 3x 3x 2x 3x 3x 3x 3x 2x 3x 3x 3x 2x 2x 29x 3x 3x 36x 2x 2x 29x 9x 3x 3x 1x 2x 1x 1x | /**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
'use strict';
type ClickEvent = any;
type KeyboardEvent = any;
type ResponderEvent = any;
export type PressResponderConfig = $ReadOnly<{|
// The gesture can be interrupted by a parent gesture, e.g., scroll.
// Defaults to true.
cancelable?: ?boolean,
// Whether to disable initialization of the press gesture.
disabled?: ?boolean,
// Duration (in addition to `delayPressStart`) after which a press gesture is
// considered a long press gesture. Defaults to 500 (milliseconds).
delayLongPress?: ?number,
// Duration to wait after press down before calling `onPressStart`.
delayPressStart?: ?number,
// Duration to wait after letting up before calling `onPressEnd`.
delayPressEnd?: ?number,
// Called when a long press gesture has been triggered.
onLongPress?: ?(event: ResponderEvent) => void,
// Called when a press gestute has been triggered.
onPress?: ?(event: ClickEvent) => void,
// Called when the press is activated to provide visual feedback.
onPressChange?: ?(event: ResponderEvent) => void,
// Called when the press is activated to provide visual feedback.
onPressStart?: ?(event: ResponderEvent) => void,
// Called when the press location moves. (This should rarely be used.)
onPressMove?: ?(event: ResponderEvent) => void,
// Called when the press is deactivated to undo visual feedback.
onPressEnd?: ?(event: ResponderEvent) => void
|}>;
export type EventHandlers = $ReadOnly<{|
onClick: (event: ClickEvent) => void,
onContextMenu: (event: ClickEvent) => void,
onKeyDown: (event: KeyboardEvent) => void,
onResponderGrant: (event: ResponderEvent) => void,
onResponderMove: (event: ResponderEvent) => void,
onResponderRelease: (event: ResponderEvent) => void,
onResponderTerminate: (event: ResponderEvent) => void,
onResponderTerminationRequest: (event: ResponderEvent) => boolean,
onStartShouldSetResponder: (event: ResponderEvent) => boolean
|}>;
type TouchState =
| 'NOT_RESPONDER'
| 'RESPONDER_INACTIVE_PRESS_START'
| 'RESPONDER_ACTIVE_PRESS_START'
| 'RESPONDER_ACTIVE_LONG_PRESS_START'
| 'ERROR';
type TouchSignal =
| 'DELAY'
| 'RESPONDER_GRANT'
| 'RESPONDER_RELEASE'
| 'RESPONDER_TERMINATED'
| 'LONG_PRESS_DETECTED';
const DELAY = 'DELAY';
const ERROR = 'ERROR';
const LONG_PRESS_DETECTED = 'LONG_PRESS_DETECTED';
const NOT_RESPONDER = 'NOT_RESPONDER';
const RESPONDER_ACTIVE_LONG_PRESS_START = 'RESPONDER_ACTIVE_LONG_PRESS_START';
const RESPONDER_ACTIVE_PRESS_START = 'RESPONDER_ACTIVE_PRESS_START';
const RESPONDER_INACTIVE_PRESS_START = 'RESPONDER_INACTIVE_PRESS_START';
const RESPONDER_GRANT = 'RESPONDER_GRANT';
const RESPONDER_RELEASE = 'RESPONDER_RELEASE';
const RESPONDER_TERMINATED = 'RESPONDER_TERMINATED';
const Transitions = Object.freeze({
NOT_RESPONDER: {
DELAY: ERROR,
RESPONDER_GRANT: RESPONDER_INACTIVE_PRESS_START,
RESPONDER_RELEASE: ERROR,
RESPONDER_TERMINATED: ERROR,
LONG_PRESS_DETECTED: ERROR
},
RESPONDER_INACTIVE_PRESS_START: {
DELAY: RESPONDER_ACTIVE_PRESS_START,
RESPONDER_GRANT: ERROR,
RESPONDER_RELEASE: NOT_RESPONDER,
RESPONDER_TERMINATED: NOT_RESPONDER,
LONG_PRESS_DETECTED: ERROR
},
RESPONDER_ACTIVE_PRESS_START: {
DELAY: ERROR,
RESPONDER_GRANT: ERROR,
RESPONDER_RELEASE: NOT_RESPONDER,
RESPONDER_TERMINATED: NOT_RESPONDER,
LONG_PRESS_DETECTED: RESPONDER_ACTIVE_LONG_PRESS_START
},
RESPONDER_ACTIVE_LONG_PRESS_START: {
DELAY: ERROR,
RESPONDER_GRANT: ERROR,
RESPONDER_RELEASE: NOT_RESPONDER,
RESPONDER_TERMINATED: NOT_RESPONDER,
LONG_PRESS_DETECTED: RESPONDER_ACTIVE_LONG_PRESS_START
},
ERROR: {
DELAY: NOT_RESPONDER,
RESPONDER_GRANT: RESPONDER_INACTIVE_PRESS_START,
RESPONDER_RELEASE: NOT_RESPONDER,
RESPONDER_TERMINATED: NOT_RESPONDER,
LONG_PRESS_DETECTED: NOT_RESPONDER
}
});
const isActiveSignal = (signal) =>
signal === RESPONDER_ACTIVE_PRESS_START || signal === RESPONDER_ACTIVE_LONG_PRESS_START;
const isButtonRole = (element) => element.getAttribute('role') === 'button';
const isPressStartSignal = (signal) =>
signal === RESPONDER_INACTIVE_PRESS_START ||
signal === RESPONDER_ACTIVE_PRESS_START ||
signal === RESPONDER_ACTIVE_LONG_PRESS_START;
const isTerminalSignal = (signal) =>
signal === RESPONDER_TERMINATED || signal === RESPONDER_RELEASE;
const isValidKeyPress = (event) => {
const { key, target } = event;
const role = target.getAttribute('role');
const isSpacebar = key === ' ' || key === 'Spacebar';
return key === 'Enter' || (isSpacebar && role === 'button');
};
const DEFAULT_LONG_PRESS_DELAY_MS = 450; // 500 - 50
const DEFAULT_PRESS_DELAY_MS = 50;
/**
* =========================== PressResponder Tutorial ===========================
*
* The `PressResponder` class helps you create press interactions by analyzing the
* geometry of elements and observing when another responder (e.g. ScrollView)
* has stolen the touch lock. It offers hooks for your component to provide
* interaction feedback to the user:
*
* - When a press has activated (e.g. highlight an element)
* - When a press has deactivated (e.g. un-highlight an element)
* - When a press sould trigger an action, meaning it activated and deactivated
* while within the geometry of the element without the lock being stolen.
*
* A high quality interaction isn't as simple as you might think. There should
* be a slight delay before activation. Moving your finger beyond an element's
* bounds should trigger deactivation, but moving the same finger back within an
* element's bounds should trigger reactivation.
*
* In order to use `PressResponder`, do the following:
*
* const pressResponder = new PressResponder(config);
*
* 2. Choose the rendered component who should collect the press events. On that
* element, spread `pressability.getEventHandlers()` into its props.
*
* return (
* <View {...this.state.pressResponder.getEventHandlers()} />
* );
*
* 3. Reset `PressResponder` when your component unmounts.
*
* componentWillUnmount() {
* this.state.pressResponder.reset();
* }
*
* ==================== Implementation Details ====================
*
* `PressResponder` only assumes that there exists a `HitRect` node. The `PressRect`
* is an abstract box that is extended beyond the `HitRect`.
*
* # Geometry
*
* ┌────────────────────────┐
* │ ┌──────────────────┐ │ - Presses start anywhere within `HitRect`.
* │ │ ┌────────────┐ │ │
* │ │ │ VisualRect │ │ │
* │ │ └────────────┘ │ │ - When pressed down for sufficient amount of time
* │ │ HitRect │ │ before letting up, `VisualRect` activates.
* │ └──────────────────┘ │
* │ Out Region o │
* └────────────────────│───┘
* └────── When the press is released outside the `HitRect`,
* the responder is NOT eligible for a "press".
*
* # State Machine
*
* ┌───────────────┐ ◀──── RESPONDER_RELEASE
* │ NOT_RESPONDER │
* └───┬───────────┘ ◀──── RESPONDER_TERMINATED
* │
* │ RESPONDER_GRANT (HitRect)
* │
* ▼
* ┌─────────────────────┐ ┌───────────────────┐ ┌───────────────────┐
* │ RESPONDER_INACTIVE_ │ DELAY │ RESPONDER_ACTIVE_ │ T + DELAY │ RESPONDER_ACTIVE_ │
* │ PRESS_START ├────────▶ │ PRESS_START ├────────────▶ │ LONG_PRESS_START │
* └─────────────────────┘ └───────────────────┘ └───────────────────┘
*
* T + DELAY => LONG_PRESS_DELAY + DELAY
*
* Not drawn are the side effects of each transition. The most important side
* effect is the invocation of `onLongPress`. Only when the browser produces a
* `click` event is `onPress` invoked.
*/
export default class PressResponder {
_config: PressResponderConfig;
_eventHandlers: ?EventHandlers = null;
_isPointerTouch: ?boolean = false;
_longPressDelayTimeout: ?TimeoutID = null;
_longPressDispatched: ?boolean = false;
_pressDelayTimeout: ?TimeoutID = null;
_pressOutDelayTimeout: ?TimeoutID = null;
_selectionTerminated: ?boolean;
_touchActivatePosition: ?$ReadOnly<{|
pageX: number,
pageY: number
|}>;
_touchState: TouchState = NOT_RESPONDER;
constructor(config: PressResponderConfig) {
this.configure(config);
}
configure(config: PressResponderConfig): void {
this._config = config;
}
/**
* Resets any pending timers. This should be called on unmount.
*/
reset(): void {
this._cancelLongPressDelayTimeout();
this._cancelPressDelayTimeout();
this._cancelPressOutDelayTimeout();
}
/**
* Returns a set of props to spread into the interactive element.
*/
getEventHandlers(): EventHandlers {
if (this._eventHandlers == null) {
this._eventHandlers = this._createEventHandlers();
}
return this._eventHandlers;
}
_createEventHandlers(): EventHandlers {
const start = (event: ResponderEvent, shouldDelay?: boolean): void => {
event.persist();
this._cancelPressOutDelayTimeout();
this._longPressDispatched = false;
this._selectionTerminated = false;
this._touchState = NOT_RESPONDER;
this._isPointerTouch = event.nativeEvent.type === 'touchstart';
this._receiveSignal(RESPONDER_GRANT, event);
const delayPressStart = normalizeDelay(
this._config.delayPressStart,
0,
DEFAULT_PRESS_DELAY_MS
);
if (shouldDelay !== false && delayPressStart > 0) {
this._pressDelayTimeout = setTimeout(() => {
this._receiveSignal(DELAY, event);
}, delayPressStart);
} else {
this._receiveSignal(DELAY, event);
}
const delayLongPress = normalizeDelay(
this._config.delayLongPress,
10,
DEFAULT_LONG_PRESS_DELAY_MS
);
this._longPressDelayTimeout = setTimeout(() => {
this._handleLongPress(event);
}, delayLongPress + delayPressStart);
};
const end = (event: ResponderEvent): void => {
this._receiveSignal(RESPONDER_RELEASE, event);
};
const keyupHandler = (event: KeyboardEvent) => {
const { onPress } = this._config;
const { target } = event;
Eif (this._touchState !== NOT_RESPONDER && isValidKeyPress(event)) {
end(event);
document.removeEventListener('keyup', keyupHandler);
const role = target.getAttribute('role');
const elementType = target.tagName.toLowerCase();
const isNativeInteractiveElement =
role === 'link' ||
elementType === 'a' ||
elementType === 'button' ||
elementType === 'input' ||
elementType === 'select' ||
elementType === 'textarea';
Eif (onPress != null && !isNativeInteractiveElement) {
onPress(event);
}
}
};
return {
onStartShouldSetResponder: (event): boolean => {
const { disabled } = this._config;
Iif (disabled && isButtonRole(event.currentTarget)) {
event.stopPropagation();
}
Eif (disabled == null) {
return true;
}
return !disabled;
},
onKeyDown: (event) => {
const { disabled } = this._config;
const { key, target } = event;
Eif (!disabled && isValidKeyPress(event)) {
Eif (this._touchState === NOT_RESPONDER) {
start(event, false);
// Listen to 'keyup' on document to account for situations where
// focus is moved to another element during 'keydown'.
document.addEventListener('keyup', keyupHandler);
}
const role = target.getAttribute('role');
const isSpacebarKey = key === ' ' || key === 'Spacebar';
const isButtonRole = role === 'button' || role === 'menuitem';
Iif (isSpacebarKey && isButtonRole) {
// Prevent spacebar scrolling the window
event.preventDefault();
}
event.stopPropagation();
}
},
onResponderGrant: (event) => start(event),
onResponderMove: (event) => {
if (this._config.onPressMove != null) {
this._config.onPressMove(event);
}
const touch = getTouchFromResponderEvent(event);
if (this._touchActivatePosition != null) {
const deltaX = this._touchActivatePosition.pageX - touch.pageX;
const deltaY = this._touchActivatePosition.pageY - touch.pageY;
if (Math.hypot(deltaX, deltaY) > 10) {
this._cancelLongPressDelayTimeout();
}
}
},
onResponderRelease: (event) => end(event),
onResponderTerminate: (event) => {
if (event.nativeEvent.type === 'selectionchange') {
this._selectionTerminated = true;
}
this._receiveSignal(RESPONDER_TERMINATED, event);
},
onResponderTerminationRequest: (event): boolean => {
const { cancelable, disabled, onLongPress } = this._config;
// If `onLongPress` is provided, don't terminate on `contextmenu` as default
// behavior will be prevented for non-mouse pointers.
if (
!disabled &&
onLongPress != null &&
this._isPointerTouch &&
event.nativeEvent.type === 'contextmenu'
) {
return false;
}
if (cancelable == null) {
return true;
}
return cancelable;
},
// NOTE: this diverges from react-native in 3 significant ways:
// * The `onPress` callback is not connected to the responder system (the native
// `click` event must be used but is dispatched in many scenarios where no pointers
// are on the screen.) Therefore, it's possible for `onPress` to be called without
// `onPress{Start,End}` being called first.
// * The `onPress` callback is only be called on the first ancestor of the native
// `click` target that is using the PressResponder.
// * The event's `nativeEvent` is a `MouseEvent` not a `TouchEvent`.
onClick: (event: any): void => {
const { disabled, onPress } = this._config;
Eif (!disabled) {
// If long press dispatched, cancel default click behavior.
// If the responder terminated because text was selected during the gesture,
// cancel the default click behavior.
event.stopPropagation();
Iif (this._longPressDispatched || this._selectionTerminated) {
event.preventDefault();
} else Eif (onPress != null && event.altKey === false) {
onPress(event);
}
} else {
if (isButtonRole(event.currentTarget)) {
event.stopPropagation();
}
}
},
// If `onLongPress` is provided and a touch pointer is being used, prevent the
// default context menu from opening.
onContextMenu: (event: any): void => {
const { disabled, onLongPress } = this._config;
Eif (!disabled) {
Iif (onLongPress != null && this._isPointerTouch && !event.defaultPrevented) {
event.preventDefault();
event.stopPropagation();
}
} else {
if (isButtonRole(event.currentTarget)) {
event.stopPropagation();
}
}
}
};
}
/**
* Receives a state machine signal, performs side effects of the transition
* and stores the new state. Validates the transition as well.
*/
_receiveSignal(signal: TouchSignal, event: ResponderEvent): void {
const prevState = this._touchState;
let nextState = null;
Eif (Transitions[prevState] != null) {
nextState = Transitions[prevState][signal];
}
Iif (this._touchState === NOT_RESPONDER && signal === RESPONDER_RELEASE) {
return;
}
Iif (nextState == null || nextState === ERROR) {
console.error(`PressResponder: Invalid signal ${signal} for state ${prevState} on responder`);
} else Eif (prevState !== nextState) {
this._performTransitionSideEffects(prevState, nextState, signal, event);
this._touchState = nextState;
}
}
/**
* Performs a transition between touchable states and identify any activations
* or deactivations (and callback invocations).
*/
_performTransitionSideEffects(
prevState: TouchState,
nextState: TouchState,
signal: TouchSignal,
event: ResponderEvent
): void {
if (isTerminalSignal(signal)) {
this._isPointerTouch = false;
this._touchActivatePosition = null;
this._cancelLongPressDelayTimeout();
}
if (isPressStartSignal(prevState) && signal === LONG_PRESS_DETECTED) {
const { onLongPress } = this._config;
// Long press is not supported for keyboards because 'click' can be dispatched
// immediately (and multiple times) after 'keydown'.
Iif (onLongPress != null && event.nativeEvent.key == null) {
onLongPress(event);
this._longPressDispatched = true;
}
}
const isPrevActive = isActiveSignal(prevState);
const isNextActive = isActiveSignal(nextState);
if (!isPrevActive && isNextActive) {
this._activate(event);
} else if (isPrevActive && !isNextActive) {
this._deactivate(event);
}
if (isPressStartSignal(prevState) && signal === RESPONDER_RELEASE) {
const { onLongPress, onPress } = this._config;
Eif (onPress != null) {
const isPressCanceledByLongPress =
onLongPress != null && prevState === RESPONDER_ACTIVE_LONG_PRESS_START;
Eif (!isPressCanceledByLongPress) {
// If we never activated (due to delays), activate and deactivate now.
if (!isNextActive && !isPrevActive) {
this._activate(event);
this._deactivate(event);
}
}
}
}
this._cancelPressDelayTimeout();
}
_activate(event: ResponderEvent): void {
const { onPressChange, onPressStart } = this._config;
const touch = getTouchFromResponderEvent(event);
this._touchActivatePosition = {
pageX: touch.pageX,
pageY: touch.pageY
};
Eif (onPressStart != null) {
onPressStart(event);
}
if (onPressChange != null) {
onPressChange(true);
}
}
_deactivate(event: ResponderEvent): void {
const { onPressChange, onPressEnd } = this._config;
function end() {
Eif (onPressEnd != null) {
onPressEnd(event);
}
if (onPressChange != null) {
onPressChange(false);
}
}
const delayPressEnd = normalizeDelay(this._config.delayPressEnd);
Iif (delayPressEnd > 0) {
this._pressOutDelayTimeout = setTimeout(() => {
end();
}, delayPressEnd);
} else {
end();
}
}
_handleLongPress(event: ResponderEvent): void {
Eif (
this._touchState === RESPONDER_ACTIVE_PRESS_START ||
this._touchState === RESPONDER_ACTIVE_LONG_PRESS_START
) {
this._receiveSignal(LONG_PRESS_DETECTED, event);
}
}
_cancelLongPressDelayTimeout(): void {
if (this._longPressDelayTimeout != null) {
clearTimeout(this._longPressDelayTimeout);
this._longPressDelayTimeout = null;
}
}
_cancelPressDelayTimeout(): void {
if (this._pressDelayTimeout != null) {
clearTimeout(this._pressDelayTimeout);
this._pressDelayTimeout = null;
}
}
_cancelPressOutDelayTimeout(): void {
Iif (this._pressOutDelayTimeout != null) {
clearTimeout(this._pressOutDelayTimeout);
this._pressOutDelayTimeout = null;
}
}
}
function normalizeDelay(delay: ?number, min = 0, fallback = 0): number {
return Math.max(min, delay ?? fallback);
}
function getTouchFromResponderEvent(event: ResponderEvent) {
const { changedTouches, touches } = event.nativeEvent;
if (touches != null && touches.length > 0) {
return touches[0];
}
if (changedTouches != null && changedTouches.length > 0) {
return changedTouches[0];
}
return event.nativeEvent;
}
|