refactor[devtools]: highlight an array of elements for native (#27734)
We are currently just pass the first element, which diverges from the implementation for web. This is especially bad if you are inspecting something like a list, where host fiber can represent multiple elements. This part runs on the backend of React DevTools, so it should not affect cases for React Native when frontend version can be more up-to-date than backend's. I will double-check it before merging. Once version of `react-devtools-core` is updated in React Native, this should be supported, I will work on that later.
Ruslan Lesiutin committed
Nov 23, 2023 at 11:31 UTC
fbc9b68d61aba17a5a1119caac22647d0897486a
3 files changed
+62
-34
packages/react-devtools-shared/src/backend/utils.js
+6
@@ -283,3 +283,9 @@ export function gt(a: string = '', b: string = ''): boolean {
283
export function gte(a: string = '', b: string = ''): boolean {
284
return compareVersions(a, b) > -1;
285
}
286
+
287
+export const isReactNativeEnvironment = (): boolean => {
288
+ // We've been relying on this for such a long time
289
+ // We should probably define the client for DevTools on the backend side and share it with the frontend
290
+ return window.document == null;
291
+};
packages/react-devtools-shared/src/backend/views/Highlighter/Highlighter.js
+31
-19
@@ -9,6 +9,8 @@
9
10
import type Agent from 'react-devtools-shared/src/backend/agent';
11
12
+import {isReactNativeEnvironment} from 'react-devtools-shared/src/backend/utils';
13
+
14
import Overlay from './Overlay';
15
16
const SHOW_DURATION = 2000;
@@ -16,11 +18,11 @@ const SHOW_DURATION = 2000;
18
let timeoutID: TimeoutID | null = null;
19
let overlay: Overlay | null = null;
20
19
-export function hideOverlay(agent: Agent) {
20
- if (window.document == null) {
21
- agent.emit('hideNativeHighlight');
22
- return;
23
- }
21
+function hideOverlayNative(agent: Agent): void {
22
+ agent.emit('hideNativeHighlight');
23
+}
24
+
25
+function hideOverlayWeb(): void {
26
timeoutID = null;
27
28
if (overlay !== null) {
@@ -29,27 +31,26 @@ export function hideOverlay(agent: Agent) {
31
}
32
}
33
32
-export function showOverlay(
33
- elements: Array<HTMLElement> | null,
34
+export function hideOverlay(agent: Agent): void {
35
+ return isReactNativeEnvironment()
36
+ ? hideOverlayNative(agent)
37
+ : hideOverlayWeb();
38
+}
39
+
40
+function showOverlayNative(elements: Array<HTMLElement>, agent: Agent): void {
41
+ agent.emit('showNativeHighlight', elements);
42
+}
43
+
44
+function showOverlayWeb(
45
+ elements: Array<HTMLElement>,
46
componentName: string | null,
47
agent: Agent,
48
hideAfterTimeout: boolean,
37
-) {
38
- if (window.document == null) {
39
- if (elements != null && elements[0] != null) {
40
- agent.emit('showNativeHighlight', elements[0]);
41
- }
42
- return;
43
- }
44
-
49
+): void {
50
if (timeoutID !== null) {
51
clearTimeout(timeoutID);
52
}
53
49
- if (elements == null) {
50
- return;
51
- }
52
-
54
if (overlay === null) {
55
overlay = new Overlay(agent);
56
}
@@ -60,3 +61,14 @@ export function showOverlay(
61
timeoutID = setTimeout(() => hideOverlay(agent), SHOW_DURATION);
62
}
63
}
64
+
65
+export function showOverlay(
66
+ elements: Array<HTMLElement>,
67
+ componentName: string | null,
68
+ agent: Agent,
69
+ hideAfterTimeout: boolean,
70
+): void {
71
+ return isReactNativeEnvironment()
72
+ ? showOverlayNative(elements, agent)
73
+ : showOverlayWeb(elements, componentName, agent, hideAfterTimeout);
74
+}
packages/react-devtools-shared/src/backend/views/TraceUpdates/canvas.js
+25
-15
@@ -12,6 +12,8 @@ import type {Rect} from '../utils';
12
import type {NativeType} from '../../types';
13
import type Agent from '../../agent';
14
15
+import {isReactNativeEnvironment} from 'react-devtools-shared/src/backend/utils';
16
+
17
const OUTLINE_COLOR = '#f0f0f0';
18
19
// Note these colors are in sync with DevTools Profiler chart colors.
@@ -30,17 +32,16 @@ const COLORS = [
32
33
let canvas: HTMLCanvasElement | null = null;
34
33
-export function draw(nodeToData: Map<NativeType, Data>, agent: Agent): void {
34
- if (window.document == null) {
35
- const nodesToDraw = [];
36
- iterateNodes(nodeToData, (_, color, node) => {
37
- nodesToDraw.push({node, color});
38
- });
39
-
40
- agent.emit('drawTraceUpdates', nodesToDraw);
41
- return;
42
- }
35
+function drawNative(nodeToData: Map<NativeType, Data>, agent: Agent) {
36
+ const nodesToDraw = [];
37
+ iterateNodes(nodeToData, (_, color, node) => {
38
+ nodesToDraw.push({node, color});
39
+ });
40
41
+ agent.emit('drawTraceUpdates', nodesToDraw);
42
+}
43
+
44
+function drawWeb(nodeToData: Map<NativeType, Data>) {
45
if (canvas === null) {
46
initialize();
47
}
@@ -58,6 +59,12 @@ export function draw(nodeToData: Map<NativeType, Data>, agent: Agent): void {
59
});
60
}
61
62
+export function draw(nodeToData: Map<NativeType, Data>, agent: Agent): void {
63
+ return isReactNativeEnvironment()
64
+ ? drawNative(nodeToData, agent)
65
+ : drawWeb(nodeToData);
66
+}
67
+
68
function iterateNodes(
69
nodeToData: Map<NativeType, Data>,
70
execute: (rect: Rect | null, color: string, node: NativeType) => void,
@@ -97,12 +104,11 @@ function drawBorder(
104
context.setLineDash([0]);
105
}
106
100
-export function destroy(agent: Agent): void {
101
- if (window.document == null) {
102
- agent.emit('disableTraceUpdates');
103
- return;
104
- }
107
+function destroyNative(agent: Agent) {
108
+ agent.emit('disableTraceUpdates');
109
+}
110
111
+function destroyWeb() {
112
if (canvas !== null) {
113
if (canvas.parentNode != null) {
114
canvas.parentNode.removeChild(canvas);
@@ -111,6 +117,10 @@ export function destroy(agent: Agent): void {
117
}
118
}
119
120
+export function destroy(agent: Agent): void {
121
+ return isReactNativeEnvironment() ? destroyNative(agent) : destroyWeb();
122
+}
123
+
124
function initialize(): void {
125
canvas = window.document.createElement('canvas');
126
canvas.style.cssText = `