Skip to content

Commit

Permalink
fix: 修复 onFingerDown 影响 onDoubleTap 的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Houfeng committed Aug 22, 2023
1 parent 9d4a938 commit b117376
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
12 changes: 5 additions & 7 deletions examples/develop.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Finger, FingerProxy, FingerProxyContainer, HostPointerEvent } from '../src';
import { Finger, FingerProxy, FingerProxyContainer } from '../src';
import React, { CSSProperties } from "react"

import { FingerShortcutEvent } from '../src';
Expand Down Expand Up @@ -29,18 +29,16 @@ export const onShortcut = (event: FingerShortcutEvent) => {
});
};

function onPointerDown(event: HostPointerEvent) {
console.log('event', event);
}

export function App() {
return (
<FingerBoxWrapper className='box' style={boxStyle} tabIndex={0}>
<FingerProxy onShortcut={onShortcut} />
<FingeredDiv
onTapHold={(event) => console.log('onTapHold', event)}
onTapHold={event => console.log('onTapHold', event)}
onDoubleTap={event => console.log('onDoubleTap', event)}
onPointerDown={onPointerDown}
onFingerDown={event => console.log('onFingerDown', event)}
onPinch={event => console.log('onPinch', event)}
onPointerDown={event => console.log('onPointerDown', event)}
>
Tap me
</FingeredDiv >
Expand Down
7 changes: 5 additions & 2 deletions src/core/FingerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type FingerContext = {
flags: Map<symbol, unknown>;
cleanTimers: () => void;
cleanPointers: () => void;
cleanFlags: () => void;
cleanFlags: (flags?: symbol[]) => void;
clean: () => void;
};

Expand Down Expand Up @@ -56,7 +56,10 @@ export function FingerContext(): FingerContext {
pointers.clear();
changedPointers.clear();
};
const cleanFlags = () => flags.clear();
const cleanFlags = (flagKeys?: symbol[]) => {
if (!flagKeys || flagKeys.length < 1) return flags.clear();
flagKeys.forEach((key) => flags.delete(key));
};
const clean = () => {
cleanTimers();
cleanPointers();
Expand Down
4 changes: 3 additions & 1 deletion src/events/FingerBasicProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { FingerProvider } from "../core/FingerProviders";

const LATEST_POS = Symbol();

const ALL_FLAGS = [LATEST_POS];

export const FingerBasicProvider: FingerProvider = {
name: "Basic",
events: ["onFingerDown", "onFingerMove", "onFingerUp", "onFingerCancel"],

handlePointerDown: ({ events, context, pointer }) => {
if (pointer.isPrimary) context.cleanFlags();
if (pointer.isPrimary) context.cleanFlags(ALL_FLAGS);
context.flags.delete(LATEST_POS);
if (!events.onFingerDown) return;
const { getPointers, getChangedPointers } = context;
Expand Down
11 changes: 10 additions & 1 deletion src/events/FingerPinchProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ const ORIGIN_CENTER = Symbol();
const LATEST_CENTER = Symbol();
const ORIGIN_ROTATE = Symbol();

const ALL_FLAGS = [
PINCHING,
DETAIL,
ORIGIN_DIST,
ORIGIN_CENTER,
LATEST_CENTER,
ORIGIN_ROTATE,
];

export const FingerPinchProvider: FingerProvider = {
name: "Pinch",
events: ["onPinchStart", "onPinch", "onPinchEnd"],

handlePointerDown: ({ events, context, pointer }) => {
pointer.target.setPointerCapture?.(pointer.pointerId);
if (pointer.isPrimary) context.cleanFlags();
if (pointer.isPrimary) context.cleanFlags(ALL_FLAGS);
const { flags, getPointers, getChangedPointers } = context;
const pointers = getPointers();
const changedPointers = getChangedPointers();
Expand Down
4 changes: 3 additions & 1 deletion src/events/FingerSwipeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const { swipeMinDistanceThreshold, swipeMaxDurationThreshold } = FingerOptions;
const CANCELED = Symbol();
const START_TIME = Symbol();

const ALL_FLAGS = [CANCELED, START_TIME];

type SwipeDirection = "up" | "down" | "left" | "right";
type SwipeEventNames =
| "onSwipeUp"
Expand All @@ -38,7 +40,7 @@ export const FingerSwipeProvider: FingerProvider = {

handlePointerDown: ({ context, pointer }) => {
pointer.target.setPointerCapture?.(pointer.pointerId);
if (pointer.isPrimary) context.cleanFlags();
if (pointer.isPrimary) context.cleanFlags(ALL_FLAGS);
const { flags, getPointers } = context;
flags.set(CANCELED, getPointers().length > 1);
flags.set(START_TIME, Date.now());
Expand Down

0 comments on commit b117376

Please sign in to comment.