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 all commits
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
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
20 changes: 18 additions & 2 deletions src/controllers/handlePointerDown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,26 @@ 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;

const isRightClick = event.button === 2;

if (isRightClick || usedTouch) {
// invoke the setTimeout callback if handlePointerUp is not called within 500ms (hold event)
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,6 +66,8 @@ 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;
Expand Down
3 changes: 3 additions & 0 deletions src/types/Behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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 MouseEventHandler = HandlerFn<React.MouseEvent<HTMLDivElement>>;
export type KeyboardEventHandler = HandlerFn<React.KeyboardEvent<HTMLDivElement>>;
export type CompositionEventHandler = HandlerFn<React.CompositionEvent<HTMLDivElement>>;
Expand All @@ -23,11 +24,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;
}
};