Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hold detection functionality #345

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor hold functionality
  • Loading branch information
webloopbox committed Feb 14, 2024
commit ac10daebbe24b0d5dfceb98242435190bd4b4a78
10 changes: 9 additions & 1 deletion src/behaviors/CellSelectionBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,16 @@ export const CellSelectionBehavior: Behavior = {
},

handlePointerEnter(event, store) {
return store;
},

handlePointerHold: function (event, store) {
devEnvironment && console.log("CSB/handlePointerHold");
return store;
},

handlePointerHoldTouch: function (event, store) {
devEnvironment && console.log("CSB/handlePointerHoldTouch");
return store;
},

Expand Down Expand Up @@ -165,7 +174,6 @@ export const CellSelectionBehavior: Behavior = {
},

handlePointerEnterTouch(event, store) {

return store;
},
};
10 changes: 10 additions & 0 deletions src/behaviors/DefaultBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ export const DefaultBehavior = (config: DefaultBehaviorConfig = CONFIG_DEFAULTS)
return store;
},

handlePointerHold: function (event, store) {
devEnvironment && console.log("DB/handlePointerHold");
return store;
},

handlePointerHoldTouch: function (event, store) {
devEnvironment && console.log("DB/handlePointerHoldTouch");
return store;
},

handleKeyDown: function (event, store) {
return handleKeyDown(event, store, { moveHorizontallyOnEnter: config.moveHorizontallyOnEnter });
},
Expand Down
65 changes: 15 additions & 50 deletions src/controllers/handlePointerDown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@ import { updateStoreWithApiAndEventHandler } from "../utils/updateStoreWithApiAn
import { ReactGridStore } from "../types/ReactGridStore.ts";

export const handlePointerDown = (event: React.PointerEvent<HTMLDivElement>, storeApi: StoreApi<ReactGridStore>) => {
const usedTouch = event.pointerType !== "mouse"; // * keep in mind there is also "pen" pointerType

let holdTimeoutId: NodeJS.Timeout;

if (event.button === 2 || usedTouch) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the event is a right-click or a touch event, a timeout is set to trigger. holdTimeoutId is used to clear the timeout if a pointer up event occurs before 500 ms, so the hold handler function will not be called

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Can you create some sort of constant assertion or simply a normal const that will describe event.button === 2, so the code will be easier to understand?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, it's a good idea

holdTimeoutId = setTimeout(() => {
const handler = usedTouch
? getNewestState().currentBehavior.handlePointerHoldTouch
: getNewestState().currentBehavior.handlePointerHold;
updateWithStoreApi(event, handler);
}, 500);
}

function getNewestState() {
return storeApi.getState();
}

const usedTouch = event.pointerType !== "mouse"; // * keep in mind there is also "pen" pointerType

const updateWithStoreApi = <TEvent extends React.SyntheticEvent<HTMLElement> | PointerEvent>(
event: TEvent,
handler?: HandlerFn<TEvent>
Expand Down Expand Up @@ -52,61 +63,15 @@ export const handlePointerDown = (event: React.PointerEvent<HTMLDivElement>, sto
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);

if (holdTimeoutId) clearTimeout(holdTimeoutId);

const handler = usedTouch
? getNewestState().currentBehavior.handlePointerUpTouch
: getNewestState().currentBehavior.handlePointerUp;

updateWithStoreApi(event, handler);
};

function detectHold(event: React.PointerEvent<HTMLDivElement>, usedTouch: boolean) {
return new Promise((resolve) => {
let startTime: number = 0;
let isCheckingHold: boolean = false;
let intervalId: NodeJS.Timeout;

function handleHold() {
console.log("Hold detected");
isCheckingHold = false;
}

function checkHoldDuration() {
if (!isCheckingHold) return;
if (Date.now() - startTime >= 500) {
handleHold();
clearInterval(intervalId);
}
}

function pointerDownHandler(e: React.PointerEvent<HTMLDivElement>) {
if ((e.button === 2 || usedTouch) && !isCheckingHold) {
// right mouse button or touch
startTime = Date.now();
isCheckingHold = true;
intervalId = setInterval(checkHoldDuration, 50); // check every 50ms
window.addEventListener("pointerup", pointerUpHandler);
}
}

function pointerUpHandler(e: PointerEvent) {
if (e.button === 2 || usedTouch) {
isCheckingHold = false;
clearInterval(intervalId);
window.removeEventListener("pointerup", pointerUpHandler);
if (Date.now() - startTime >= 200) {
resolve(true); // resolve if the button was held down for 200ms or more
}
}
}

pointerDownHandler(event);
});
}

detectHold(event, usedTouch).then(() => {
console.log("Hold action completed");
});

// Remove listeners after pointerDown
window.addEventListener("pointermove", handlePointerMove);
window.addEventListener("pointerup", handlePointerUp);
Expand Down
4 changes: 4 additions & 0 deletions src/types/Behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type HandlerFn<TEvent extends React.SyntheticEvent | Event> = (
) => ReactGridStore;

export type PointerEventHandler = HandlerFn<React.PointerEvent<HTMLDivElement> | PointerEvent>;
export type PointerHoldEventHandler = HandlerFn<React.PointerEvent<HTMLDivElement> | PointerEvent>;
export type PointerHoldTouchEventHandler = HandlerFn<React.PointerEvent<HTMLDivElement> | PointerEvent>;
webloopbox marked this conversation as resolved.
Show resolved Hide resolved
export type MouseEventHandler = HandlerFn<React.MouseEvent<HTMLDivElement>>;
export type KeyboardEventHandler = HandlerFn<React.KeyboardEvent<HTMLDivElement>>;
export type CompositionEventHandler = HandlerFn<React.CompositionEvent<HTMLDivElement>>;
Expand All @@ -23,11 +25,13 @@ export type Behavior = {
handlePointerMove?: PointerEventHandler;
handlePointerLeave?: PointerEventHandler;
handlePointerUp?: PointerEventHandler;
handlePointerHold?: PointerHoldEventHandler;

handlePointerDownTouch?: PointerEventHandler;
handlePointerMoveTouch?: PointerEventHandler;
handlePointerEnterTouch?: PointerEventHandler;
handlePointerUpTouch?: PointerEventHandler;
handlePointerHoldTouch?: PointerHoldEventHandler;

handleDoubleClick?: MouseEventHandler;

Expand Down
2 changes: 1 addition & 1 deletion src/utils/updateStoreWithApiAndEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export const updateStoreWithApiAndEventHandler = <TEvent extends React.Synthetic
}

return store;
}
};