Skip to content

Commit b695f2b

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

File tree

2 files changed

+59
-33
lines changed

2 files changed

+59
-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: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ type Props = {
2323
...
2424
};
2525

26+
type DragStartCommit = {
27+
dragStartCommitIndex: number,
28+
rectLeft: number,
29+
rectBottom: number,
30+
};
31+
2632
function SnapshotCommitListItem({data: itemData, index, style}: Props) {
2733
const {
2834
commitDurations,
2935
commitTimes,
3036
filteredCommitIndices,
31-
isMouseDown,
3237
maxDuration,
3338
selectedCommitIndex,
3439
selectCommitIndex,
@@ -44,6 +49,56 @@ function SnapshotCommitListItem({data: itemData, index, style}: Props) {
4449
selectCommitIndex,
4550
]);
4651

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

0 commit comments

Comments
 (0)