Skip to content

Commit 1b127a9

Browse files
committed
move dragging logic to parent
1 parent ecd0110 commit 1b127a9

File tree

2 files changed

+112
-60
lines changed

2 files changed

+112
-60
lines changed

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

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import * as React from 'react';
11-
import {useEffect, useMemo, useRef} from 'react';
11+
import {useEffect, useMemo, useRef, useState} from 'react';
1212
import AutoSizer from 'react-virtualized-auto-sizer';
1313
import {FixedSizeList} from 'react-window';
1414
import SnapshotCommitListItem from './SnapshotCommitListItem';
@@ -24,6 +24,13 @@ export type ItemData = {|
2424
selectedCommitIndex: number | null,
2525
selectedFilteredCommitIndex: number | null,
2626
selectCommitIndex: (index: number) => void,
27+
startCommitDrag: (newDragStartCommit: DragStartCommit) => void,
28+
|};
29+
30+
type State = {|
31+
dragCommitStarted: boolean,
32+
dragStartCommit: DragStartCommit | null,
33+
modifiedIframes: Map<HTMLElement, string> | null,
2734
|};
2835

2936
type Props = {|
@@ -72,6 +79,12 @@ type ListProps = {|
7279
width: number,
7380
|};
7481

82+
type DragStartCommit = {
83+
dragStartCommitIndex: number,
84+
rectLeft: number,
85+
width: number,
86+
};
87+
7588
function List({
7689
commitDurations,
7790
selectedCommitIndex,
@@ -105,6 +118,87 @@ function List({
105118
[commitDurations],
106119
);
107120

121+
const maxCommitIndex = filteredCommitIndices.length - 1;
122+
123+
const [state, setState] = useState<State>({
124+
dragCommitStarted: false,
125+
dragStartCommit: null,
126+
modifiedIframes: null,
127+
});
128+
129+
const startCommitDrag = (newDragStartCommit: DragStartCommit) => {
130+
const element = divRef.current;
131+
if (element !== null) {
132+
const iframes = element.ownerDocument.querySelectorAll('iframe');
133+
if (iframes.length > 0) {
134+
const modifiedIframesMap = new Map();
135+
for (let i = 0; i < iframes.length; i++) {
136+
if (iframes[i].style.pointerEvents !== 'none') {
137+
modifiedIframesMap.set(iframes[i], iframes[i].style.pointerEvents);
138+
iframes[i].style.pointerEvents = 'none';
139+
}
140+
}
141+
setState({
142+
dragCommitStarted: true,
143+
dragStartCommit: newDragStartCommit,
144+
modifiedIframes: modifiedIframesMap,
145+
});
146+
}
147+
}
148+
};
149+
150+
const handleDragCommit = (e: any) => {
151+
const {dragCommitStarted, dragStartCommit, modifiedIframes} = state;
152+
if (dragCommitStarted === false || dragStartCommit === null) return;
153+
if (e.buttons === 0) {
154+
if (modifiedIframes !== null) {
155+
modifiedIframes.forEach((value, iframe) => {
156+
iframe.style.pointerEvents = value;
157+
});
158+
}
159+
setState({
160+
dragCommitStarted: false,
161+
dragStartCommit: null,
162+
modifiedIframes: null,
163+
});
164+
return;
165+
}
166+
167+
let newCommitIndex = dragStartCommit.dragStartCommitIndex;
168+
let newCommitRectLeft = dragStartCommit.rectLeft;
169+
170+
if (e.pageX < dragStartCommit.rectLeft) {
171+
while (e.pageX < newCommitRectLeft) {
172+
newCommitRectLeft = newCommitRectLeft - 1 - dragStartCommit.width;
173+
newCommitIndex -= 1;
174+
}
175+
} else {
176+
let newCommitRectRight = newCommitRectLeft + dragStartCommit.width;
177+
while (e.pageX > newCommitRectRight) {
178+
newCommitRectRight = newCommitRectRight + 1 + dragStartCommit.width;
179+
newCommitIndex += 1;
180+
}
181+
}
182+
183+
if (newCommitIndex < 0) {
184+
newCommitIndex = 0;
185+
} else if (newCommitIndex > maxCommitIndex) {
186+
newCommitIndex = maxCommitIndex;
187+
}
188+
selectCommitIndex(newCommitIndex);
189+
};
190+
191+
useEffect(() => {
192+
const element = divRef.current;
193+
if (element !== null) {
194+
const ownerDocument = element.ownerDocument;
195+
ownerDocument.addEventListener('mousemove', handleDragCommit);
196+
return () => {
197+
ownerDocument.removeEventListener('mousemove', handleDragCommit);
198+
};
199+
}
200+
}, [state]);
201+
108202
// Pass required contextual data down to the ListItem renderer.
109203
const itemData = useMemo<ItemData>(
110204
() => ({
@@ -115,6 +209,7 @@ function List({
115209
selectedCommitIndex,
116210
selectedFilteredCommitIndex,
117211
selectCommitIndex,
212+
startCommitDrag,
118213
}),
119214
[
120215
commitDurations,
@@ -124,6 +219,7 @@ function List({
124219
selectedCommitIndex,
125220
selectedFilteredCommitIndex,
126221
selectCommitIndex,
222+
startCommitDrag,
127223
],
128224
);
129225

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

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ type Props = {
2323
...
2424
};
2525

26-
type DragStartCommit = {
27-
dragStartCommitIndex: number,
28-
rectLeft: number,
29-
};
30-
3126
function SnapshotCommitListItem({data: itemData, index, style}: Props) {
3227
const {
3328
commitDurations,
@@ -36,66 +31,18 @@ function SnapshotCommitListItem({data: itemData, index, style}: Props) {
3631
maxDuration,
3732
selectedCommitIndex,
3833
selectCommitIndex,
34+
startCommitDrag,
3935
} = itemData;
4036

4137
index = filteredCommitIndices[index];
4238

4339
const commitDuration = commitDurations[index];
4440
const commitTime = commitTimes[index];
4541

46-
const handleClick = useCallback(() => selectCommitIndex(index), [
47-
index,
48-
selectCommitIndex,
49-
]);
50-
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-
};
42+
const memoizedSelectCommitIndex = useCallback(
43+
() => selectCommitIndex(index),
44+
[index, selectCommitIndex],
45+
);
9946

10047
// Guard against commits with duration 0
10148
const percentage =
@@ -105,10 +52,19 @@ function SnapshotCommitListItem({data: itemData, index, style}: Props) {
10552
// Leave a 1px gap between snapshots
10653
const width = parseFloat(style.width) - 1;
10754

55+
const handleMouseDown = (e: any) => {
56+
memoizedSelectCommitIndex();
57+
const rect = e.target.getBoundingClientRect();
58+
startCommitDrag({
59+
dragStartCommitIndex: index,
60+
rectLeft: rect.left,
61+
width,
62+
});
63+
};
64+
10865
return (
10966
<div
11067
className={styles.Outer}
111-
onClick={handleClick}
11268
onMouseDown={handleMouseDown}
11369
style={{
11470
...style,

0 commit comments

Comments
 (0)