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

View File

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