Skip to content

Commit 7c1a16d

Browse files
hawkgskirjs
authored andcommitted
refactor(devtools): create highlight overlay on node selection (angular#60744)
Currently, highlighting works only when a directive explorer node is hovered. The change enables this feature on node selection as well. PR Close angular#60744
1 parent 1d85f0c commit 7c1a16d

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

devtools/projects/ng-devtools-backend/src/lib/client-event-subscribers.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ import {serializeDirectiveState} from './state-serializer/state-serializer';
5757
import {runOutsideAngular, unwrapSignal} from './utils';
5858
import {DirectiveForestHooks} from './hooks/hooks';
5959

60+
type InspectorRef = {ref: ComponentInspector | null};
61+
6062
export const subscribeToClientEvents = (
6163
messageBus: MessageBus<Events>,
6264
depsForTestOnly?: {
6365
directiveForestHooks?: typeof DirectiveForestHooks;
6466
},
6567
): void => {
68+
const inspector: InspectorRef = {ref: null};
69+
6670
messageBus.on('shutdown', shutdownCallback(messageBus));
6771

6872
messageBus.on(
@@ -75,7 +79,7 @@ export const subscribeToClientEvents = (
7579
messageBus.on('startProfiling', startProfilingCallback(messageBus));
7680
messageBus.on('stopProfiling', stopProfilingCallback(messageBus));
7781

78-
messageBus.on('setSelectedComponent', selectedComponentCallback);
82+
messageBus.on('setSelectedComponent', selectedComponentCallback(inspector));
7983

8084
messageBus.on('getNestedProperties', getNestedPropertiesCallback(messageBus));
8185
messageBus.on('getRoutes', getRoutesCallback(messageBus));
@@ -94,7 +98,8 @@ export const subscribeToClientEvents = (
9498
});
9599

96100
if (appIsAngularInDevMode() && appIsSupportedAngularVersion() && appIsAngularIvy()) {
97-
setupInspector(messageBus);
101+
inspector.ref = setupInspector(messageBus);
102+
98103
// Often websites have `scroll` event listener which triggers
99104
// Angular's change detection. We don't want to constantly send
100105
// update requests, instead we want to request an update at most
@@ -175,12 +180,13 @@ const stopProfilingCallback = (messageBus: MessageBus<Events>) => () => {
175180
messageBus.emit('profilerResults', [stopProfiling()]);
176181
};
177182

178-
const selectedComponentCallback = (position: ElementPosition) => {
183+
const selectedComponentCallback = (inspector: InspectorRef) => (position: ElementPosition) => {
179184
const node = queryDirectiveForest(
180185
position,
181186
initializeOrGetDirectiveForestHooks().getIndexedDirectiveForest(),
182187
);
183188
setConsoleReference({node, position});
189+
inspector.ref?.highlightByPosition(position);
184190
};
185191

186192
const getNestedPropertiesCallback =
@@ -335,7 +341,7 @@ const checkForAngular = (messageBus: MessageBus<Events>): void => {
335341
]);
336342
};
337343

338-
const setupInspector = (messageBus: MessageBus<Events>) => {
344+
const setupInspector = (messageBus: MessageBus<Events>): ComponentInspector => {
339345
const inspector = new ComponentInspector({
340346
onComponentEnter: (id: number) => {
341347
messageBus.emit('highlightComponent', [id]);
@@ -358,6 +364,8 @@ const setupInspector = (messageBus: MessageBus<Events>) => {
358364

359365
messageBus.on('createHydrationOverlay', inspector.highlightHydrationNodes);
360366
messageBus.on('removeHydrationOverlay', inspector.removeHydrationHighlights);
367+
368+
return inspector;
361369
};
362370

363371
export interface SerializableDirectiveInstanceType extends DirectiveType {

devtools/projects/ng-devtools-backend/src/lib/highlighter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {ngDebugClient} from './ng-debug-api/ng-debug-api';
1212

1313
let hydrationOverlayItems: HTMLElement[] = [];
1414
let selectedElementOverlay: HTMLElement | null = null;
15+
let selectedElement: Node | null = null;
1516

1617
const DEV_TOOLS_HIGHLIGHT_NODE_ID = '____ngDevToolsHighlight';
1718

@@ -83,8 +84,12 @@ export function getDirectiveName(dir: Type<unknown> | undefined | null): string
8384
}
8485

8586
export function highlightSelectedElement(el: Node): void {
87+
if (el === selectedElement) {
88+
return;
89+
}
8690
unHighlight();
8791
selectedElementOverlay = addHighlightForElement(el);
92+
selectedElement = el;
8893
}
8994

9095
export function highlightHydrationElement(el: Node, status: HydrationStatus) {

0 commit comments

Comments
 (0)