Blame view

天文台pc/tianwentai-ui/node_modules/motion-dom/dist/es/gestures/press/index.mjs 3.55 KB
bc518174   王天杨   提交两个项目文件
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
  import { isHTMLElement } from '../../utils/is-html-element.mjs';
  import { isDragActive } from '../drag/state/is-active.mjs';
  import { isNodeOrChild } from '../utils/is-node-or-child.mjs';
  import { isPrimaryPointer } from '../utils/is-primary-pointer.mjs';
  import { setupGesture } from '../utils/setup.mjs';
  import { isElementKeyboardAccessible } from './utils/is-keyboard-accessible.mjs';
  import { enableKeyboardPress } from './utils/keyboard.mjs';
  import { isPressing } from './utils/state.mjs';
  
  /**
   * Filter out events that are not primary pointer events, or are triggering
   * while a Motion gesture is active.
   */
  function isValidPressEvent(event) {
      return isPrimaryPointer(event) && !isDragActive();
  }
  const claimedPointerDownEvents = new WeakSet();
  /**
   * Create a press gesture.
   *
   * Press is different to `"pointerdown"`, `"pointerup"` in that it
   * automatically filters out secondary pointer events like right
   * click and multitouch.
   *
   * It also adds accessibility support for keyboards, where
   * an element with a press gesture will receive focus and
   *  trigger on Enter `"keydown"` and `"keyup"` events.
   *
   * This is different to a browser's `"click"` event, which does
   * respond to keyboards but only for the `"click"` itself, rather
   * than the press start and end/cancel. The element also needs
   * to be focusable for this to work, whereas a press gesture will
   * make an element focusable by default.
   *
   * @public
   */
  function press(targetOrSelector, onPressStart, options = {}) {
      const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
      const startPress = (startEvent) => {
          const target = startEvent.currentTarget;
          if (!isValidPressEvent(startEvent))
              return;
          if (claimedPointerDownEvents.has(startEvent))
              return;
          isPressing.add(target);
          if (options.stopPropagation) {
              claimedPointerDownEvents.add(startEvent);
          }
          const onPressEnd = onPressStart(target, startEvent);
          const onPointerEnd = (endEvent, success) => {
              window.removeEventListener("pointerup", onPointerUp);
              window.removeEventListener("pointercancel", onPointerCancel);
              if (isPressing.has(target)) {
                  isPressing.delete(target);
              }
              if (!isValidPressEvent(endEvent)) {
                  return;
              }
              if (typeof onPressEnd === "function") {
                  onPressEnd(endEvent, { success });
              }
          };
          const onPointerUp = (upEvent) => {
              onPointerEnd(upEvent, target === window ||
                  target === document ||
                  options.useGlobalTarget ||
                  isNodeOrChild(target, upEvent.target));
          };
          const onPointerCancel = (cancelEvent) => {
              onPointerEnd(cancelEvent, false);
          };
          window.addEventListener("pointerup", onPointerUp, eventOptions);
          window.addEventListener("pointercancel", onPointerCancel, eventOptions);
      };
      targets.forEach((target) => {
          const pointerDownTarget = options.useGlobalTarget ? window : target;
          pointerDownTarget.addEventListener("pointerdown", startPress, eventOptions);
          if (isHTMLElement(target)) {
              target.addEventListener("focus", (event) => enableKeyboardPress(event, eventOptions));
              if (!isElementKeyboardAccessible(target) &&
                  !target.hasAttribute("tabindex")) {
                  target.tabIndex = 0;
              }
          }
      });
      return cancelEvents;
  }
  
  export { press };
  //# sourceMappingURL=index.mjs.map