Skip to content

Commit 40d7e86

Browse files
committed
move dragging logic to parent
1 parent ecd0110 commit 40d7e86

File tree

2 files changed

+105
-60
lines changed

2 files changed

+105
-60
lines changed

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

Lines changed: 90 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';
@@ -72,6 +72,12 @@ type ListProps = {|
7272
width: number,
7373
|};
7474

75+
type DragStartCommit = {
76+
dragStartCommitIndex: number,
77+
rectLeft: number,
78+
width: number,
79+
};
80+
7581
function List({
7682
commitDurations,
7783
selectedCommitIndex,
@@ -105,6 +111,87 @@ function List({
105111
[commitDurations],
106112
);
107113

114+
const maxCommitIndex = filteredCommitIndices.length - 1;
115+
116+
const [state, setState] = useState({
117+
dragCommitStarted: false,
118+
dragStartCommit: null,
119+
modifiedIframes: null,
120+
});
121+
122+
const startCommitDrag = (newDragStartCommit: DragStartCommit) => {
123+
const element = divRef.current;
124+
if (element !== null) {
125+
const iframes = element.ownerDocument.querySelectorAll('iframe');
126+
if (iframes.length > 0) {
127+
const modifiedIframesMap = new Map();
128+
for (let i = 0; i < iframes.length; i++) {
129+
if (iframes[i].style.pointerEvents !== 'none') {
130+
modifiedIframesMap.set(iframes[i], iframes[i].style.pointerEvents);
131+
iframes[i].style.pointerEvents = 'none';
132+
}
133+
}
134+
setState({
135+
dragCommitStarted: true,
136+
dragStartCommit: newDragStartCommit,
137+
modifiedIframes: modifiedIframesMap,
138+
});
139+
}
140+
}
141+
};
142+
143+
const handleDragCommit = (e: any) => {
144+
const {dragCommitStarted, dragStartCommit, modifiedIframes} = state;
145+
if (dragCommitStarted === false || dragStartCommit === null) return;
146+
if (e.buttons === 0) {
147+
if (modifiedIframes !== null) {
148+
modifiedIframes.forEach((value, iframe) => {
149+
iframe.style.pointerEvents = value;
150+
});
151+
}
152+
setState({
153+
dragCommitStarted: false,
154+
dragStartCommit: null,
155+
modifiedIframes: null,
156+
});
157+
return;
158+
}
159+
160+
let newCommitIndex = dragStartCommit.dragStartCommitIndex;
161+
let newCommitRectLeft = dragStartCommit.rectLeft;
162+
163+
if (e.pageX < dragStartCommit.rectLeft) {
164+
while (e.pageX < newCommitRectLeft) {
165+
newCommitRectLeft = newCommitRectLeft - 1 - dragStartCommit.width;
166+
newCommitIndex -= 1;
167+
}
168+
} else {
169+
let newCommitRectRight = newCommitRectLeft + dragStartCommit.width;
170+
while (e.pageX > newCommitRectRight) {
171+
newCommitRectRight = newCommitRectRight + 1 + dragStartCommit.width;
172+
newCommitIndex += 1;
173+
}
174+
}
175+
176+
if (newCommitIndex < 0) {
177+
newCommitIndex = 0;
178+
} else if (newCommitIndex > maxCommitIndex) {
179+
newCommitIndex = maxCommitIndex;
180+
}
181+
selectCommitIndex(newCommitIndex);
182+
};
183+
184+
useEffect(() => {
185+
const element = divRef.current;
186+
if (element !== null) {
187+
const ownerDocument = element.ownerDocument;
188+
ownerDocument.addEventListener('mousemove', handleDragCommit);
189+
return () => {
190+
ownerDocument.removeEventListener('mousemove', handleDragCommit);
191+
};
192+
}
193+
}, [state]);
194+
108195
// Pass required contextual data down to the ListItem renderer.
109196
const itemData = useMemo<ItemData>(
110197
() => ({
@@ -115,6 +202,7 @@ function List({
115202
selectedCommitIndex,
116203
selectedFilteredCommitIndex,
117204
selectCommitIndex,
205+
startCommitDrag,
118206
}),
119207
[
120208
commitDurations,
@@ -124,6 +212,7 @@ function List({
124212
selectedCommitIndex,
125213
selectedFilteredCommitIndex,
126214
selectCommitIndex,
215+
startCommitDrag,
127216
],
128217
);
129218

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)