Make the pointerType into an Enum to be consistent with other values in events

This commit is contained in:
Eoghan Murray
2026-04-01 12:00:00 +08:00
parent 78127d72ab
commit 11c973efb0
2 changed files with 21 additions and 8 deletions

View File

@@ -20,6 +20,7 @@ import {
mousePosition, mousePosition,
mouseInteractionCallBack, mouseInteractionCallBack,
MouseInteractions, MouseInteractions,
PointerTypes,
listenerHandler, listenerHandler,
scrollCallback, scrollCallback,
styleSheetRuleCallback, styleSheetRuleCallback,
@@ -236,11 +237,17 @@ function initMouseInteractionObserver({
if (isBlocked(target, blockClass, blockSelector, true)) { if (isBlocked(target, blockClass, blockSelector, true)) {
return; return;
} }
let pointerType = null; let pointerType: PointerTypes | null = null;
let e = event; let e = event;
if ('pointerType' in e) { if ('pointerType' in e) {
pointerType = (e as PointerEvent).pointerType; // touch / pen / mouse Object.keys(PointerTypes)
if (pointerType === 'touch') { .forEach((pointerKey: keyof typeof PointerKeys) => {
if ((e as PointerEvent).pointerType === pointerKey.toLowerCase()) {
pointerType = PointerTypes[pointerKey];
return;
}
});
if (pointerType === PointerTypes.Touch) {
if (MouseInteractions[eventKey] === MouseInteractions.MouseDown) { if (MouseInteractions[eventKey] === MouseInteractions.MouseDown) {
// we are actually listening on 'pointerdown' // we are actually listening on 'pointerdown'
eventKey = 'TouchStart'; eventKey = 'TouchStart';
@@ -250,14 +257,14 @@ function initMouseInteractionObserver({
// we are actually listening on 'pointerup' // we are actually listening on 'pointerup'
eventKey = 'TouchEnd'; eventKey = 'TouchEnd';
} }
} else if (pointerType == 'pen') { } else if (pointerType == PointerTypes.Pen) {
// TODO: these will get incorrectly emitted as MouseDown/MouseUp // TODO: these will get incorrectly emitted as MouseDown/MouseUp
} }
} else if (legacy_isTouchEvent(event)) { } else if (legacy_isTouchEvent(event)) {
e = event.changedTouches[0]; e = event.changedTouches[0];
pointerType = 'touch'; pointerType = PointerTypes.Touch;
} }
if (pointerType) { if (pointerType !== null) {
currentPointerType = pointerType; currentPointerType = pointerType;
} else if (MouseInteractions[eventKey] === MouseInteractions.Click) { } else if (MouseInteractions[eventKey] === MouseInteractions.Click) {
pointerType = currentPointerType; pointerType = currentPointerType;
@@ -273,7 +280,7 @@ function initMouseInteractionObserver({
id, id,
x: clientX, x: clientX,
y: clientY, y: clientY,
...(pointerType && { pointerType }), ...(pointerType !== null && { pointerType }),
}); });
}; };
}; };

View File

@@ -362,6 +362,12 @@ export enum MouseInteractions {
TouchCancel, TouchCancel,
} }
export enum PointerTypes {
Mouse,
Pen,
Touch,
}
export enum CanvasContext { export enum CanvasContext {
'2D', '2D',
WebGL, WebGL,
@@ -404,7 +410,7 @@ type mouseInteractionParam = {
id: number; id: number;
x: number; x: number;
y: number; y: number;
pointerType?: string; pointerType?: PointerTypes;
}; };
export type mouseInteractionCallBack = (d: mouseInteractionParam) => void; export type mouseInteractionCallBack = (d: mouseInteractionParam) => void;