Skip to content

Commit ecd0110

Browse files
committed
allow to continue dragging
1 parent 333deb7 commit ecd0110

File tree

2 files changed

+57
-33
lines changed

2 files changed

+57
-33
lines changed

packages/react-devtools-shared/src/devtools/views/Profiler/SnapshotCommitList.js

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import * as React from 'react';
11-
import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
11+
import {useEffect, useMemo, useRef} from 'react';
1212
import AutoSizer from 'react-virtualized-auto-sizer';
1313
import {FixedSizeList} from 'react-window';
1414
import SnapshotCommitListItem from './SnapshotCommitListItem';
@@ -20,7 +20,6 @@ export type ItemData = {|
2020
commitDurations: Array<number>,
2121
commitTimes: Array<number>,
2222
filteredCommitIndices: Array<number>,
23-
isMouseDown: boolean,
2423
maxDuration: number,
2524
selectedCommitIndex: number | null,
2625
selectedFilteredCommitIndex: number | null,
@@ -97,28 +96,6 @@ function List({
9796
}
9897
}, [listRef, selectedFilteredCommitIndex]);
9998

100-
// When the mouse is down, dragging over a commit should auto-select it.
101-
// This provides a nice way for users to swipe across a range of commits to compare them.
102-
const [isMouseDown, setIsMouseDown] = useState(false);
103-
const handleMouseDown = useCallback(() => {
104-
setIsMouseDown(true);
105-
}, []);
106-
const handleMouseUp = useCallback(() => {
107-
setIsMouseDown(false);
108-
}, []);
109-
useEffect(() => {
110-
if (divRef.current === null) {
111-
return () => {};
112-
}
113-
114-
// It's important to listen to the ownerDocument to support the browser extension.
115-
// Here we use portals to render individual tabs (e.g. Profiler),
116-
// and the root document might belong to a different window.
117-
const ownerDocument = divRef.current.ownerDocument;
118-
ownerDocument.addEventListener('mouseup', handleMouseUp);
119-
return () => ownerDocument.removeEventListener('mouseup', handleMouseUp);
120-
}, [divRef, handleMouseUp]);
121-
12299
const itemSize = useMemo(
123100
() => Math.max(minBarWidth, width / filteredCommitIndices.length),
124101
[filteredCommitIndices, width],
@@ -134,7 +111,6 @@ function List({
134111
commitDurations,
135112
commitTimes,
136113
filteredCommitIndices,
137-
isMouseDown,
138114
maxDuration,
139115
selectedCommitIndex,
140116
selectedFilteredCommitIndex,
@@ -144,7 +120,6 @@ function List({
144120
commitDurations,
145121
commitTimes,
146122
filteredCommitIndices,
147-
isMouseDown,
148123
maxDuration,
149124
selectedCommitIndex,
150125
selectedFilteredCommitIndex,
@@ -153,11 +128,7 @@ function List({
153128
);
154129

155130
return (
156-
<div
157-
onMouseDown={handleMouseDown}
158-
onMouseUp={handleMouseUp}
159-
ref={divRef}
160-
style={{height, width}}>
131+
<div ref={divRef} style={{height, width}}>
161132
<FixedSizeList
162133
className={styles.List}
163134
layout="horizontal"

packages/react-devtools-shared/src/devtools/views/Profiler/SnapshotCommitListItem.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ type Props = {
2323
...
2424
};
2525

26+
type DragStartCommit = {
27+
dragStartCommitIndex: number,
28+
rectLeft: number,
29+
};
30+
2631
function SnapshotCommitListItem({data: itemData, index, style}: Props) {
2732
const {
2833
commitDurations,
2934
commitTimes,
3035
filteredCommitIndices,
31-
isMouseDown,
3236
maxDuration,
3337
selectedCommitIndex,
3438
selectCommitIndex,
@@ -44,6 +48,55 @@ function SnapshotCommitListItem({data: itemData, index, style}: Props) {
4448
selectCommitIndex,
4549
]);
4650

51+
let dragStartCommit: DragStartCommit | null = null;
52+
const maxCommitIndex = filteredCommitIndices.length - 1;
53+
54+
const handleDrag = (e: any) => {
55+
if (e.buttons === 0) {
56+
document.removeEventListener('mousemove', handleDrag);
57+
const iframe = document.querySelector('iframe');
58+
if (iframe) iframe.style.pointerEvents = 'auto';
59+
dragStartCommit = null;
60+
return;
61+
}
62+
if (dragStartCommit === null) return;
63+
64+
let newCommitIndex = index;
65+
let newCommitRectLeft = dragStartCommit.rectLeft;
66+
67+
if (e.pageX < dragStartCommit.rectLeft) {
68+
while (e.pageX < newCommitRectLeft) {
69+
newCommitRectLeft = newCommitRectLeft - 1 - width;
70+
newCommitIndex -= 1;
71+
}
72+
} else {
73+
let newCommitRectRight = newCommitRectLeft + 1 + width;
74+
while (e.pageX > newCommitRectRight) {
75+
newCommitRectRight = newCommitRectRight + 1 + width;
76+
newCommitIndex += 1;
77+
}
78+
}
79+
80+
if (newCommitIndex < 0) {
81+
newCommitIndex = 0;
82+
} else if (newCommitIndex > maxCommitIndex) {
83+
newCommitIndex = maxCommitIndex;
84+
}
85+
selectCommitIndex(newCommitIndex);
86+
};
87+
88+
const handleMouseDown = (e: any) => {
89+
handleClick();
90+
document.addEventListener('mousemove', handleDrag);
91+
const iframe = document.querySelector('iframe');
92+
if (iframe) iframe.style.pointerEvents = 'none';
93+
const rect = e.target.getBoundingClientRect();
94+
dragStartCommit = {
95+
dragStartCommitIndex: index,
96+
rectLeft: rect.left,
97+
};
98+
};
99+
47100
// Guard against commits with duration 0
48101
const percentage =
49102
Math.min(1, Math.max(0, commitDuration / maxDuration)) || 0;
@@ -56,7 +109,7 @@ function SnapshotCommitListItem({data: itemData, index, style}: Props) {
56109
<div
57110
className={styles.Outer}
58111
onClick={handleClick}
59-
onMouseEnter={isMouseDown ? handleClick : null}
112+
onMouseDown={handleMouseDown}
60113
style={{
61114
...style,
62115
width,

0 commit comments

Comments
 (0)