Skip to content

Commit

Permalink
Reduce verbosity/redundancy of new pointerType attribute (rrweb-io#1206)
Browse files Browse the repository at this point in the history
* Reduce verbosity/redundancy of output of new pointerType attribute by confining it only to clicks (and to a MouseDown/MouseUp from a PointerTypes.Pen, as these don't yet have a dedicated PendDown/PenUp)

* Update how the changeset will read for the next release based on the trimming in this PR

* Prefer triple equals

* The assignment to the outer `pointerType` variable in an anonymous function was somehow missed by the `typings` check

* Apply formatting changes

* Update packages/rrweb/src/record/observer.ts

* Update packages/rrweb/src/record/observer.ts

---------

Co-authored-by: Yun Feng <yun.feng0817@gmail.com>
  • Loading branch information
eoghanmurray and YunFeng0817 committed Aug 8, 2023
1 parent 6dab739 commit 0593bb0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .changeset/little-suits-leave.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
'@rrweb/types': minor
---

click events (as well as mousedown/mouseup/touchstart/touchend events) now include a `.pointerType` attribute which distinguishes between ['pen', 'mouse' and 'touch' events](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerType)
click events now include a `.pointerType` attribute which distinguishes between ['pen', 'mouse' and 'touch' events](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerType). There is no new PenDown/PenUp events, but these can be detected with a MouseDown/MouseUp + pointerType=pen
30 changes: 21 additions & 9 deletions packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,17 @@ function initMouseInteractionObserver({
let pointerType: PointerTypes | null = null;
let thisEventKey = eventKey;
if ('pointerType' in event) {
Object.keys(PointerTypes).forEach(
(pointerKey: keyof typeof PointerTypes) => {
if (event.pointerType === pointerKey.toLowerCase()) {
pointerType = PointerTypes[pointerKey];
return;
}
},
);
switch (event.pointerType) {
case 'mouse':
pointerType = PointerTypes.Mouse;
break;
case 'touch':
pointerType = PointerTypes.Touch;
break;
case 'pen':
pointerType = PointerTypes.Pen;
break;
}
if (pointerType === PointerTypes.Touch) {
if (MouseInteractions[eventKey] === MouseInteractions.MouseDown) {
// we are actually listening on 'pointerdown'
Expand All @@ -262,14 +265,23 @@ function initMouseInteractionObserver({
// we are actually listening on 'pointerup'
thisEventKey = 'TouchEnd';
}
} else if (pointerType == PointerTypes.Pen) {
} else if (pointerType === PointerTypes.Pen) {
// TODO: these will get incorrectly emitted as MouseDown/MouseUp
}
} else if (legacy_isTouchEvent(event)) {
pointerType = PointerTypes.Touch;
}
if (pointerType !== null) {
currentPointerType = pointerType;
if (
(thisEventKey.startsWith('Touch') &&
pointerType === PointerTypes.Touch) ||
(thisEventKey.startsWith('Mouse') &&
pointerType === PointerTypes.Mouse)
) {
// don't output redundant info
pointerType = null;
}
} else if (MouseInteractions[eventKey] === MouseInteractions.Click) {
pointerType = currentPointerType;
currentPointerType = null; // cleanup as we've used it
Expand Down
Loading

0 comments on commit 0593bb0

Please sign in to comment.